【发布时间】:2014-08-16 21:13:55
【问题描述】:
场所:
我的 Emacs 在其原始 defun (up-list) 之一中有一个小错误(无法从 "" 内部上升)。这个 defun 对于绑定到 C-M-u 的常用命令之一 (backward-up-list) 至关重要。
因此,我想实现以下目标:
- 编写一个名为
my-up-list的新defun,其中修复了错误; - 重新编写原生 defun
backward-up-list以调用上述新的 defun(而不是原生 bugyup-list)。 (通过使用相同的 defun 名称重新编写,我打算保留其原始方便的键绑定。)
按照 Emacs 教程Here,我实现了如下:
- 我在 .emacs 文件中写了一个新的 defun
my-up-list; (见最后代码) - 我在 .emacs 文件中用同名重写了 defun
backward-up-list; (见最后的代码)。
但是,当我通过在"|"(| 是光标位置)中对其进行测试时,我遇到了以下错误:
backward-up-list: 参数数量错误: (lambda nil (interactive) (let ((s (syntax-ppss))) (if (nth 3 s) (progn (goto-char (nth 8 s) )))) (condition-case nil (progn (up-list)) (error nil))), 1 [2 times]
问题:
- 仅通过将 .emacs 文件中具有相同名称的新实现?
- 我的代码可能出了什么问题?
参考:
(my-up-list 来自here)
;; I only changed "up-list" to "my-up-list" -modeller
(defun backward-up-list (&optional arg)
"Move backward out of one level of parentheses.
With ARG, do this that many times.
A negative argument means move forward but still to a less deep spot.
This command assumes point is not in a string or comment."
(interactive "^p")
(my-up-list (- (or arg 1))))
;; copied from solution to another question - modeller
(defun my-up-list ()
(interactive)
(let ((s (syntax-ppss)))
(when (nth 3 s)
(goto-char (nth 8 s))))
(ignore-errors (up-list)))
【问题讨论】:
-
如果重新定义函数的目的是保留它的键绑定,Emacs 可以为您提供命令重新映射。见
C-h i g(elisp) Remapping CommandsRET。在这里你可以使用(global-set-key [remap backward-up-list] 'my-backward-up-list)。 -
@phils 感谢您的提示。学习另一种方法来实现这一点真是太好了。
-
不清楚(至少对我来说)你为什么要这样做。 Vanilla
up-list让你摆脱困境,刚刚过去。然后再次调用它会将您带到my-up-list带您的确切位置。因此,如果没有my-up-list,您只需按两次up-list键即可获得相同的效果。作为奖励,您可以使用up-list,只要您想越过字符串。如果您将诸如(my-)up-list之类的命令绑定到某个键,那么您通常希望它是一个简单的、可重复的键。 -
@Drew,我在原版
up-list中收到“扫描错误:括号不平衡”。我使用(自编译 Emacs 23.4 64 位)。请在此处查看代码:lpaste.net/109513 -
啊,是的,在 Emacs 23 和 24(包括最新的 24.4 预测试)中存在
up-list错误,在 Emacs 22 和之前版本中存在不同的错误。您可以从最近的 Emacs 24.4 开发快照中获取定义,其中 Emacs 23-24 错误已得到修复。
标签: emacs