diff options
Diffstat (limited to 'emacs/inits/11-completion.el')
| -rw-r--r-- | emacs/inits/11-completion.el | 192 |
1 files changed, 73 insertions, 119 deletions
diff --git a/emacs/inits/11-completion.el b/emacs/inits/11-completion.el index 149a7b8..aa12538 100644 --- a/emacs/inits/11-completion.el +++ b/emacs/inits/11-completion.el @@ -1,4 +1,4 @@ -;; Time-stamp: <2024-09-24 17h43 EDT (6e0a849e)> +;; Time-stamp: <2026-01-02 16h17 GMT (d63c23ed)> (use-package savehist :ensure t @@ -25,131 +25,85 @@ ;; Enable recursive minibuffers (setq enable-recursive-minibuffers t)) -;; ----------------------------------------------------------------------------- -;; Vertico +(use-package icomplete + :hook ((after-init + . (lambda () (icomplete-vertical-mode 1))) + (icomplete-minibuffer-setup + . (lambda () (setq truncate-lines t)))) -(use-package vertico - :ensure t - :bind (:map vertico-map - ("<tab>" . vertico-insert) - ("DEL" . vertico-directory-delete-char)) :init - (vertico-mode) - (setq vertico-cycle t) - (setq completion-styles '(substring))) - -;; ----------------------------------------------------------------------------- -;; Orderless + (defun my-completing-read-in-region (start end collection &optional predicate) + "Prompt for completion of region in the minibuffer if non-unique. + Use as a value for `completion-in-region-function'." + (let* ((initial (buffer-substring-no-properties start end)) + (all (completion-all-completions initial collection predicate + (length initial))) + (completion (cond + ((atom all) nil) + ((and (consp all) (atom (cdr all))) (car all)) + (t (completing-read + "Completion: " collection predicate t initial))))) + (cond (completion (completion--replace start end completion) t) + (t (message "No completion") nil)))) + + (defun my-icomplete-backspace () + (interactive) + (if (looking-back "[^/]/" (line-beginning-position)) + (backward-kill-word 1) + (delete-backward-char 1))) -(use-package orderless - :ensure t + (defun my-switch-to-buffer-with-recentf () + "Switch to buffer or recent file with visual separation." + (interactive) + (let* ((current-buffer (buffer-name (current-buffer))) + (buffers (seq-remove + (lambda (b) (or (string-prefix-p " " b) (string= b current-buffer))) + (mapcar #'buffer-name (buffer-list)))) + (recent-files (seq-take recentf-list 20)) + (separator "─── Recent Files ───") + (buffer-cands (mapcar (lambda (buf) + (propertize buf 'multi-category (cons 'buffer buf))) + buffers)) + (header-cand (propertize separator 'multi-category (cons 'separator t))) + (file-cands (mapcar (lambda (file) + (propertize file 'multi-category (cons 'file file))) + recent-files)) + (all-cands (append buffer-cands (list header-cand) file-cands)) + (my-table (lambda (string pred action) + (pcase action + ('metadata '(metadata (category . multi-category) (cycle-sort-function . identity))) + ('all all-cands) + ('lambda (or (try-completion string all-cands pred) string)) + (_ (seq-filter (lambda (c) (string-prefix-p string (substring-no-properties c))) + all-cands)))))) + + (let ((result (completing-read "Switch to: " my-table nil t))) + (unless (string-empty-p result) + (cond + ((string= result separator) nil) + ((file-exists-p result) (find-file result)) + (t (switch-to-buffer result))))))) + + :bind (("C-x b" . my-switch-to-buffer-with-recentf) + (:map icomplete-minibuffer-map + ("TAB" . icomplete-force-complete) + ("RET" . icomplete-force-complete-and-exit) + ("<backspace>" . my-icomplete-backspace))) :config - (setq orderless-component-separator 'orderless-escapable-split-on-space - completion-styles '(orderless basic) - completion-category-overrides '((file (styles basic))))) + (setq icomplete-compute-delay 0 + icomplete-show-matches-on-no-input t + icomplete-hide-common-prefix nil + icomplete-prospects-height 10 + icomplete-max-delay-chars 0 + icomplete-scroll t) -;; ----------------------------------------------------------------------------- -;; Consult - -(use-package consult - :ensure t - :bind (;; C-c bindings (mode-specific-map) - ("C-c M-x" . consult-mode-command) - ("C-c h" . consult-history) - ("C-c k" . consult-kmacro) - ("C-c m" . consult-man) - ("C-c i" . consult-info) - ([remap Info-search] . consult-info) - ;; C-x bindings (ctl-x-map) - ("C-x M-:" . consult-complex-command) ;; orig. repeat-complex-command - ("C-x b" . consult-buffer) ;; orig. switch-to-buffer - ("C-x 4 b" . consult-buffer-other-window) ;; orig. switch-to-buffer-other-window - ("C-x 5 b" . consult-buffer-other-frame) ;; orig. switch-to-buffer-other-frame - ("C-x t b" . consult-buffer-other-tab) ;; orig. switch-to-buffer-other-tab - ("C-x r b" . consult-bookmark) ;; orig. bookmark-jump - ("C-x p b" . consult-project-buffer) ;; orig. project-switch-to-buffer - ;; Custom M-# bindings for fast register access - ("M-#" . consult-register-load) - ("M-'" . consult-register-store) ;; orig. abbrev-prefix-mark (unrelated) - ("C-M-#" . consult-register) - ;; Other custom bindings - ("M-y" . consult-yank-pop) ;; orig. yank-pop - ("<help> a" . consult-apropos) ;; orig. apropos-command - ;; M-g bindings (goto-map) - ("M-g e" . consult-compile-error) - ("M-g M-g" . consult-goto-line) ;; orig. goto-line - ("M-g g" . consult-goto-line) - ("M-g o" . consult-outline) ;; Alternative: consult-org-heading - ("M-g m" . consult-mark) - ("M-g k" . consult-global-mark) - ("M-g i" . consult-imenu) - ("M-g I" . consult-imenu-multi) - ;; M-s bindings (search-map) - ("M-s d" . consult-find) ;; Alternative: consult-fd - ("M-s c" . consult-locate) - ("M-s g" . consult-grep) - ("M-s G" . consult-git-grep) - ("M-s r" . consult-ripgrep) - ("M-s l" . consult-line) - ("M-s L" . consult-line-multi) - ("M-s k" . consult-keep-lines) - ("M-s u" . consult-focus-lines) - ) - :hook (completion-list-mode . consult-preview-at-point-mode) - :init - ;; Optionally configure the register formatting. This improves the register - ;; preview for `consult-register', `consult-register-load', - ;; `consult-register-store' and the Emacs built-ins. - (setq register-preview-delay 0 - register-preview-function #'consult-register-format) + (setq completion-styles '(flex basic partial-completion) + completion-ignore-case t) - ;; Optionally tweak the register preview window. - ;; This adds thin lines, sorting and hides the mode line of the window. - (advice-add #'register-preview :override #'consult-register-window) + (advice-add 'completion-at-point + :after #'minibuffer-hide-completions) - ;; Use Consult to select xref locations with preview - (setq xref-show-xrefs-function #'consult-xref - xref-show-definitions-function #'consult-xref) - - :config - (defun previous-history-or-next () - (interactive) - (if (string= "" (minibuffer-contents-no-properties)) - (progn - (previous-history-element 1) - (end-of-line)) - (vertico-next))) - - (defvar my-consult-line-map - (let ((map (make-sparse-keymap))) - (define-key map "\C-s" #'previous-history-or-next) - map)) - - (consult-customize consult-line :keymap my-consult-line-map - consult-ripgrep - consult-git-grep - consult-grep - consult-bookmark - consult-recent-file - consult-xref - consult--source-bookmark - consult--source-file-register - consult--source-recent-file - consult--source-project-recent-file) - - ;; Optionally configure the narrowing key. - ;; Both < and C-+ work reasonably well. - (setq consult-narrow-key "<") ;; (kbd "C-+") - - (setq consult-buffer-sources '(consult--source-hidden-buffer - consult--source-modified-buffer - consult--source-buffer - consult--source-recent-file - consult--source-file-register - consult--source-bookmark)) - - ;; Ues consult for capfs - (setq completion-in-region-function #'consult-completion-in-region)) + (setq completion-in-region-function #'my-completing-read-in-region)) ;; ----------------------------------------------------------------------------- ;; Marginalia |
