5.23 A theme-agnostic hook for theme loading

The themes are designed with the intent to be useful to Emacs users of varying skill levels, from beginners to experts. This means that we try to make things easier by not expecting anyone reading this document to be proficient in Emacs Lisp or programming in general.

Such a case is with the use of the modus-themes-after-load-theme-hook, which runs after modus-themes-toggle, modus-themes-load-operandi, or modus-themes-load-vivendi is evaluated. We recommend using that hook for advanced customizations, because (1) we know for sure that it is available once the themes are loaded, and (2) anyone consulting this manual, especially the sections on enabling and loading the themes, will be in a good position to benefit from that hook.

Advanced users who have a need to switch between the Modus themes and other items will find that such a hook does not meet their requirements: it only works with the Modus themes and only with the aforementioned functions.

A theme-agnostic setup can be configured thus:

(defvar after-enable-theme-hook nil
   "Normal hook run after enabling a theme.")

(defun run-after-enable-theme-hook (&rest _args)
   "Run `after-enable-theme-hook'."
   (run-hooks 'after-enable-theme-hook))

(advice-add 'enable-theme :after #'run-after-enable-theme-hook)

This creates the after-enable-theme-hook and makes it run after each call to enable-theme, which means that it will work for all themes and also has the benefit that it does not depend on functions such as modus-themes-toggle and the others mentioned above. enable-theme is called internally by load-theme, so the hook works everywhere.

Now this specific piece of Elisp may be simple for experienced users, but it is not easy to read for newcomers, including the author of the Modus themes for the first several months of their time as an Emacs user. Hence our hesitation to recommend it as part of the standard setup of the Modus themes (it is generally a good idea to understand what the implications are of advising a function).