【问题标题】:How to rewrite a native defun in Emacs?如何在 Emacs 中重写原生的 defun?
【发布时间】:2014-08-16 21:13:55
【问题描述】:

场所

我的 Emacs 在其原始 defun (up-list) 之一中有一个小错误(无法从 "" 内部上升)。这个 defun 对于绑定到 C-M-u 的常用命令之一 (backward-up-list) 至关重要。

因此,我想实现以下目标:

  1. 编写一个名为my-up-list 的新defun,其中修复了错误;
  2. 重新编写原生 defun backward-up-list 以调用上述新的 defun(而不是原生 bugy up-list)。 (通过使用相同的 defun 名称重新编写,我打算保留其原始方便的键绑定。)

按照 Emacs 教程Here,我实现了如下:

  1. 我在 .emacs 文件中写了一个新的 defun my-up-list; (见最后代码)
  2. 我在 .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


【解决方案1】:

我猜你的 my-up-list 函数需要接受与原始 up-list 相同的参数,并用它们调用 up-list?

【讨论】:

  • 你是对的。顺便说一句,有没有一种简单的方法可以从down-list 编写my-down-list,它确实与my-up-list 对称工作。我还是个口齿不清的文盲。
  • 我自己就是一个 elisp 新手。我觉得你需要做这种修补听起来很奇怪——你在 emacs 邮件列表上问过吗?
  • 电子邮件列表页面似乎不再存储电子邮件列表。无论如何感谢cmets。
【解决方案2】:

最简单的方法是使用“建议”系统。这提供了一些简单的方法来包装和扩展现有功能。 elisp 手册中有一个完整的部分解释了如何做到这一点。

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多