feat(minibuffer): prioritize basename matches in vertico file completion
This commit is contained in:
@@ -115,7 +115,33 @@
|
|||||||
(setq read-file-name-completion-ignore-case t
|
(setq read-file-name-completion-ignore-case t
|
||||||
read-buffer-completion-ignore-case t
|
read-buffer-completion-ignore-case t
|
||||||
completion-ignore-case t)
|
completion-ignore-case t)
|
||||||
)
|
:config
|
||||||
|
;; Prioriza el basename sobre la ruta en comandos de archivo (categorías
|
||||||
|
;; `file' y `project-file'); en el resto respeta el orden normal. Rank:
|
||||||
|
;; 0 exacto (nombre o sin extensión), 1 prefijo, 2 subcadena, 3 solo carpeta.
|
||||||
|
(defun kj/vertico--rank (cand query)
|
||||||
|
"Return match rank of CAND's basename against QUERY (0 best, 3 worst)."
|
||||||
|
(let ((name (downcase (file-name-nondirectory (directory-file-name cand)))))
|
||||||
|
(cond ((or (string= name query) (string= (file-name-sans-extension name) query)) 0)
|
||||||
|
((string-prefix-p query name) 1)
|
||||||
|
((string-search query name) 2)
|
||||||
|
(t 3))))
|
||||||
|
|
||||||
|
(defun kj/vertico-sort (cands)
|
||||||
|
"Prioritize basename matches for file candidates; default order otherwise."
|
||||||
|
(let* ((base (or (vertico--metadata-get 'display-sort-function)
|
||||||
|
vertico-sort-function))
|
||||||
|
(cands (if base (funcall base (copy-sequence cands)) cands))
|
||||||
|
(query (and (memq (vertico--metadata-get 'category) '(file project-file))
|
||||||
|
(car (last (split-string (minibuffer-contents) "[ \t\n]+" t))))))
|
||||||
|
(if (or (null query) (string-suffix-p "/" query))
|
||||||
|
cands
|
||||||
|
(setq query (downcase (file-name-nondirectory query)))
|
||||||
|
(mapcar #'cdr (sort (mapcar (lambda (c) (cons (my/vertico--rank c query) c))
|
||||||
|
cands)
|
||||||
|
#'car-less-than-car)))))
|
||||||
|
|
||||||
|
(setq vertico-sort-override-function #'kj/vertico-sort))
|
||||||
|
|
||||||
(provide 'init-minibuffer)
|
(provide 'init-minibuffer)
|
||||||
;;; init-minibuffer.el ends here
|
;;; init-minibuffer.el ends here
|
||||||
|
|||||||
Reference in New Issue
Block a user