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

From: Adam Sjøgren <[email-masked]>
Date: Fri, 28 Mar 2025 21:09:32 +0100

Stefan writes:

> 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)))
>
> 🙁

Yeah, ok, not exactly pretty.

For my use-case generating

    (setq last-kbd-macro (kmacro--keys
       (kmacro "A B B A")))

instead of:

    (setq last-kbd-macro
       (kmacro "A B B A"))

would be sufficient. I think that would be better than what happens
currently, but I can see it is not as comprehensive as also handling
the counter stuff.

How does that actually work for a named macro? If I name the macro
"hep" and insert it, I get:

    (defalias 'hep
       (kmacro "A B B A"))

Maybe that's because I didn't use any counters?

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

Better than inserting some code that doesn't work, agreed.

> 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?

I rarely use macros, and I never use named macros (I always forget
about the possibility).

>From time to time I edit a list of (spam)domains to reject, and I
often create a macro that inserts an email-address and changes the .
to [.] and deletes everything in front of the @.

Instead of recreating that macro when I edit that list, I thought
"Hey, I can probably save that macro in the file and reconstitute it
when I need it".

So I found M-x insert-kbd-macro and didn't give a name when prompted
(as the macro didn't have one), and it promisingly inserted some code,
which I tried evaluating and executing.

I wondered why it didn't work, and at first I assumed I was doing
something wrong.

Then I tried in an older Emacs and saw it generate something slightly
different, which worked, and then I looked in macros.el and found code
that seemed to specifically handle last-kbd-macro.

Which lead me to believe that it ought to work.

I can see that the manual and the code do not quite agree, but I still
think it is a nice and natural functionality that you can save the
current macro and get it back without having to name it.

With C-x e I can repeat the macro by pressing e more times. Is that
functionality present when I do M-x hep (ie run a named macro)?

  Best regards,

    Adam

-- 
 "People just generally like to disagree."                  Adam Sjøgren
                                                       [email-masked]