summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mendler <mail@daniel-mendler.de>2024-03-26 03:33:10 +0100
committerDaniel Mendler <mail@daniel-mendler.de>2024-03-26 03:45:35 +0100
commit7cdc49da9760b06f3cd4487cf25dd90f87a6b6db (patch)
treede36d120282074180c552eecd98313a42a8f6eb0
parent7dc4fb2e4138fdd74b6d566e4e196c0f120751f9 (diff)
corfu--recompute: Preserve selected candidate on further input
-rw-r--r--corfu.el32
1 files changed, 18 insertions, 14 deletions
diff --git a/corfu.el b/corfu.el
index 97f47fe..637ef6b 100644
--- a/corfu.el
+++ b/corfu.el
@@ -643,7 +643,8 @@ FRAME is the existing frame."
(completing-file (eq (corfu--metadata-get 'category) 'file))
(`(,all . ,hl) (corfu--filter-completions str table pred pt corfu--metadata))
(base (or (when-let ((z (last all))) (prog1 (cdr z) (setcdr z nil))) 0))
- (corfu--base (substring str 0 base)))
+ (corfu--base (substring str 0 base))
+ (pre nil))
;; Filter the ignored file extensions. We cannot use modified predicate for
;; this filtering, since this breaks the special casing in the
;; `completion-file-name-table' for `file-exists-p' and `file-directory-p'.
@@ -661,21 +662,25 @@ FRAME is the existing frame."
all (corfu--move-prefix-candidates-to-front field all))
(when (and completing-file (not (string-suffix-p "/" field)))
(setq all (corfu--move-to-front (concat field "/") all)))
- (setq all (corfu--move-to-front field all))
+ (setq all (corfu--move-to-front field all)
+ pre (if (or (eq corfu-preselect 'prompt) (not all)
+ (and completing-file (eq corfu-preselect 'directory)
+ (= (length corfu--base) (length str))
+ (test-completion str table pred))
+ (and (eq corfu-preselect 'valid)
+ (not (equal field (car all)))
+ (not (and completing-file (equal (concat field "/") (car all))))
+ (test-completion str table pred)))
+ -1 0))
`((corfu--base . ,corfu--base)
(corfu--metadata . ,corfu--metadata)
(corfu--candidates . ,all)
(corfu--total . ,(length all))
(corfu--hilit . ,(or hl #'identity))
- (corfu--preselect . ,(if (or (eq corfu-preselect 'prompt) (not all)
- (and completing-file (eq corfu-preselect 'directory)
- (= (length corfu--base) (length str))
- (test-completion str table pred))
- (and (eq corfu-preselect 'valid)
- (not (equal field (car all)))
- (not (and completing-file (equal (concat field "/") (car all))))
- (test-completion str table pred)))
- -1 0)))))
+ (corfu--preselect . ,pre)
+ (corfu--index . ,(or (and (>= corfu--index 0) (/= corfu--index corfu--preselect)
+ (seq-position all (nth corfu--index corfu--candidates)))
+ pre)))))
(defun corfu--update (&optional interruptible)
"Update state, optionally INTERRUPTIBLE."
@@ -702,9 +707,8 @@ FRAME is the existing frame."
t)))
('nil (keyboard-quit))
((and state (pred consp))
- (dolist (s state) (set (car s) (cdr s)))
- (setq corfu--input input
- corfu--index corfu--preselect))))
+ (setq corfu--input input)
+ (dolist (s state) (set (car s) (cdr s))))))
input))
(defun corfu--match-symbol-p (pattern sym)