Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

関数にリンクを貼りたいだけの時はdefalias、細工をしたければdefadvice

defaliasは名前の通り、メソッドエイリアスを貼るもの。
もう一方のdefadviceは対象メソッドの挙動を途中で奪ったり後始末にプラスαしたりするメソッドです。

defalias

   (defalias 'yes-or-no-p 'y-or-n-p)

挙動は素直ですし、それ以上の事はできません。

defadvice

 (defun foo () (interactive) (insert "hello"))
    (defadvice foo (after foo-after activate) (insert " world!\n"))
    (foo) ;=> hello world!

消す時も楽チン

 (ad-unadvise 'foo)
   (foo)

使用上の注意

黒魔術の香りがしますが、実際その通りで

Advising is a source of major headaches because of unexpected
interactions with other pieces of code...
Advice have to be written *extremely* carefully.
I.e. if you can do it without `advice', then don't use `advice'.

DrewAdams

などと書かれる始末。
コアに手を突っ込む場合などは特に注意が必要です。
例のように、defadvice経由である事を明示しておいた方が良いですね。