2011-02-23 93 views
13

我一直在使用emacs 23(python.el)一個多月,我對默認的自動縮進設置感到不滿。emacs 23 python.el自動縮進樣式 - 可以這樣配置嗎?

目前,我的Python文件被自動縮進如下:

x = a_function_with_dict_parameter({ 
            'test' : 'Here is a value', 
            'second' : 'Another value', 
            }) 
a_function_with_multiline_parameters(on='First', line='Line', 
            now_on='Second', next_line='Line', 
            next='Third', finally='Line') 

我寧願如果我能設置自動縮進設置這樣相同的代碼可以很容易地被格式化:

x = a_function_with_dict_parameter({ 
    'test' : 'Here is a value', 
    'second' : 'Another value', 
}) 
a_function_with_multiline_parameters(on='First', line='Line', 
    now_on='Second', next_line='Line', next='Third', finally='Line') 

看起來,我想如何執行自動縮進的邏輯是:

如果上一行的最後一個字符(非註釋/空白)爲a,則增加t他縮進1級。 否則,使用相同的縮進級別。

但是使用該邏輯,TAB需要實際增加當前行的縮進級別。 (目前,TAB只能將行移動到自動縮進級別)

有誰知道我如何修改emacs自動縮進以實現我想要的樣式?

回答

1

嘗試使用最後的fgallina的python.el版本。它包含許多其他improvements

我使用這個版本,TAB有你想要的行爲,但我對python.el做了一些修改,所以我不能確定你會得到相同的行爲。如果是這樣,請告訴我。

+0

不幸的是,這並不像我所描述的那樣工作。自動縮進之後,不斷按下「TAB」會使該線滾動通過負值(朝向邊距)縮進級別。此外,如果我手動操作縮進級別,後續行不會保持在同一級別。好消息是我可以查看python.el源代碼,看看我能否弄清楚事情是如何工作的,並且可能會自己弄清楚如何修改它。 – brildum 2011-02-24 15:01:36

-1

的Mx定製組RET 蟒蛇RET

3

你可以試試這個和平的代碼:

(require 'python) 

; indentation 
(defadvice python-calculate-indentation (around outdent-closing-brackets) 
    "Handle lines beginning with a closing bracket and indent them so that 
    they line up with the line containing the corresponding opening bracket." 
(save-excursion 
    (beginning-of-line) 
    (let ((syntax (syntax-ppss))) 
    (if (and (not (eq 'string (syntax-ppss-context syntax))) 
      (python-continuation-line-p) 
      (cadr syntax) 
      (skip-syntax-forward "-") 
      (looking-at "\\s)")) 
     (progn 
      (forward-char 1) 
      (ignore-errors (backward-sexp)) 
      (setq ad-return-value (current-indentation))) 
     ad-do-it)))) 

(ad-activate 'python-calculate-indentation) 

現在,一個簡單的Python字典是這樣的:

a = {'foo': 'bar', 
    'foobar': 'barfoo' 
    } 

成爲.. 。

a = {'foo': 'bar', 
    'foobar': 'barfoo' 
} 
+0

這個問題可以更新emacs 24嗎?函數'python-calculate-indentation'不再存在 - 最接近的替換似乎是'python-indent-calculate-indentation',但是這個函數的操作語義似乎已經改變了。 – barik 2013-08-02 17:10:59