Skip to content

Commit 1eaa535

Browse files
committed
feat(core): add the +hook-once! macro
1 parent 09a9cea commit 1eaa535

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

core/me-loaddefs.el

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ triggered.
390390
DEPTH and LOCAL are passed as is to `add-hook'.
391391
392392
(fn HOOK SECS FUNCTION &optional DEPTH LOCAL)" nil t)
393+
(autoload '+hook-once! "../elisp/+minemacs" "\
394+
Hook BODY in HOOK, it runs only once.
395+
396+
(fn HOOK &rest BODY)" nil t)
397+
(function-put '+hook-once! 'lisp-indent-function 1)
393398
(autoload '+compile-functions "../elisp/+minemacs" "\
394399
Queue FNS to be byte/natively-compiled after a brief delay.
395400
@@ -404,7 +409,7 @@ Load environment variables from the file saved in
404409
Add ROOTS to ignored projects, recentf, etc.
405410
406411
(fn &rest ROOTS)")
407-
(register-definition-prefixes "../elisp/+minemacs" '("+eval-when-idle-"))
412+
(register-definition-prefixes "../elisp/+minemacs" '("+eval-when-idle-" "+hook-once-num"))
408413

409414

410415
;;; Generated autoloads from ../elisp/+primitives.el

elisp/+minemacs.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ DEPTH and LOCAL are passed as is to `add-hook'."
235235
(run-with-idle-timer ,secs nil ,function))
236236
(add-hook ,hook #',f-name ,depth ,local))))
237237

238+
(defvar +hook-once-num 0)
239+
240+
;;;###autoload
241+
(defmacro +hook-once! (hook &rest body)
242+
"Hook BODY in HOOK, it runs only once."
243+
(declare (indent 1))
244+
(let ((fn-name (intern (format "+hook-once--function-%d-h" (cl-incf +hook-once-num)))))
245+
`(add-hook
246+
',hook
247+
(defun ,fn-name ()
248+
,(macroexp-progn body)
249+
(remove-hook ',hook ',fn-name)))))
250+
238251
;; Adapted from: Doom Emacs
239252
;;;###autoload
240253
(defun +compile-functions (&rest fns)

0 commit comments

Comments
 (0)