Up: DIY Use a hook at the post-load-theme phase   [Index]

7.21.1 DIY A theme-agnostic hook for theme loading

[ NOTE: The following is for versions of Emacs before 29. For Emacs 29 or higher, users can rely on the built-in enable-theme-functions (Using a hook at the post-load-theme phase). ]

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 modus-themes-after-load-theme-hook, which runs after the modus-themes-load-theme function (used by the command modus-themes-toggle). 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.

The downside of the theme-agnostic hook is that any functions added to it will likely not be able to benefit from macro calls that read the active theme, such as modus-themes-with-colors. Not all Emacs themes have the same capabilities.

In this document, we cover modus-themes-after-load-theme-hook though the user can replace it with after-enable-theme-hook should they need to (provided they understand the implications).

Up: DIY Use a hook at the post-load-theme phase   [Index]