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

16 python emacs

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

my_var = [
    'val1',
    'val2',
    'val3',
    ]
Run Code Online (Sandbox Code Playgroud)

我会比较喜欢

my_var = [
    'val1',
    'val2',
    'val3',
]
Run Code Online (Sandbox Code Playgroud)

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

my_func('first_arg', 'another_arg', {
        'key1': val1,
        'key2': val2,
        })
Run Code Online (Sandbox Code Playgroud)

我想看看

my_func('first_arg', 'another_arg', {
    'key1': val1,
    'key2': val2,
})
Run Code Online (Sandbox Code Playgroud)

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

Gar*_*ees 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)
Run Code Online (Sandbox Code Playgroud)

有关此答案中使用的一些Emacs功能的讨论,请参阅此类似问题.


std*_*err 1

您需要查看 python-mode.el 的 py-indent-line 函数。