简体   繁体   English

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

[英]In emacs python-mode customize multi-line statement indentation

I am using the python-mode shipped with emacs 23. I want to customize the auto-indentation of mult-line statements. 我正在使用emacs 23附带的python模式。我想自定义多行语句的自动缩进。 For example currently emacs prefers the following 例如,目前emacs更喜欢以下内容

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

I would prefer 我会比较喜欢

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

Also, when creating functions with a trailing list or dict emacs prefers 此外,当创建具有尾随列表或dict的功能时,emacs更喜欢

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

I would like to see 我想看看

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

Is it possible to create these customizations to python-mode in emacs? 是否可以在emacs中为python-mode创建这些自定义? I am not able to find any documentation creating these customizations. 我无法找到任何创建这些自定义的文档。

Something like this, perhaps? 也许是这样的事情?

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

See this similar question for a discussion of some of the Emacs features used in this answer. 有关此答案中使用的一些Emacs功能的讨论,请参阅此类似问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM