dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

corfu.el (53779B)


      1 ;;; corfu.el --- Completion Overlay Region FUnction -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2021, 2022  Free Software Foundation, Inc.
      4 
      5 ;; Author: Daniel Mendler <mail@daniel-mendler.de>
      6 ;; Maintainer: Daniel Mendler <mail@daniel-mendler.de>
      7 ;; Created: 2021
      8 ;; Version: 0.34
      9 ;; Package-Requires: ((emacs "27.1"))
     10 ;; Homepage: https://github.com/minad/corfu
     11 
     12 ;; This file is part of GNU Emacs.
     13 
     14 ;; This program is free software: you can redistribute it and/or modify
     15 ;; it under the terms of the GNU General Public License as published by
     16 ;; the Free Software Foundation, either version 3 of the License, or
     17 ;; (at your option) any later version.
     18 
     19 ;; This program is distributed in the hope that it will be useful,
     20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22 ;; GNU General Public License for more details.
     23 
     24 ;; You should have received a copy of the GNU General Public License
     25 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     26 
     27 ;;; Commentary:
     28 
     29 ;; Corfu enhances the default completion in region function with a
     30 ;; completion overlay. The current candidates are shown in a popup
     31 ;; below or above the point. Corfu can be considered the minimalistic
     32 ;; completion-in-region counterpart of Vertico.
     33 
     34 ;;; Code:
     35 
     36 (require 'seq)
     37 (eval-when-compile
     38   (require 'cl-lib)
     39   (require 'subr-x))
     40 
     41 (defgroup corfu nil
     42   "Completion Overlay Region FUnction."
     43   :group 'convenience
     44   :prefix "corfu-")
     45 
     46 (defcustom corfu-count 10
     47   "Maximal number of candidates to show."
     48   :type 'integer)
     49 
     50 (defcustom corfu-scroll-margin 2
     51   "Number of lines at the top and bottom when scrolling.
     52 The value should lie between 0 and corfu-count/2."
     53   :type 'integer)
     54 
     55 (defcustom corfu-min-width 15
     56   "Popup minimum width in characters."
     57   :type 'integer)
     58 
     59 (defcustom corfu-max-width 100
     60   "Popup maximum width in characters."
     61   :type 'integer)
     62 
     63 (defcustom corfu-cycle nil
     64   "Enable cycling for `corfu-next' and `corfu-previous'."
     65   :type 'boolean)
     66 
     67 (defcustom corfu-on-exact-match 'insert
     68   "Configure how a single exact match should be handled."
     69   :type '(choice (const insert) (const quit) (const nil)))
     70 
     71 (defcustom corfu-continue-commands
     72   ;; nil is undefined command
     73   '(nil ignore universal-argument universal-argument-more digit-argument
     74         "\\`corfu-" "\\`scroll-other-window")
     75   "Continue Corfu completion after executing these commands."
     76   :type '(repeat (choice regexp symbol)))
     77 
     78 (defcustom corfu-preview-current 'insert
     79   "Preview currently selected candidate.
     80 If the variable has the value `insert', the candidate is automatically
     81 inserted on further input."
     82   :type '(choice boolean (const insert)))
     83 
     84 (defcustom corfu-preselect-first t
     85   "Preselect first candidate."
     86   :type 'boolean)
     87 
     88 (defcustom corfu-separator ?\s
     89   "Component separator character.
     90 The character used for separating components in the input. The presence
     91 of this separator character will inhibit quitting at completion
     92 boundaries, so that any further characters can be entered. To enter the
     93 first separator character, call `corfu-insert-separator' (bound to M-SPC
     94 by default). Useful for multi-component completion styles such as
     95 Orderless."
     96   :type 'character)
     97 
     98 (defcustom corfu-quit-at-boundary 'separator
     99   "Automatically quit at completion boundary.
    100 nil: Never quit at completion boundary.
    101 t: Always quit at completion boundary.
    102 separator: Quit at boundary if no `corfu-separator' has been inserted."
    103   :type '(choice boolean (const separator)))
    104 
    105 (defcustom corfu-quit-no-match 'separator
    106   "Automatically quit if no matching candidate is found.
    107 When staying alive even if there is no match a warning message is
    108 shown in the popup.
    109 nil: Stay alive even if there is no match.
    110 t: Quit if there is no match.
    111 separator: Only stay alive if there is no match and
    112 `corfu-separator' has been inserted."
    113   :type '(choice boolean (const separator)))
    114 
    115 (defcustom corfu-excluded-modes nil
    116   "List of modes excluded by `global-corfu-mode'."
    117   :type '(repeat symbol))
    118 
    119 (defcustom corfu-left-margin-width 0.5
    120   "Width of the left margin in units of the character width."
    121   :type 'float)
    122 
    123 (defcustom corfu-right-margin-width 0.5
    124   "Width of the right margin in units of the character width."
    125   :type 'float)
    126 
    127 (defcustom corfu-bar-width 0.2
    128   "Width of the bar in units of the character width."
    129   :type 'float)
    130 
    131 (defcustom corfu-margin-formatters nil
    132   "Registry for margin formatter functions.
    133 Each function of the list is called with the completion metadata as
    134 argument until an appropriate formatter is found. The function should
    135 return a formatter function, which takes the candidate string and must
    136 return a string, possibly an icon."
    137   :type 'hook)
    138 
    139 (defcustom corfu-sort-function #'corfu-sort-length-alpha
    140   "Default sorting function, used if no `display-sort-function' is specified."
    141   :type `(choice
    142           (const :tag "No sorting" nil)
    143           (const :tag "By length and alpha" ,#'corfu-sort-length-alpha)
    144           (function :tag "Custom function")))
    145 
    146 (defcustom corfu-sort-override-function nil
    147   "Override sort function which overrides the `display-sort-function'."
    148   :type '(choice (const nil) function))
    149 
    150 (defcustom corfu-auto-prefix 3
    151   "Minimum length of prefix for auto completion.
    152 The completion backend can override this with
    153 :company-prefix-length."
    154   :type 'integer)
    155 
    156 (defcustom corfu-auto-delay 0.2
    157   "Delay for auto completion."
    158   :type 'float)
    159 
    160 (defcustom corfu-auto-commands
    161   '("self-insert-command\\'"
    162     c-electric-colon c-electric-lt-gt c-electric-slash c-scope-operator)
    163   "Commands which initiate auto completion."
    164   :type '(repeat (choice regexp symbol)))
    165 
    166 (defcustom corfu-auto nil
    167   "Enable auto completion."
    168   :type 'boolean)
    169 
    170 (defgroup corfu-faces nil
    171   "Faces used by Corfu."
    172   :group 'corfu
    173   :group 'faces)
    174 
    175 (defface corfu-default
    176   '((((class color) (min-colors 88) (background dark)) :background "#191a1b")
    177     (((class color) (min-colors 88) (background light)) :background "#f0f0f0")
    178     (t :background "gray"))
    179   "Default face, foreground and background colors used for the popup.")
    180 
    181 (defface corfu-current
    182   '((((class color) (min-colors 88) (background dark))
    183      :background "#00415e" :foreground "white")
    184     (((class color) (min-colors 88) (background light))
    185      :background "#c0efff" :foreground "black")
    186     (t :background "blue" :foreground "white"))
    187   "Face used to highlight the currently selected candidate.")
    188 
    189 (defface corfu-bar
    190   '((((class color) (min-colors 88) (background dark)) :background "#a8a8a8")
    191     (((class color) (min-colors 88) (background light)) :background "#505050")
    192     (t :background "gray"))
    193   "The background color is used for the scrollbar indicator.")
    194 
    195 (defface corfu-border
    196   '((((class color) (min-colors 88) (background dark)) :background "#323232")
    197     (((class color) (min-colors 88) (background light)) :background "#d7d7d7")
    198     (t :background "gray"))
    199   "The background color used for the thin border.")
    200 
    201 (defface corfu-annotations
    202   '((t :inherit completions-annotations))
    203   "Face used for annotations.")
    204 
    205 (defface corfu-deprecated
    206   '((t :inherit shadow :strike-through t))
    207   "Face used for deprecated candidates.")
    208 
    209 (defvar corfu-map
    210   (let ((map (make-sparse-keymap)))
    211     (define-key map [remap beginning-of-buffer] #'corfu-first)
    212     (define-key map [remap end-of-buffer] #'corfu-last)
    213     (define-key map [remap scroll-down-command] #'corfu-scroll-down)
    214     (define-key map [remap scroll-up-command] #'corfu-scroll-up)
    215     (define-key map [remap next-line] #'corfu-next)
    216     (define-key map [remap previous-line] #'corfu-previous)
    217     (define-key map [remap completion-at-point] #'corfu-complete)
    218     (define-key map [down] #'corfu-next)
    219     (define-key map [up] #'corfu-previous)
    220     (define-key map [remap keyboard-escape-quit] #'corfu-reset)
    221     ;; XXX [tab] is bound because of org-mode
    222     ;; The binding should be removed from org-mode-map.
    223     (define-key map [tab] #'corfu-complete)
    224     (define-key map "\M-n" #'corfu-next)
    225     (define-key map "\M-p" #'corfu-previous)
    226     (define-key map "\C-g" #'corfu-quit)
    227     (define-key map "\r" #'corfu-insert)
    228     (define-key map "\t" #'corfu-complete)
    229     (define-key map "\M-g" 'corfu-info-location)
    230     (define-key map "\M-h" 'corfu-info-documentation)
    231     (define-key map "\M- " #'corfu-insert-separator)
    232     map)
    233   "Corfu keymap used when popup is shown.")
    234 
    235 (defvar corfu--auto-timer nil
    236   "Auto completion timer.")
    237 
    238 (defvar-local corfu--candidates nil
    239   "List of candidates.")
    240 
    241 (defvar-local corfu--metadata nil
    242   "Completion metadata.")
    243 
    244 (defvar-local corfu--base ""
    245   "Base string, which is concatenated with the candidate.")
    246 
    247 (defvar-local corfu--total 0
    248   "Length of the candidate list `corfu--candidates'.")
    249 
    250 (defvar-local corfu--highlight #'identity
    251   "Deferred candidate highlighting function.")
    252 
    253 (defvar-local corfu--index -1
    254   "Index of current candidate or negative for prompt selection.")
    255 
    256 (defvar-local corfu--preselect -1
    257   "Index of preselected candidate, negative for prompt selection.")
    258 
    259 (defvar-local corfu--scroll 0
    260   "Scroll position.")
    261 
    262 (defvar-local corfu--input nil
    263   "Cons of last prompt contents and point.")
    264 
    265 (defvar-local corfu--preview-ov nil
    266   "Current candidate overlay.")
    267 
    268 (defvar-local corfu--extra nil
    269   "Extra completion properties.")
    270 
    271 (defvar-local corfu--change-group nil
    272   "Undo change group.")
    273 
    274 (defvar corfu--frame nil
    275   "Popup frame.")
    276 
    277 (defconst corfu--state-vars
    278   '(corfu--base
    279     corfu--candidates
    280     corfu--highlight
    281     corfu--index
    282     corfu--preselect
    283     corfu--scroll
    284     corfu--input
    285     corfu--total
    286     corfu--preview-ov
    287     corfu--extra
    288     corfu--change-group
    289     corfu--metadata)
    290   "Buffer-local state variables used by Corfu.")
    291 
    292 (defvar corfu--frame-parameters
    293   '((no-accept-focus . t)
    294     (no-focus-on-map . t)
    295     (min-width . t)
    296     (min-height . t)
    297     (width . 0)
    298     (height . 0)
    299     (border-width . 0)
    300     (child-frame-border-width . 1)
    301     (left-fringe . 0)
    302     (right-fringe . 0)
    303     (vertical-scroll-bars . nil)
    304     (horizontal-scroll-bars . nil)
    305     (menu-bar-lines . 0)
    306     (tool-bar-lines . 0)
    307     (tab-bar-lines . 0)
    308     (no-other-frame . t)
    309     (unsplittable . t)
    310     (undecorated . t)
    311     (cursor-type . nil)
    312     (visibility . nil)
    313     (no-special-glyphs . t)
    314     (desktop-dont-save . t))
    315   "Default child frame parameters.")
    316 
    317 (defvar corfu--buffer-parameters
    318   '((mode-line-format . nil)
    319     (header-line-format . nil)
    320     (tab-line-format . nil)
    321     (tab-bar-format . nil) ;; Emacs 28 tab-bar-format
    322     (frame-title-format . "")
    323     (truncate-lines . t)
    324     (cursor-in-non-selected-windows . nil)
    325     (cursor-type . nil)
    326     (show-trailing-whitespace . nil)
    327     (display-line-numbers . nil)
    328     (left-fringe-width . nil)
    329     (right-fringe-width . nil)
    330     (left-margin-width . 0)
    331     (right-margin-width . 0)
    332     (fringes-outside-margins . 0)
    333     (fringe-indicator-alist . nil)
    334     (indicate-empty-lines . nil)
    335     (indicate-buffer-boundaries . nil)
    336     (buffer-read-only . t))
    337   "Default child frame buffer parameters.")
    338 
    339 (defvar corfu--mouse-ignore-map
    340   (let ((map (make-sparse-keymap)))
    341     (dotimes (i 7)
    342       (dolist (k '(mouse down-mouse drag-mouse double-mouse triple-mouse))
    343         (define-key map (vector (intern (format "%s-%s" k (1+ i)))) #'ignore)))
    344     map)
    345   "Ignore all mouse clicks.")
    346 
    347 (defun corfu--make-buffer (name)
    348   "Create buffer with NAME."
    349   (let ((fr face-remapping-alist)
    350         (ls line-spacing)
    351         (buffer (get-buffer-create name)))
    352     (with-current-buffer buffer
    353       ;;; XXX HACK install mouse ignore map
    354       (use-local-map corfu--mouse-ignore-map)
    355       (dolist (var corfu--buffer-parameters)
    356         (set (make-local-variable (car var)) (cdr var)))
    357       (setq-local face-remapping-alist (copy-tree fr)
    358                   line-spacing ls)
    359       (cl-pushnew 'corfu-default (alist-get 'default face-remapping-alist))
    360       buffer)))
    361 
    362 ;; Function adapted from posframe.el by tumashu
    363 (defvar x-gtk-resize-child-frames) ;; Not present on non-gtk builds
    364 (defun corfu--make-frame (frame x y width height buffer)
    365   "Show BUFFER in child frame at X/Y with WIDTH/HEIGHT.
    366 FRAME is the existing frame."
    367   (when-let (timer (and (frame-live-p frame)
    368                         (frame-parameter frame 'corfu--hide-timer)))
    369     (cancel-timer timer)
    370     (set-frame-parameter frame 'corfu--hide-timer nil))
    371   (let* ((window-min-height 1)
    372          (window-min-width 1)
    373          (inhibit-redisplay t)
    374          (x-gtk-resize-child-frames
    375           (let ((case-fold-search t))
    376             (and
    377              ;; XXX HACK to fix resizing on gtk3/gnome taken from posframe.el
    378              ;; More information:
    379              ;; * https://github.com/minad/corfu/issues/17
    380              ;; * https://gitlab.gnome.org/GNOME/mutter/-/issues/840
    381              ;; * https://lists.gnu.org/archive/html/emacs-devel/2020-02/msg00001.html
    382              (string-match-p "gtk3" system-configuration-features)
    383              (string-match-p "gnome\\|cinnamon"
    384                              (or (getenv "XDG_CURRENT_DESKTOP")
    385                                  (getenv "DESKTOP_SESSION") ""))
    386              'resize-mode)))
    387          (after-make-frame-functions)
    388          (parent (window-frame)))
    389     (unless (and (frame-live-p frame) (eq (frame-parent frame) parent))
    390       (when frame (delete-frame frame))
    391       (setq frame (make-frame
    392                    `((parent-frame . ,parent)
    393                      (minibuffer . ,(minibuffer-window parent))
    394                      ;; Set `internal-border-width' for Emacs 27
    395                      (internal-border-width
    396                       . ,(alist-get 'child-frame-border-width corfu--frame-parameters))
    397                      ,@corfu--frame-parameters))))
    398     ;; XXX HACK Setting the same frame-parameter/face-background is not a nop.
    399     ;; Check before applying the setting. Without the check, the frame flickers
    400     ;; on Mac. We have to apply the face background before adjusting the frame
    401     ;; parameter, otherwise the border is not updated (BUG?).
    402     (let* ((face (if (facep 'child-frame-border) 'child-frame-border 'internal-border))
    403            (new (face-attribute 'corfu-border :background nil 'default)))
    404       (unless (equal (face-attribute face :background frame 'default) new)
    405         (set-face-background face new frame)))
    406     (let ((new (face-attribute 'corfu-default :background nil 'default)))
    407       (unless (equal (frame-parameter frame 'background-color) new)
    408         (set-frame-parameter frame 'background-color new)))
    409     (let ((win (frame-root-window frame)))
    410       (set-window-buffer win buffer)
    411       ;; Disallow selection of root window (#63)
    412       (set-window-parameter win 'no-delete-other-windows t)
    413       (set-window-parameter win 'no-other-window t)
    414       ;; Mark window as dedicated to prevent frame reuse (#60)
    415       (set-window-dedicated-p win t))
    416     ;; XXX HACK: Child frame popup behavior improved on Emacs 29.
    417     ;; It seems we may not need the Emacs 27/28 hacks anymore.
    418     (if (eval-when-compile (< emacs-major-version 29))
    419         (let (inhibit-redisplay)
    420           (set-frame-size frame width height t)
    421           (if (frame-visible-p frame)
    422               ;; XXX HACK Avoid flicker when frame is already visible.
    423               ;; Redisplay, wait for resize and then move the frame.
    424               (unless (equal (frame-position frame) (cons x y))
    425                 (redisplay 'force)
    426                 (sleep-for 0.01)
    427                 (set-frame-position frame x y))
    428             ;; XXX HACK: Force redisplay, otherwise the popup sometimes does not
    429             ;; display content.
    430             (set-frame-position frame x y)
    431             (redisplay 'force)
    432             (make-frame-visible frame)))
    433       (set-frame-size frame width height t)
    434       (unless (equal (frame-position frame) (cons x y))
    435         (set-frame-position frame x y))
    436       (unless (frame-visible-p frame)
    437         (make-frame-visible frame)))
    438     (redirect-frame-focus frame parent)
    439     frame))
    440 
    441 (defun corfu--popup-show (pos off width lines &optional curr lo bar)
    442   "Show LINES as popup at POS - OFF.
    443 WIDTH is the width of the popup.
    444 The current candidate CURR is highlighted.
    445 A scroll bar is displayed from LO to LO+BAR."
    446   (let ((lh (default-line-height)))
    447     (with-current-buffer (corfu--make-buffer " *corfu*")
    448       (let* ((ch (default-line-height))
    449              (cw (default-font-width))
    450              (ml (ceiling (* cw corfu-left-margin-width)))
    451              (mr (ceiling (* cw corfu-right-margin-width)))
    452              (bw (ceiling (min mr (* cw corfu-bar-width))))
    453              (marginl (and (> ml 0) (propertize " " 'display `(space :width (,ml)))))
    454              (marginr (and (> mr 0) (propertize " " 'display `(space :align-to right))))
    455              (sbar (when (> bw 0)
    456                      (concat (propertize " " 'display `(space :align-to (- right (,mr))))
    457                              (propertize " " 'display `(space :width (,(- mr bw))))
    458                              (propertize " " 'face 'corfu-bar 'display `(space :width (,bw))))))
    459              (pos (posn-x-y (posn-at-point pos)))
    460              (width (+ (* width cw) ml mr))
    461              ;; XXX HACK: Minimum popup height must be at least 1 line of the
    462              ;; parent frame (#261).
    463              (height (max lh (* (length lines) ch)))
    464              (edge (window-inside-pixel-edges))
    465              (border (alist-get 'child-frame-border-width corfu--frame-parameters))
    466              (x (max 0 (min (+ (car edge) (- (or (car pos) 0) ml (* cw off) border))
    467                             (- (frame-pixel-width) width))))
    468              (yb (+ (cadr edge) (window-tab-line-height) (or (cdr pos) 0) lh))
    469              (y (if (> (+ yb (* corfu-count ch) lh lh) (frame-pixel-height))
    470                     (- yb height lh border border)
    471                   yb))
    472              (row 0))
    473         (with-silent-modifications
    474           (erase-buffer)
    475           (insert (mapconcat (lambda (line)
    476                                (let ((str (concat marginl line
    477                                                   (if (and lo (<= lo row (+ lo bar)))
    478                                                       sbar
    479                                                     marginr))))
    480                                  (when (eq row curr)
    481                                    (add-face-text-property
    482                                     0 (length str) 'corfu-current 'append str))
    483                                  (cl-incf row)
    484                                  str))
    485                              lines "\n"))
    486           (goto-char (point-min)))
    487         (setq corfu--frame (corfu--make-frame corfu--frame x y
    488                                               width height (current-buffer)))))))
    489 
    490 (defun corfu--hide-frame-deferred (frame)
    491   "Deferred hiding of child FRAME."
    492   (when (and (frame-live-p frame) (frame-visible-p frame))
    493     (set-frame-parameter frame 'corfu--hide-timer nil)
    494     (make-frame-invisible frame)
    495     (with-current-buffer (window-buffer (frame-root-window frame))
    496       (with-silent-modifications
    497         (erase-buffer)))))
    498 
    499 (defun corfu--hide-frame (frame)
    500   "Hide child FRAME."
    501   (when (and (frame-live-p frame) (frame-visible-p frame)
    502              (not (frame-parameter frame 'corfu--hide-timer)))
    503     (set-frame-parameter frame 'corfu--hide-timer
    504                          (run-at-time 0 nil #'corfu--hide-frame-deferred frame))))
    505 
    506 (defun corfu--popup-hide ()
    507   "Hide Corfu popup."
    508   (corfu--hide-frame corfu--frame))
    509 
    510 (defun corfu--popup-support-p ()
    511   "Return non-nil if child frames are supported."
    512   (display-graphic-p))
    513 
    514 (defun corfu--move-to-front (elem list)
    515   "Move ELEM to front of LIST."
    516   (if-let (found (member elem list))
    517       (let ((head (list (car found))))
    518         (nconc head (delq (setcar found nil) list)))
    519     list))
    520 
    521 ;; bug#47711: Deferred highlighting for `completion-all-completions'
    522 ;; XXX There is one complication: `completion--twq-all' already adds
    523 ;; `completions-common-part'.
    524 (defun corfu--all-completions (&rest args)
    525   "Compute all completions for ARGS with deferred highlighting."
    526   (cl-letf* ((orig-pcm (symbol-function #'completion-pcm--hilit-commonality))
    527              (orig-flex (symbol-function #'completion-flex-all-completions))
    528              ((symbol-function #'completion-flex-all-completions)
    529               (lambda (&rest args)
    530                 ;; Unfortunately for flex we have to undo the deferred
    531                 ;; highlighting, since flex uses the completion-score for
    532                 ;; sorting, which is applied during highlighting.
    533                 (cl-letf (((symbol-function #'completion-pcm--hilit-commonality) orig-pcm))
    534                   (apply orig-flex args))))
    535              ;; Defer the following highlighting functions
    536              (hl #'identity)
    537              ((symbol-function #'completion-hilit-commonality)
    538               (lambda (cands prefix &optional base)
    539                 (setq hl (lambda (x) (nconc (completion-hilit-commonality x prefix base) nil)))
    540                 (and cands (nconc cands base))))
    541              ((symbol-function #'completion-pcm--hilit-commonality)
    542               (lambda (pattern cands)
    543                 (setq hl (lambda (x)
    544                            ;; `completion-pcm--hilit-commonality' sometimes
    545                            ;; throws an internal error for example when entering
    546                            ;; "/sudo:://u".
    547                            (condition-case nil
    548                                (completion-pcm--hilit-commonality pattern x)
    549                              (t x))))
    550                 cands)))
    551     ;; Only advise orderless after it has been loaded to avoid load order issues
    552     (if (and (fboundp 'orderless-highlight-matches)
    553              (fboundp 'orderless-pattern-compiler))
    554         (cl-letf (((symbol-function 'orderless-highlight-matches)
    555                    (lambda (pattern cands)
    556                      (let ((regexps (orderless-pattern-compiler pattern)))
    557                        (setq hl (lambda (x) (orderless-highlight-matches regexps x))))
    558                      cands)))
    559           (cons (apply #'completion-all-completions args) hl))
    560       (cons (apply #'completion-all-completions args) hl))))
    561 
    562 (defsubst corfu--length-string< (x y)
    563   "Sorting predicate which compares X and Y first by length then by `string<'."
    564   (or (< (length x) (length y)) (and (= (length x) (length y)) (string< x y))))
    565 
    566 (defun corfu-sort-length-alpha (list)
    567   "Sort LIST by length and alphabetically."
    568   (sort list #'corfu--length-string<))
    569 
    570 (defmacro corfu--partition! (list form)
    571   "Evaluate FORM for every element and partition LIST."
    572   (let ((head1 (make-symbol "head1"))
    573         (head2 (make-symbol "head2"))
    574         (tail1 (make-symbol "tail1"))
    575         (tail2 (make-symbol "tail2")))
    576     `(let* ((,head1 (cons nil nil))
    577             (,head2 (cons nil nil))
    578             (,tail1 ,head1)
    579             (,tail2 ,head2))
    580        (while ,list
    581          (if (let ((it (car ,list))) ,form)
    582              (progn
    583                (setcdr ,tail1 ,list)
    584                (pop ,tail1))
    585            (setcdr ,tail2 ,list)
    586            (pop ,tail2))
    587          (pop ,list))
    588        (setcdr ,tail1 (cdr ,head2))
    589        (setcdr ,tail2 nil)
    590        (setq ,list (cdr ,head1)))))
    591 
    592 (defun corfu--move-prefix-candidates-to-front (field candidates)
    593   "Move CANDIDATES which match prefix of FIELD to the beginning."
    594   (let* ((word (substring field 0
    595                           (seq-position field corfu-separator)))
    596          (len (length word)))
    597     (corfu--partition!
    598      candidates
    599      (and (>= (length it) len)
    600           (eq t (compare-strings word 0 len it 0 len
    601                                  completion-ignore-case))))))
    602 
    603 (defun corfu--sort-function ()
    604   "Return the sorting function."
    605   (or corfu-sort-override-function
    606       (corfu--metadata-get 'display-sort-function)
    607       corfu-sort-function))
    608 
    609 (defun corfu--recompute (str pt table pred)
    610   "Recompute state from STR, PT, TABLE and PRED."
    611   (pcase-let* ((before (substring str 0 pt))
    612                (after (substring str pt))
    613                (corfu--metadata (completion-metadata before table pred))
    614                ;; bug#47678: `completion-boundaries` fails for `partial-completion`
    615                ;; if the cursor is moved between the slashes of "~//".
    616                ;; See also vertico.el which has the same issue.
    617                (bounds (or (condition-case nil
    618                                (completion-boundaries before table pred after)
    619                              (t (cons 0 (length after))))))
    620                (field (substring str (car bounds) (+ pt (cdr bounds))))
    621                (completing-file (eq (corfu--metadata-get 'category) 'file))
    622                (`(,all . ,hl) (corfu--all-completions str table pred pt corfu--metadata))
    623                (base (or (when-let (z (last all)) (prog1 (cdr z) (setcdr z nil))) 0))
    624                (corfu--base (substring str 0 base)))
    625     ;; Filter the ignored file extensions. We cannot use modified predicate for
    626     ;; this filtering, since this breaks the special casing in the
    627     ;; `completion-file-name-table' for `file-exists-p' and `file-directory-p'.
    628     (when completing-file (setq all (completion-pcm--filename-try-filter all)))
    629     (setq all (delete-consecutive-dups (funcall (or (corfu--sort-function) #'identity) all)))
    630     (setq all (corfu--move-prefix-candidates-to-front field all))
    631     (when (and completing-file (not (string-suffix-p "/" field)))
    632       (setq all (corfu--move-to-front (concat field "/") all)))
    633     (setq all (corfu--move-to-front field all))
    634     `((corfu--base . ,corfu--base)
    635       (corfu--metadata . ,corfu--metadata)
    636       (corfu--candidates . ,all)
    637       (corfu--total . ,(length all))
    638       (corfu--highlight . ,hl)
    639       ;; Select the prompt when the input is a valid completion
    640       ;; and if it is not equal to the first candidate.
    641       (corfu--preselect . ,(if (or (not corfu-preselect-first) (not all)
    642                                    (and (not (equal field (car all)))
    643                                         (not (and completing-file (equal (concat field "/") (car all))))
    644                                         (test-completion str table pred)))
    645                                -1 0)))))
    646 
    647 (defun corfu--update (&optional interruptible)
    648   "Update state, optionally INTERRUPTIBLE."
    649   (pcase-let* ((`(,beg ,end ,table ,pred) completion-in-region--data)
    650                (pt (- (point) beg))
    651                (str (buffer-substring-no-properties beg end))
    652                (input (cons str pt)))
    653     (unless (equal corfu--input input)
    654       ;; Redisplay such that the input becomes immediately visible before the
    655       ;; expensive candidate recomputation is performed (Issue #48). See also
    656       ;; corresponding vertico#89.
    657       (when interruptible (redisplay))
    658       ;; Bind non-essential=t to prevent Tramp from opening new connections,
    659       ;; without the user explicitly requesting it via M-TAB.
    660       (pcase (let ((non-essential t))
    661                ;; XXX Guard against errors during candidate generation.
    662                ;; For example dabbrev throws error "No dynamic expansion ... found".
    663                ;; TODO Report this as a bug? Are completion tables supposed to throw errors?
    664                (condition-case err
    665                    (if interruptible
    666                        (while-no-input (corfu--recompute str pt table pred))
    667                      (corfu--recompute str pt table pred))
    668                  (error
    669                   (message "Corfu completion error: %s" (error-message-string err))
    670                   t)))
    671         ('nil (keyboard-quit))
    672         ((and state (pred consp))
    673          (dolist (s state) (set (car s) (cdr s)))
    674          (setq corfu--input input
    675                corfu--index corfu--preselect))))
    676     input))
    677 
    678 (defun corfu--match-symbol-p (pattern sym)
    679   "Return non-nil if SYM is matching an element of the PATTERN list."
    680   (and (symbolp sym)
    681        (cl-loop for x in pattern
    682                 thereis (if (symbolp x)
    683                             (eq sym x)
    684                           (string-match-p x (symbol-name sym))))))
    685 
    686 (defun corfu-quit ()
    687   "Quit Corfu completion."
    688   (interactive)
    689   (completion-in-region-mode -1))
    690 
    691 (defun corfu-reset ()
    692   "Reset Corfu completion.
    693 This command can be executed multiple times by hammering the ESC key. If a
    694 candidate is selected, unselect the candidate. Otherwise reset the input. If
    695 there hasn't been any input, then quit."
    696   (interactive)
    697   (if (/= corfu--index corfu--preselect)
    698       (progn
    699         (corfu--goto -1)
    700         (setq this-command #'corfu-first))
    701     ;; Cancel all changes and start new change group.
    702     (cancel-change-group corfu--change-group)
    703     (activate-change-group (setq corfu--change-group (prepare-change-group)))
    704     (when (eq last-command #'corfu-reset) (corfu-quit))))
    705 
    706 (defun corfu--affixate (cands)
    707   "Annotate CANDS with annotation function."
    708   (setq cands
    709         (if-let (aff (or (corfu--metadata-get 'affixation-function)
    710                          (plist-get corfu--extra :affixation-function)))
    711             (funcall aff cands)
    712           (if-let (ann (or (corfu--metadata-get 'annotation-function)
    713                            (plist-get corfu--extra :annotation-function)))
    714               (cl-loop for cand in cands collect
    715                        (let ((suffix (or (funcall ann cand) "")))
    716                          ;; The default completion UI adds the
    717                          ;; `completions-annotations' face if no other faces are
    718                          ;; present. We use a custom `corfu-annotations' face to
    719                          ;; allow further styling which fits better for popups.
    720                          (unless (text-property-not-all 0 (length suffix) 'face nil suffix)
    721                            (setq suffix (propertize suffix 'face 'corfu-annotations)))
    722                          (list cand "" suffix)))
    723             (cl-loop for cand in cands collect (list cand "" "")))))
    724   (let* ((dep (plist-get corfu--extra :company-deprecated))
    725          (completion-extra-properties corfu--extra)
    726          (mf (run-hook-with-args-until-success 'corfu-margin-formatters corfu--metadata)))
    727     (cl-loop for x in cands for (c . _) = x do
    728              (when mf
    729                (setf (cadr x) (funcall mf c)))
    730              (when (and dep (funcall dep c))
    731                (setcar x (setq c (substring c)))
    732                (add-face-text-property 0 (length c) 'corfu-deprecated 'append c)))
    733     (cons mf cands)))
    734 
    735 (defun corfu--metadata-get (prop)
    736   "Return PROP from completion metadata."
    737   ;; Note: Do not use `completion-metadata-get' in order to avoid Marginalia.
    738   ;; The Marginalia annotators are too heavy for the Corfu popup!
    739   (cdr (assq prop corfu--metadata)))
    740 
    741 (defun corfu--format-candidates (cands)
    742   "Format annotated CANDS."
    743   (setq cands
    744         (cl-loop for c in cands collect
    745                  (cl-loop for s in c collect
    746                           (replace-regexp-in-string "[ \t]*\n[ \t]*" " " s))))
    747   (let* ((cw (cl-loop for x in cands maximize (string-width (car x))))
    748          (pw (cl-loop for x in cands maximize (string-width (cadr x))))
    749          (sw (cl-loop for x in cands maximize (string-width (caddr x))))
    750          (width (+ pw cw sw))
    751          ;; -4 because of margins and some additional safety
    752          (max-width (min corfu-max-width (- (frame-width) 4))))
    753     (when (> width max-width)
    754       (setq sw (max 0 (- max-width pw cw))
    755             width (+ pw cw sw)))
    756     (when (< width corfu-min-width)
    757       (setq cw (+ cw (- corfu-min-width width))
    758             width corfu-min-width))
    759     (setq width (min width max-width))
    760     (list pw width
    761           (cl-loop for (cand prefix suffix) in cands collect
    762                    (truncate-string-to-width
    763                     (concat prefix
    764                             (make-string (max 0 (- pw (string-width prefix))) ?\s)
    765                             cand
    766                             (when (/= sw 0)
    767                               (make-string
    768                                (+ (max 0 (- cw (string-width cand)))
    769                                   (max 0 (- sw (string-width suffix))))
    770                                ?\s))
    771                             suffix)
    772                     width)))))
    773 
    774 (defun corfu--compute-scroll ()
    775   "Compute new scroll position."
    776   (let ((off (max (min corfu-scroll-margin (/ corfu-count 2)) 0))
    777         (corr (if (= corfu-scroll-margin (/ corfu-count 2)) (1- (mod corfu-count 2)) 0)))
    778     (setq corfu--scroll (min (max 0 (- corfu--total corfu-count))
    779                              (max 0 (+ corfu--index off 1 (- corfu-count))
    780                                   (min (- corfu--index off corr) corfu--scroll))))))
    781 
    782 (defun corfu--candidates-popup (pos)
    783   "Show candidates popup at POS."
    784   (corfu--compute-scroll)
    785   (pcase-let* ((last (min (+ corfu--scroll corfu-count) corfu--total))
    786                (bar (ceiling (* corfu-count corfu-count) corfu--total))
    787                (lo (min (- corfu-count bar 1) (floor (* corfu-count corfu--scroll) corfu--total)))
    788                (`(,mf . ,acands) (corfu--affixate (funcall corfu--highlight
    789                                    (seq-subseq corfu--candidates corfu--scroll last))))
    790                (`(,pw ,width ,fcands) (corfu--format-candidates acands))
    791                ;; Disable the left margin if a margin formatter is active.
    792                (corfu-left-margin-width (if mf 0 corfu-left-margin-width)))
    793     ;; Nonlinearity at the end and the beginning
    794     (when (/= corfu--scroll 0)
    795       (setq lo (max 1 lo)))
    796     (when (/= last corfu--total)
    797       (setq lo (min (- corfu-count bar 2) lo)))
    798     (corfu--popup-show (+ pos (length corfu--base)) pw width fcands (- corfu--index corfu--scroll)
    799                        (and (> corfu--total corfu-count) lo) bar)))
    800 
    801 (defun corfu--preview-current (beg end)
    802   "Show current candidate as overlay given BEG and END."
    803   (when-let (cand (and corfu-preview-current (>= corfu--index 0)
    804                        (/= corfu--index corfu--preselect)
    805                        (nth corfu--index corfu--candidates)))
    806     (setq beg (+ beg (length corfu--base))
    807           corfu--preview-ov (make-overlay beg end nil))
    808     (overlay-put corfu--preview-ov 'priority 1000)
    809     (overlay-put corfu--preview-ov 'window (selected-window))
    810     (overlay-put corfu--preview-ov (if (= beg end) 'after-string 'display) cand)))
    811 
    812 (defun corfu--exhibit (&optional auto)
    813   "Exhibit Corfu UI.
    814 AUTO is non-nil when initializing auto completion."
    815   (pcase-let ((`(,beg ,end ,table ,pred) completion-in-region--data)
    816               (`(,str . ,pt) (corfu--update 'interruptible)))
    817     (cond
    818      ;; 1) Single exactly matching candidate and no further completion is possible.
    819      ((and (not (equal str ""))
    820            (equal (car corfu--candidates) str) (not (cdr corfu--candidates))
    821            (not (consp (completion-try-completion str table pred pt corfu--metadata)))
    822            (or auto corfu-on-exact-match))
    823       ;; Quit directly when initializing auto completion.
    824       (if (or auto (eq corfu-on-exact-match 'quit))
    825           (corfu-quit)
    826         (corfu--done str 'finished)))
    827      ;; 2) There exist candidates => Show candidates popup.
    828      (corfu--candidates
    829       (corfu--candidates-popup beg)
    830       (corfu--preview-current beg end)
    831       (redisplay 'force)) ;; XXX HACK Ensure that popup is redisplayed
    832      ;; 3) No candidates & corfu-quit-no-match & initialized => Confirmation popup.
    833      ((pcase-exhaustive corfu-quit-no-match
    834         ('t nil)
    835         ('nil corfu--input)
    836         ('separator (seq-contains-p (car corfu--input) corfu-separator)))
    837       (corfu--popup-show beg 0 8 '(#("No match" 0 8 (face italic))))
    838       (redisplay 'force)) ;; XXX HACK Ensure that popup is redisplayed
    839      ;; 4) No candidates & auto completing or initialized => Quit.
    840      ((or auto corfu--input) (corfu-quit)))))
    841 
    842 (defun corfu--pre-command ()
    843   "Insert selected candidate unless command is marked to continue completion."
    844   (when corfu--preview-ov
    845     (delete-overlay corfu--preview-ov)
    846     (setq corfu--preview-ov nil))
    847   ;; Ensure that state is initialized before next Corfu command
    848   (when (and (symbolp this-command) (string-prefix-p "corfu-" (symbol-name this-command)))
    849     (corfu--update))
    850   (when (and (eq corfu-preview-current 'insert)
    851              (/= corfu--index corfu--preselect)
    852              ;; See the comment about `overriding-local-map' in `corfu--post-command'.
    853              (not (or overriding-terminal-local-map
    854                       (corfu--match-symbol-p corfu-continue-commands this-command))))
    855     (corfu--insert 'exact)))
    856 
    857 (defun corfu-insert-separator ()
    858   "Insert a separator character, inhibiting quit on completion boundary.
    859 See `corfu-separator' for more details."
    860   (interactive)
    861   (insert corfu-separator))
    862 
    863 (defun corfu--continue-p ()
    864   "Continue completion?"
    865   (pcase-let ((pt (point))
    866               (`(,beg ,end . ,_) completion-in-region--data))
    867     (and beg end
    868          (eq (marker-buffer beg) (current-buffer))
    869          ;; Check ranges
    870          (<= beg pt end)
    871          (save-excursion
    872            (goto-char beg)
    873            (let ((inhibit-field-text-motion t))
    874              (<= (line-beginning-position) pt (line-end-position))))
    875          (or
    876           ;; We keep Corfu alive if a `overriding-terminal-local-map' is
    877           ;; installed, e.g., the `universal-argument-map'. It would be good to
    878           ;; think about a better criterion instead. Unfortunately relying on
    879           ;; `this-command' alone is insufficient, since the value of
    880           ;; `this-command' gets clobbered in the case of transient keymaps.
    881           overriding-terminal-local-map
    882           ;; Check if it is an explicitly listed continue command
    883           (corfu--match-symbol-p corfu-continue-commands this-command)
    884           (and (or (not corfu--input) (< beg end)) ;; Check for empty input
    885                (or (not corfu-quit-at-boundary) ;; Check separator or predicate
    886                    (and (eq corfu-quit-at-boundary 'separator)
    887                         (or (eq this-command #'corfu-insert-separator)
    888                             ;; with separator, any further chars allowed
    889                             (seq-contains-p (car corfu--input) corfu-separator)))
    890                    (funcall completion-in-region-mode--predicate)))))))
    891 
    892 (defun corfu--post-command ()
    893   "Refresh Corfu after last command."
    894   (if (corfu--continue-p)
    895       (corfu--exhibit)
    896     (corfu-quit))
    897   (when corfu-auto
    898     (corfu--auto-post-command)))
    899 
    900 (defun corfu--goto (index)
    901   "Go to candidate with INDEX."
    902   (setq corfu--index (max corfu--preselect (min index (1- corfu--total)))))
    903 
    904 (defun corfu-next (&optional n)
    905   "Go forward N candidates."
    906   (interactive "p")
    907   (let ((index (+ corfu--index (or n 1))))
    908     (corfu--goto
    909      (cond
    910       ((not corfu-cycle) index)
    911       ((= corfu--total 0) -1)
    912       ((< corfu--preselect 0) (1- (mod (1+ index) (1+ corfu--total))))
    913       (t (mod index corfu--total))))))
    914 
    915 (defun corfu-previous (&optional n)
    916   "Go backward N candidates."
    917   (interactive "p")
    918   (corfu-next (- (or n 1))))
    919 
    920 (defun corfu-scroll-down (&optional n)
    921   "Go back by N pages."
    922   (interactive "p")
    923   (corfu--goto (max 0 (- corfu--index (* (or n 1) corfu-count)))))
    924 
    925 (defun corfu-scroll-up (&optional n)
    926   "Go forward by N pages."
    927   (interactive "p")
    928   (corfu-scroll-down (- (or n 1))))
    929 
    930 (defun corfu-first ()
    931   "Go to first candidate, or to the prompt when the first candidate is selected."
    932   (interactive)
    933   (corfu--goto (if (> corfu--index 0) 0 -1)))
    934 
    935 (defun corfu-last ()
    936   "Go to last candidate."
    937   (interactive)
    938   (corfu--goto (1- corfu--total)))
    939 
    940 (defun corfu-complete ()
    941   "Try to complete current input.
    942 If a candidate is selected, insert it."
    943   (interactive)
    944   (pcase-let ((`(,beg ,end ,table ,pred) completion-in-region--data))
    945     (if (>= corfu--index 0)
    946         ;; Continue completion with selected candidate
    947         (progn
    948           (corfu--insert nil)
    949           ;; Exit with status 'finished if input is a valid match and no further
    950           ;; completion is possible. Furthermore treat the completion as
    951           ;; finished if we are at the end of a boundary, even if other longer
    952           ;; candidates would still match, since the user invoked `corfu-complete'
    953           ;; with an explicitly selected candidate!
    954           (let ((newstr (buffer-substring-no-properties beg end)))
    955             (when (and (test-completion newstr table pred)
    956                        (or
    957                         (not (consp (completion-try-completion
    958                                      newstr table pred (length newstr)
    959                                      (completion-metadata newstr table pred))))
    960                         (equal (completion-boundaries newstr table pred "") '(0 . 0))))
    961               (corfu--done newstr 'finished))))
    962       ;; Try to complete the current input string
    963       (let* ((pt (max 0 (- (point) beg)))
    964              (str (buffer-substring-no-properties beg end))
    965              (metadata (completion-metadata (substring str 0 pt) table pred)))
    966         (pcase (completion-try-completion str table pred pt metadata)
    967           ('t
    968            (goto-char end)
    969            (corfu--done str 'finished))
    970           (`(,newstr . ,newpt)
    971            (unless (equal str newstr)
    972              ;; bug#55205: completion--replace removes properties!
    973              (completion--replace beg end (concat newstr)))
    974            (goto-char (+ beg newpt))
    975            ;; Exit with status 'finished if input is a valid match
    976            ;; and no further completion is possible.
    977            (when (and (test-completion newstr table pred)
    978                       (not (consp (completion-try-completion
    979                                    newstr table pred newpt
    980                                    (completion-metadata (substring newstr 0 newpt) table pred)))))
    981              (corfu--done newstr 'finished))))))))
    982 
    983 (defun corfu--insert (status)
    984   "Insert current candidate, exit with STATUS if non-nil."
    985   (pcase-let* ((`(,beg ,end . ,_) completion-in-region--data)
    986                (str (buffer-substring-no-properties beg end)))
    987     ;; XXX There is a small bug here, depending on interpretation.
    988     ;; When completing "~/emacs/master/li|/calc" where "|" is the
    989     ;; cursor, then the candidate only includes the prefix
    990     ;; "~/emacs/master/lisp/", but not the suffix "/calc". Default
    991     ;; completion has the same problem when selecting in the
    992     ;; *Completions* buffer. See bug#48356.
    993     (setq str (concat corfu--base (substring-no-properties
    994                                    (nth corfu--index corfu--candidates))))
    995     ;; bug#55205: completion--replace removes properties!
    996     (completion--replace beg end (concat str))
    997     (corfu--goto -1) ;; Reset selection, but continue completion.
    998     (when status (corfu--done str status)))) ;; Exit with status
    999 
   1000 (defun corfu--done (str status)
   1001   "Call the `:exit-function' with STR and STATUS and exit completion."
   1002   (let ((exit (plist-get corfu--extra :exit-function)))
   1003     ;; For successfull completions, amalgamate undo operations,
   1004     ;; such that completion can be undone in a single step.
   1005     (undo-amalgamate-change-group corfu--change-group)
   1006     (corfu-quit)
   1007     (when exit (funcall exit str status))))
   1008 
   1009 (defun corfu-insert ()
   1010   "Insert current candidate.
   1011 Quit if no candidate is selected."
   1012   (interactive)
   1013   (if (>= corfu--index 0)
   1014       (corfu--insert 'finished)
   1015     (corfu-quit)))
   1016 
   1017 (defun corfu--setup ()
   1018   "Setup Corfu completion state."
   1019   (setq corfu--extra completion-extra-properties)
   1020   (completion-in-region-mode 1)
   1021   (undo-boundary) ;; Necessary to support `corfu-reset'
   1022   (activate-change-group (setq corfu--change-group (prepare-change-group)))
   1023   (setcdr (assq #'completion-in-region-mode minor-mode-overriding-map-alist) corfu-map)
   1024   (add-hook 'pre-command-hook #'corfu--pre-command nil 'local)
   1025   (add-hook 'post-command-hook #'corfu--post-command)
   1026   ;; Disable default post-command handling, since we have our own
   1027   ;; checks in `corfu--post-command'.
   1028   (remove-hook 'post-command-hook #'completion-in-region--postch)
   1029   (let ((sym (make-symbol "corfu--teardown"))
   1030         (buf (current-buffer)))
   1031     (fset sym (lambda ()
   1032                 ;; Ensure that the teardown runs in the correct buffer, if still alive.
   1033                 (unless completion-in-region-mode
   1034                   (remove-hook 'completion-in-region-mode-hook sym)
   1035                   (with-current-buffer (if (buffer-live-p buf) buf (current-buffer))
   1036                     (corfu--teardown)))))
   1037     (add-hook 'completion-in-region-mode-hook sym)))
   1038 
   1039 (defun corfu--teardown ()
   1040   "Teardown Corfu."
   1041   (corfu--popup-hide)
   1042   (remove-hook 'pre-command-hook #'corfu--pre-command 'local)
   1043   (remove-hook 'post-command-hook #'corfu--post-command)
   1044   (when corfu--preview-ov (delete-overlay corfu--preview-ov))
   1045   (accept-change-group corfu--change-group)
   1046   (mapc #'kill-local-variable corfu--state-vars))
   1047 
   1048 (defun corfu--in-region (&rest args)
   1049   "Corfu completion in region function called with ARGS."
   1050   ;; XXX We can get an endless loop when `completion-in-region-function' is set
   1051   ;; globally to `corfu--in-region'. This should never happen.
   1052   (apply (if (corfu--popup-support-p) #'corfu--in-region-1
   1053            (default-value 'completion-in-region-function))
   1054          args))
   1055 
   1056 (defun corfu--in-region-1 (beg end table &optional pred)
   1057   "Complete in region, see `completion-in-region' for BEG, END, TABLE, PRED."
   1058   (barf-if-buffer-read-only)
   1059   ;; Restart the completion. This can happen for example if C-M-/
   1060   ;; (`dabbrev-completion') is pressed while the Corfu popup is already open.
   1061   (when completion-in-region-mode (corfu-quit))
   1062   (let* ((pt (max 0 (- (point) beg)))
   1063          (str (buffer-substring-no-properties beg end))
   1064          (metadata (completion-metadata (substring str 0 pt) table pred))
   1065          (exit (plist-get completion-extra-properties :exit-function))
   1066          (threshold (completion--cycle-threshold metadata))
   1067          (completion-in-region-mode-predicate
   1068           (or completion-in-region-mode-predicate (lambda () t))))
   1069     (pcase (completion-try-completion str table pred pt metadata)
   1070       ('nil (corfu--message "No match") nil)
   1071       ('t (goto-char end)
   1072           (corfu--message "Sole match")
   1073           (when exit (funcall exit str 'finished))
   1074           t)
   1075       (`(,newstr . ,newpt)
   1076        (let* ((state (corfu--recompute str pt table pred))
   1077               (base (alist-get 'corfu--base state))
   1078               (total (alist-get 'corfu--total state))
   1079               (candidates (alist-get 'corfu--candidates state)))
   1080          (unless (markerp beg) (setq beg (copy-marker beg)))
   1081          (setq end (copy-marker end t)
   1082                completion-in-region--data (list beg end table pred))
   1083          (unless (equal str newstr)
   1084            ;; bug#55205: completion--replace removes properties!
   1085            (completion--replace beg end (concat newstr)))
   1086          (goto-char (+ beg newpt))
   1087          (if (= total 1)
   1088              ;; If completion is finished and cannot be further completed,
   1089              ;; return 'finished. Otherwise setup the Corfu popup.
   1090              (cond
   1091               ((consp (completion-try-completion
   1092                        newstr table pred newpt
   1093                        (completion-metadata newstr table pred)))
   1094                (corfu--setup))
   1095               (exit (funcall exit newstr 'finished)))
   1096            (if (or (= total 0) (not threshold)
   1097                    (and (not (eq threshold t)) (< threshold total)))
   1098                (corfu--setup)
   1099              (corfu--cycle-candidates total candidates (+ (length base) beg) end)
   1100              ;; Do not show Corfu when "trivially" cycling, i.e.,
   1101              ;; when the completion is finished after the candidate.
   1102              (unless (equal (completion-boundaries (car candidates) table pred "")
   1103                             '(0 . 0))
   1104                (corfu--setup)))))
   1105        t))))
   1106 
   1107 (defun corfu--message (&rest msg)
   1108   "Show completion MSG."
   1109   (let (message-log-max) (apply #'message msg)))
   1110 
   1111 (defun corfu--cycle-candidates (total cands beg end)
   1112   "Cycle between TOTAL number of CANDS.
   1113 See `completion-in-region' for the arguments BEG, END, TABLE, PRED."
   1114   (let* ((idx 0)
   1115          (map (make-sparse-keymap))
   1116          (replace (lambda ()
   1117                     (interactive)
   1118                     ;; bug#55205: completion--replace removes properties!
   1119                     (completion--replace beg end (concat (nth idx cands)))
   1120                     (corfu--message "Cycling %d/%d..." (1+ idx) total)
   1121                     (setq idx (mod (1+ idx) total))
   1122                     (set-transient-map map))))
   1123     (define-key map [remap completion-at-point] replace)
   1124     (define-key map [remap corfu-complete] replace)
   1125     (define-key map (vector last-command-event) replace)
   1126     (funcall replace)))
   1127 
   1128 (defun corfu--auto-complete-deferred (&optional tick)
   1129   "Initiate auto completion if TICK did not change."
   1130   (setq corfu--auto-timer nil)
   1131   (when (and (not completion-in-region-mode)
   1132              (or (not tick) (equal tick (corfu--auto-tick))))
   1133     (pcase (while-no-input ;; Interruptible capf query
   1134              (run-hook-wrapped 'completion-at-point-functions #'corfu--capf-wrapper))
   1135       (`(,fun ,beg ,end ,table . ,plist)
   1136        (let ((completion-in-region-mode-predicate
   1137               (lambda () (eq beg (car-safe (funcall fun)))))
   1138              (completion-extra-properties plist))
   1139          (setq completion-in-region--data
   1140                (list (if (markerp beg) beg (copy-marker beg))
   1141                      (copy-marker end t)
   1142                      table
   1143                      (plist-get plist :predicate)))
   1144          (corfu--setup)
   1145          (corfu--exhibit 'auto))))))
   1146 
   1147 (defun corfu--auto-post-command ()
   1148   "Post command hook which initiates auto completion."
   1149   (when corfu--auto-timer
   1150     (cancel-timer corfu--auto-timer)
   1151     (setq corfu--auto-timer nil))
   1152   (when (and (not completion-in-region-mode)
   1153              (not defining-kbd-macro)
   1154              (not buffer-read-only)
   1155              (corfu--match-symbol-p corfu-auto-commands this-command)
   1156              (corfu--popup-support-p))
   1157     (if (<= corfu-auto-delay 0)
   1158         (corfu--auto-complete-deferred)
   1159       ;; NOTE: Do not use idle timer since this leads to unacceptable slowdowns,
   1160       ;; in particular if flyspell-mode is enabled.
   1161       (setq corfu--auto-timer
   1162             (run-at-time corfu-auto-delay nil
   1163                          #'corfu--auto-complete-deferred (corfu--auto-tick))))))
   1164 
   1165 (defun corfu--auto-tick ()
   1166   "Return the current tick/status of the buffer.
   1167 Auto completion is only performed if the tick did not change."
   1168   (list (current-buffer) (buffer-chars-modified-tick) (point)))
   1169 
   1170 ;;;###autoload
   1171 (define-minor-mode corfu-mode
   1172   "Completion Overlay Region FUnction."
   1173   :global nil :group 'corfu
   1174   (cond
   1175    (corfu-mode
   1176     ;; FIXME: Install advice which fixes `completion--capf-wrapper', such that
   1177     ;; it respects the completion styles for non-exclusive capfs. See FIXME in
   1178     ;; the `completion--capf-wrapper' function in minibuffer.el, where the
   1179     ;; issue has been mentioned. We never uninstall this advice since the
   1180     ;; advice is active *globally*.
   1181     (advice-add #'completion--capf-wrapper :around #'corfu--capf-wrapper-advice)
   1182     (advice-add #'eldoc-display-message-no-interference-p :before-while #'corfu--allow-eldoc)
   1183     (and corfu-auto (add-hook 'post-command-hook #'corfu--auto-post-command nil 'local))
   1184     (setq-local completion-in-region-function #'corfu--in-region))
   1185    (t
   1186     (remove-hook 'post-command-hook #'corfu--auto-post-command 'local)
   1187     (kill-local-variable 'completion-in-region-function))))
   1188 
   1189 (defun corfu--capf-wrapper (fun &optional prefix)
   1190   "Wrapper for `completion-at-point' FUN.
   1191 The wrapper determines if the capf is applicable at the current position
   1192 and performs sanity checking on the returned result. PREFIX is a prefix
   1193 length override, set to t for manual completion."
   1194   (pcase (funcall fun)
   1195     ((and res `(,beg ,end ,table . ,plist))
   1196      (and (integer-or-marker-p beg) ;; Valid capf result
   1197           (<= beg (point) end)      ;; Sanity checking
   1198           ;; When auto completing, check the prefix length!
   1199           (let ((len (or prefix
   1200                          (plist-get plist :company-prefix-length)
   1201                          (- (point) beg))))
   1202             (or (eq len t) (>= len corfu-auto-prefix)))
   1203           ;; For non-exclusive capfs, check for valid completion.
   1204           (or (not (eq 'no (plist-get plist :exclusive)))
   1205               (let* ((str (buffer-substring-no-properties beg end))
   1206                      (pt (- (point) beg))
   1207                      (pred (plist-get plist :predicate))
   1208                      (md (completion-metadata (substring str 0 pt) table pred)))
   1209                 ;; We use `completion-try-completion' to check if there are
   1210                 ;; completions. The upstream `completion--capf-wrapper' uses
   1211                 ;; `try-completion' which is incorrect since it only checks for
   1212                 ;; prefix completions.
   1213                 (completion-try-completion str table pred pt md)))
   1214           (cons fun res)))))
   1215 
   1216 (defun corfu--capf-wrapper-advice (orig fun which)
   1217   "Around advice for `completion--capf-wrapper'.
   1218 The ORIG function takes the FUN and WHICH arguments."
   1219   (if corfu-mode (corfu--capf-wrapper fun t) (funcall orig fun which)))
   1220 
   1221 ;;;###autoload
   1222 (define-globalized-minor-mode global-corfu-mode corfu-mode corfu--on :group 'corfu)
   1223 
   1224 (defun corfu--on ()
   1225   "Turn `corfu-mode' on."
   1226   (unless (or noninteractive
   1227               (eq (aref (buffer-name) 0) ?\s)
   1228               (memq major-mode corfu-excluded-modes))
   1229     (corfu-mode 1)))
   1230 
   1231 (defun corfu--allow-eldoc ()
   1232   "Return non-nil if Corfu is currently not active."
   1233   (not (and corfu-mode completion-in-region-mode)))
   1234 
   1235 ;; Emacs 28: Do not show Corfu commands with M-X
   1236 (dolist (sym '(corfu-next corfu-previous corfu-first corfu-last corfu-quit corfu-reset
   1237                corfu-complete corfu-insert corfu-scroll-up corfu-scroll-down
   1238                corfu-insert-separator))
   1239   (put sym 'completion-predicate #'ignore))
   1240 
   1241 (provide 'corfu)
   1242 ;;; corfu.el ends here