Files
emacs-config/lisp/init-completion.el
2025-09-08 12:14:12 +02:00

205 lines
7.2 KiB
EmacsLisp

;;; init-completion.el --- Modern completion configuration -*- lexical-binding: t -*-
;;; Commentary:
;;; Configuration for Vertico, Consult, Orderless, Marginalia, and Embark
;;; This replaces Helm with a lighter, faster, more native completion system
;;; Code:
;;; Vertico - Vertical completion UI
(use-package vertico
:ensure t
:init
(vertico-mode)
:config
;; Different scroll margin
(setq vertico-scroll-margin 0)
;; Show more candidates
(setq vertico-count 15)
;; Grow and shrink the Vertico minibuffer
(setq vertico-resize t)
;; Enable cycling
(setq vertico-cycle t))
;;; Savehist - Persist history over Emacs restarts
(use-package savehist
:ensure nil
:init
(savehist-mode))
;;; Orderless - Powerful completion style
(use-package orderless
:ensure t
:custom
(completion-styles '(orderless basic))
(completion-category-overrides '((file (styles basic partial-completion)))))
;;; Marginalia - Rich annotations in minibuffer
(use-package marginalia
:ensure t
:init
(marginalia-mode)
:bind (:map minibuffer-local-map
("M-A" . marginalia-cycle)))
;;; Consult - Practical commands based on completing-read
(use-package consult
:ensure t
:bind (;; C-c bindings (mode-specific-map)
("C-c h" . consult-history)
("C-c m" . consult-mode-command)
("C-c k" . consult-kmacro)
;; C-x bindings (ctl-x-map)
("C-x M-:" . consult-complex-command)
("C-x b" . consult-buffer)
("C-x 4 b" . consult-buffer-other-window)
("C-x 5 b" . consult-buffer-other-frame)
("C-x r b" . consult-bookmark)
("C-x p b" . consult-project-buffer)
;; Custom M-# bindings for fast register access
("M-#" . consult-register-load)
("M-'" . consult-register-store)
("C-M-#" . consult-register)
;; Other custom bindings
("M-y" . consult-yank-pop)
;; M-g bindings (goto-map)
("M-g e" . consult-compile-error)
("M-g f" . consult-flymake)
("M-g g" . consult-goto-line)
("M-g M-g" . consult-goto-line)
("M-g o" . consult-outline)
("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)
("M-s D" . 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)
;; Isearch integration
("M-s e" . consult-isearch-history)
:map isearch-mode-map
("M-e" . consult-isearch-history)
("M-s e" . consult-isearch-history)
("M-s l" . consult-line)
("M-s L" . consult-line-multi)
;; Minibuffer history
:map minibuffer-local-map
("M-s" . consult-history)
("M-r" . consult-history))
;; Enable automatic preview at point in the *Completions* buffer.
: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.5
register-preview-function #'consult-register-format)
;; Optionally tweak the register preview window.
(advice-add #'register-preview :override #'consult-register-window)
;; Use Consult to select xref locations with preview
(setq xref-show-xrefs-function #'consult-xref
xref-show-definitions-function #'consult-xref)
:config
;; Optionally configure preview. The default value
;; is 'any, such that any key triggers the preview.
(setq consult-preview-key 'any)
;; Configure other variables and modes
(setq consult-narrow-key "<") ;; "C-+"
;; Make narrowing help available in the minibuffer.
(define-key consult-narrow-map (vconcat consult-narrow-key "?") #'consult-narrow-help)
;; By default `consult-project-function' uses `project-root' from project.el.
;; Configure a different project root function.
(autoload 'projectile-project-root "projectile")
(setq consult-project-function (lambda (_) (projectile-project-root))))
;;; Embark - Contextual actions
(use-package embark
:ensure t
:bind
(("C-." . embark-act) ;; pick some comfortable binding
("C-;" . embark-dwim) ;; good alternative: M-.
("C-h B" . embark-bindings)) ;; alternative for `describe-bindings'
:init
;; Optionally replace the key help with a completing-read interface
(setq prefix-help-command #'embark-prefix-help-command)
:config
;; Hide the mode line of the Embark live/completions buffers
(add-to-list 'display-buffer-alist
'("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
nil
(window-parameters (mode-line-format . none)))))
;;; Embark-Consult - Integration between Embark and Consult
(use-package embark-consult
:ensure t
:hook
(embark-collect-mode . consult-preview-at-point-mode))
;;; Corfu - In-buffer completion popup
(use-package corfu
:ensure t
:custom
(corfu-cycle t) ;; Enable cycling for `corfu-next/previous'
(corfu-auto t) ;; Enable auto completion
(corfu-auto-delay 0.2)
(corfu-auto-prefix 2)
(corfu-separator ?\s) ;; Orderless field separator
(corfu-quit-at-boundary nil) ;; Never quit at completion boundary
(corfu-quit-no-match nil) ;; Never quit, even if there is no match
(corfu-preview-current nil) ;; Disable current candidate preview
(corfu-preselect 'prompt) ;; Preselect the prompt
(corfu-on-exact-match nil) ;; Configure handling of exact matches
(corfu-scroll-margin 5) ;; Use scroll margin
:init
(global-corfu-mode))
;;; Cape - Completion extensions for Corfu
(use-package cape
:ensure t
:init
;; Add `completion-at-point-functions', used by `completion-at-point'.
(add-to-list 'completion-at-point-functions #'cape-dabbrev)
(add-to-list 'completion-at-point-functions #'cape-file)
(add-to-list 'completion-at-point-functions #'cape-keyword)
;;(add-to-list 'completion-at-point-functions #'cape-tex)
;;(add-to-list 'completion-at-point-functions #'cape-sgml)
;;(add-to-list 'completion-at-point-functions #'cape-rfc1345)
;;(add-to-list 'completion-at-point-functions #'cape-abbrev)
;;(add-to-list 'completion-at-point-functions #'cape-dict)
;;(add-to-list 'completion-at-point-functions #'cape-symbol)
;;(add-to-list 'completion-at-point-functions #'cape-line)
)
;;; Additional Consult commands for enhanced functionality
(defun consult-ripgrep-project-root ()
"Search project root with ripgrep."
(interactive)
(let ((root (or (projectile-project-root) default-directory)))
(consult-ripgrep root)))
(global-set-key (kbd "C-c r") 'consult-ripgrep-project-root)
;;; Make completion work nicely with Projectile
(with-eval-after-load 'projectile
(define-key projectile-command-map (kbd "b") #'consult-project-buffer)
(define-key projectile-command-map (kbd "r") #'consult-ripgrep))
(provide 'init-completion)
;;; init-completion.el ends here