• Stars
    star
    533
  • Rank 80,171 (Top 2 %)
  • Language
    Emacs Lisp
  • License
    GNU General Publi...
  • Created over 2 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🦸cape.el - Completion At Point Extensions

cape.el - Let your completions fly!

GNU Emacs GNU ELPA GNU-devel ELPA MELPA MELPA Stable

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

You can register the cape-* functions in the completion-at-point-functions list. This makes the backends available for completion, which is usually invoked by pressing TAB or M-TAB. The functions can also be invoked interactively to trigger the respective completion at point. You can bind them directly to a key in your user configuration. Notable commands/Capfs are cape-line for completion of a line from the current buffer and cape-file for completion of a file name. The commands cape-symbol and cape-elisp-block are useful for documentation of Elisp packages or configurations, since they completes Elisp anywhere.

Cape has the super power to transform Company backends into Capfs and merge multiple Capfs into a Super-Capf! These transformers allow you to still take advantage of Company backends even if you are not using Company as frontend.

Available Capfs

  • cape-dabbrev: Complete word from current buffers. See also dabbrev-capf on Emacs 29.
  • cape-elisp-block: Complete Elisp in Org or Markdown code block.
  • cape-file: Complete file name.
  • cape-history: Complete from Eshell, Comint or minibuffer history.
  • cape-keyword: Complete programming language keyword.
  • cape-symbol: Complete Elisp symbol.
  • cape-abbrev: Complete abbreviation (add-global-abbrev, add-mode-abbrev).
  • cape-dict: Complete word from dictionary file.
  • cape-line: Complete entire line from current buffer.
  • cape-tex: Complete Unicode char from TeX command, e.g. \hbar.
  • cape-sgml: Complete Unicode char from SGML entity, e.g., &alpha.
  • cape-rfc1345: Complete Unicode char using RFC 1345 mnemonics.

Configuration

Cape is available on GNU ELPA and MELPA. You can install the package with package-install. In the following we present a sample configuration based on the popular use-package macro.

;; Enable Corfu completion UI
;; See the Corfu README for more configuration tips.
(use-package corfu
  :init
  (global-corfu-mode))

;; Add extensions
(use-package cape
  ;; Bind dedicated completion commands
  ;; Alternative prefix keys: C-c p, M-p, M-+, ...
  :bind (("C-c p p" . completion-at-point) ;; capf
         ("C-c p t" . complete-tag)        ;; etags
         ("C-c p d" . cape-dabbrev)        ;; or dabbrev-completion
         ("C-c p h" . cape-history)
         ("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 l" . cape-line)
         ("C-c p w" . cape-dict)
         ("C-c p \\" . cape-tex)
         ("C-c p _" . cape-tex)
         ("C-c p ^" . cape-tex)
         ("C-c p &" . cape-sgml)
         ("C-c p r" . cape-rfc1345))
  :init
  ;; Add `completion-at-point-functions', used by `completion-at-point'.
  ;; NOTE: The order matters!
  (add-to-list 'completion-at-point-functions #'cape-dabbrev)
  (add-to-list 'completion-at-point-functions #'cape-file)
  (add-to-list 'completion-at-point-functions #'cape-elisp-block)
  ;;(add-to-list 'completion-at-point-functions #'cape-history)
  ;;(add-to-list 'completion-at-point-functions #'cape-keyword)
  ;;(add-to-list 'completion-at-point-functions #'cape-tex)
  ;;(add-to-list 'completion-at-point-functions #'cape-sgml)
  ;;(add-to-list 'completion-at-point-functions #'cape-rfc1345)
  ;;(add-to-list 'completion-at-point-functions #'cape-abbrev)
  ;;(add-to-list 'completion-at-point-functions #'cape-dict)
  ;;(add-to-list 'completion-at-point-functions #'cape-symbol)
  ;;(add-to-list 'completion-at-point-functions #'cape-line)
)

CAPF adapters and transformers

Company adapter

Wrap your Company backend in a Cape and turn it into a Capf!

Cape provides the adapter cape-company-to-capf for Company backends. The adapter transforms Company backends to Capfs which are understood by the built-in Emacs completion mechanism. The function is approximately the inverse of the company-capf backend from Company. The adapter can be used as follows:

;; Use Company backends as Capfs.
(setq-local completion-at-point-functions
  (mapcar #'cape-company-to-capf
    (list #'company-files #'company-ispell #'company-dabbrev)))

Note that the adapter does not require Company to be installed or enabled. Backends implementing the Company specification do not necessarily have to depend on Company, however in practice most backends do. The following shows a small example completion backend, which can be used with both completion-at-point (Corfu, default completion) and Company.

(defvar emojis
  '((":-D" . "😀")
    (";-)" . "😉")
    (":-/" . "😕")
    (":-(" . "🙁")
    (":-*" . "😙")))

(defun emoji-backend (action &optional arg &rest _)
  (pcase action
    ('prefix (and (memq (char-before) '(?: ?\;))
                  (cons (string (char-before)) t)))
    ('candidates (all-completions arg emojis))
    ('annotation (concat " " (cdr (assoc arg emojis))))
    ('post-completion
     (let ((str (buffer-substring (- (point) 3) (point))))
       (delete-region (- (point) 3) (point))
     (insert (cdr (assoc str emojis)))))))

;; Register emoji backend with `completion-at-point'
(setq completion-at-point-functions
      (list (cape-company-to-capf #'emoji-backend)))

;; Register emoji backend with Company.
(setq company-backends '(emoji-backend))

It is possible to merge/group multiple Company backends and use them as a single Capf using the company--multi-backend-adapter function from Company. The adapter transforms multiple Company backends into a single Company backend, which can then be used as a Capf via cape-company-to-capf.

(require 'company)
;; Use the company-dabbrev and company-elisp backends together.
(setq completion-at-point-functions
      (list
       (cape-company-to-capf
        (apply-partially #'company--multi-backend-adapter
                         '(company-dabbrev company-elisp)))))

Super-Capf - Merging multiple Capfs

Throw multiple Capfs under the Cape and get a Super-Capf!

Cape supports merging multiple Capfs using the function cape-super-capf. This feature is EXPERIMENTAL and should only be used in special scenarios. Don’t use cape-super-capf if you are not 100% sure that you need it! If you decide to use the function, you are on UNSUPPORTED TERRITORY.

Note that cape-super-capf is not needed if you want to use multiple Capfs which are tried one by one, e.g., it is perfectly possible to use cape-file together with the Lsp-mode Capf or other programming mode Capfs by adding cape-file to the completion-at-point-functions list. The file completion will be available in comments and string literals. cape-super-capf is only needed if you want to combine multiple Capfs, such that the candidates from multiple sources appear together in the completion list at the same time.

Completion table merging works only for tables which are sufficiently well-behaved and tables which do not define completion boundaries. cape-super-capf has the same restrictions as completion-table-merge and completion-table-in-turn. As a simple rule of thumb, cape-super-capf works only well for static completion functions like cape-dabbrev, cape-keyword, cape-dict, etc., but not for complex multi-step completions like cape-file.

;; Merge the dabbrev, dict and keyword capfs, display candidates together.
(setq-local completion-at-point-functions
            (list (cape-super-capf #'cape-dabbrev #'cape-dict #'cape-keyword)))

;; Alternative: Define named Capf instead of using the anonymous Capf directly
(defalias 'cape-dabbrev+dict+keyword
  (cape-super-capf #'cape-dabbrev #'cape-dict #'cape-keyword))
(setq-local completion-at-point-functions (list #'cape-dabbrev+dict+keyword))

See also the aforementioned company--multi-backend-adapter from Company, which allows you to merge multiple Company backends.

Capf-Buster - Cache busting

The Capf-Buster ensures that you always get a fresh set of candidates!

If a Capf caches the candidates for too long we can use a cache busting Capf-transformer. For example the Capf merging function cape-super-capf creates a Capf, which caches the candidates for the whole lifetime of the Capf. Therefore you may want to combine a merged Capf with a cache buster under some circumstances. It is noteworthy that the company-capf backend from Company refreshes the completion table frequently. With the cape-capf-buster we can achieve a similarly refreshing strategy.

(setq-local completion-at-point-functions
            (list (cape-capf-buster #'some-caching-capf)))

Capf transformers

Cape provides a set of additional Capf transformation functions, which are mostly meant to used by experts to fine tune the Capf behavior and Capf interaction. These can either be used as advices (cape-wrap-*) or to create a new Capf from an existing Capf (cape-capf-*). You can bind the Capfs created by the Capf transformers with defalias to a function symbol.

  • cape-interactive-capf, cape-interactive: Create a Capf which can be called interactively.
  • cape-wrap-accept-all, cape-capf-accept-all: Create a Capf which accepts every input as valid.
  • cape-wrap-silent, cape-capf-silent: Wrap a chatty Capf and silence it.
  • cape-wrap-purify, cape-capf-purify: Purify a broken Capf and ensure that it does not modify the buffer.
  • cape-wrap-nonexclusive, cape-capf-nonexclusive: Mark Capf as non-exclusive.
  • cape-wrap-noninterruptible, cape-capf-noninterruptible: Protect a Capf which does not like to be interrupted.
  • cape-wrap-case-fold, cape-capf-case-fold: Create a Capf which is case insensitive.
  • cape-wrap-properties, cape-capf-properties: Add completion properties to a Capf.
  • cape-wrap-predicate, cape-capf-predicate: Add candidate predicate to a Capf.
  • cape-wrap-prefix-length, cape-capf-prefix-length: Enforce a minimal prefix length.
  • cape-wrap-inside-comment, cape-capf-inside-comment: Ensure that Capf triggers only inside comment.
  • cape-wrap-inside-string, cape-capf-inside-string: Ensure that Capf triggers only inside a string literal.

In the following we show a few example configurations, which have come up on the Cape or Corfu issue tracker or the Corfu wiki. I use some of these tweaks in my personal configuration.

;; Example 1: Sanitize the `pcomplete-completions-at-point' Capf.  The Capf has
;; undesired side effects on Emacs 28 and earlier.  These advices are not needed
;; on Emacs 29 and newer.
(when (< emacs-major-version 29)
  (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-silent)
  (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-purify))

;; Example 2: Configure a Capf with a specific auto completion prefix length
(setq-local completion-at-point-functions
            (list (cape-capf-prefix-length #'cape-dabbrev 2)))

;; Example 3: Named Capf
(defalias 'cape-dabbrev-min-2 (cape-capf-prefix-length #'cape-dabbrev 2))
(setq-local completion-at-point-functions (list #'cape-dabbrev-min-2))

;; Example 4: Define a defensive Dabbrev Capf, which accepts all inputs.  If you
;; use Corfu and `corfu-auto=t', the first candidate won't be auto selected even
;; if `corfu-preselect=first'. You can use this instead of `cape-dabbrev'.
(defun my-cape-dabbrev-accept-all ()
  (cape-wrap-accept-all #'cape-dabbrev))
(add-to-list 'completion-at-point-functions #'my-cape-dabbrev-accept-all)

;; Example 5: Define interactive Capf which can be bound to a key.  Here we wrap
;; the `elisp-completion-at-point' such that we can complete Elisp code
;; explicitly in arbitrary buffers.
(keymap-global-set "C-c p e" (cape-interactive-capf #'elisp-completion-at-point))

;; Example 6: Ignore :keywords in Elisp completion.
(defun ignore-elisp-keywords (sym)
  (not (keywordp sym)))
(setq-local completion-at-point-functions
            (list (cape-capf-predicate #'elisp-completion-at-point
                                       #'ignore-elisp-keywords)))

Contributions

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

More Repositories

1

org-modern

🦄 Modern Org Style
Emacs Lisp
1,344
star
2

vertico

💫 vertico.el - VERTical Interactive COmpletion
Emacs Lisp
1,344
star
3

consult

🔍 consult.el - Consulting completing-read
Emacs Lisp
1,088
star
4

corfu

🏝️ corfu.el - COmpletion in Region FUnction
Emacs Lisp
1,000
star
5

marginalia

📜 marginalia.el - Marginalia in the minibuffer
Emacs Lisp
695
star
6

osm

🌍 osm.el - OpenStreetMap viewer for Emacs
Emacs Lisp
496
star
7

tempel

🏛️ TempEl - Simple templates for Emacs
Emacs Lisp
464
star
8

jinx

🪄 Enchanted Spell Checker
Emacs Lisp
328
star
9

affe

🐒 affe.el - Asynchronous Fuzzy Finder for Emacs
Emacs Lisp
202
star
10

lmdb

Ruby bindings for the OpenLDAP's Lightning Memory-Mapped Database (LMDB)
C
106
star
11

goggles

goggles.el - Pulse modified region
Emacs Lisp
92
star
12

bibsync

BibSync is a tool to synchronize scientific papers and bibtex bibliography files
Ruby
60
star
13

paripari

Fast parser combinator library for Haskell with two strategies (Fast acceptor and slower reporter with decent error messages)
Haskell
56
star
14

hasklig-mode

hasklig-mode.el - Hasklig ligatures for emacs
Emacs Lisp
39
star
15

gridslides

LaTeX package to create free form slides with blocks placed on a grid
TeX
38
star
16

3delta

Host software especially suited for delta 3d printers
Tcl
36
star
17

pacgem

Gem installer for Arch Linux
Ruby
34
star
18

bookmark-view

bookmark-view.el - Use bookmarks to persist the current view
Emacs Lisp
34
star
19

osm2shp

Convert large OpenStreetMap files to shapefiles (Uses sqlite3 db as temporary storage)
C++
29
star
20

consult-flycheck

Consult integration for Flycheck
Emacs Lisp
28
star
21

chasm

Java Bytecode Assembler and Disassembler which uses S-expressions
Java
26
star
22

tab-bookmark

Persist Emacs Tabs as Bookmarks
Emacs Lisp
25
star
23

unit

Unit support for numbers
Ruby
24
star
24

mini-popup

Emacs Lisp
22
star
25

gitrb

Unmaintained: Simple git implementation in ruby similar to grit, based on git_store
Ruby
21
star
26

recursion-indicator

Recursion indicator for the mode-line
Emacs Lisp
20
star
27

henk

pure type system language - henk 2000 ported to megaparsec, pretty
Haskell
20
star
28

persist

Minimal binary serialization library with focus on performance
Haskell
17
star
29

rack-embed

Rack middleware which embeds small images as base64 data-url in css/html files if the browser supports it
Ruby
16
star
30

polyp

Emacs Lisp
12
star
31

writer-cps-mtl

Stricter drop in replacements for WriterT and RWST
Haskell
11
star
32

intro

Safe and minimal Haskell Prelude
Haskell
10
star
33

writer-cps-transformers

Compatibility package for old transformers: Stricter drop in replacements for WriterT and RWST
Haskell
10
star
34

wl-pprint-annotated

Wadler/Leijen pretty printer with annotations and API conforming to modern Haskell
Haskell
7
star
35

uchronia

uchronia.el - Rewrite the minibuffer history
Emacs Lisp
7
star
36

tasty-stats

Collect statistics of your Tasty testsuite
Haskell
7
star
37

tasty-auto

Deprecated: Auto discovery for the Tasty test framework, use tasty-discover instead
Haskell
6
star
38

quickcheck-special

Edge cases and special values for QuickCheck Arbitrary instances
Haskell
5
star
39

thinkpad-tools

Unmaintained: Some scripts used on my Thinkpad X60t
Python
5
star
40

typohero

TypoHero enhances your typography
Ruby
5
star
41

diascope

Unmaintained: S5: A Simple Standards-Based Slide Show System. Alternative implementation using jquery
JavaScript
5
star
42

9mount

Unmaintained fork of http://sqweek.dnsdojo.org/hg/9mount
C
4
star
43

colorful-monoids

colorful-monoids: Styled console text output using ANSI escape sequences
Haskell
4
star
44

omega

Haskell
4
star
45

distcc

distcc scripts
4
star
46

evaluator

Mathematical expression evaluator for infix notation. Supports variables and functions.
Ruby
3
star
47

xosdbar

show osd on X11, similar to osd_cat but with update support
C
3
star
48

wl-pprint-console

Wadler/Leijen pretty printer with annotations and support for colorful console output
Haskell
3
star
49

mapgen

XCSoar Map Generator
Python
3
star
50

andromeda.hs

This is a direct port of Andrej Bauer's dependent type theory implementation from OCaml to Haskell
Haskell
3
star
51

ihs

Interpolated Haskell
LLVM
3
star
52

vle

Verilog Experiments
Verilog
3
star
53

console-style

Styled console text output using ANSI escape sequences
Haskell
3
star
54

doxygen2adoc

Doxygen import for Asciidoc using XSLT and the Doxygen XML output
XSLT
3
star
55

analyzer

Some tools for physics calculations
C++
3
star
56

bigint-unboxed

JavaScript big integers with support for ES6 BigInt and unboxed small integers
JavaScript
2
star
57

i3-config

qubes i3 configuration
Shell
2
star
58

special-values

Haskell
2
star
59

hashtable

Hashtable in C with open addressing and specialization via macros
C
2
star
60

TaskList

Mediawiki plugin which implements a task tracker
PHP
2
star
61

implant

Linux from scratch package manager using symlinks - similar to GoboLinux (2008)
Shell
2
star
62

hexns

Nameserver for IPv6 which resolves Hexspeak subdomains
C
2
star
63

emacs-theme

Emacs Lisp
2
star
64

arch-packages

My arch pkbuilds
Shell
2
star
65

temple-mustache

Temple-based implementation of Mustache (Just a finger exercises to test temple)
Ruby
2
star
66

encode-string

String encoding and decoding in Haskell
Haskell
2
star
67

writer-cps-monads-tf

Stricter drop in replacements for WriterT and RWST
Haskell
2
star
68

slim-examples

http://slim-lang.com/
Ruby
2
star
69

imaginator

Unmaintained: Image generator for LaTex/graphviz source
Ruby
1
star
70

safe-convert

Safe type conversions in Haskell
Haskell
1
star
71

echoxul

echo2 java web app + xul (2006)
Java
1
star
72

persist-state

Haskell
1
star
73

strict-base

Strict versions of some standard Haskell data types
Haskell
1
star
74

writer-cps-full

Stricter drop in replacements for WriterT and RWST
Haskell
1
star
75

writer-cps-exceptions

Control.Monad.Catch instances for CPS WriterT and RWST monad transformers
Haskell
1
star
76

grouptsp

Travelling salesman (2004)
Java
1
star
77

pasty

easy to deploy, single file, under 50 line, command line pastebin
PHP
1
star
78

arduino-pi-gefrickel

Arduino/Rasberry Pi Gefrickel - Arduino/Rasberry Pi Fiddling
JavaScript
1
star
79

intro-prelude

Reexport Intro as Prelude
Haskell
1
star
80

os-experiment

simple i386 os with console and multithreading (2005)
C
1
star