add-hook-x
A convenience macro for add-hook, yet another missing part of Emacs.
Comparison with hook-helpers
Unlike hook-helpers, add-hook-x does not create new entities
(e.g. functions), but introduces a short notation for some patterns (which I
found annoying). So, if you have N hooks and M functions and you want to add all
functions to each hook, you can do it much shorter with add-hook-x. But if you
need to define a new function, hook-helpers really can help.
Motivation
Let's rewrite one of these patterns. For example, this.
(add-hook 'emacs-lisp-mode-hook #'enable-paredit-mode)
(add-hook 'eval-expression-minibuffer-setup-hook #'enable-paredit-mode)
(add-hook 'ielm-mode-hook #'enable-paredit-mode)
(add-hook 'lisp-mode-hook #'enable-paredit-mode)
(add-hook 'lisp-interaction-mode-hook #'enable-paredit-mode)
(add-hook 'scheme-mode-hook #'enable-paredit-mode)First, you can use add-hook-x exactly as add-hook (and remove-hook-x as
remove-hook).
(add-hook-x 'emacs-lisp-mode-hook #'enable-paredit-mode)
(add-hook-x 'eval-expression-minibuffer-setup-hook #'enable-paredit-mode)
(add-hook-x 'ielm-mode-hook #'enable-paredit-mode)
(add-hook-x 'lisp-mode-hook #'enable-paredit-mode)
(add-hook-x 'lisp-interaction-mode-hook #'enable-paredit-mode)
(add-hook-x 'scheme-mode-hook #'enable-paredit-mode)The first argument to add-hook is always a quoted symbol, and often it ends
with -hook, so, macro will add a quote and a -hook postfix to every
non-quoted symbol.
(add-hook-x emacs-lisp-mode #'enable-paredit-mode)
(add-hook-x eval-expression-minibuffer-setup #'enable-paredit-mode)
(add-hook-x ielm-mode #'enable-paredit-mode)
(add-hook-x lisp-mode #'enable-paredit-mode)
(add-hook-x lisp-interaction-mode #'enable-paredit-mode)
(add-hook-x scheme-mode #'enable-paredit-mode)The second argument is always a quoted function, again, we can omit quotation.
(add-hook-x emacs-lisp-mode enable-paredit-mode)
(add-hook-x eval-expression-minibuffer-setup enable-paredit-mode)
(add-hook-x ielm-mode enable-paredit-mode)
(add-hook-x lisp-mode enable-paredit-mode)
(add-hook-x lisp-interaction-mode enable-paredit-mode)
(add-hook-x scheme-mode enable-paredit-mode)Above, I copied the same thing five times in a row, let's get rid of this ugliness. The first argument can be a list of symbols (quoted or not).
(add-hook-x (emacs-lisp-mode
eval-expression-minibuffer-setup
ielm-mode
lisp-mode
lisp-interaction-mode
scheme-mode)
enable-paredit-mode)The second argument also can be a list of functions.