我已经使用emacs23 (python.el)一个多月了,我对默认的自动缩进设置不满意。
目前,我的Python文件自动缩进如下:
x = a_function_with_dict_parameter({
'test' : 'Here is a value',
'second' : 'Another value',
})
a_function_with_multiline_parameters(on='First', line='Line',
now_on='Second', next_line='Line',
next='Third', finally='Line')我更希望我可以设置自动缩进设置,以便可以轻松地格式化相同的代码:
x = a_function_with_dict_parameter({
'test' : 'Here is a value',
'second' : 'Another value',
})
a_function_with_multiline_parameters(on='First', line='Line',
now_on='Second', next_line='Line', next='Third', finally='Line')似乎我希望自动缩进的执行方式的逻辑是:
如果前一行的最后一个字符(非注释/空格)是:,则将缩进级别增加1。否则,使用相同的缩进级别。
但是使用这种逻辑,TAB实际上需要增加当前行的缩进级别。(目前,TAB仅将行移动到自动缩进级别)
有人知道如何修改emacs自动缩进来实现我想要的样式吗?
发布于 2011-03-19 18:17:56
你可以尝试一下这个简单的代码:
(require 'python)
; indentation
(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)现在,一个简单的python字典如下:
a = {'foo': 'bar',
'foobar': 'barfoo'
}变成了..。
a = {'foo': 'bar',
'foobar': 'barfoo'
}发布于 2011-02-24 21:49:13
试试最新的fgallina的python.el版本。它包含许多其他的improvements。
我使用这个版本,TAB有你想要的行为,但是我对python.el做了几次修改,所以我不能确定你会得到同样的行为。如果是这样,请让我知道。
发布于 2011-03-16 06:05:20
M-x customize-group RET python RET
https://stackoverflow.com/questions/5094649
复制相似问题