Skip to content

Instantly share code, notes, and snippets.

@pkkm
Created April 21, 2013 11:48
Show Gist options
  • Save pkkm/5429343 to your computer and use it in GitHub Desktop.
Save pkkm/5429343 to your computer and use it in GitHub Desktop.
The `add-hook-one-time` macro that doesn't work and some evaluation results.
;;; The macro:
(defmacro add-hook-one-time (hook function)
"Add FUNCTION to HOOK. Remove it after its first execution."
(let ((wrapper-function (make-symbol "wrapper-function-symbol")))
`(progn
(defun ,wrapper-function ()
"Wrapper function that will be executed only once, and then removed from the hook."
(funcall ,function)
(remove-hook ,hook ,wrapper-function))
(add-hook ,hook ,wrapper-function))))
;;; Evaluated form:
(macroexpand
'(add-hook-one-time 'emacs-lisp-mode-hook
(lambda ()
(message "Hello, world!"))))
;;; Result:
(progn
(defun wrapper-function-symbol nil
"Wrapper function that will be executed only once, and then removed from the hook."
(funcall
(lambda nil
(message "Hello, world!")))
(remove-hook 'emacs-lisp-mode-hook wrapper-function-symbol))
(add-hook 'emacs-lisp-mode-hook wrapper-function-symbol))
;;; Evaluated form:
(add-hook-one-time 'emacs-lisp-mode-hook
(lambda () (message "Hello, world!")))
;;; Error:
Symbol's value as variable is void: wrapper-function-symbol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment