diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2021-04-20 13:47:53 +0200 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2021-04-20 13:54:43 +0200 |
| commit | fa8a43e1e7497930d9db2274833877629f423abb (patch) | |
| tree | 2f37536883ab44fe5d901610fb608da5751735b8 | |
| parent | 027023845d0ab06d57fe6edb68ffc295ce9f1d95 (diff) | |
Exit from `corfu-complete`
When no further completion is possible and the current
string is a valid match, exit with status 'finished.
See #11
| -rw-r--r-- | corfu.el | 45 |
1 files changed, 27 insertions, 18 deletions
@@ -334,7 +334,7 @@ If `line-spacing/=nil' or in text-mode, the background color is used instead.") (setq corfu--overlays nil) (unless (or (< corfu--index 0) (string-match-p corfu--keep-alive (prin1-to-string this-command))) - (corfu--insert 'exact))) ;; Complete with "exact" input + (corfu--insert 'exact))) (defun corfu-abort () "Abort Corfu completion." @@ -517,16 +517,29 @@ If `line-spacing/=nil' or in text-mode, the background color is used instead.") (defun corfu-complete () "Try to complete current input." (interactive) - (if (>= corfu--index 0) - (corfu--insert nil) ;; Continue completion - (pcase-let* ((`(,beg ,end ,table ,pred) completion-in-region--data) - (pt (max 0 (- (point) beg))) - (str (buffer-substring-no-properties beg end)) - (metadata (completion-metadata (substring str 0 pt) table pred))) - (pcase (completion-try-completion str table pred pt metadata) - ((and `(,newstr . ,newpt) (guard (not (equal str newstr)))) - (completion--replace beg end newstr) - (goto-char (+ beg newpt))))))) + (pcase-let ((`(,beg ,end ,table ,pred) completion-in-region--data)) + (if (>= corfu--index 0) + (corfu--insert nil) + (let* ((pt (max 0 (- (point) beg))) + (str (buffer-substring-no-properties beg end)) + (metadata (completion-metadata (substring str 0 pt) table pred))) + (pcase (completion-try-completion str table pred pt metadata) + ((and `(,newstr . ,newpt) (guard (not (equal str newstr)))) + (completion--replace beg end newstr) + (goto-char (+ beg newpt)))))) + ;; When no further completion is possible and the current + ;; string is a valid match, exit with status 'finished. + (let ((str (buffer-substring-no-properties beg end))) + (when (and (not (stringp (try-completion str table pred))) + (test-completion str table pred)) + (corfu--done str 'finished))))) + +(defun corfu--done (str status) + "Call the `:exit-function' with STR and STATUS and exit completion." + ;; XXX Is the :exit-function handling sufficient? + (when-let (exit (plist-get corfu--extra-properties :exit-function)) + (funcall exit str status)) + (completion-in-region-mode -1)) (defun corfu--insert (status) "Insert current candidate, exit with STATUS if non-nil." @@ -545,13 +558,9 @@ If `line-spacing/=nil' or in text-mode, the background color is used instead.") (setq str (concat (substring str 0 corfu--base) (substring-no-properties (nth (max 0 corfu--index) corfu--candidates)))) - (completion--replace beg end str)) - (if (not status) - (setq corfu--index -1) ;; Reset selection, but continue completion. - ;; XXX Is the :exit-function handling sufficient? - (when-let (exit (plist-get corfu--extra-properties :exit-function)) - (funcall exit str status)) - (completion-in-region-mode -1)))) + (completion--replace beg end str) + (setq corfu--index -1)) ;; Reset selection, but continue completion. + (when status (corfu--done str status)))) ;; Exit with status (defun corfu-insert () "Insert current candidate." |
