about summary refs log tree commit diff stats
path: root/.emacs.d/lisp/init-minibuffer.el
diff options
context:
space:
mode:
authorDavid Morgan <djm_uk@protonmail.com>2024-09-26 17:02:03 +0100
committerDavid Morgan <djm_uk@protonmail.com>2024-09-26 17:02:03 +0100
commit38fe5a77794a530162ef451536e0907bf76c12d3 (patch)
treebe5d72d7a3a9b7df08fefeefed1d911bd96304c3 /.emacs.d/lisp/init-minibuffer.el
parentfd03e29e94de46aefb11c14d580ae70a73023907 (diff)
downloaddotfiles-38fe5a77794a530162ef451536e0907bf76c12d3.tar.gz
Allow toggling sorting files with directories first
Diffstat (limited to '.emacs.d/lisp/init-minibuffer.el')
-rw-r--r--.emacs.d/lisp/init-minibuffer.el18
1 files changed, 17 insertions, 1 deletions
diff --git a/.emacs.d/lisp/init-minibuffer.el b/.emacs.d/lisp/init-minibuffer.el
index 2b38c81..1d5b212 100644
--- a/.emacs.d/lisp/init-minibuffer.el
+++ b/.emacs.d/lisp/init-minibuffer.el
@@ -74,12 +74,28 @@
         (select-window (minibuffer-selected-window))
       (select-window (active-minibuffer-window))))
 
+  ;; https://github.com/minad/vertico/wiki#customize-sorting-based-on-completion-category
+  (defun sort-directories-first (files)
+    ;; Still sort by history position, length and alphabetically
+    (setq files (vertico-sort-history-length-alpha files))
+    ;; But then move directories first
+    (nconc (seq-filter (lambda (x) (string-suffix-p "/" x)) files)
+           (seq-remove (lambda (x) (string-suffix-p "/" x)) files)))
+  (defun toggle-sort-directories-first ()
+    (interactive)
+    (if (eq vertico-sort-function 'sort-directories-first)
+        (set (make-local-variable 'vertico-sort-function) 'vertico-sort-history-length-alpha)
+      (set (make-local-variable 'vertico-sort-function) 'sort-directories-first))
+    (setq vertico--input t)
+    (vertico--update))
 
   :bind (("C-M-<" . up-from-outside)
          ("C-M->" . down-from-outside)
          ("C-M-+" . preview-from-outside)
          ("M-X" . to-and-fro-minibuffer)
-         (:map vertico-map ("M-RET" . minibuffer-force-complete-and-exit))))
+         (:map vertico-map
+               ("M-RET" . minibuffer-force-complete-and-exit)
+               ("M-D" . toggle-sort-directories-first))))
 
 (use-extension vertico vertico-multiform
   :config