branch: externals/corfu commit bad4878c403921cdedf64e6c79d1e2fba1c4dc5e Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
Add corfu-reset, improve undo handling (Fix #86) --- README.org | 4 ++-- corfu.el | 29 ++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index d2bd998..14bcad6 100644 --- a/README.org +++ b/README.org @@ -144,7 +144,8 @@ - =RET= -> ~corfu-insert~ - =M-g= -> ~corfu-show-location~ - =M-h= -> ~corfu-show-documentation~ - - =C-g=, =ESC ESC ESC= -> ~corfu-quit~ + - =C-g= -> ~corfu-quit~ + - ~keyboard-escape-quit~ -> ~corfu-reset~ * Complementary packages @@ -167,7 +168,6 @@ - Corfu falls back to the default Completion buffer on non-graphical displays, since Corfu requires child frames. - - The abort handling could be improved, for example the input could be undone. - No sorting by history, since ~completion-at-point~ does not maintain a history (See branch =history= for a possible solution). - There is currently no equivalent for =company-quickhelp=. Documentation and source diff --git a/corfu.el b/corfu.el index 988a271..6412744 100644 --- a/corfu.el +++ b/corfu.el @@ -191,7 +191,7 @@ return a string, possibly an icon." (define-key map [remap completion-at-point] #'corfu-complete) (define-key map [down] #'corfu-next) (define-key map [up] #'corfu-previous) - (define-key map [remap keyboard-escape-quit] #'corfu-quit) + (define-key map [remap keyboard-escape-quit] #'corfu-reset) ;; XXX [tab] is bound because of org-mode ;; The binding should be removed from org-mode-map. (define-key map [tab] #'corfu-complete) @@ -238,6 +238,9 @@ return a string, possibly an icon." (defvar-local corfu--extra nil "Extra completion properties.") +(defvar-local corfu--change-group nil + "Undo change group.") + (defvar-local corfu--auto-start nil "Auto completion start time.") @@ -259,6 +262,7 @@ return a string, possibly an icon." corfu--extra corfu--auto-start corfu--echo-timer + corfu--change-group corfu--metadata) "Buffer-local state variables used by Corfu.") @@ -594,6 +598,16 @@ A scroll bar is displayed from LO to LO+BAR." (interactive) (completion-in-region-mode -1)) +(defun corfu-reset () + "Reset Corfu completion and quit if reset has been executed twice." + (interactive) + (setq corfu--index -1) + ;; Cancel all changes and start new change group. + (cancel-change-group corfu--change-group) + (activate-change-group (setq corfu--change-group (prepare-change-group))) + (when (eq last-command #'corfu-reset) + (corfu-quit))) + (defun corfu--affixate (cands) "Annotate CANDS with annotation function." (setq cands @@ -926,10 +940,13 @@ A scroll bar is displayed from LO to LO+BAR." (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 :exit-function)) - (funcall exit str status)) - (corfu-quit)) + (let ((exit (plist-get corfu--extra :exit-function))) + ;; For successfull completions, amalgamate undo operations, + ;; such that completion can be undone in a single step. + (undo-amalgamate-change-group corfu--change-group) + (corfu-quit) + ;; XXX Is the :exit-function handling sufficient? + (when exit (funcall exit str status)))) (defun corfu-insert () "Insert current candidate." @@ -942,6 +959,7 @@ A scroll bar is displayed from LO to LO+BAR." "Setup Corfu completion state." (when completion-in-region-mode (setq corfu--extra completion-extra-properties) + (activate-change-group (setq corfu--change-group (prepare-change-group))) (setcdr (assq #'completion-in-region-mode minor-mode-overriding-map-alist) corfu-map) (add-hook 'pre-command-hook #'corfu--pre-command nil 'local) (add-hook 'post-command-hook #'corfu--post-command nil 'local) @@ -969,6 +987,7 @@ A scroll bar is displayed from LO to LO+BAR." (remove-hook 'post-command-hook #'corfu--post-command 'local) (when corfu--preview-ov (delete-overlay corfu--preview-ov)) (when corfu--echo-timer (cancel-timer corfu--echo-timer)) + (accept-change-group corfu--change-group) (mapc #'kill-local-variable corfu--state-vars)) (defun corfu--completion-in-region (&rest args)