Subject: bug#77317: 31.0.50; insert-kbd-macro RET produces elisp that does not work

From: Eli Zaretskii <[email-masked]>
Date: Fri, 28 Mar 2025 13:57:07 +0300

> Date: Thu, 27 Mar 2025 19:37:39 +0100
> From:  Adam Sjøgren via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> 
> If I record a macro, for instance by pressing C-x ( A B B A C-x ) I can
> do M-x insert-kbd-macro RET to have elisp representing that macro
> inserted in the current buffer.
> 
> If I evaluate that code and then try to execute the macro with C-x e, I
> get the error: "Keyboard macros must be strings or vectors".
> 
> I was expecting the macro to be executed, inserting "ABBA".
> 
> The generated code looks like this:
> 
>     (setq last-kbd-macro
>        (kmacro "A B B A"))
> 
> and if I evaluate it, *Messages* contains:
> 
>     #[256 "\301\242\302^[^\\305\300^B\306#\210\301^L\240*\207" [[65 66
>      66 65] (0) "%d" kmacro-counter-format-start kmacro-counter
>      execute-kbd-macro kmacro-loop-setup-function] 5 kmacro]
> 
> and pressing C-x e to execute the macro results in the above mentioned
> error.
> 
> (Naming the macro and inserting it creates a defalias, which works -
> it's only the last-kbd-macro case that does not work.)

The Emacs user manual says

     If you wish to save a keyboard macro for later use, you can give it a
  name using ‘C-x C-k n’ (‘kmacro-name-last-macro’).  [...]

     Once a macro has a command name, you can save its definition in a
  file.  Then it can be used in another editing session.  First, visit the
  file you want to save the definition in.  Then use this command:

       M-x insert-kbd-macro <RET> MACRONAME <RET>

  This inserts some Lisp code that, when executed later, will define the
  same macro with the same definition it has now.  (You don't need to
  understand Lisp code to do this, because ‘insert-kbd-macro’ writes the
  Lisp code for you.)  Then save the file.  You can load the file later
  with ‘load-file’ (*note Lisp Libraries::).  If the file you save in is
  your init file ‘~/.emacs’ (*note Init File::) then the macro will be
  defined each time you run Emacs.

So it sounds like this is supposed to work for macros that have been
given a name.