emacs上的多行python缩进

2024-07-02 11:45:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个emacs新手,我希望emacs能够像这样缩进我的代码

egg = spam.foooooo('vivivivivivivivivi')\
          .foooooo('emacs', 'emacs', 'emacs', 'emacs')

默认情况下,无法自动执行此操作(无需手动插入空格或C-C>;),因为emacs总是缩进4个空格(除非Im将多个参数拆分为多行)。在

最好的方法是什么?在

附言:如果这是一个坏主意(反对政治公众人物8或其他),请告诉我


Tags: 方法代码gt参数egg情况手动spam
2条回答

这很难看,需要您编写一些emacs lisp。我需要学习EmacsLisp,所以如果它不是那么难看的话,我可能会准备好去做。但我不是。看起来你可以学习emacs lisp:)(如果你真的想这么做的话)。我有点嫉妒。不管怎样,你说告诉你这是个坏主意是一个可以接受的答案,所以这里是:

这是一个糟糕的文体选择。不是吗

egg = spam.foo('viviviv')
egg = egg.foo('emacs', 'emacs', 'emacs')

更容易阅读?在

虽然不是专门针对pep8的,但它提到行继续字符的使用应该保持在最低限度。而且,这最明确和客观地违背了政治公众人物第八次会议的精神。我只是不知道怎么做;)

我同意Aaron关于您的风格选择的可取性,但是由于我也同意他关于EmacsLisp很有趣的观点,我将描述您如何实现这一点。在

Emacspython-mode计算函数^{}中一行的缩进,用于处理连续行的相关部分深埋在函数内部,无法轻松配置它。在

所以我们有两个选择:

  1. 将整个python-calculate-indentation替换为我们自己的版本(每当{}发生变化时,python-mode就会成为维护的噩梦);或者
  2. Advise”函数python-calculate-indentation:也就是说,将它包装在我们自己的函数中,该函数处理我们感兴趣的情况,否则将遵从原始函数。在

选项(2)在这种情况下似乎是可行的。所以让我们开始吧!首先要阅读manual on advice,它建议我们的建议应该如下所示:

(defadvice python-calculate-indentation (around continuation-with-dot)
  "Handle continuation lines that start with a dot and try to
line them up with a dot in the line they continue from."
  (unless 
      (this-line-is-a-dotted-continuation-line) ; (TODO)
    ad-do-it))

这里ad-do-it是一个神奇的令牌,defadvice用原始函数代替它。从Python背景出发,您可能会问:“为什么不使用这种装饰风格?”Emacs建议机制设计为(1)将建议与原始建议很好地分开;以及(2)为一个不需要协作的函数提供多个建议;(3)允许您单独控制哪些建议被打开和关闭。当然可以想象用Python编写类似的东西。在

以下是如何判断当前行是否是虚线延续线:

^{pr2}$

这有一个问题:对beginning-of-line的调用实际上将点移动到行的开头。哎呀。我们不想在仅仅计算缩进时移动点。所以我们最好用一个调用^{}来结束这一点,以确保该点不会走投无路。在

我们可以通过向后跳过标记或带圆括号的表达式(Lisp称之为“S-expressions”或“sexps”)来找到需要对齐的点,直到找到点,否则就可以找到语句的开头。在缓冲区的受限部分进行搜索的一个好的Emacs习惯用法是narrow缓冲区只包含我们想要的部分:

(narrow-to-region (point)
                  (save-excursion
                    (end-of-line -1)
                    (python-beginning-of-statement)
                    (point)))

然后继续向后跳过sexps,直到找到点,或者直到backward-sexp停止前进:

(let ((p -1))
  (while (/= p (point))
    (setq p (point))
    (when (looking-back "\\.")
      ;; Found the dot to line up with.
      (setq ad-return-value (1- (current-column)))
      ;; Stop searching backward and report success (TODO)
      ...)
    (backward-sexp)))

这里ad-return-value是一个神奇的变量,defadvice使用它来从建议的函数返回值。丑陋但实用。在

现在有两个问题。首先,backward-sexp可以在某些情况下发出错误信号,因此我们最好捕获该错误:

(ignore-errors (backward-sexp))

另一个问题是跳出循环,同时也表示成功。我们可以通过声明一个命名的block,然后调用return-from,同时完成这两个操作。Blocks and exits是常见的Lisp特性,因此我们需要(require 'cl)

让我们把它们放在一起:

(require 'cl)

(defadvice python-calculate-indentation (around continuation-with-dot)
  "Handle continuation lines that start with a dot and try to
line them up with a dot in the line they continue from."
  (unless 
      (block 'found-dot
        (save-excursion
          (beginning-of-line)
          (when (and (python-continuation-line-p)
                     (looking-at "\\s-*\\."))
            (save-restriction
              ;; Handle dotted continuation line.
              (narrow-to-region (point)
                                (save-excursion
                                  (end-of-line -1)
                                  (python-beginning-of-statement)
                                  (point)))
              ;; Move backwards until we find a dot or can't move backwards
              ;; any more (e.g. because we hit a containing bracket)
              (let ((p -1))
                (while (/= p (point))
                  (setq p (point))
                  (when (looking-back "\\.")
                    (setq ad-return-value (1- (current-column)))
                    (return-from 'found-dot t))
                  (ignore-errors (backward-sexp))))))))
    ;; Use original indentation.
    ad-do-it))

(ad-activate 'python-calculate-indentation)

我不认为这是最好的方法,但它说明了一系列比较棘手的emac和Lisp特性:adviceexcursionsnarrowingmoving over sexpserror handlingblocks and exits。享受吧!在

相关问题 更多 >