[emacs-berlin] Understanding hooks
jman
emacs-berlin at city17.xyz
Thu Sep 12 17:06:44 UTC 2024
Hi, thank you Michael for your insights and suggestions! Inline reply.
> If you have read the documentation, you already know about buffer local
> and global hook bindings.
> You manipulate the global hook values above - this is obviously not
> correct if you are using multiple buffers (what one typically does in
> Emacs...). `add-hook' and `remove-hook' provide an optional argument to
> manipulate the local hook bindings. Use it.
I did read the documentation of `{add,remove}-hook` but I struggle to understand
the LOCAL flag.
Quoting:
----------s---------s----------
"The optional fourth argument, LOCAL, if non-nil, says to modify
the hook’s buffer-local value rather than its global value.
This makes the hook buffer-local and it makes t a member of the
buffer-local value. That acts as a flag to run the hook
functions of the global value as well as in the local value."
----------e---------e----------
I find this verbiage a bit confusing: does the global hook runs in
the current buffer or not with LOCAL `t`?
Anyway, based on your suggestions, I /think/ this was correct:
----------s---------s----------
;; Add whitespace-cleanup EVERYWHERE before saving a buffer
(add-hook 'before-save-hook 'whitespace-cleanup)
;; Don't do anything when saving an email
(remove-hook 'mu4e-compose-mode-hook 'before-save-hook t)
----------e---------e----------
Now when I compose an email, `before-save-hook` looks like this:
----------s---------s----------
before-save-hook is a variable defined in ‘files.el’.
Its value is (mu4e--compose-before-save t)
Original value was nil
Local in buffer "No subject"; global value is
(whitespace-cleanup)
----------e---------e----------
but when I save the email draft, whitespace-cleanup is /still/ executed,
so clearly I still don't understand how this machinery works. Looking
into it :-)
Best,
More information about the emacs-berlin
mailing list