Skip to content

Commit

Permalink
feat(core): add +advice-once!
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed May 19, 2024
1 parent 0fbf888 commit f3df815
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions core/me-lib.el
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,28 @@ This inhebits both the echo area and the `*Messages*' buffer."
,(macroexp-progn body)
(remove-hook ',hook ',fn-name)))))

(defvar +advice-once-num 0)

(defmacro +advice-once! (fns how &rest body)
"Run BODY as a HOW advice for FNS, and make it run only once.
FNS can be one function or a list of functions, quoted or not.
Inside BODY, you will have access to the original args as `orig-args'."
(declare (indent 2))
(let* ((fns (ensure-list (+unquote fns)))
(fn-name (intern (format "+advice-once--function-%d-h" (cl-incf +advice-once-num))))
(external-forms)
(internal-forms))
(dolist (fn fns)
(push `(advice-add ',fn ,how ',fn-name) external-forms)
(push `(advice-remove ',fn ',fn-name) internal-forms))
(macroexp-progn
(append
(list (append `(defun ,fn-name (&rest orig-args))
internal-forms body))
external-forms))))

(defcustom +first-file-hook-ignore-list nil
"A list of files to ignore in the `minemacs-first-*-file-hook'."
:group 'minemacs-core
Expand Down

0 comments on commit f3df815

Please sign in to comment.