【问题标题】:In emacs python-mode customize multi-line statement indentation在emacs python-mode中自定义多行语句缩进
【发布时间】:2010-11-27 18:00:12
【问题描述】:

我正在使用 emacs 23 附带的 python 模式。我想自定义多行语句的自动缩进。例如目前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 创建这些自定义?我找不到任何创建这些自定义项的文档。

【问题讨论】:

  • 我通常只是反复按TAB,直到它停在所需的位置。
  • 这不适用于多行语句中的额外行。
  • 您只需要在多行语句的第一行和最后一行按TAB。第一行为以下所有行设置缩进,在最后一行按TAB 更改右括号的缩进。

标签: python emacs


【解决方案1】:

大概是这样的吧?

(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 功能的讨论。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多