summaryrefslogtreecommitdiff
path: root/README.org
blob: 8e77b3a522754eb9787054997331a635ac9f4cc5 (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
#+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

* Introduction

Corfu enhances the default completion in region function with a completion
overlay. The current candidates are shown in a popup overlay below or above the
point. Corfu can be considered the minimalistic completion-in-region counterpart
of the [[https://github.com/minad/vertico][Vertico]] minibuffer UI.

Icomplete implements both completion-in-region and minibuffer completion in a
single package. While Corfu and Vertico are technically similar to Icomplete,
Corfu and Vertico are two separate packages in order to optimize the UI for the
two distinct use cases, also leading to code bases which are easier to
understand.

Corfu is a minimal package (~500 lines of code without whitespace and comments).
In contrast to the featureful and complex Company package, Corfu only offers a
completion UI and does not include custom completion backends. Completions are
generated by calling the ~completion-at-point-functions~ (Capfs). Many code
completion backends provide such a Capf, such that relying only on the default
completion is often sufficient. For example the language server packages, [[https://github.com/joaotavora/eglot][Eglot]]
and [[https://github.com/emacs-lsp/lsp-mode][Lsp-mode]], both implement a Capf.

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

* Features

- Popup display with scrollbar indicator and arrow key navigation
- The popup must be summoned explicitly by =TAB=
- The current candidate is inserted with =TAB= and selected with =RET=
- Candidates sorting by prefix, string length and alphabetically
- Completion is automatically terminated after candidate selection
- Filter string can contain arbitrary characters and spaces (needed
  when filtering with the [[https://github.com/oantolin/orderless][Orderless]] completion style)
- Deferred completion style highlighting for performance
- Jumping to location/documentation of current candidate (Company extension)
- Support for ~annotation-function~ and ~affixation-function~

Notable non-feature: Timer-based idle completions are not supported.

* 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 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.
Here is an example configuration:

#+begin_src emacs-lisp
  ;; Configure corfu
  (use-package corfu
    ;; Optionally use TAB for cycling, default is `corfu-complete'.
    ;; :bind (:map corfu-map
    ;;        ("TAB" . corfu-next)
    ;;        ("S-TAB" . corfu-previous))

    ;; Enable the overlay only for certain modes.
    ;; For example it is not a useful UI for completions at point in the
    ;; minibuffer.
    :hook ((prog-mode . corfu-mode)
           (shell-mode . corfu-mode)
           (eshell-mode . corfu-mode))

    :config

    ;; Optionally enable cycling for `corfu-next' and `corfu-previous'.
    ;; (setq corfu-cycle t)
  )

  ;; Use the `orderless' completion style.
  ;; Enable `partial-completion' for files to allow path expansion.
  ;; You may prefer to use `initials' instead of `partial-completion'.
  (use-package orderless
    :init
    (setq completion-styles '(orderless)
          completion-category-defaults nil
          completion-category-overrides '((file (styles . (partial-completion))))))

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

    ;; Enable indentation+completion using the TAB key.
    ;; Completion is often bound to M-TAB.
    (setq tab-always-indent 'complete))
#+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=, =ESC ESC ESC= -> ~corfu-abort~

* Complementary packages

Corfu works well together with all packages providing code completion via the
~completion-at-point-functions~. Furthermore it supports various completion
styles, including the advanced [[https://github.com/oantolin/orderless][Orderless]] completion style, where the filtering
expressions are separated by spaces.

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

* Caveats

This package is experimental and new. I am not yet claiming that this package
works correctly. There are a few known technical caveats.

- The overlay popup is brittle (Alternatives to consider: Posframe, Postip)
- The thin popup borders are only drawn if =line-spacing=nil=.
- The abort handling could be improved, for example the input could be undone.
- The ~completion-in-region-mode-predicate~ is ignored in order to
  give the completion style full control. The predicate asks the backend if
  the starting point of the completion changed.
- Completion is terminated if there are no matches. Add optional confirmation?
- Company kind icons and metadata are not supported (~company-kind~, ~company-docsig~)
- No support for multi-backends like Company (Implement a multi-capf?)
- No support for sorting by history, since ~completion-at-point~ does not
  maintain a history (See branch =history= for a possible solution).

* Contributions

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