开发者

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. For example currently emacs prefers the followi开发者_JAVA技巧ng

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

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? 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.


You'll want to look at python-mode.el at the function py-indent-line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜