Skip to content

Commit

Permalink
feat(core): add the +hook-once! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Apr 16, 2023
1 parent 09a9cea commit 1eaa535
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/me-loaddefs.el
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ triggered.
DEPTH and LOCAL are passed as is to `add-hook'.
(fn HOOK SECS FUNCTION &optional DEPTH LOCAL)" nil t)
(autoload '+hook-once! "../elisp/+minemacs" "\
Hook BODY in HOOK, it runs only once.
(fn HOOK &rest BODY)" nil t)
(function-put '+hook-once! 'lisp-indent-function 1)
(autoload '+compile-functions "../elisp/+minemacs" "\
Queue FNS to be byte/natively-compiled after a brief delay.
Expand All @@ -404,7 +409,7 @@ Load environment variables from the file saved in
Add ROOTS to ignored projects, recentf, etc.
(fn &rest ROOTS)")
(register-definition-prefixes "../elisp/+minemacs" '("+eval-when-idle-"))
(register-definition-prefixes "../elisp/+minemacs" '("+eval-when-idle-" "+hook-once-num"))


;;; Generated autoloads from ../elisp/+primitives.el
Expand Down
13 changes: 13 additions & 0 deletions elisp/+minemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ DEPTH and LOCAL are passed as is to `add-hook'."
(run-with-idle-timer ,secs nil ,function))
(add-hook ,hook #',f-name ,depth ,local))))

(defvar +hook-once-num 0)

;;;###autoload
(defmacro +hook-once! (hook &rest body)
"Hook BODY in HOOK, it runs only once."
(declare (indent 1))
(let ((fn-name (intern (format "+hook-once--function-%d-h" (cl-incf +hook-once-num)))))
`(add-hook
',hook
(defun ,fn-name ()
,(macroexp-progn body)
(remove-hook ',hook ',fn-name)))))

;; Adapted from: Doom Emacs
;;;###autoload
(defun +compile-functions (&rest fns)
Expand Down

0 comments on commit 1eaa535

Please sign in to comment.