Skip to content

ivy display function

Shaun edited this page May 24, 2020 · 7 revisions

Intro

By default, Ivy displays the candidate list in the minibuffer. However, it’s very easy to make it display where you want: in a different window, or a frame, or an overlay etc.

Example:

(setq ivy-display-functions-alist
      '((counsel-M-x . ivy-display-function-lv)
        (ivy-completion-in-region . ivy-display-function-overlay)))

The display with lv-message and popup-tip is more of a proof-of-concept; they are provided for reference. The display with ivy-overlay is higher quality and already enabled by default.

Display with ivy-overlay

Here’s a demo of calling C-M-i (complete-symbol) with ivy-mode active. The input and candidates are displayed at point in an overlay.

Demo:

Display with ivy-posframe

See ivy-posframe.

Display candidates with lv

(require 'lv)

(defun ivy-display-function-lv (text)
  (let ((lv-force-update t))
    (lv-message
     (replace-regexp-in-string
      "%" "%%"
      (if (string-match "\\`\n" text)
          (substring text 1)
        text)))))

Display candidates with popup

(require 'popup)

(defun ivy-display-function-popup (text)
  (with-ivy-window
    (popup-tip
     (setq ivy-insert-debug
           (substring text 1))
     :nostrip t)))

Display candidates with window

(defun ivy-display-function-window (text)
  (let ((buffer (get-buffer-create "*ivy-candidate-window*"))
        (str (with-current-buffer (get-buffer-create " *Minibuf-1*")
               (let ((point (point))
                     (string (concat (buffer-string) "  " text)))
                 (add-face-text-property
                  (- point 1) point 'ivy-cursor t string)
                 string))))
    (with-current-buffer buffer
      (let ((inhibit-read-only t))
        (erase-buffer)
        (insert str)))
    (with-ivy-window
      (display-buffer
       buffer
       `((display-buffer-reuse-window
          display-buffer-below-selected)
         (window-height . ,(1+ (ivy--height (ivy-state-caller ivy-last)))))))))