簡體   English   中英

emacs 23 python.el自動縮進樣式 - 可以配置嗎?

[英]emacs 23 python.el auto-indent style — can this be configured?

我已經使用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')

似乎我喜歡自動縮進的邏輯是:

如果前一行的最后一個字符(非注釋/空白)是:,則將縮進級別增加1.否則,使用相同的縮進級別。

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

有誰知道如何修改emacs auto-indentation來實現我想要的風格?

你可以嘗試這種代碼安靜:

(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 dict:

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

成為...

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

試試最后一個fgallina的python.el版本。 它包含許多其他改進

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

Mx customize-group RET python RET

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM