branch: externals/vertico commit 31d01916a09a3a7dc2b5f640dfaaac5d09dfbef8 Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
Improve formatting of flat display --- extensions/vertico-flat.el | 67 +++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/extensions/vertico-flat.el b/extensions/vertico-flat.el index 10ac579..8e5a473 100644 --- a/extensions/vertico-flat.el +++ b/extensions/vertico-flat.el @@ -32,36 +32,56 @@ (require 'vertico) -(defvar vertico-flat--group-format nil) +(defcustom vertico-flat-separator + (list (propertize "{" 'face 'minibuffer-prompt) + (propertize " | " 'face 'minibuffer-prompt) + (propertize "}" 'face 'minibuffer-prompt)) + "Separator strings." + :type '(list string string string) + :group 'vertico) (defun vertico-flat--display (candidates) "Display CANDIDATES horizontally." (move-overlay vertico--candidates-ov (point-max) (point-max)) - (when (>= vertico--index 0) - (setq candidates - (seq-drop-while (lambda (cand) - (let ((face (get-text-property 0 'face cand))) - (not (if (listp face) - (memq 'vertico-current face) - (eq 'vertico-current face))))) - candidates))) - (setq candidates - (seq-map-indexed (lambda (cand idx) - (string-trim - (replace-regexp-in-string - "[ \t]+" (if (= idx 0) #(" " 0 1 (face vertico-current)) " ") - (substring cand 0 -1)))) - candidates)) (overlay-put vertico--candidates-ov 'after-string (concat #(" " 0 1 (cursor t)) (if candidates - (concat "{" (string-join candidates " | ") "}") + (concat (car vertico-flat-separator) + (string-join candidates (cadr vertico-flat-separator)) + (caddr vertico-flat-separator)) "[No match]")))) -(defun vertico-flat--affixate (_ candidates) - "Return CANDIDATES without adding annotations." - candidates) +(defun vertico-flat--format-candidates (_metadata) + "Format candidates." + (let* ((index vertico--index) + (count vertico-count) + (candidates (nthcdr vertico--index vertico--candidates)) + (width (- (window-width) + (length (car vertico-flat-separator)) + (length (caddr vertico-flat-separator)) + (point-max) + (if vertico--count-ov + (length (overlay-get vertico--count-ov 'before-string)) + 0))) + (result)) + (while (and candidates (> width 0) (> count 0)) + (let ((cand (car candidates))) + (when (string-match-p "\n" cand) + (setq cand (vertico--truncate-multiline cand width))) + (setq cand (string-trim + (replace-regexp-in-string + "[ \t]+" (if (= index vertico--index) #(" " 0 1 (face vertico-current)) " ") + (vertico--format-candidate cand "" "" index vertico--index)))) + (setq index (1+ index) + count (1- count) + width (- width (string-width cand) (length (cadr vertico-flat-separator)))) + (when (or (not result) (> width 0)) + (push cand result)) + (pop candidates))) + (unless (or (= vertico--total 0) (= index vertico--total)) + (push "…" result)) + (nreverse result))) ;;;###autoload (define-minor-mode vertico-flat-mode @@ -69,13 +89,10 @@ :global t (cond (vertico-flat-mode - (setq vertico-flat--group-format vertico-group-format - vertico-group-format nil) - (advice-add #'vertico--affixate :override #'vertico-flat--affixate) + (advice-add #'vertico--format-candidates :override #'vertico-flat--format-candidates) (advice-add #'vertico--display-candidates :override #'vertico-flat--display)) (t - (setq vertico-group-format vertico-flat--group-format) - (advice-remove #'vertico--affixate #'vertico-flat--affixate) + (advice-remove #'vertico--format-candidates #'vertico-flat--format-candidates) (advice-remove #'vertico--display-candidates #'vertico-flat--display)))) (provide 'vertico-flat)