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

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

问题描述

我已经重写并独立测试了由与 Emacs 24 捆绑在一起的 Emacs 函数之一调用的内部函数的行为.在我的 init.el 中 - 我的函数的行为是否覆盖了捆绑函数?

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?

我遵循了 advicefset 等的各种线程,但很困惑.

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

推荐答案

@iainH 通过描述目标,您往往会更快地获得有用的答案你正在努力完成,那么你到目前为止所拥有的.我要求提供代码尝试帮助您做您想做的事,而无需覆盖任何内容.

@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.

我还是不明白你为什么不直接使用 defun ?我猜测可能发生的情况是您在 init 文件中使用了 defun,但原始文件函数尚未加载(参见 autoload).一段时间后,你做导致加载具有原始定义的文件和您的自定义函数被原始函数覆盖.

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.

如果这是问题所在,您有三个选择(假设您想要从telnet.el"覆盖telnet-initial-filter:

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

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

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

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

  • 仅在文件加载后才指示 Emacs 加载您的函数eval-after-load 机制.

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

  • 使用建议.

  • Use advice.

    其中 2 个最好.1也可以,但你必须加载一个文件,你可能永远不要在会话中使用.3是最坏的.任何涉及defadvice应该作为最后的选择.

    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天全站免登陆