Skip to content

Instantly share code, notes, and snippets.

@bdarcus
Created June 30, 2021 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdarcus/734689a8e3dbfa402130ba11c8e55556 to your computer and use it in GitHub Desktop.
Save bdarcus/734689a8e3dbfa402130ba11c8e55556 to your computer and use it in GitHub Desktop.
vertico crm experiments
; some experimental code from minad, to improve CRM experience
(defun vertico--crm-indicator (args)
"Add prompt indicator, modify ARGS passed to `completing-read-multiple'."
(cons (concat "[CRM] " (car args)) (cdr args)))
(defun vertico--crm-separator (&rest _)
"Add separator between selected items, when inserting."
(when (eq minibuffer-completion-table #'crm--collection-fn)
;; You may want to use something else than ", ".
(insert ", ")))
(defun vertico--crm-remove-selected (candidates)
"Remove already selected items from the CANDIDATES."
(if (eq minibuffer-completion-table #'crm--collection-fn)
(pcase-let*
((`(,base ,_ ,all) candidates)
(items (split-string
(substring (minibuffer-contents-no-properties) 0 base)
crm-separator t)))
(setq all (cl-delete-if (lambda (x) (member x items)) all))
(list base (length all) all))
candidates))
(advice-add #'completing-read-multiple
:filter-args #'vertico--crm-indicator)
(advice-add #'vertico-insert
:after #'vertico--crm-separator)
(advice-add #'vertico--recompute-candidates
:filter-return #'vertico--crm-remove-selected)
; https://github.com/minad/vertico/issues/1#issuecomment-817378284
; You can write a function which you register as an after-change-function hook in the minibuffer.
; This function splits the already selected candidates and replaces the candidates with
; (propertize candidate 'display "short-candidate-version").
(minibuffer-with-setup-hook
(lambda ()
(add-hook 'after-change-functions
(lambda (&rest _)
(save-excursion
(save-match-data
(let ((pos (minibuffer-prompt-end)))
(goto-char pos)
(while (< pos (point-max))
(when (re-search-forward (concat crm-separator "\\|\\'") nil 'noerror)
(when-let (short (cdr (assoc (string-trim (buffer-substring-no-properties
pos (match-beginning 0)))
candidates)))
(put-text-property pos (match-beginning 0) 'display short))
(setq pos (point))))))))
nil 'local))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment