我正在用Emacs编写Python脚本,并且已经用M-x auto-fill-mode
激活了自动填充次要模式。我似乎总是遇到的一个问题是,这种填充模式往往会在多行之间断开带引号的字符串,而不进行任何补偿调整,从而导致脚本运行时出现错误。
例如:
print 'the quick brown fox jumped over the lazy dog and
then did something else'
这在运行时会导致SyntaxError: EOL while scanning string literal
。
在Emacs中有没有一种填充模式,它是Python的“字符串感知”,并自动进行例如Python style - line continuation with strings?中讨论的行连续/相关调整之一,而不是天真地拆分字符串导致错误?
发布于 2015-06-16 23:49:15
编辑我自己禁用了它,因为它会导致Emacs在某些文档上消耗大量的处理器时间。我只是生活在糟糕的填充模式中--如果某些东西能正确地完成这一点,我会很高兴的:
some_list.append("This is a very long string which I would like to "
"break in a sensible way")
/EDIT
我已经为同样的问题绞尽脑汁了一段时间,最终找到了这个解决方案。
这似乎对我很有效。不幸的是,我认为这在所有模式下都关闭了对任何带引号的字符串的填充,而不仅仅是Python。我相信比我更强的elisp-fu的人可以想出一个修改,将其限制为python模式,但这是一个比上面提出的更好的解决方案,IMO。
这个解决方案取自这个相关的答案--如果你喜欢的话,去给this answer一些提升的爱。
(defun odd-number-of-single-quotes-this-paragraph-so-far ()
(oddp (how-many "'" (save-excursion (backward-paragraph) (point)) (point))))
(defun odd-number-of-double-quotes-this-paragraph-so-far ()
(oddp (how-many "\"" (save-excursion (backward-paragraph) (point)) (point))))
(add-to-list 'fill-nobreak-predicate
'odd-number-of-single-quotes-this-paragraph-so-far)
(add-to-list 'fill-nobreak-predicate
'odd-number-of-double-quotes-this-paragraph-so-far)
“”“
发布于 2019-07-15 08:30:59
使用当前的python-mode.el
它的行为应该是这样:
print 'the quick brown fox jumped over the lazy dog and then did something asd asdf \
asdf elssdsd e'
https://stackoverflow.com/questions/23755506
复制