diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2023-12-22 17:39:49 +0100 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2023-12-22 18:23:08 +0100 |
| commit | 9cebcfedca0ab165fcfc8a56acc2d2513a02b9e3 (patch) | |
| tree | 69656003ca1d3ac553ba4906b409820b9e2ff82e /corfu.el | |
| parent | 8b5dbebea284a8b5b06bbd5dd20c3ce82c13cb1a (diff) | |
Refactoring, Simplify handling of corfu-on-exact-match=show
Diffstat (limited to 'corfu.el')
| -rw-r--r-- | corfu.el | 59 |
1 files changed, 29 insertions, 30 deletions
@@ -72,7 +72,11 @@ The value should lie between 0 and corfu-count/2." :type 'boolean) (defcustom corfu-on-exact-match 'insert - "Configure how a single exact match should be handled." + "Configure how a single exact match should be handled. +- nil: No special handling, continue completion. +- insert: Insert candidate, quit and call the `:exit-function'. +- quit: Quit completion without further action. +- show: Initiate completion even for a single match only." :type '(choice (const insert) (const show) (const quit) (const nil))) (defcustom corfu-continue-commands @@ -852,9 +856,13 @@ Lookup STR in CANDS to restore text properties." (corfu-quit) (corfu--exit-function str status cands))) -(defun corfu--setup () - "Setup Corfu completion state." - (setq corfu--extra completion-extra-properties) +(defun corfu--setup (beg end table pred) + "Setup Corfu completion state. +See `completion-in-region' for the arguments BEG, END, TABLE, PRED." + (setq beg (if (markerp beg) beg (copy-marker beg)) + end (if (and (markerp end) (marker-insertion-type end)) end (copy-marker end t)) + completion-in-region--data (list beg end table pred) + corfu--extra completion-extra-properties) (completion-in-region-mode 1) (activate-change-group (setq corfu--change-group (prepare-change-group))) (setcdr (assq #'completion-in-region-mode minor-mode-overriding-map-alist) corfu-map) @@ -899,19 +907,15 @@ Lookup STR in CANDS to restore text properties." ('nil (corfu--message "No match") nil) ('t (goto-char end) (corfu--message "Sole match") - (if (not (eq corfu-on-exact-match 'show)) - (corfu--exit-function - str 'finished - (alist-get 'corfu--candidates (corfu--recompute str pt table pred))) - (unless (markerp beg) (setq beg (copy-marker beg))) - (setq end (copy-marker end t) - completion-in-region--data (list beg end table pred)) - (corfu--setup)) + (if (eq corfu-on-exact-match 'show) + (corfu--setup beg end table pred) + (corfu--exit-function + str 'finished + (alist-get 'corfu--candidates (corfu--recompute str pt table pred)))) t) (`(,newstr . ,newpt) - (unless (markerp beg) (setq beg (copy-marker beg))) - (setq end (copy-marker end t) - completion-in-region--data (list beg end table pred)) + (setq beg (if (markerp beg) beg (copy-marker beg)) + end (copy-marker end t)) (corfu--replace beg end newstr) (goto-char (+ beg newpt)) (let* ((state (corfu--recompute newstr newpt table pred)) @@ -919,24 +923,24 @@ Lookup STR in CANDS to restore text properties." (total (alist-get 'corfu--total state)) (candidates (alist-get 'corfu--candidates state))) (if (= total 1) - ;; If completion is finished and cannot be further completed, - ;; and the value of corfu-on-exact-match is not 'show, - ;; return 'finished. Otherwise setup the Corfu popup. + ;; If completion is finished and cannot be further completed, and + ;; the value of `corfu-on-exact-match' is not 'show, return + ;; 'finished. Otherwise setup the Corfu popup. (if (or (eq corfu-on-exact-match 'show) (consp (completion-try-completion newstr table pred newpt (completion-metadata newstr table pred)))) - (corfu--setup) + (corfu--setup beg end table pred) (corfu--exit-function newstr 'finished candidates)) (if (or (= total 0) (not threshold) (and (not (eq threshold t)) (< threshold total))) - (corfu--setup) + (corfu--setup beg end table pred) (corfu--cycle-candidates total candidates (+ (length base) beg) end) ;; Do not show Corfu when "trivially" cycling, i.e., ;; when the completion is finished after the candidate. (unless (equal (completion-boundaries (car candidates) table pred "") '(0 . 0)) - (corfu--setup))))) + (corfu--setup beg end table pred))))) t)))) (defun corfu--message (&rest msg) @@ -972,12 +976,7 @@ See `completion-in-region' for the arguments BEG, END, TABLE, PRED." (when-let ((newbeg (car-safe (funcall fun)))) (= newbeg beg)))) (completion-extra-properties plist)) - (setq completion-in-region--data - (list (if (markerp beg) beg (copy-marker beg)) - (copy-marker end t) - table - (plist-get plist :predicate))) - (corfu--setup) + (corfu--setup beg end table (plist-get plist :predicate)) (corfu--exhibit 'auto)))))) (defun corfu--auto-post-command () @@ -1131,10 +1130,10 @@ AUTO is non-nil when initializing auto completion." (cond ;; 1) Single exactly matching candidate and no further completion is possible. ((and (not (equal str "")) - (not (eq corfu-on-exact-match 'show)) (equal (car corfu--candidates) str) (not (cdr corfu--candidates)) - (not (consp (completion-try-completion str table pred pt corfu--metadata))) - (or auto corfu-on-exact-match)) + (not (eq corfu-on-exact-match 'show)) + (or auto corfu-on-exact-match) + (not (consp (completion-try-completion str table pred pt corfu--metadata)))) ;; Quit directly when initializing auto completion. (if (or auto (eq corfu-on-exact-match 'quit)) (corfu-quit) |
