Function: minibuffer-with-setup-hook

minibuffer-with-setup-hook is a macro defined in files.el.gz.

Signature

(minibuffer-with-setup-hook FUN &rest BODY)

Documentation

Temporarily add FUN to minibuffer-setup-hook while executing BODY.

By default, FUN is prepended to minibuffer-setup-hook. But if FUN is of the form (:append FUN1), FUN1 will be appended to minibuffer-setup-hook instead of prepending it.

BODY should use the minibuffer at most once. Recursive uses of the minibuffer are unaffected (FUN is not called additional times).

This macro actually adds an auxiliary function that calls FUN, rather than FUN itself, to minibuffer-setup-hook.

Probably introduced at or before Emacs version 25.1.

Aliases

ediff-minibuffer-with-setup-hook (obsolete since 28.1)

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defmacro minibuffer-with-setup-hook (fun &rest body)
  "Temporarily add FUN to `minibuffer-setup-hook' while executing BODY.

By default, FUN is prepended to `minibuffer-setup-hook'.  But if FUN is of
the form `(:append FUN1)', FUN1 will be appended to `minibuffer-setup-hook'
instead of prepending it.

BODY should use the minibuffer at most once.
Recursive uses of the minibuffer are unaffected (FUN is not
called additional times).

This macro actually adds an auxiliary function that calls FUN,
rather than FUN itself, to `minibuffer-setup-hook'."
  (declare (indent 1) (debug ([&or (":append" form) [&or symbolp form]] body)))
  (let ((hook (make-symbol "setup-hook"))
        (funsym (make-symbol "fun"))
        (append nil))
    (when (eq (car-safe fun) :append)
      (setq append '(t) fun (cadr fun)))
    `(let ((,funsym ,fun)
           ;; Use a symbol to make sure `add-hook' doesn't waste time
           ;; in `equal'ity testing (bug#46326).
           (,hook (make-symbol "minibuffer-setup")))
       (fset ,hook (lambda ()
                     ;; Clear out this hook so it does not interfere
                     ;; with any recursive minibuffer usage.
                     (remove-hook 'minibuffer-setup-hook ,hook)
                     (funcall ,funsym)))
       (unwind-protect
           (progn
             (add-hook 'minibuffer-setup-hook ,hook ,@append)
             ,@body)
         (remove-hook 'minibuffer-setup-hook ,hook)))))