diff --git a/core/core-lib.el b/core/core-lib.el index 412dbb6f9..9919e46f7 100644 --- a/core/core-lib.el +++ b/core/core-lib.el @@ -65,6 +65,19 @@ compilation." (save-silently t)) ,@forms))) +(defmacro add-transient-hook! (hook &rest forms) + "Attaches transient forms to a hook (can also be a function, which will be +advised instead). These forms will be evaluated only once when that +function/hook is first invoked, then it detaches itself." + (let ((fn (intern (format "doom--transient-hook-%s" hook)))) + `(progn + (defun ,fn (&rest _) + ,@forms + ,(cond ((functionp hook) `(advice-remove ',hook ',fn)) + ((symbolp hook) `(remove-hook ',hook ',fn)))) + ,(cond ((functionp hook) `(advice-add ',hook :before ',fn)) + ((symbolp hook) `(add-hook ',hook ',fn)))))) + (defmacro add-hook! (&rest args) "A convenience macro for `add-hook'. Takes, in order: diff --git a/modules/feature/snippets/config.el b/modules/feature/snippets/config.el index f2230eb2c..35ae0562d 100644 --- a/modules/feature/snippets/config.el +++ b/modules/feature/snippets/config.el @@ -13,10 +13,7 @@ :init ;; Ensure `yas-reload-all' is called as late as possible. Other modules could ;; have additional configuration for yasnippet. For example, file-templates. - (add-hook 'yas-minor-mode-hook '+snippets|load) - (defun +snippets|load (&rest _) - (yas-reload-all) - (remove-hook 'yas-minor-mode-hook '+snippets|load)) + (add-transient-hook! yas-minor-mode-hook (yas-reload-all)) (add-hook! (text-mode prog-mode snippet-mode markdown-mode org-mode) 'yas-minor-mode-on) diff --git a/modules/ui/doom-modeline/config.el b/modules/ui/doom-modeline/config.el index 114b7a937..9da46b066 100644 --- a/modules/ui/doom-modeline/config.el +++ b/modules/ui/doom-modeline/config.el @@ -33,10 +33,8 @@ ;; mode-line. (def-package! evil-anzu :init - (defun +evil*lazy-load-evil-anzu (&rest _) (require 'evil-anzu)) - (advice-add 'evil-ex-start-search :before '+evil*lazy-load-evil-anzu) + (add-transient-hook! evil-ex-start-search (require 'evil-anzu)) :config - (advice-remove 'evil-ex-start-search '+evil*lazy-load-evil-anzu) (setq anzu-cons-mode-line-p nil anzu-minimum-input-length 1 anzu-search-threshold 250))