Fix potential hashing clashes w/ transient hooks

This commit is contained in:
Henrik Lissner 2020-03-03 18:58:45 -05:00
parent f8a69464d0
commit e66769293c
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -236,6 +236,8 @@ The current file is the file from which `add-to-load-path!' is used."
(dolist (dir (list ,@dirs)) (dolist (dir (list ,@dirs))
(cl-pushnew (expand-file-name dir) load-path)))) (cl-pushnew (expand-file-name dir) load-path))))
;;; Hooks
(defvar doom--transient-counter 0)
(defmacro add-transient-hook! (hook-or-function &rest forms) (defmacro add-transient-hook! (hook-or-function &rest forms)
"Attaches a self-removing function to HOOK-OR-FUNCTION. "Attaches a self-removing function to HOOK-OR-FUNCTION.
@ -246,9 +248,12 @@ 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 (intern (format "doom--transient-%s-h" (sxhash hook-or-function))))) ;; Avoid `make-symbol' and `gensym' here because an interned symbol is
;; easier to debug in backtraces (and is visible to `describe-function')
(fn (intern (format "doom--transient-%d-h" (cl-incf doom--transient-counter)))))
`(let ((sym ,hook-or-function)) `(let ((sym ,hook-or-function))
(defun ,fn (&rest _) (defun ,fn (&rest _)
,(format "Transient hook for %S" (doom-unquote hook-or-function))
,@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))