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

From: Stefan Monnier <[email-masked]>
Date: Fri, 28 Mar 2025 13:50:33 -0400

> The generated code looks like this:
>
>     (setq last-kbd-macro
>        (kmacro "A B B A"))

Hmm... this is nicely readable, but indeed it bumps into the following
problem:

A keyboard macro is made of 3 elements: the sequence of events, of
course, but also a counter and format string.
The "currently installed" (aka "last") keyboard macro stores those
3 pieces of info in, respectively:

    last-kbd-macro
    kmacro-counter
    kmacro-counter-format

When saved elsewhere (e.g. in the `kmacro-ring` or in an init file),
these three elements are bundled into a single `kmacro` object.

So to avoid the error you're seeing, as the code currently stands, the
`(setq last-kbd-macro (kmacro ...))` should be replaced with either
a call to `kmacro-split-ring-element` or something which has the same
effect:

    (defun kmacro-split-ring-element (elt)
      (setq last-kbd-macro (kmacro--keys elt)
            kmacro-counter (kmacro--counter elt)
            kmacro-counter-format-start (kmacro--format elt)))

🙁

Maybe a better fix would be to "force" the user to give a name to the
macro before inserting it.

Could you explain exactly how you bumped into this problem, more
specifically what was your intention behind the use of
`insert-kbd-macro` without giving the macro a name?

        Stefan