refactor(lib): simplify fn!, add-transient-hook!

* lisp/doom-lib.el:
  - (fn!): unroll the loop into a single, fast setplist that
    doesn't require a cl-lib macro (autoloading which seems to throw an
    error on flatpak/snap builds; still investigating that one).
  - (add-transient-hook!): Removes a redundant let-bind.  `sym` is
    already lexically bound outside the function. This will break
    anywhere lexical-binding is nil though. Not sure if I should cater
    to that scenario...
This commit is contained in:
Henrik Lissner 2022-09-24 10:48:00 +02:00
parent 6c76b98dbb
commit a4b58311da
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -443,15 +443,13 @@ ARGLIST."
(allow-other-keys arglist)) (allow-other-keys arglist))
,@body))) ,@body)))
(let ((i 1)) (setplist 'doom--fn-crawl '(%2 2 %3 3 %4 4 %5 5 %6 6 %7 7 %8 8 %9 9))
(dolist (sym '(%2 %3 %4 %5 %6 %7 %8 %9))
(put 'fn! sym (cl-incf i))))
(defun doom--fn-crawl (data args) (defun doom--fn-crawl (data args)
(cond ((symbolp data) (cond ((symbolp data)
(when-let (when-let
(pos (cond ((eq data '%*) 0) (pos (cond ((eq data '%*) 0)
((memq data '(% %1)) 1) ((memq data '(% %1)) 1)
((get 'fn! data)))) ((get 'doom--fn-crawl data))))
(when (and (= pos 1) (when (and (= pos 1)
(aref args 1) (aref args 1)
(not (eq data (aref args 1)))) (not (eq data (aref args 1))))
@ -730,9 +728,8 @@ advised)."
(defun ,fn (&rest _) (defun ,fn (&rest _)
,(format "Transient hook for %S" (doom-unquote hook-or-function)) ,(format "Transient hook for %S" (doom-unquote hook-or-function))
,@forms ,@forms
(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))