core-lib: refactor defer-until!, add-transient-hook!

Now uses an interned hook name (that could be removed, unlike the
uninterned symbol make-symbol creates).

Also, for consistency: fun -> fn

No fun allowed!
This commit is contained in:
Henrik Lissner 2019-07-21 14:40:38 +02:00
parent b742bf086f
commit 3306eaa17c
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -158,7 +158,7 @@ Accepts the same arguments as `message'."
;; ;;
;;; Macros ;;; Sugars
(defmacro λ! (&rest body) (defmacro λ! (&rest body)
"Expands to (lambda () (interactive) ,@body)." "Expands to (lambda () (interactive) ,@body)."
@ -206,17 +206,14 @@ HOOK-OR-FUNCTION can be a quoted hook or a sharp-quoted function (which will be
advised)." advised)."
(declare (indent 1)) (declare (indent 1))
(let ((append (if (eq (car forms) :after) (pop forms))) (let ((append (if (eq (car forms) :after) (pop forms)))
(fn (if (symbolp (car forms)) (fn (intern (format "doom--transient-%s-h" (sxhash hook-or-function)))))
(intern (format "doom--transient-%s-h" (pop forms)))
(make-symbol "doom--transient-h"))))
`(let ((sym ,hook-or-function)) `(let ((sym ,hook-or-function))
(fset ',fn (defun ,fn (&rest _)
(lambda (&rest _)
,@forms ,@forms
(let ((sym ,hook-or-function)) (let ((sym ,hook-or-function))
(cond ((functionp sym) (advice-remove sym #',fn)) (cond ((functionp sym) (advice-remove sym #',fn))
((symbolp sym) (remove-hook sym #',fn)))) ((symbolp sym) (remove-hook sym #',fn))))
(unintern ',fn nil))) (unintern ',fn nil))
(cond ((functionp sym) (cond ((functionp sym)
(advice-add ,hook-or-function ,(if append :after :before) #',fn)) (advice-add ,hook-or-function ,(if append :after :before) #',fn))
((symbolp sym) ((symbolp sym)
@ -487,16 +484,16 @@ serve as a predicated alternative to `after!'."
(declare (indent defun) (debug t)) (declare (indent defun) (debug t))
`(if ,condition `(if ,condition
(progn ,@body) (progn ,@body)
,(let ((fun (make-symbol "doom--delay-form-h"))) ,(let ((fn (intern (format "doom--delay-form-%s-h" (sxhash (cons condition body))))))
`(progn `(progn
(fset ',fun (lambda (&rest args) (fset ',fn (lambda (&rest args)
(when ,(or condition t) (when ,(or condition t)
(remove-hook 'after-load-functions #',fun) (remove-hook 'after-load-functions #',fn)
(unintern ',fun nil) (unintern ',fn nil)
(ignore args) (ignore args)
,@body))) ,@body)))
(put ',fun 'permanent-local-hook t) (put ',fn 'permanent-local-hook t)
(add-hook 'after-load-functions #',fun))))) (add-hook 'after-load-functions #',fn)))))
(defmacro defer-feature! (feature &optional mode) (defmacro defer-feature! (feature &optional mode)
"Pretend FEATURE hasn't been loaded yet, until FEATURE-hook is triggered. "Pretend FEATURE hasn't been loaded yet, until FEATURE-hook is triggered.