Python 模式在 Emacs 23 和 24 之间发生了相当大的变化。没有任何配置可以让你做你想做的事。
但是,Emacs 非常灵活,您可以建议该(python-indent-context)
函数使其返回不同的结果,从而导致您想要的行为。该函数(python-indent-context)
返回一个字符,在该字符处测量缩进并用于缩进当前行。默认情况下,当在字符串中时,它返回字符串开头所在的点。因此,您的行将缩进到字符串开头的缩进处。我们可以很容易地修改它以返回前一个非空行中的一个点,例如:
(defun python-fake-indent-context (orig-fun &rest args)
(let ((res (apply orig-fun args))) ; Get the original result
(pcase res
(`(:inside-string . ,start) ; When inside a string
`(:inside-string . ,(save-excursion ; Find a point in previous non-empty line
(beginning-of-line)
(backward-sexp)
(point))))
(_ res)))) ; Otherwise, return the result as is
;; Add the advice
(advice-add 'python-indent-context :around #'python-fake-indent-context)
使用旧 Emacs 的旧 defadvice 可以达到相同的效果:
(defadvice python-indent-context (after python-fake-indent-context)
(pcase ad-return-value
(`(:inside-string . ,start) ; When inside a string
(setq ad-return-value ; Set return value
`(:inside-string . ,(save-excursion ; Find a point in previous non-empty line
(beginning-of-line)
(backward-sexp)
(point)))))))
(ad-activate 'python-indent-context)