在emacs中,python-mode自定义多行语句缩进

时间:2010-11-27 18:00:13

标签: python emacs

我正在使用emacs 23附带的python-mode。我想自定义多行语句的自动缩进。例如,目前emacs更喜欢以下

my_var = [
    'val1',
    'val2',
    'val3',
    ]

我更喜欢

my_var = [
    'val1',
    'val2',
    'val3',
]

此外,当创建具有尾随列表或dict的函数时,emacs更喜欢

my_func('first_arg', 'another_arg', {
        'key1': val1,
        'key2': val2,
        })

我想看看

my_func('first_arg', 'another_arg', {
    'key1': val1,
    'key2': val2,
})

是否可以在emacs中为python-mode创建这些自定义?我无法找到任何创建这些自定义的文档。

2 个答案:

答案 0 :(得分:11)

也许是这样的事情?

(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)

有关此答案中使用的一些Emacs功能的讨论,请参阅this similar question

答案 1 :(得分:1)

你想在函数py-indent-line看一下python-mode.el。