From d528ab4b131e8b6c47af801bf5bd05e252aef18a Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 13 Jan 2023 22:52:44 -0600 Subject: Add +completions.el I don't use it but it's there for ... fun --- lisp/+completions.el | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lisp/+completions.el 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 @@ +;;; +completions.el --- Better default completions -*- lexical-binding: t; -*- + +;; From https://robbmann.io/posts/emacs-29-completions/ + +(defun minibuffer@bindings () + (unless vertico-mode + ;; Up/down when completing in the minibuffer + (define-key minibuffer-local-map (kbd "C-p") #'minibuffer-previous-completion) + (define-key minibuffer-local-map (kbd "C-n") #'minibuffer-next-completion) + + ;; Up/down when competing in a normal buffer + (define-key completion-in-region-mode-map (kbd "C-p") + #'minibuffer-previous-completion) + (define-key completion-in-region-mode-map (kbd "C-n") + #'minibuffer-next-completion))) + +(add-hook 'vertico-mode-hook #'minibuffer@bindings) + +(defun renz/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 renz/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) + (renz/sort-by-alpha-length elems))) + +(defun renz/completion-category () + "Return completion category." + (when-let ((window (active-minibuffer-window))) + (with-current-buffer (window-buffer window) + (completion-metadata-get + (completion-metadata (buffer-substring-no-properties + (minibuffer-prompt-end) + (max (minibuffer-prompt-end) (point))) + minibuffer-completion-table + minibuffer-completion-predicate) + 'category)))) + +(defun renz/sort-multi-category (elems) + "Sort ELEMS per completion category." + (pcase (renz/completion-category) + ('nil elems) ; no sorting + ('kill-ring elems) + ('project-file (renz/sort-by-alpha-length elems)) + (_ (renz/sort-by-history elems)))) + +(setq completion-auto-help 'always + completion-auto-select 'second-tab + completions-max-height 10 + completions-header-format nil + completion-show-help nil + completions-format 'one-column + save-completions-file-name (etc/ "completions") + completions-sort #'renz/sort-multi-category) + +(add-hook 'completion-list-mode-hook #'truncate-lines-local-mode) + +(provide '+completions) +;;; +completions.el ends here -- cgit 1.4.1-21-gabe81