Emacs 24 にバンドルされている Emacs 関数の 1 つによって呼び出される内部関数の動作を書き直してスタンドアロンでテストしましたinit.el
。バンドルされた関数をオーバーライドする関数の動作を組み込む (たとえば、自分の関数に) 好ましい方法は何ですか?
advice
私はvsfset
など のさまざまなスレッドをたどりましたが、混乱しています。
Emacs 24 にバンドルされている Emacs 関数の 1 つによって呼び出される内部関数の動作を書き直してスタンドアロンでテストしましたinit.el
。バンドルされた関数をオーバーライドする関数の動作を組み込む (たとえば、自分の関数に) 好ましい方法は何ですか?
advice
私はvsfset
など のさまざまなスレッドをたどりましたが、混乱しています。
@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"):
Load the file yourself before you define your own version.
(require 'telnet)
(defun telnet-initial-filter (proc string)
...)
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)
...))
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.
この質問を自己完結型にするために、アドバイスを使用してそれを行う方法を次に示します。
(defadvice telnet-initial-filter (around my-telnet-initial-filter-stuff act)
"Things to do when running `telnet-initial-filter'."
(message "Before")
ad-do-it
(message "After") )
これは主に、既存の関数を置き換えるのではなく、ラップする場合に明らかに役立ちます (その場合は再定義するだけです)。
アドバイスの使用方法の詳細については、マニュアルを参照してください。