commit ac2c0fe61a9481a51f06567b1fee0e802f922124 parent 58884bdbd3916dc2d08ee5de6cd83eaac9038e71 Author: Vincent Demeester <vincent@sbr.pm> Date: Fri, 17 Nov 2023 16:28:15 +0100 tools/emacs: update some mct configuration Signed-off-by: Vincent Demeester <vincent@sbr.pm> Diffstat:
M | tools/emacs/config/config-completion.el | | | 44 | +++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 43 insertions(+), 1 deletion(-)
diff --git a/tools/emacs/config/config-completion.el b/tools/emacs/config/config-completion.el @@ -103,7 +103,49 @@ (mct-live-update-delay 0.6) (mct-persist-dynamic-completion t) :config - (mct-mode 1)) + (mct-mode 1) + (defun my-sort-by-alpha-length (elems) + "Sort ELEMS first alphabetically, then by length." + (sort elems (lambda (c1 c2) + (or (string-version-lessp c1 c2) + (< (length c1) (length c2)))))) + + (defun my-sort-by-history (elems) + "Sort ELEMS by minibuffer history. +Use `mct-sort-sort-by-alpha-length' if no history is available." + (if-let ((hist (and (not (eq minibuffer-history-variable t)) + (symbol-value minibuffer-history-variable)))) + (minibuffer--sort-by-position hist elems) + (my-sort-by-alpha-length elems))) + + (defun my-sort-multi-category (elems) + "Sort ELEMS per completion category." + (pcase (mct--completion-category) + ('nil elems) ; no sorting + ('kill-ring elems) + ('project-file (my-sort-by-alpha-length elems)) + (_ (my-sort-by-history elems)))) + + ;; Specify the sorting function. + (setq completions-sort #'my-sort-multi-category) + + ;; Add prompt indicator to `completing-read-multiple'. We display + ;; [`completing-read-multiple': <separator>], e.g., + ;; [`completing-read-multiple': ,] if the separator is a comma. This + ;; is adapted from the README of the `vertico' package by Daniel + ;; Mendler. I made some small tweaks to propertize the segments of + ;; the prompt. + (defun crm-indicator (args) + (cons (format "[`crm-separator': %s] %s" + (propertize + (replace-regexp-in-string + "\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" "" + crm-separator) + 'face 'error) + (car args)) + (cdr args))) + + (advice-add #'completing-read-multiple :filter-args #'crm-indicator)) (use-package marginalia :unless noninteractive