Emacs24和python-mode:docstrings中的缩进

时间:2015-08-17 20:25:54

标签: emacs indentation emacs24 python-mode python.el

我有以下代码/文字:

def f():
    """
    Return nothing.

    .. NOTE::

        First note line
second note line

在Emacs23(23.4.1)中,我能够在最后一行按下TAB("第二个音符行&#34 ;; nomatter这行是如何缩进的)并且它正确对齐如下:

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正确对齐块?

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

如何编辑在单独缓冲区中解串的部分?这将允许python-mode及其所有功能。

这里的初稿 - 原始字符串将存储在kill-ring中:

static

准备好后,将内容复制回原始缓冲区。 哪些仍有待实施。