minad / corfu

:desert_island: corfu.el - COmpletion in Region FUnction
GNU General Public License v3.0
1.06k stars 42 forks source link

corfu-reset: quit when input is equal to initial input #378

Closed olnw closed 9 months ago

olnw commented 9 months ago

I have perceived a problem with the current implementation of corfu-reset. Please inform me if I am misunderstanding anything.

Scenario A: you would like to close Corfu immediately after it has opened. In this case, you would have to run corfu-reset twice.

Scenario B: you manually invoke corfu (say, with completion-at-point) and then scroll through the candidate list. Then, you would like to quit the pop-up by running corfu-reset.

Here is a proof-of-concept solution using advice. Of course, a real solution would directly modify the involved functions. I am also not familiar with the Corfu codebase, so what follows is likely sub-optimal.

(defvar my-corfu-initial-input nil
  "Initial Corfu prompt contents.")

(advice-add
 'corfu-quit
 :after
 (defun my-corfu-reset-initial-input (&rest _)
   (setf my-corfu-initial-input nil)))

(advice-add
 'corfu--update
 :after
 (defun my-corfu-set-initial-input (&rest _)
   (unless my-corfu-initial-input
     (setf my-corfu-initial-input corfu--input))))

(advice-add
 'corfu-reset
 :override
 (defun my-corfu-reset ()
   (interactive)
   (if (/= corfu--index corfu--preselect)
       (progn
         (corfu--goto -1)
         (setq this-command #'corfu-first))
     (if (equal my-corfu-initial-input corfu--input)
         (corfu-quit)

       ;; Cancel all changes and start new change group.
       (cancel-change-group corfu--change-group)
       (activate-change-group (setq corfu--change-group (prepare-change-group)))))))
minad commented 9 months ago

Good idea. Please let me know if the improved corfu-reset works as expected.

olnw commented 9 months ago

Thanks, it works how I expected. Unless I'm mistaken, it's now unnecessary to check if the last command was corfu-reset, since by that point a new change group has been started and (equal str (buffer-substring-no-properties beg end)) will be t.

minad commented 9 months ago

Thanks, indeed!