Subject: bug#25612: 26.0.50; Slightly suboptimal message for disabled commands

From: Eli Zaretskii <[email-masked]>
Date: Fri, 03 Feb 2017 23:16:27 +0200

> From: Philipp Stephani <[email-masked]>
> Date: Fri, 03 Feb 2017 15:54:06 +0000
> Cc: [email-masked]
> 
>  Yes, this-command-keys returns just "^M" instead of the expected
>  "\370erase-buffer^M" it returned in Emacs 24.2 and older.
> 
> git bisect says
> 
> b593d6a999b21dfee6939b24866a5ec6fbe7d11b is the first bad commit
> commit b593d6a999b21dfee6939b24866a5ec6fbe7d11b
> Author: Aaron S. Hawley <[email-masked]>
> Date: Tue May 1 12:10:02 2012 -0400
> 
> Reimplement execute-extended-command in Elisp.
> * src/keyboard.c (Fexecute_extended_command, Vsuggest_key_bindings):
> Move to simple.el.
> * lisp/simple.el (suggest-key-bindings, execute-extended-command):
> Move from keyboard.c.

Yes, when execute-extended-command was reimplemented in Lisp, the
special code which produced "\370erase-buffer^M" was lost.

Does the change below produce good results?  If so, can anyone suggest
a more elegant way of squirreling M-x into this-command-keys?

diff --git a/lisp/simple.el b/lisp/simple.el
index 441713a..c0dad2d 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1733,6 +1733,9 @@ execute-extended-command
 		       (where-is-internal function overriding-local-map t))))
     (unless (commandp function)
       (error "`%s' is not a valid command name" command-name))
+    ;; Some features, such as novice.el, rely on this-command-keys
+    ;; including M-x COMMAND-NAME RET.
+    (set--this-command-keys (concat "\M-x" (symbol-name function) "\r"))
     (setq this-command function)
     ;; Normally `real-this-command' should never be changed, but here we really
     ;; want to pretend that M-x <cmd> RET is nothing more than a "key
diff --git a/src/keyboard.c b/src/keyboard.c
index a86e7c5..b1eeb03 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -10001,6 +10001,28 @@ See also `this-command-keys-vector'.  */)
 			   XVECTOR (this_command_keys)->contents);
 }
 
+DEFUN ("set--this-command-keys", Fset__this_command_keys,
+       Sset__this_command_keys, 1, 1, 0,
+       doc: /* Set the vector to be returned by `this-command-keys'.
+The argument KEYS must be a string.
+Internal use only.  */)
+  (Lisp_Object keys)
+{
+  CHECK_STRING (keys);
+
+  this_command_key_count = 0;
+  this_single_command_key_start = 0;
+  int key0 = SREF (keys, 0);
+
+  if (key0 == 248)
+    add_command_key (make_number ('x' | meta_modifier));
+  else
+    add_command_key (make_number (key0));
+  for (int i = 1; i < SCHARS (keys); i++)
+    add_command_key (make_number (SREF (keys, i)));
+  return Qnil;
+}
+
 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
        doc: /* Return the key sequence that invoked this command, as a vector.
 However, if the command has called `read-key-sequence', it returns
@@ -11211,6 +11233,7 @@ syms_of_keyboard (void)
   defsubr (&Sthis_command_keys_vector);
   defsubr (&Sthis_single_command_keys);
   defsubr (&Sthis_single_command_raw_keys);
+  defsubr (&Sset__this_command_keys);
   defsubr (&Sclear_this_command_keys);
   defsubr (&Ssuspend_emacs);
   defsubr (&Sabort_recursive_edit);