首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Emacs24和python-mode:文档字符串中的缩进

Emacs24和python-mode:文档字符串中的缩进
EN

Stack Overflow用户
提问于 2015-08-18 04:25:54
回答 2查看 738关注 0票数 1

我有以下代码/文本:

代码语言:javascript
复制
def f():
    """
    Return nothing.

    .. NOTE::

        First note line
second note line

在Emacs23 (23.4.1)中,我可以在最后一行(“第二个注释行”;不管该行是如何缩进的)中按Tab键,它可以像这样正确地对齐:

代码语言:javascript
复制
def f():
    """
    Return nothing.

    .. NOTE::

        First note line
        second note line

也就是说,它使用前一行,并以相同的方式缩进后一行。

现在在Emacs24 (24.3.1)中,这不再起作用,它是这样对齐的:

代码语言:javascript
复制
def f():
    """
    Return nothing.

    .. NOTE::

        First note line
    second note line

即,它对齐多行字符串块,但不依赖于前一行。

它只影响文档字符串;代码按照我想要的方式缩进。我正在使用python-mode。如何更改此设置,以便按TAB键正确对齐块?

EN

回答 2

Stack Overflow用户

发布于 2015-08-18 05:25:32

在Emacs 23和24之间,Python模式发生了很大的变化。没有任何配置可以让你做你想做的事情。

但是,Emacs非常灵活,您可以建议(python-indent-context)函数让它返回一个不同的结果,该结果将导致您想要的行为。函数(python-indent-context)返回一个字符,在该字符处测量缩进并用于缩进当前行。默认情况下,当在字符串中时,它返回字符串开头所在的点。因此,您的行将缩进到字符串开头的缩进位置。我们可以很容易地修改它,以返回前一个非空行中的一个点,例如:

代码语言:javascript
复制
(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也可以达到同样的效果:

代码语言:javascript
复制
(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)
票数 0
EN

Stack Overflow用户

发布于 2015-08-19 14:55:10

如果在单独的缓冲区中编辑反字符串的部分呢?这将允许python模式及其所有功能。

在这里,第一个草稿-原始字符串将存储在kill-ring中:

代码语言:javascript
复制
(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)))))

准备好后,将内容复制回原始缓冲区。这还有待于实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32059126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档