覆盖emacs lisp函数的首选方法? [英] Preferred method of overriding an emacs lisp function?

查看:132
本文介绍了覆盖emacs lisp函数的首选方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重写并独立地测试了由Emacs 24捆绑的Emacs函数之一调用的内部函数的行为。在我的 init.el - 我的函数的行为覆盖捆绑的功能?



我已经遵循各种线程的建议 vs fset 等等,我很困惑。

解决方案

@iainH通过描述您要完成的目标
,您可以更快地获得有用的答案,那么你到目前为止。我要求代码
尝试帮助你做你想做的,而不必覆盖任何东西。



我还是不明白你为什么不t只是使用 defun ?我怀疑
可能发生的情况是您在init文件中使用 defun ,但原来的
函数尚未加载(请参阅autoload )。有一段时间后,您会执行
的某些事情,导致原始定义的文件被加载,并且
自定义函数被原来的覆盖。



如果是这个问题,你有三个选择(假设你要
从telnet.el覆盖 telnet-initial-filter ):


  1. 在定义自己的版本之前自己加载文件。

     (require'telnet)
    (defun telnet-initial-filter(proc string)
    ...)


  2. 直接Emacs仅在文件加载
    eval-after-load 机制。

     (eval-after-loadtelnet
    '(defun telnet-initial-filter(proc string)
    ...))
    / pre>

  3. 使用建议。




<其中2个是最好的。 1也可以,但你必须加载一个文件,你可以
从来没有在会话中使用。 3是最差的任何涉及 defadvice
的内容应作为最后的手段选择。


I have rewritten and stand-alone tested the behaviour of an inner function invoked by one of the Emacs functions bundled with Emacs 24. What is the preferred way of incorporating - e.g. in my init.el - my function's behaviour overriding the bundled function?

I have followed various threads of advice vs fset etc. and am confused.

解决方案

@iainH You tend to get to a useful answer faster by describing what goal you're trying to accomplish, then what you have so far. I asked for code to try to help you do what you want to do without having to overwrite anything.

I still don't understand why you don't just use defun though? I suspect what may happen is you use defun in your init file, but the original function isn't loaded yet (see autoload). Some time later, you do something to cause the file with the original definition to be loaded and your custom function is overwritten by the original.

If this is the problem, you have three options (let's say you want to overwrite telnet-initial-filter from "telnet.el"):

  1. Load the file yourself before you define your own version.

    (require 'telnet)
    (defun telnet-initial-filter (proc string)
      ...)
    

  2. Direct Emacs to load your function only after the file loads with the eval-after-load mechanism.

    (eval-after-load "telnet"
      '(defun telnet-initial-filter (proc string)
         ...))
    

  3. Use advice.

Of these, 2 is best. 1 is also okay, but you have to load a file you may never use in a session. 3 is the worst. Anything involving defadvice should be left as a last resort option.

这篇关于覆盖emacs lisp函数的首选方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆