简体   繁体   English

模式本地缺陷

[英]Mode-local defadvice

Is it possible to modify function's behavior with defadvice for specific mode/buffer only? 是否可以仅针对特定模式/缓冲区使用defadvice修改函数的行为? I want mouse-yank-primary to insert extra text, but only when i'm in specific mode. 我希望mouse-yank-primary插入其他文本,但仅当我处于特定模式时才可以。 I've tried 'defadvice after' for mouse-yank-primary, but once activated it works in all other buffer as well. 我已经尝试过“ defadvice after”(鼠标后摇),但是一旦激活,它也可以在所有其他缓冲区中使用。

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. 我认为可以通过在模式挂钩中将鼠标按钮重新绑定到我自己的功能上来解决,但是elisp手册说使用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: 为此,您不需要模式挂钩,通常只需修改模式的键盘映射即可:

(eval-after-load '<mode>
  '(define-key <mode>-map [mouse-2] 'my-mouse-yank-primary))

Localizing an advice to a major mode is a bit harder. 将建议本地化为主要模式要困难一些。 There's no argument you can pass to defadvice to 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. 您没有传递给defadvice参数,但是可以在模式挂钩中设置某些变量的局部缓冲区值,然后在建议代码中检查该值。 If it's set, do something special. 如果已设置,请执行一些特殊的操作。 If not, just evaluate ad-do-it . 如果没有,请评估ad-do-it

Localizing an advice to a major mode is easy: 将建议本地化为主要模式很容易:

(defadvice foo (after bar activate)
  (when (derived-mode-p 'python-mode)
    (do (something) now)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM