summaryrefslogtreecommitdiff
path: root/README.org
blob: bb6402a52d8f27de72f879c4e8e8e3a2e365d21d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#+title: corfu.el - Completion Overlay Region FUnction
#+author: Daniel Mendler
#+language: en
#+export_file_name: corfu.texi
#+texinfo_dir_category: Emacs
#+texinfo_dir_title: Corfu: (corfu).
#+texinfo_dir_desc: Completion Overlay Region FUnction

#+html: <a href="https://www.gnu.org/software/emacs/"><img alt="GNU Emacs" src="https://github.com/minad/corfu/blob/screenshots/emacs.svg?raw=true"/></a>
#+html: <a href="http://elpa.gnu.org/packages/corfu.html"><img alt="GNU ELPA" src="https://elpa.gnu.org/packages/corfu.svg"/></a>
#+html: <a href="http://elpa.gnu.org/devel/corfu.html"><img alt="GNU-devel ELPA" src="https://elpa.gnu.org/devel/corfu.svg"/></a>

* 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 [[https://github.com/minad/vertico][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, [[https://github.com/joaotavora/eglot][Eglot]] and [[https://github.com/emacs-lsp/lsp-mode][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. The Emacs built-in Capfs are mostly
  sufficient, but a few additional Capfs and completion functions are provided
  by the [[https://github.com/minad/cape][Cape]] package.

  *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/screenshots/light.png?raw=true]]

  [[https://github.com/minad/corfu/blob/screenshots/dark.png?raw=true]]

* 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 previewed (configuable via ~corfu-preview-current~).
  - The selected candidate automatically committed on further input by default
    (configurable via ~corfu-commit-predicate~).
  - The [[https://github.com/oantolin/orderless][Orderless]] completion style is supported. The filter string can contain
    arbitrary characters, including spaces, if ~corfu-quit-at-boundary~ is nil.
  - Deferred completion style highlighting for performance.
  - Jumping to location/documentation of current candidate.
  - Show candidate documentation/signature string in the echo area.
  - Deprecated candidates are crossed out in the display.
  - Support for annotations (~annotation-function~, ~affixation-function~).
  - Icons can be provided by an external package via margin formatter functions.

* Installation and Configuration

  Corfu is available from [[http://elpa.gnu.org/packages/corfu.html][GNU ELPA]], such that it can be installed directly via
  ~package-install~. After installation, the global minor mode can be enabled with
  =M-x corfu-global-mode=. In order to configure Corfu and other packages in your
  init.el, you may want to use ~use-package~.

  Corfu is highly flexible and customizable via ~corfu-*~ customization variables.
  For filtering I recommend to give Orderless completion a try, which is
  different from the familiar prefix TAB completion. Corfu can be used with the
  default completion styles, the use of Orderless is not a necessity. See also
  the [[https://github.com/minad/corfu/wiki][Corfu Wiki]] for additional configuration tips.

  Here is an example configuration:

  #+begin_src emacs-lisp
    (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-preview-current nil)    ;; Disable current candidate preview
      ;; (corfu-preselect-first nil)    ;; Disable candidate preselection
      ;; (corfu-echo-documentation nil) ;; Disable documentation in the echo area
      ;; (corfu-scroll-margin 5)        ;; Use scroll margin

      ;; 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)
      ;;       orderless-component-separator #'orderless-escapable-split-on-space)
      (setq completion-styles '(orderless)
            completion-category-defaults nil
            completion-category-overrides '((file (styles . (partial-completion))))))

    ;; Use dabbrev 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 apply to 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))
  #+end_src

** TAB-and-Go completion

You may be interested in configuring Corfu in TAB-and-Go style. Pressing TAB
moves to the next candidate and further input will then commit the selection.

#+begin_src emacs-lisp
  (use-package corfu
    ;; TAB-and-Go customizations
    :custom
    (corfu-cycle t)             ;; Enable cycling for `corfu-next/previous'
    (corfu-preselect-first nil) ;; Disable candidate preselection

    ;; 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))

    :init
    (corfu-global-mode))
#+end_src

* 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= -> ~corfu-quit~
  - ~keyboard-escape-quit~ -> ~corfu-reset~

* 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 [[https://github.com/oantolin/orderless][Orderless]] completion style, where the filtering
  expressions are separated by spaces (see ~corfu-quit-at-boundary~).

  I collect additional Capf backends and =completion-in-region= commands in my
  small [[https://github.com/minad/cape][Cape]] package. For example the package provides a file name and a dabbrev
  completion backend.

  Icons are supported by Corfu via an external package. For example the
  [[https://github.com/jdtsmith/kind-icon][kind-icon]] package provides beautifully styled SVG icons based on monochromatic
  icon sets like material design.

  You may also want to look into my [[https://github.com/minad/vertico][Vertico]] package. Vertico is the minibuffer
  counterpart of Corfu.

* Caveats

  Corfu is robust in most scenarios. There are a few known technical caveats.

  - Corfu falls back to the default Completion buffer on non-graphical displays,
    since Corfu requires child frames.
  - No sorting by history, since ~completion-at-point~ does not
    maintain a history (See branch =history= for a possible solution).
  - There is currently no equivalent for =company-quickhelp=. Documentation and source
    can be opened manually in a separate buffer.
  - Company has the ability to merge/group the candidates of multiple backends
    in some scenarios. This feature is implemented by the experimental function
    ~cape-super-capf~ of the [[https://github.com/minad/cape][Cape]] package.

* Contributions

  Since this package is part of [[http://elpa.gnu.org/packages/corfu.html][GNU ELPA]] contributions require a copyright
  assignment to the FSF.