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))
,@body)))
(let ((i 1))
(dolist (sym '(%2 %3 %4 %5 %6 %7 %8 %9))
(put 'fn! sym (cl-incf i))))
(setplist 'doom--fn-crawl '(%2 2 %3 3 %4 4 %5 5 %6 6 %7 7 %8 8 %9 9))
(defun doom--fn-crawl (data args)
(cond ((symbolp data)
(when-let
(pos (cond ((eq data '%*) 0)
((memq data '(% %1)) 1)
((get 'fn! data))))
((get 'doom--fn-crawl data))))
(when (and (= pos 1)
(aref args 1)
(not (eq data (aref args 1))))
@ -730,9 +728,8 @@ advised)."
(defun ,fn (&rest _)
,(format "Transient hook for %S" (doom-unquote hook-or-function))
,@forms
(let ((sym ,hook-or-function))
(cond ((functionp sym) (advice-remove sym #',fn))
((symbolp sym) (remove-hook sym #',fn))))
((symbolp sym) (remove-hook sym #',fn)))
(unintern ',fn nil))
(cond ((functionp sym)
(advice-add ,hook-or-function ,(if append? :after :before) #',fn))