Subject: bug#27470: 26.0.50; this-command-keys and interactive input

From: Eli Zaretskii <[email-masked]>
Date: Sat, 24 Jun 2017 10:01:17 +0300

> From: Michael Heerdegen <[email-masked]>
> Date: Sat, 24 Jun 2017 07:58:47 +0200
> 
> (defun test (arg)
>   (interactive (list (read-from-minibuffer "Input: ")))
>   (message "%S" (cons this-command (this-command-keys)))
>   arg)
> 
> (global-set-key [f12] #'test)
> 
> where f12 stands for some random key sequence.
> 
> Hit f12 and give any input.  I get the following message:
> 
> (test . "^M")
> 
> I would expect (test . f12).

This happens because read-from-minibuffer enters recursive-edit, and
we don't preserve this-command-keys across recursive-edit invocations.

This has been Emacs behavior since forever.

The patch below seems to fix this, but I have no idea what it could
break, given how old this behavior is.  So if we are to accept this,
I'd like to have a few people step forward and run with the change for
a month or so, to see there are no adverse effects.  We've been burnt
several times lately by changes in keyboard-input stuff that broke
subtle but very useful features.

Thanks.

diff --git a/src/minibuf.c b/src/minibuf.c
index 1bbe276..5617941 100644
--- a/src/minibuf.c
+++ b/src/minibuf.c
@@ -497,6 +497,8 @@ read_minibuf (Lisp_Object map, Lisp_Object initial, Lisp_Object prompt,
 				  Fcons (Vminibuffer_history_position,
 					 Fcons (Vminibuffer_history_variable,
 						minibuf_save_list))))));
+  minibuf_save_list
+    = Fcons (Fthis_command_keys_vector (), minibuf_save_list);
 
   record_unwind_protect_void (read_minibuf_unwind);
   minibuf_level++;
@@ -836,6 +838,11 @@ read_minibuf_unwind (void)
   Fset_buffer (XWINDOW (window)->contents);
 
   /* Restore prompt, etc, from outer minibuffer level.  */
+  Lisp_Object key_vec = Fcar (minibuf_save_list);
+  eassert (VECTORP (key_vec));
+  this_command_key_count = XFASTINT (Flength (key_vec));
+  this_command_keys = key_vec;
+  minibuf_save_list = Fcdr (minibuf_save_list);
   minibuf_prompt = Fcar (minibuf_save_list);
   minibuf_save_list = Fcdr (minibuf_save_list);
   minibuf_prompt_width = XFASTINT (Fcar (minibuf_save_list));