我有以下代码/文本:
def f():
"""
Return nothing.
.. NOTE::
First note line
second note line在Emacs23 (23.4.1)中,我可以在最后一行(“第二个注释行”;不管该行是如何缩进的)中按Tab键,它可以像这样正确地对齐:
def f():
"""
Return nothing.
.. NOTE::
First note line
second note line也就是说,它使用前一行,并以相同的方式缩进后一行。
现在在Emacs24 (24.3.1)中,这不再起作用,它是这样对齐的:
def f():
"""
Return nothing.
.. NOTE::
First note line
second note line即,它对齐多行字符串块,但不依赖于前一行。
它只影响文档字符串;代码按照我想要的方式缩进。我正在使用python-mode。如何更改此设置,以便按TAB键正确对齐块?
发布于 2015-08-18 05:25:32
在Emacs 23和24之间,Python模式发生了很大的变化。没有任何配置可以让你做你想做的事情。
但是,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)发布于 2015-08-19 14:55:10
如果在单独的缓冲区中编辑反字符串的部分呢?这将允许python模式及其所有功能。
在这里,第一个草稿-原始字符串将存储在kill-ring中:
(defun temp-edit-docstring ()
"Edit docstring in python-mode. "
(interactive "*")
(let ((orig (point))
(pps (parse-partial-sexp (point-min) (point))))
(when (nth 3 pps)
(let* (;; relative position in string
(relpos (- orig (+ 2 (nth 8 pps))))
(beg (progn (goto-char (nth 8 pps))
(skip-chars-forward (char-to-string (char-after)))(push-mark)(point)))
(end (progn (goto-char (nth 8 pps))
(forward-sexp)
(skip-chars-backward (char-to-string (char-before)))
(point)))
(docstring (buffer-substring beg end)))
(kill-region beg end)
(set-buffer (get-buffer-create "Edit docstring"))
(erase-buffer)
(switch-to-buffer (current-buffer))
(insert docstring)
(python-mode)
(goto-char relpos)))))准备好后,将内容复制回原始缓冲区。这还有待于实现。
https://stackoverflow.com/questions/32059126
复制相似问题