Subject: bug#65805: 30.0.50; quoted-insert doesn't work in zap-to-char

From: Juri Linkov <[email-masked]>
Date: Wed, 13 Sep 2023 19:39:32 +0300

>> I have a patch where exit-minibuffer in read-char-from-minibuffer-map
>> is bound to a new command that ensures that the minibuffer contains
>> exactly 1 character before exiting.  But need more testing to see
>> what is better.
>
> `read-char-from-minibuffer` usually doesn't require the user to use an
> explicit key to exit (because it tries to mimic `read-char`).

I don't remember how I got into a situation that required typing RET
to exit the minibuffer.  Probably pasted a character into the minibuffer
with C-y.  I see that your patch handles this now.

@@ -3499,7 +3499,7 @@ read-char-from-minibuffer-map
   (let ((map (make-sparse-keymap)))
     (set-keymap-parent map minibuffer-local-map)
 
-    (define-key map [remap self-insert-command] #'read-char-from-minibuffer-insert-char)
+    ;; (define-key map [remap self-insert-command] #'read-char-from-minibuffer-insert-char)
     (define-key map [remap exit-minibuffer] #'read-char-from-minibuffer-insert-other)

Maybe remapping exit-minibuffer is not needed anymore too?

@@ -3530,7 +3530,7 @@ read-char-from-minibuffer-insert-other
 such key, this command discard all minibuffer input and displays
 an error message."
   (interactive)
-  (when (minibufferp)
+  (when (minibufferp) ;;FIXME: Why?

Because of bug#45029.  Maybe this bug number should be mentioned
in the comments.

@@ -3578,6 +3578,10 @@ read-char-from-minibuffer
                                         (interactive)
                                         (let ((help-form msg)) ; lexically bound msg
                                           (help-form-show)))))
+                        ;; FIXME: We use `read-char-from-minibuffer-insert-char'
+                        ;; here only as a kind of alias of `self-insert-command'
+                        ;; to prevent those keys from being remapped to
+                        ;; `read-char-from-minibuffer-insert-other'.
                         (dolist (char chars)
                           (define-key map (vector char)
                                       #'read-char-from-minibuffer-insert-char))
                         (define-key map [remap self-insert-command]
                                     #'read-char-from-minibuffer-insert-other)

Interesting that [remap self-insert-command] is applied even to the
commands explicitly bound to 'self-insert-command' in the same keymap.
I see no way to override this.  So 'read-char-from-minibuffer-insert-char'
can't be replaced with 'self-insert-command'.

@@ -3589,7 +3593,15 @@ read-char-from-minibuffer
                 read-char-from-minibuffer-map))
          ;; Protect this-command when called from pre-command-hook (bug#45029)
          (this-command this-command)
-         (result (progn
+         (result (minibuffer-with-setup-hook
+		     (lambda ()
+		       (add-hook 'post-command-hook
+				 (lambda ()
+				   ;; FIXME: Should we use `<='?
+				   (if (= (1+ (minibuffer-prompt-end))
+					  (point-max))
+                                       (exit-minibuffer)))
+				 nil 'local))

I think we should use `<=' to handle the case when a string with
more than 1 character is pasted with 'C-y'.  This still requires
some special-handling like I intended to do in a new command that
exits the minibuffer where it should remove extra characters to
leave only one: either the first or the last one.  Perhaps leaving
the first character makes more sense.