选项(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))
(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)))
另一个问题是跳出循环,同时也表示成功。我们可以通过声明一个命名的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)
这很难看,需要您编写一些emacs lisp。我需要学习EmacsLisp,所以如果它不是那么难看的话,我可能会准备好去做。但我不是。看起来你可以学习emacs lisp:)(如果你真的想这么做的话)。我有点嫉妒。不管怎样,你说告诉你这是个坏主意是一个可以接受的答案,所以这里是:
这是一个糟糕的文体选择。不是吗
更容易阅读?在
虽然不是专门针对pep8的,但它提到行继续字符的使用应该保持在最低限度。而且,这最明确和客观地违背了政治公众人物第八次会议的精神。我只是不知道怎么做;)
我同意Aaron关于您的风格选择的可取性,但是由于我也同意他关于EmacsLisp很有趣的观点,我将描述您如何实现这一点。在
Emacs} 中一行的缩进,用于处理连续行的相关部分深埋在函数内部,无法轻松配置它。在
python-mode
计算函数^{所以我们有两个选择:
python-calculate-indentation
替换为我们自己的版本(每当{python-mode
就会成为维护的噩梦);或者python-calculate-indentation
:也就是说,将它包装在我们自己的函数中,该函数处理我们感兴趣的情况,否则将遵从原始函数。在选项(2)在这种情况下似乎是可行的。所以让我们开始吧!首先要阅读manual on advice,它建议我们的建议应该如下所示:
这里
ad-do-it
是一个神奇的令牌,defadvice
用原始函数代替它。从Python背景出发,您可能会问:“为什么不使用这种装饰风格?”Emacs建议机制设计为(1)将建议与原始建议很好地分开;以及(2)为一个不需要协作的函数提供多个建议;(3)允许您单独控制哪些建议被打开和关闭。当然可以想象用Python编写类似的东西。在以下是如何判断当前行是否是虚线延续线:
^{pr2}$这有一个问题:对} 来结束这一点,以确保该点不会走投无路。在
beginning-of-line
的调用实际上将点移动到行的开头。哎呀。我们不想在仅仅计算缩进时移动点。所以我们最好用一个调用^{我们可以通过向后跳过标记或带圆括号的表达式(Lisp称之为“S-expressions”或“sexps”)来找到需要对齐的点,直到找到点,否则就可以找到语句的开头。在缓冲区的受限部分进行搜索的一个好的Emacs习惯用法是narrow缓冲区只包含我们想要的部分:
然后继续向后跳过sexps,直到找到点,或者直到
backward-sexp
停止前进:这里
ad-return-value
是一个神奇的变量,defadvice
使用它来从建议的函数返回值。丑陋但实用。在现在有两个问题。首先,
backward-sexp
可以在某些情况下发出错误信号,因此我们最好捕获该错误:另一个问题是跳出循环,同时也表示成功。我们可以通过声明一个命名的
block
,然后调用return-from
,同时完成这两个操作。Blocks and exits是常见的Lisp特性,因此我们需要(require 'cl)
让我们把它们放在一起:
我不认为这是最好的方法,但它说明了一系列比较棘手的emac和Lisp特性:advice,excursions,narrowing,moving over sexps,error handling,blocks and exits。享受吧!在
相关问题 更多 >
编程相关推荐