Skip to content

Live preview

Implementing live preview requires the definition of a state or preview function as defined by ‘consult--with-preview’. The preview function receives the candidate and some action to perform (e.g., ‘'preview’). In its simplest form supporting live preview, it looks something like this:

emacs-lisp
(defun testibus--preview (action cand)
  (pcase action
    ('preview
     (with-current-buffer-window " *testibus*" 'action nil
       (erase-buffer)
       (insert (format "input: %s\n" cand))))))

See the docstring of ‘consult--with-preview’ for the lifecycle of the action argument. Once defined, we can use this preview function in ‘consult--read’:

emacs-lisp
(consult--read
 (consult--dynamic-collection
  (lambda (input callback)
    (dotimes (i 3)
      (sleep-for 0.1) ;; Simulate work
      (funcall callback (mapcar (lambda (s) (format "%s%s" s i))
                                (split-string input nil t))))))
 :prompt "run testibus: "
 :state #'testibus--preview)