Emacs Asked by caseneuve on December 5, 2021
I want to write a minor which evaluates some code in the buffer after saving a file. I activate after-save-hook for this mode:
(add-hook 'my-minor-mode-name-hook #'(lambda () (add-hook 'after-save-hook #'a-func-from-my-minor-mode))
Everything works as intended until I disable the minor mode with the command my-minor-mode-name
. I get the message that the mode has been disabled but after-save-hook
is still active (although evaluating after-save-hook
in the buffer indicates that a-func-from-my-minor-mode
is not on the list).
I want a behaviour where disabling the minor mode removes automatically the hook — how should I fix that?
The minor mode's hook is called both when activating and deactivating the minor mode, so how 'bout
(add-hook 'my-minor-mode-name-hook
(lambda ()
(if my-minor-mode-name
(add-hook 'after-save-hook #'a-func-from-my-minor-mode nil 'local)
(remove-hook 'after-save-hook #'a-func-from-my-minor-mode 'local))))
Note that your calls to add-hook
are both global, so even though you only add to after-save-hook
when the minor mode is called, this addition applies to all buffers. Maybe that was indeed what you wanted, but I assumed it's not in the above example code.
Answered by Stefan on December 5, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP