branch: externals/corfu
commit 037d756a861e138fa622e4dd1a4dd61a815fffd3
Author: Daniel Mendler <[email protected]>
Commit: Daniel Mendler <[email protected]>
Move auto completion code to corfu-auto.el
---
corfu.el | 83 ++-------------------------------
extensions/corfu-auto.el | 117 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 121 insertions(+), 79 deletions(-)
diff --git a/corfu.el b/corfu.el
index 7e9dd1868b..00f1ea04ae 100644
--- a/corfu.el
+++ b/corfu.el
@@ -171,35 +171,6 @@ This function is used even if a completion table specifies
its
own sort function."
:type '(choice (const nil) function))
-(defcustom corfu-auto-trigger ""
- "Characters which trigger auto completion.
-If a trigger character is detected `corfu-auto-prefix' is ignored."
- :type 'string)
-
-(defcustom corfu-auto-prefix 3
- "Minimum length of prefix for auto completion.
-The completion backend can override this with :company-prefix-length.
-It is not recommended to use a small prefix length (below 2), since this
-will create high load for Emacs. See also `corfu-auto-delay' and
-`corfu-auto-trigger'."
- :type 'natnum)
-
-(defcustom corfu-auto-delay 0.2
- "Delay for auto completion.
-It is not recommended to use a short delay or even 0, since this will
-create high load for Emacs, in particular if executing the completion
-backend is costly. Instead of reducing the delay too much, try
-`corfu-auto-trigger' to trigger immediate completion after certain
-characters."
- :type 'float)
-
-(defcustom corfu-auto-commands
- '("self-insert-command\\'" "delete-backward-char\\'"
"\\`backward-delete-char"
- c-electric-colon c-electric-lt-gt c-electric-slash c-scope-operator)
- "Commands which initiate auto completion.
-The list can contain either command symbols or regular expressions."
- :type '(repeat (choice regexp symbol)))
-
(defcustom corfu-auto nil
"Enable auto completion.
Auto completion is disabled by default for safety and unobtrusiveness.
@@ -277,9 +248,6 @@ settings `corfu-auto-delay', `corfu-auto-prefix' and
"M-h" 'corfu-info-documentation
"M-SPC" #'corfu-insert-separator)
-(defvar corfu--auto-timer (timer-create)
- "Auto completion timer.")
-
(defvar corfu--candidates nil
"List of candidates.")
@@ -1051,51 +1019,6 @@ See `completion-in-region' for the arguments BEG, END,
TABLE, PRED."
(define-key map (vector last-command-event) replace)
(funcall replace)))
-(defun corfu--auto-complete-deferred (&optional tick)
- "Initiate auto completion if TICK did not change."
- (corfu--protect
- (lambda ()
- (when (and (not completion-in-region-mode)
- (or (not tick) (equal tick (corfu--auto-tick))))
- (pcase (while-no-input ;; Interruptible Capf query
- (run-hook-wrapped
- 'completion-at-point-functions
- #'corfu--capf-wrapper corfu-auto-prefix corfu-auto-trigger))
- (`(,fun ,beg ,end ,table . ,plist)
- (let ((completion-in-region-mode-predicate
- (lambda ()
- (when-let* ((newbeg (car-safe (funcall fun))))
- (= newbeg beg))))
- (completion-extra-properties plist))
- (corfu--setup beg end table (plist-get plist :predicate))
- (corfu--exhibit 'auto))))))))
-
-(defun corfu--auto-post-command ()
- "Post command hook which initiates auto completion."
- (corfu--protect
- (lambda ()
- (cancel-timer corfu--auto-timer)
- (when (and (not completion-in-region-mode)
- (not defining-kbd-macro)
- (not buffer-read-only)
- (corfu--match-symbol-p corfu-auto-commands this-command)
- (corfu--popup-support-p))
- (if (or (<= corfu-auto-delay 0)
- (seq-contains-p corfu-auto-trigger last-command-event))
- (corfu--auto-complete-deferred)
- ;; Do not use `timer-set-idle-time' since this leads to
- ;; unpredictable pauses, in particular with `flyspell-mode'.
- (timer-set-time corfu--auto-timer
- (timer-relative-time nil corfu-auto-delay))
- (timer-set-function corfu--auto-timer #'corfu--auto-complete-deferred
- (list (corfu--auto-tick)))
- (timer-activate corfu--auto-timer))))))
-
-(defun corfu--auto-tick ()
- "Return the current tick/status of the buffer.
-Auto completion is only performed if the tick did not change."
- (list (selected-window) (current-buffer) (buffer-chars-modified-tick)
(point)))
-
(cl-defgeneric corfu--popup-show (pos off width lines &optional curr lo bar)
"Show LINES as popup at POS - OFF.
WIDTH is the width of the popup.
@@ -1455,10 +1378,12 @@ Quit if no candidate is selected."
:group 'corfu :keymap corfu-mode-map
(cond
(corfu-mode
- (and corfu-auto (add-hook 'post-command-hook #'corfu--auto-post-command
100 'local))
+ (when corfu-auto
+ (require 'corfu-auto)
+ (add-hook 'post-command-hook 'corfu-auto--post-command 100 'local))
(setq-local completion-in-region-function #'corfu--in-region))
(t
- (remove-hook 'post-command-hook #'corfu--auto-post-command 'local)
+ (remove-hook 'post-command-hook 'corfu-auto--post-command 'local)
(kill-local-variable 'completion-in-region-function))))
(defcustom global-corfu-minibuffer t
diff --git a/extensions/corfu-auto.el b/extensions/corfu-auto.el
new file mode 100644
index 0000000000..6cb0e066a7
--- /dev/null
+++ b/extensions/corfu-auto.el
@@ -0,0 +1,117 @@
+;;; corfu-auto.el --- Auto completion -*- lexical-binding: t -*-
+
+;; Copyright (C) 2021-2026 Free Software Foundation, Inc.
+
+;; Author: Daniel Mendler <[email protected]>
+;; Maintainer: Daniel Mendler <[email protected]>
+;; Created: 2022
+;; Version: 2.7
+;; Package-Requires: ((emacs "29.1") (compat "30") (corfu "2.7"))
+;; URL: https://github.com/minad/corfu
+
+;; This file is part of GNU Emacs.
+
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Automatically show the popup. Enable `corfu-auto-mode'.
+
+;;; Code:
+
+(require 'corfu)
+
+(defcustom corfu-auto-trigger ""
+ "Characters which trigger auto completion.
+If a trigger character is detected `corfu-auto-prefix' is ignored."
+ :type 'string
+ :group 'corfu)
+
+(defcustom corfu-auto-prefix 3
+ "Minimum length of prefix for auto completion.
+The completion backend can override this with :company-prefix-length.
+It is not recommended to use a small prefix length (below 2), since this
+will create high load for Emacs. See also `corfu-auto-delay' and
+`corfu-auto-trigger'."
+ :type 'natnum
+ :group 'corfu)
+
+(defcustom corfu-auto-delay 0.2
+ "Delay for auto completion.
+It is not recommended to use a short delay or even 0, since this will
+create high load for Emacs, in particular if executing the completion
+backend is costly. Instead of reducing the delay too much, try
+`corfu-auto-trigger' to trigger immediate completion after certain
+characters."
+ :type 'float
+ :group 'corfu)
+
+(defcustom corfu-auto-commands
+ '("self-insert-command\\'" "delete-backward-char\\'"
"\\`backward-delete-char"
+ c-electric-colon c-electric-lt-gt c-electric-slash c-scope-operator)
+ "Commands which initiate auto completion.
+The list can contain either command symbols or regular expressions."
+ :type '(repeat (choice regexp symbol))
+ :group 'corfu)
+
+(defvar corfu-auto--timer (timer-create)
+ "Auto completion timer.")
+
+(defun corfu-auto--complete-deferred (&optional tick)
+ "Initiate auto completion if TICK did not change."
+ (corfu--protect
+ (lambda ()
+ (when (and (not completion-in-region-mode)
+ (or (not tick) (equal tick (corfu-auto--tick))))
+ (pcase (while-no-input ;; Interruptible Capf query
+ (run-hook-wrapped
+ 'completion-at-point-functions
+ #'corfu--capf-wrapper corfu-auto-prefix corfu-auto-trigger))
+ (`(,fun ,beg ,end ,table . ,plist)
+ (let ((completion-in-region-mode-predicate
+ (lambda ()
+ (when-let* ((newbeg (car-safe (funcall fun))))
+ (= newbeg beg))))
+ (completion-extra-properties plist))
+ (corfu--setup beg end table (plist-get plist :predicate))
+ (corfu--exhibit 'auto))))))))
+
+(defun corfu-auto--post-command ()
+ "Post command hook which initiates auto completion."
+ (corfu--protect
+ (lambda ()
+ (cancel-timer corfu-auto--timer)
+ (when (and (not completion-in-region-mode)
+ (not defining-kbd-macro)
+ (not buffer-read-only)
+ (corfu--match-symbol-p corfu-auto-commands this-command)
+ (corfu--popup-support-p))
+ (if (or (<= corfu-auto-delay 0)
+ (seq-contains-p corfu-auto-trigger last-command-event))
+ (corfu-auto--complete-deferred)
+ ;; Do not use `timer-set-idle-time' since this leads to
+ ;; unpredictable pauses, in particular with `flyspell-mode'.
+ (timer-set-time corfu-auto--timer
+ (timer-relative-time nil corfu-auto-delay))
+ (timer-set-function corfu-auto--timer #'corfu-auto--complete-deferred
+ (list (corfu-auto--tick)))
+ (timer-activate corfu-auto--timer))))))
+
+(defun corfu-auto--tick ()
+ "Return the current tick/status of the buffer.
+Auto completion is only performed if the tick did not change."
+ (list (selected-window) (current-buffer) (buffer-chars-modified-tick)
(point)))
+
+(provide 'corfu-auto)
+;;; corfu-auto.el ends here