Breaking change: rewrite add-transient-hook!

HOOK is now evaluated. Hooks should be quoted (and functions
sharp-quoted).

This also fixes commit 0150f78e.
This commit is contained in:
Henrik Lissner 2017-06-05 16:41:39 +02:00
parent 0d9c0e7f54
commit baad7953bf
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
6 changed files with 26 additions and 18 deletions

View file

@ -93,20 +93,28 @@ compilation."
(save-silently t)) (save-silently t))
,@forms))) ,@forms)))
(defvar doom--transient-counter 0)
(defmacro add-transient-hook! (hook &rest forms) (defmacro add-transient-hook! (hook &rest forms)
"Attaches transient forms to a hook (can also be a function, which will be "Attaches transient forms to a HOOK.
advised instead). These forms will be evaluated only once when that
function/hook is first invoked, then it detaches itself." HOOK can be a quoted hook or a sharp-quoted function (which will be advised).
These forms will be evaluated once when that function/hook is first invoked,
then it detaches itself."
(declare (indent 1)) (declare (indent 1))
(let ((fn (intern (format "doom--transient-hook-%s" hook))) (let ((append (eq (car forms) :after))
(append (eq (car forms) :after))) (fn (intern (format "doom-transient-hook-%s" (cl-incf doom--transient-counter)))))
`(progn `(when ,hook
(defun ,fn (&rest _) (fset ',fn
,@forms (lambda (&rest _)
,(cond ((functionp hook) `(advice-remove #',hook #',fn)) ,@forms
((symbolp hook) `(remove-hook ',hook #',fn)))) (cond ((functionp ,hook) (advice-remove ,hook #',fn))
,(cond ((functionp hook) `(advice-add #',hook ,(if append :after :before) #',fn)) ((symbolp ,hook) (remove-hook ,hook #',fn)))
((symbolp hook) `(add-hook ',hook #',fn ,append)))))) (unintern ',fn nil)))
(cond ((functionp ,hook)
(advice-add ,hook ,(if append :after :before) #',fn))
((symbolp ,hook)
(add-hook ,hook #',fn ,append))))))
(defmacro add-hook! (&rest args) (defmacro add-hook! (&rest args)
"A convenience macro for `add-hook'. Takes, in order: "A convenience macro for `add-hook'. Takes, in order:

View file

@ -285,7 +285,7 @@ properties."
"Force spawned term buffer to share with the eshell popup (if necessary)." "Force spawned term buffer to share with the eshell popup (if necessary)."
(when (doom-popup-p) (when (doom-popup-p)
(set-window-dedicated-p nil nil) (set-window-dedicated-p nil nil)
(add-transient-hook! eshell-query-kill-processes :after (add-transient-hook! #'eshell-query-kill-processes :after
(set-window-dedicated-p nil t))) (set-window-dedicated-p nil t)))
(apply orig-fn args)) (apply orig-fn args))
(advice-add #'eshell-exec-visual :around #'doom*eshell-undedicate-popup)) (advice-add #'eshell-exec-visual :around #'doom*eshell-undedicate-popup))

View file

@ -151,7 +151,7 @@ see if NAME should be activated.
(add-hook! ,name (add-hook! ,name
(run-hook-with-args 'doom-project-hook ',name)) (run-hook-with-args 'doom-project-hook ',name))
,(when init-form ,(when init-form
`(add-transient-hook! ',(intern-soft (format "%s-hook" name)) `(add-transient-hook! ',(intern (format "%s-hook" name))
,init-form))))) ,init-form)))))
(provide 'core-projects) (provide 'core-projects)

View file

@ -13,7 +13,7 @@
:init :init
;; Ensure `yas-reload-all' is called as late as possible. Other modules could ;; Ensure `yas-reload-all' is called as late as possible. Other modules could
;; have additional configuration for yasnippet. For example, file-templates. ;; have additional configuration for yasnippet. For example, file-templates.
(add-transient-hook! yas-minor-mode-hook (yas-reload-all)) (add-transient-hook! 'yas-minor-mode-hook (yas-reload-all))
(add-hook! (text-mode prog-mode snippet-mode) (add-hook! (text-mode prog-mode snippet-mode)
#'yas-minor-mode-on) #'yas-minor-mode-on)

View file

@ -36,7 +36,7 @@
(def-package! evil-anzu (def-package! evil-anzu
:when (featurep 'evil) :when (featurep 'evil)
:init :init
(add-transient-hook! evil-ex-start-search (require 'evil-anzu)) (add-transient-hook! #'evil-ex-start-search (require 'evil-anzu))
:config :config
(setq anzu-cons-mode-line-p nil (setq anzu-cons-mode-line-p nil
anzu-minimum-input-length 1 anzu-minimum-input-length 1

View file

@ -44,14 +44,14 @@
;; `add-transient-hook!' ;; `add-transient-hook!'
(ert-deftest transient-hooks () (ert-deftest transient-hooks ()
(let (hooks value) (let (hooks value)
(add-transient-hook! hooks (setq value t)) (add-transient-hook! 'hooks (setq value t))
(run-hooks 'hooks) (run-hooks 'hooks)
(should (eq value t)) (should (eq value t))
(should (null hooks)))) (should (null hooks))))
(ert-deftest transient-function () (ert-deftest transient-function ()
(let (value) (let (value)
(add-transient-hook! ignore (setq value (not value))) (add-transient-hook! #'ignore (setq value (not value)))
(ignore t) (ignore t)
(should (eq value t)) (should (eq value t))
;; repeat to ensure it was only run once ;; repeat to ensure it was only run once