Add multiple functions as interceptors to Emacs
How can I simplify something like the following code in a file init.el
?
(add-hook 'org-mode-hook 'turn-on-auto-revert-mode)
(add-hook 'org-mode-hook 'turn-on-org-cdlatex)
(add-hook 'org-mode-hook 'smartparens-mode)
(add-hook 'org-mode-hook 'abbrev-mode)
I have several other lines like, including some lambda functions added in org-mode-hook
...
Personally, I highly recommend adding functions lambda
to hooks. The main reason is that if you change content and reevaluate the expression add-hook
, the hook contains both the old and the new lambda expression. The second reason is that it looks bad when looking at the hook, it is better to see the function name compared to a large lambda expression.
Instead, I would suggest using:
(defun my-org-mode-hook ()
(turn-on-auto-revert-mode)
(turn-on-org-cdlatex)
(smartparens-mode 1)
(abbrev-mode 1)))
(add-hook 'org-mode-hook 'my-org-mode-hook)
Note: you can use global-auto-revert-mode
to enable auto-recovery on all buffers, so you don't need to enable it for all major modes.
I am using a simple one dolist
:
(dolist (fn '(turn-on-auto-revert-mode
turn-on-org-cdlatex
smartparens-mode
abbrev-mode))
(add-hook 'org-mode-hook fn))
This will allow you to still remove individual hooks from remove-hook
or from the customization interface.