Is it possible to modify function’s behavior with defadvice for specific mode/buffer only? I want mouse-yank-primary to insert extra text, but only when i’m in specific mode. I’ve tried ‘defadvice after’ for mouse-yank-primary, but once activated it works in all other buffer as well.
I think it can be resolved by rebinding mouse button to my own function in mode hook, but elisp manual says it’s better to use defadvice.
Rebinding the mouse button to another function seems preferable to me. You don’t need a mode hook for that, usually you just modify the mode’s keymap:
Localizing an advice to a major mode is a bit harder. There’s no argument you can pass to
defadviceto do that, but you can set some variable’s buffer-local value in a mode hook, and then check this value in the advice code. If it’s set, do something special. If not, just evaluatead-do-it.