diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/+completions.el | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lisp/+completions.el b/lisp/+completions.el new file mode 100644 index 0000000..9baaaf8 --- /dev/null +++ b/lisp/+completions.el | |||
@@ -0,0 +1,65 @@ | |||
1 | ;;; +completions.el --- Better default completions -*- lexical-binding: t; -*- | ||
2 | |||
3 | ;; From https://robbmann.io/posts/emacs-29-completions/ | ||
4 | |||
5 | (defun minibuffer@bindings () | ||
6 | (unless vertico-mode | ||
7 | ;; Up/down when completing in the minibuffer | ||
8 | (define-key minibuffer-local-map (kbd "C-p") #'minibuffer-previous-completion) | ||
9 | (define-key minibuffer-local-map (kbd "C-n") #'minibuffer-next-completion) | ||
10 | |||
11 | ;; Up/down when competing in a normal buffer | ||
12 | (define-key completion-in-region-mode-map (kbd "C-p") | ||
13 | #'minibuffer-previous-completion) | ||
14 | (define-key completion-in-region-mode-map (kbd "C-n") | ||
15 | #'minibuffer-next-completion))) | ||
16 | |||
17 | (add-hook 'vertico-mode-hook #'minibuffer@bindings) | ||
18 | |||
19 | (defun renz/sort-by-alpha-length (elems) | ||
20 | "Sort ELEMS first alphabetically, then by length." | ||
21 | (sort elems (lambda (c1 c2) | ||
22 | (or (string-version-lessp c1 c2) | ||
23 | (< (length c1) (length c2)))))) | ||
24 | |||
25 | (defun renz/sort-by-history (elems) | ||
26 | "Sort ELEMS by minibuffer history. | ||
27 | Use `mct-sort-sort-by-alpha-length' if no history is available." | ||
28 | (if-let ((hist (and (not (eq minibuffer-history-variable t)) | ||
29 | (symbol-value minibuffer-history-variable)))) | ||
30 | (minibuffer--sort-by-position hist elems) | ||
31 | (renz/sort-by-alpha-length elems))) | ||
32 | |||
33 | (defun renz/completion-category () | ||
34 | "Return completion category." | ||
35 | (when-let ((window (active-minibuffer-window))) | ||
36 | (with-current-buffer (window-buffer window) | ||
37 | (completion-metadata-get | ||
38 | (completion-metadata (buffer-substring-no-properties | ||
39 | (minibuffer-prompt-end) | ||
40 | (max (minibuffer-prompt-end) (point))) | ||
41 | minibuffer-completion-table | ||
42 | minibuffer-completion-predicate) | ||
43 | 'category)))) | ||
44 | |||
45 | (defun renz/sort-multi-category (elems) | ||
46 | "Sort ELEMS per completion category." | ||
47 | (pcase (renz/completion-category) | ||
48 | ('nil elems) ; no sorting | ||
49 | ('kill-ring elems) | ||
50 | ('project-file (renz/sort-by-alpha-length elems)) | ||
51 | (_ (renz/sort-by-history elems)))) | ||
52 | |||
53 | (setq completion-auto-help 'always | ||
54 | completion-auto-select 'second-tab | ||
55 | completions-max-height 10 | ||
56 | completions-header-format nil | ||
57 | completion-show-help nil | ||
58 | completions-format 'one-column | ||
59 | save-completions-file-name (etc/ "completions") | ||
60 | completions-sort #'renz/sort-multi-category) | ||
61 | |||
62 | (add-hook 'completion-list-mode-hook #'truncate-lines-local-mode) | ||
63 | |||
64 | (provide '+completions) | ||
65 | ;;; +completions.el ends here | ||