UP | HOME

Completion Configuration

Table of Contents


This section contains configuration to set up completion tools in Emacs.

Cape

Cape (completion at point extensions) provides some extensions to make autocompletion better and plays well with Corfu.

(use-package cape
  :straight t
  :bind (("C-c p p" . completion-at-point)
         ("C-c p t" . complete-tag)
         ("C-c p d" . cape-dabbrev)
         ("C-c p f" . cape-file)
         ("C-c p k" . cape-keyword)
         ("C-c p s" . cape-symbol)
         ("C-c p a" . cape-abbrev)
         ("C-c p \\" . cape-tex)
         ("C-c p _" . cape-tex)
         ("C-c p ^" . cape-tex))
  :init
  (add-to-list 'completion-at-point-functions #'cape-file)
  (add-to-list 'completion-at-point-functions #'cape-tex)
  (add-to-list 'completion-at-point-functions #'cape-dabbrev)
  (add-to-list 'completion-at-point-functions #'cape-keyword)
  (add-to-list 'completion-at-point-functions #'cape-symbol))

Cape Completion Functions

Cape provides a number of completion-at-point functions that can be used. Below are the functions that I use that have not been intrusive.

The following will complete file names in buffers:

(add-to-list 'completion-at-point-functions #'cape-file)

This will complete items in TeX and LaTeX modes:

(add-to-list 'completion-at-point-functions #'cape-tex)

Search other buffers for completion candidates:

(add-to-list 'completion-at-point-functions #'cape-dabbrev)

Complete keywords in programming modes:

(add-to-list 'completion-at-point-functions #'cape-keyword)

Complete Emacs-lisp symbols:

(add-to-list 'completion-at-point-functions #'cape-symbol)

Consult

Consult provides a number of highly useful commands. The complete list of commands can be found here.

(use-package consult
  :straight t
  :bind (("C-c h" . consult-history)
         ("C-c m" . consult-mode-command)
         ("C-c k" . consult-kmacro)
         ("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)
         ("M-#" . consult-register-load)
         ("M-'" . consult-register-store)
         ("C-M-#" . consult-register)
         ("M-y" . consult-yank-pop)
         ("<help> a" . consult-apropos)
         ("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 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 m" . consult-multi-occur)
         ("M-s k" . consult-keep-lines)
         ("M-s u" . consult-focus-lines)
         ("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))
  :config
  (setq consult-project-root-function
        (lambda ()
          (when-let (project (project-current))
            (car (project-roots project)))))
  (setq consult-narrow-key "<"))

Change Register Preview to Consult

Change the default Emacs register-preview-function to use consult effectively.

(register-preview-function #'consult-register-format)

Consult Notes

With Consult Notes, users can use Consult to search through note directories specified by the consult-notes-sources list. Consult will then let you search through these notes with completion frameworks and has good integration with Org Roam.

(use-package consult-notes
  :straight (:type git :host github :repo "mclear-tools/consult-notes")
  :after consult
  :custom
  (consult-notes-sources '(("GTD"  ?g  "~/Dropbox/gtd/")
                                  ("Org Roam"  ?r  "~/Dropbox/org-roam/")))
  :commands (consult-notes
             consult-notes-search-all
             consult-notes-org-roam-find-node
             consult-notes-org-roam-find-node-relation)
  :config
  (consult-notes-org-roam-mode))

Consult YASnippet

If you have a tendency to forget about all of those YASnippet template abbrevs, then Consult YASnippet can be used to search though your YASnippet templates using a completion framework.

(use-package consult-yasnippet
  :straight t
  :after (consult yasnippet)
  :commands (consult-yasnippet))

Consult Flycheck

Searching through Flycheck errors is very efficient with Consult Flycheck. It will list all of the errors from the current buffer and these can be searched using a completion framework.

(use-package consult-flycheck
  :straight t
  :commands (consult-flycheck)
  :after (consult flycheck))

Set Root Function for Projects

Use the built-in project function to find the root of any project. Projectile can also be used but I have yet to compare the differences between the two projects to make a decision as to which one to use.

(setq consult-project-root-function
      (lambda ()
        (when-let (project (project-current))
          (car (project-roots project)))))

Set the Narrowing Key

The narrowing key is pressed before a key used to narrow the results returned from consult. An example would be < m to list bookmarks after calling consult-buffer.

(setq consult-narrow-key "<")

Tweak Register Preview Window

The following line makes the register preview window look better by adding sorting and thin lines to break up the results into easy to view categories.

(advice-add #'register-preview
            :override #'consult-register-window)

Use Consult to Select xref Locations

Use Consult to search through and select xref locations in buffers

(xref-show-xrefs-function #'consult-xref)
(xref-show-definitions-function #'consult-xref)

Completion Cycling

Cycle through completion candidates by pressing the completion key.

(completion-cycle-threshold t)

Completion by Pressing Tab

<TAB> will indent lines by default. In order to turn <TAB> into a button that activates autocomplete, the tab-always-indent variable must be set to complete. This will cause pressing <TAB> once to indent, and twice will cause it to activate completion.

(tab-always-indent 'complete)

Corfu

Corfu for in-buffer completion. Corfu will provide an in-buffer popup window that will be populated with various completion options.

(use-package corfu
  :straight t
  :init
  (global-corfu-mode)
  :bind
  (:map corfu-map
        ("TAB" . corfu-next)
        ([tab] . corfu-next)
        ("S-TAB" . corfu-previous)
        ([backtab] . corfu-previous))
  :custom
  (corfu-auto t)
  (corfu-cycle t)
  (corfu-preselect-first nil)
  (:map corfu-map
        ("TAB" . corfu-next)
        ([tab] . corfu-next)
        ("S-TAB" . corfu-previous)
        ([backtab] . corfu-previous))
  :config
  (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-silent)
  (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-purify))

Use TAB for Selecting Candidates

Instead of using M-tab which will switch windows in XFCE—my default desktop environment—use TAB to cycle between the different possible options.

(:map corfu-map
      ("TAB" . corfu-next)
      ([tab] . corfu-next)
      ("S-TAB" . corfu-previous)
      ([backtab] . corfu-previous))

Corfu-doc

Corfu-doc will bring up documentation results in a separate pop-up next to the corfu pop-up with the documentation of any function or variable results that are selected.

(use-package corfu-doc
  :straight (corfu-doc :host github
                       :repo "galeo/corfu-doc"
                       :branch "main")
  :hook
  (corfu-mode . corfu-doc-mode)
  :config
  (define-key corfu-map (kbd "M-p") #'corfu-doc-scroll-down)
  (define-key corfu-map (kbd "M-n") #'corfu-doc-scroll-up))

Embark

Embark provides the ability to execute commands on completion options found in the minibuffer.

(use-package embark
  :straight t
  :bind
  (("C-." . embark-act)
   ("C-;" . embark-dwim)
   ("C-h B" . embark-bindings))
  :init
  (setq prefix-help-command #'embark-prefix-help-command)
  :config
  (add-to-list 'display-buffer-alist
               '("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
                 nil
                 (window-parameters (mode-line-format . none))))
  (define-key embark-file-map (kbd "S") 'sudo-find-file))

Embark Mode Line

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 Integration with Consult

Allow use of Embark commands after issuing a Consult command.

(use-package embark-consult
  :straight t
  :after (embark consult)
  :demand t
  :hook
  (embark-collect-mode . consult-preview-at-point-mode))

Orderless

Orderless will allow for seaching in the minibuffer using text broken up by spaces in which the order does not matter. For instance, if M-x is entered the search results will be the same if searching for a function by using the search key find file or file find. Both entries will return the function find-file as the top result.

(use-package orderless
  :straight t
  :defer 3
  :custom
  (completion-styles '(orderless))
  (completion-category-defaults nil)
  (completion-category-overrides '((file (styles partial-completion)))))

Marginalia

Marginalia generates annotations to items in the minibuffer similar to what is accomplished with ivy-rich.

(use-package marginalia
  :straight t
  :bind (("M-A" . marginalia-cycle)
         :map minibuffer-local-map
         ("M-A" . marginalia-cycle))
  :hook
  (marginalia-mode . all-the-icons-completion-marginalia-setup)
  :init
  (marginalia-mode))

If All the Icons and All the Icons Completion is installed, activate All the Completion to work with Marginalia.

(marginalia-mode . all-the-icons-completion-marginalia-setup)

Vertico

Vertico removes the *Completions* buffer and instead, populates a vertical list of completion options in the minibuffer region. This is similar to other completion tools in Emacs such Helm, Ivy, Selectrum, and icomplete-vertical.

(use-package vertico
  :straight t
  :demand t
  :custom
  (vertico-cycle t)
  (vertico-resize t)
  :init
  (vertico-mode))

Kind Icon

Kind Icon can add icons to in-buffer completion systems. This can make it easier to visually process what type of results are being returned by the completion system.

(use-package kind-icon
  :straight t
  :after corfu
  :custom
  (kind-icon-default-face 'corfu-default) ; to compute blended backgrounds correctly
  :config
  (add-to-list 'corfu-margin-formatters #'kind-icon-margin-formatter))

End

Tell Emacs what feature this file provides.

(provide 'freemacs-completion)

Date: Time-stamp: <2022-07-21 Thu 18:36>

Author: Thomas Freeman

Created: 2022-07-21 Thu 18:55

Validate