GNU ELPA - corfu

corfu

Description
Completion Overlay Region FUnction
Latest
corfu-0.13.tar, 2021-Oct-11, 170 KiB
Maintainer
Daniel Mendler <mail@daniel-mendler.de>
Home page
https://github.com/minad/corfu
Browse ELPA's repository
CGit or Gitweb
Badge

To install this package, run in Emacs:

M-x package-install RET corfu RET

Full description

GNU ELPA GNU-devel ELPA

1 Introduction

Corfu enhances the default completion in region function with a completion overlay. The current candidates are shown in a popup below or above the point. Corfu is the minimalistic completion-in-region counterpart of the Vertico minibuffer UI.

Corfu is a minimal package, which relies on the Emacs completion facilities and concentrates on providing a polished completion UI. Completions are either provided by commands like dabbrev-completion or by pluggable backends (completion-at-point-functions, Capfs). Most programming language major modes implement a Capf. Furthermore the language server packages, Eglot and Lsp-mode, both use Capfs which talk to the LSP server to retrieve the completions.

Corfu does not include custom completion backends. In contrast, the complex Company package includes custom completion backends, which deviate from the Emacs completion infrastructure.

NOTE: Corfu uses child frames to show the popup; on non-graphical displays it will fall back to the default setting of the completion-in-region-function.

https://github.com/minad/corfu/blob/main/screenshot.png?raw=true

2 Features

  • Timer-based auto-completions (off by default, set corfu-auto).
  • Popup display with scrollbar indicator and arrow key navigation.
  • The popup can be summoned explicitly by pressing TAB at any time.
  • The current candidate is inserted with TAB and selected with RET.
  • Candidates sorting by prefix, string length and alphabetically.
  • The selected candidate is automatically committed on any further input by default. This behavior can be configured by adjusting corfu-commit-predicate.
  • Filter string can contain arbitrary characters and spaces, if corfu-quit-at-boundary is nil. This is needed when filtering with the Orderless completion style.
  • Deferred completion style highlighting for performance.
  • Jumping to location/documentation of current candidate.
  • Show candidate documentation/signature string in the echo area.
  • Support for annotation-function and affixation-function.

3 Configuration

Corfu is available from GNU ELPA, such that it can be installed directly via package-install. After installation, the local minor mode can be enabled with M-x corfu-mode. In order to configure Corfu and other packages in your init.el, you may want to use use-package. I recommend to give Orderless completion a try, which is different from the familiar prefix TAB completion. However Corfu works well with the default completion styles, the use of Orderless is not a necessity. Here is an example configuration:

(use-package corfu
  ;; Optional customizations
  ;; :custom
  ;; (corfu-cycle t)                ;; Enable cycling for `corfu-next/previous'
  ;; (corfu-auto t)                 ;; Enable auto completion
  ;; (corfu-commit-predicate nil)   ;; Do not commit selected candidates on next input
  ;; (corfu-quit-at-boundary t)     ;; Automatically quit at word boundary
  ;; (corfu-quit-no-match t)        ;; Automatically quit if there is no match
  ;; (corfu-echo-documentation nil) ;; Do not show documentation in the echo area

  ;; Optionally use TAB for cycling, default is `corfu-complete'.
  ;; :bind (:map corfu-map
  ;;        ("TAB" . corfu-next)
  ;;        ([tab] . corfu-next)
  ;;        ("S-TAB" . corfu-previous)
  ;;        ([backtab] . corfu-previous))

  ;; You may want to enable Corfu only for certain modes.
  ;; :hook ((prog-mode . corfu-mode)
  ;;        (shell-mode . corfu-mode)
  ;;        (eshell-mode . corfu-mode))

  ;; Recommended: Enable Corfu globally.
  ;; This is recommended since dabbrev can be used globally (M-/).
  :init
  (corfu-global-mode))

;; Optionally use the `orderless' completion style. See `+orderless-dispatch'
;; in the Consult wiki for an advanced Orderless style dispatcher.
;; Enable `partial-completion' for files to allow path expansion.
;; You may prefer to use `initials' instead of `partial-completion'.
(use-package orderless
  :init
  ;; Configure a custom style dispatcher (see the Consult wiki)
  ;; (setq orderless-style-dispatchers '(+orderless-dispatch))
  (setq completion-styles '(orderless)
	completion-category-defaults nil
	completion-category-overrides '((file (styles . (partial-completion))))))

;; Dabbrev works with Corfu
(use-package dabbrev
  ;; Swap M-/ and C-M-/
  :bind (("M-/" . dabbrev-completion)
	 ("C-M-/" . dabbrev-expand)))

;; A few more useful configurations...
(use-package emacs
  :init
  ;; TAB cycle if there are only few candidates
  (setq completion-cycle-threshold 3)

  ;; Emacs 28: Hide commands in M-x which do not work in the current mode.
  ;; Corfu commands are hidden, since they are not supposed to be used via M-x.
  ;; (setq read-extended-command-predicate
  ;;       #'command-completion-default-include-p)

  ;; Enable indentation+completion using the TAB key.
  ;; `completion-at-point' is often bound to M-TAB.
  (setq tab-always-indent 'complete))

4 Key bindings

Corfu uses a transient keymap corfu-map which is active while the popup is shown. The keymap defines the following remappings and bindings:

  • beginning-of-buffer -> corfu-first
  • end-of-buffer -> corfu-last
  • scroll-down-command -> corfu-scroll-down
  • scroll-up-command -> corfu-scroll-up
  • next-line, down, M-n -> corfu-next
  • previous-line, up, M-p -> corfu-previous
  • completion-at-point, TAB -> corfu-complete
  • RET -> corfu-insert
  • M-g -> corfu-show-location
  • M-h -> corfu-show-documentation
  • C-g, ESC ESC ESC -> corfu-quit

5 Complementary packages

Corfu works well together with all packages providing code completion via the completion-at-point-functions. Furthermore it supports completion styles, including the advanced Orderless completion style, where the filtering expressions are separated by spaces (see corfu-quit-at-boundary).

You may also want to look into my Vertico package. Vertico is the minibuffer counterpart of Corfu.

6 Caveats

Corfu works well in most scenarios. However there are a few known technical caveats.

  • Corfu falls back to the default Completion buffer on non-graphical displays, since Corfu requires child frames.
  • The abort handling could be improved, for example the input could be undone.
  • Company kind icons and match data are not supported (company-kind, company-match).
  • No sorting by history, since completion-at-point does not maintain a history (See branch history for a possible solution).

7 Contributions

Since this package is part of GNU ELPA contributions require a copyright assignment to the FSF.

Old versions

corfu-0.12.tar.lz2021-Oct-1182.5 KiB
corfu-0.11.tar.lz2021-Aug-1182.3 KiB
corfu-0.10.tar.lz2021-Jul-1570.5 KiB
corfu-0.9.tar.lz2021-Jun-2069.7 KiB
corfu-0.8.tar.lz2021-May-1869.5 KiB
corfu-0.7.tar.lz2021-May-1369.5 KiB
corfu-0.6.tar.lz2021-May-0469.2 KiB
corfu-0.4.tar.lz2021-Apr-2868.5 KiB
corfu-0.3.tar.lz2021-Apr-2668.0 KiB
corfu-0.2.tar.lz2021-Apr-1767.1 KiB
corfu-0.1.tar.lz2021-Apr-1753.5 KiB