From a4b58311daced01a5d5ab32212e5f5b1a259512d Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 24 Sep 2022 10:48:00 +0200 Subject: [PATCH] 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... --- lisp/doom-lib.el | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lisp/doom-lib.el b/lisp/doom-lib.el index e2c89bf76..4110e792f 100644 --- a/lisp/doom-lib.el +++ b/lisp/doom-lib.el @@ -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)))) + (cond ((functionp sym) (advice-remove sym #',fn)) + ((symbolp sym) (remove-hook sym #',fn))) (unintern ',fn nil)) (cond ((functionp sym) (advice-add ,hook-or-function ,(if append? :after :before) #',fn))