如何在Emacs中重写本机defun?

时间:2014-08-16 21:13:55

标签: emacs

处所

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

因此,我希望实现以下目标:

  1. 编写一个名为my-up-list的新defun,其中包含错误修复程序;
  2. 重新编写原生defun backward-up-list以调用上述新defun(而不是原始bug up-list)。 (通过在相同的defun名称下重写,我打算保留其原始方便的键绑定。)
  3. 通过遵循Emacs教程Here,我实现了如下:

    1. 我在.emacs文件中写了一个新的defun my-up-list; (见最后的代码)
    2. 我在.emacs文件中重写了同名的defun backward-up-list; (见最后的代码)。
    3. 然而,当我在"|"(|是光标位置)中尝试测试时,我遇到以下错误:

        

      向上 - 向上列表:参数数量错误:( lambda nil(interactive)(let((s(syntax-ppss)))(if(nth 3 s)(progn(goto-char(nth 8 s)) ))))(条件 - 案例nil(progn(up-list))(error nil))),1 [2次]

      问题:

      • 是否正确地重新编写原生defun是正确的方法 .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)))
      

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

最简单的方法是使用"建议"系统。这提供了一些包装和扩展现有函数的简单方法。 elisp手册中的整个部分解释了如何操作。