Function: consult--with-async-f

consult--with-async-f is a natively compiled function defined in consult.el.

Signature

(consult--with-async-f ASYNC BODY)

Documentation

See consult--with-async for documentation.

Source Code

;; Defined in /nix/store/6higv2v1wk916v4ynnybyrdkfy2gq44d-emacs-packages-deps/share/emacs/site-lisp/elpa/consult-20261004.957/consult.el
(defun consult--with-async-f (async body)
  "See `consult--with-async' for documentation."
  (minibuffer-with-setup-hook
      ;; Append such that we overwrite the completion style setting of
      ;; `fido-mode'.  See `consult--async-split' and `consult--split-setup'.
      (:append
       (lambda ()
         (when (consult--async-p async)
           (funcall async 'setup)
           (let* ((mb (current-buffer))
                  (fun (lambda ()
                         (when-let* ((win (active-minibuffer-window)))
                           (when (eq (window-buffer win) mb)
                             (with-current-buffer mb
                               (let ((inhibit-modification-hooks t))
                                 ;; Push input string to request refresh.
                                 (funcall async (minibuffer-contents-no-properties))))))))
                  ;; We use a symbol in order to avoid adding lambdas to the
                  ;; hook variable.  Symbol indirection because of bug#46407.
                  (hook (make-symbol "consult--async-after-change"))
                  (timer (timer-create)))
             (timer-set-function timer fun)
             ;; Delay modification hook to ensure that minibuffer is still alive
             ;; after the change, such that we don't restart a new asynchronous
             ;; search right before exiting the minibuffer.
             (fset hook (lambda (&rest _)
                          (unless (memq timer timer-list)
                            (timer-set-time timer (current-time))
                            (timer-activate timer))))
             (add-hook 'after-change-functions hook nil 'local)
             ;; Immediately start asynchronous computation. This may lead to
             ;; problems unnecessary work if content is inserted shortly
             ;; afterwards.
             (funcall fun)))))
    (let ((async (if (consult--async-p async) async (lambda (_) async))))
      (unwind-protect
          (funcall body async)
        (funcall async 'destroy)))))