[emacs-berlin] Understanding hooks

jman emacs-berlin at city17.xyz
Sat Sep 14 09:03:17 UTC 2024


> If you add to the local hook, Emacs automatically adds the t entry to
> the binding.  And both the local and the global hook will be run.

Ok, so what I am trying to do is avoiding running a function in a
specific buffer.

This code works does what I want:
```
;; Trigger whitespace-cleanup everywhere when saving a buffer
(add-hook 'before-save-hook 'whitespace-cleanup)

(defun remove-whitespace-cleanup ()
  ;; TODO: disable only for current buffer
  (remove-hook 'before-save-hook 'whitespace-cleanup))

(defun add-whitespace-cleanup ()
  (add-hook 'before-save-hook 'whitespace-cleanup))

;; Do not run `whitespace-cleanup` when composing emails
(add-hook 'mu4e-compose-mode-hook 'remove-whitespace-cleanup)

;; Add again `whitespace-cleanup` after closing the composition buffer,
;; either by sending, postponing, exiting or killing it.
(add-hook 'mu4e-compose-post-hook 'add-whitespace-cleanup)
```

but IIUC you point out this is not great because `remove-hook` rips
away `whitespace-clean` from all buffers. So let's try removing it only from
the current buffer, documentation for `remove-hook` says:

  If local is non-nil, that says to remove function from the
  buffer-local hook list instead of from the global hook list.

So:

  (remove-hook 'before-save-hook 'whitespace-cleanup t))

When I compose an email, `before-save-hook` is now described as:

  Its value is (mu4e--compose-before-save t)
  Original value was nil
  Local in buffer "No subject"; global value is
  (whitespace-cleanup)

but now whitespace-clean runs when saving this buffer.

I am really confused by all this. Is this something I should bring to
the mu4e mailing list or am I just missing something due to me not
inexperience withabout how hooks work?

Best,


More information about the emacs-berlin mailing list