Files
emacs-dotfiles/.config.d/10-completion.el

235 lines
8.7 KiB
EmacsLisp

;;; 10-completion.el --- Setup completion. -*- no-byte-compile: t; lexical-binding: t; -*-
;;; Description:
;; This file contains code setting up all the completion packages,
;; such as corfu, vertico, marginalia, orderless, etc.
;;; Code:
;; COmpletion in Region FUnction. Corfu enhances in-buffer completion
;; with a small completion popup. Corfu is the minimalistic in-buffer
;; completion counterpart of the Vertico minibuffer UI.
(use-package corfu
:ensure t
:defer nil
:commands (corfu-mode global-corfu-mode)
:hook ((prog-mode . corfu-mode)
(shell-mode . corfu-mode)
(eshell-mode . corfu-mode))
:bind (:map corfu-map
("s-SPC" . corfu-insert-separator)
("M-d" . corfu-info-documentation)
)
:custom
;; Hide commands in M-x which do not apply to the current mode.
(read-extended-command-predicate #'command-completion-default-include-p)
;; Disable Ispell completion function. As an alternative try `cape-dict'.
(text-mode-ispell-word-completion nil)
(tab-always-indent 'complete)
:init
(setq corfu-auto t)
(setq tab-always-indent 'complete)
(setq corfu-preview-current 'insert)
(setq corfu-echo-documentation nil)
(setq corfu-popupinfo-delay '(1.0 . 0.5))
;; Enable Corfu
:config
(global-corfu-mode t)
(corfu-popupinfo-mode t))
;; This package provides icons to corfu completions. Requires nerd fonts to be
;; installed to work.
(use-package nerd-icons-corfu
:config
(add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter))
;; Cape provides Completion At Point Extensions which can be used in
;; combination with Corfu, Company or the default completion UI. The
;; completion backends used by completion-at-point are so called
;; completion-at-point-functions (Capfs).
(use-package cape
:ensure t
:defer nil
:commands (cape-dabbrev cape-file cape-elisp-block)
:bind ("C-c p" . cape-prefix-map)
:init
;; Add to the global default value of `completion-at-point-functions' which is
;; used by `completion-at-point'.
(add-hook 'completion-at-point-functions #'cape-dabbrev)
(add-hook 'completion-at-point-functions #'cape-file)
(add-hook 'completion-at-point-functions #'cape-elisp-block))
;; VERTical Interactive COmpletion. Vertico provides a performant and
;; minimalistic vertical completion UI based on the default completion
;; system.
(use-package vertico
;; (Note: It is recommended to also enable the savehist package.)
:ensure t
:defer t
:commands vertico-mode
:hook (after-init . vertico-mode))
;; This package provides an orderless completion style that divides
;; the pattern into space-separated components, and matches candidates
;; that match all of the components in any order.
(use-package orderless
;; Vertico leverages Orderless' flexible matching capabilities, allowing users
;; to input multiple patterns separated by spaces, which Orderless then
;; matches in any order against the candidates.
:ensure t
:custom
(completion-styles '(orderless basic))
(completion-category-defaults nil)
(completion-category-overrides '((file (styles partial-completion)))))
;; This package provides marginalia-mode which adds marginalia to the
;; minibuffer completions. Marginalia are marks or annotations placed
;; at the margin of the page of a book or in this case helpful
;; colorful annotations placed at the margin of the minibuffer for
;; your completion candidates.
(use-package marginalia
;; Marginalia allows Embark to offer you preconfigured actions in more contexts.
;; In addition to that, Marginalia also enhances Vertico by adding rich
;; annotations to the completion candidates displayed in Vertico's interface.
:ensure t
:defer t
:commands (marginalia-mode marginalia-cycle)
:hook (after-init . marginalia-mode)
:init
(define-key minibuffer-local-map "\M-A" 'marginalia-cycle))
;; Emacs Mini-Buffer Actions Rooted in Keymaps. Embark makes it easy
;; to choose a command to run based on what is near point, both during
;; a minibuffer completion session (in a way familiar to Helm or
;; Counsel users) and in normal buffers.
(use-package embark
;; Embark is an Emacs package that acts like a context menu, allowing
;; users to perform context-sensitive actions on selected items
;; directly from the completion interface.
:ensure t
:defer t
:commands (embark-act
embark-dwim
embark-export
embark-collect
embark-bindings
embark-prefix-help-command)
:bind
(("C-." . embark-act) ;; pick some comfortable binding
("C-;" . embark-dwim) ;; good alternative: M-.
("C-h B" . embark-bindings) ;; alternative for `describe-bindings'
:map embark-flymake-map
("a" . eglot-code-actions))
:init
(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))))
(push 'embark--allow-edit
(alist-get 'eglot-code-actions embark-target-injection-hooks))
(push 'embark--ignore-target
(alist-get 'eglot-code-actions embark-target-injection-hooks)))
;; This package provides some integrations between the emabrk and
;; consult packages.
(use-package embark-consult
:ensure t
:hook
(embark-collect-mode . consult-preview-at-point-mode))
;; Consulting completing-read. Consult provides search and navigation
;; commands based on the Emacs completion function
;; completing-read. Completion allows you to quickly select an item
;; from a list of candidates.
(use-package consult
:ensure t
:bind (;; C-c bindings in `mode-specific-map'
("C-c M-x" . consult-mode-command)
("C-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 in `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 t b" . consult-buffer-other-tab)
("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 in `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 in `search-map'
("M-s d" . consult-find)
("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)
;; 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
(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
(consult-customize
consult-theme :preview-key '(:debounce 0.2 any)
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
;; :preview-key "M-."
:preview-key '(:debounce 0.4 any))
(setq consult-narrow-key "<"))
;;; 10-completion.el ends here