I have some add-hook functions in my .emacs similar to below,
Send a notification when a pomodoro is completed:
(add-hook 'org-pomodoro-finished-hook (lambda () (message "Pomodoro finished") ))
Now I sometimes change something in these lambda functions that ran from the hook and often reload the whole .emacs (as oppose to a certain region etc..).
As a result, when pomodoro finishes, I get two notifications. It looks like add-hook adds two functions to the list.
From a high level p.o.v, what's the best way of dealing with this?
As pointed out in the answer, the
add-hook only adds the function once if the function is identical to the previous definition. The thing is that changing the function no longer makes it identical and thus you get duplication in the hook list.
I.e, Re-evaluating this does not produce multiple 'MEH' messages:
(add-hook 'org-pomodoro-finished-hook (lambda () (message "MEH") ))
However, if I change "MEH" to something else (like "BLEH"), then I'll be seeing two messages (MEH and BLEH).
The solution is to point add-hook to functions and change the functions instead of changing the hooks.
In the example below, "Meh" can be changed to "Bleh", the .emacs can be re-loaded and only "Bleh" will be displayed.
(defun my/message () (message "Meh") ) (add-hook 'org-pomodoro-finished-hook 'my/message)