Subject: Re: bug#47417: Expand/fix use of completion-no-auto-exit flag

From: Juri Linkov <[email-masked]>
Date: Tue, 13 Apr 2021 22:30:35 +0300

> It seems to me a high-level view of completion-no-auto-exit is to allow
> code outside of choose-completion and choose-completion-string to finish
> the completion selection process.  Presently, this flag does inhibit
> closure of the minibuffer when choosing a completion but it does not
> inhibit closure of the list of completions if '?' has been used.  I think
> the two are connected and that whatever external code is used to utilize
> the finalized completion should also handle the closure of the completion
> window.

Currently I'm developing a patch (attached below) that allows to type
‘M-0 RET’ on a completion to insert it to the minibuffer, and quit the window
without exiting the minibuffer.

So to keep backward-compatibility of completion-no-auto-exit,
its behavior should not be changed.

Then there are two options: create a new variable, e.g.
completion-no-auto-quit - to not quit the completion window.

Or better to support a new value of the existing variable
completion-no-auto-exit, e.g. 'no-quit-window'.

> This can be accomplished by simply wrapping the call to 'quit-window'
> within 'choose-completion' with an '(unless completion-no-auto-exit...)'
> like so:
>          (unless completion-no-auto-exit (quit-window nil (posn-window
> (event-start event))))
>
> Please consider adding this as a quick fix.  Then packages like Hyperbole
> which requires 2 clicks/presses to finalize a completion selection could
> use choose-completion without having to replace the whole function.

Please describe the use cases in more details.  When I tried to use
the aforementioned condition, then after selecting a completion,
the selected window is not switched to the minibuffer window.
The completion window says selected.  I'm not sure if this is what is needed.
It seems after selecting a completion without quitting the completion window,
it's expected that the minibuffer window should be selected.

diff --git a/lisp/simple.el b/lisp/simple.el
index 12435a0680..fd17749451 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -8850,10 +8850,12 @@ next-completion
 		    (point) 'mouse-face nil beg))
 	(setq n (1+ n))))))
 
-(defun choose-completion (&optional event)
+(defun choose-completion (&optional event arg)
   "Choose the completion at point.
-If EVENT, use EVENT's position to determine the starting position."
-  (interactive (list last-nonmenu-event))
+If EVENT, use EVENT's position to determine the starting position.
+When the prefix ARG is 0, insert the completion at point to the minibuffer
+and quit the completion window without exiting the minibuffer."
+  (interactive (list last-nonmenu-event current-prefix-arg))
   ;; In case this is run via the mouse, give temporary modes such as
   ;; isearch a chance to turn off.
   (run-hooks 'mouse-leave-buffer-hook)
@@ -8861,6 +8863,7 @@ choose-completion
     (let ((buffer completion-reference-buffer)
           (base-position completion-base-position)
           (insert-function completion-list-insert-choice-function)
+          (completion-no-auto-exit (if (eq arg 0) t completion-no-auto-exit))
           (choice
            (save-excursion
              (goto-char (posn-point (event-start event)))