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

标签 python emacs

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

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

我更愿意

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

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

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

我想看看

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

是否可以在 emacs 中为 python 模式创建这些自定义项?我找不到任何创建这些自定义项的文档。

最佳答案

也许是这样的?

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

参见 this similar question讨论此答案中使用的一些 Emacs 功能。

关于python - 在emacs python-mode中自定义多行语句缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4293074/

相关文章:

Emacs - 在函数中使用 "Select All"(如何查找绑定(bind)到某个键的函数)

python - 如何在python列表中的特定索引处插入元素

python - Python 是否有 .pdbinit 文件(类似于 .gdbinit 文件)?

python - 当一条记录属于多个组时,pandas groupby

emacs - 如何重构 Clojure 源代码?

regex - 如何提取捕获的 Gnu emacs regex (regexp) 组以插入到其他地方?

python - 如何使用QPropertyAnimation配合QPainter绘制圆弧

python - LDAP 条目与多行正则表达式匹配时出现问题

bash - 如果服务器已经在运行,如何从命令行启动 emacs 但使用 emacsclient?

c - emacs 计算 .c/.h src 文件中的 c 函数