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 13:31:32 +0100

Eli writes:

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

If you look at the code, there is an explicit path for last-kbd-macro:

    (if (string= (symbol-name macroname) "")
	(progn
	  (setq macroname 'last-kbd-macro definition last-kbd-macro)
	  (insert "(setq "))
      (setq definition (symbol-function macroname))

      · macros.el L70-74

which to me indicates an intention of it working, even if the manual
doesn't mention the case. This code has been in macros.el since 1992,
according to git annotate.

If insert-kbd-macro was designed explicitly to not work on the
unnamed, last macro, shouldn't an error be raised or an empty macro
name be rejected, rather than code inserted?

If you try it in a version of Emacs released before 2022-04-01 saving
last-kbd-macro does work - eg Emacs 28.2 as included in Debian 12.

(Just as a side-note: the manual of Emacs 28.2 didn't mention the
possibility either.)

Maybe Stefan Monnier (Cc'd) can shed some light on this, as he
improved the named macro part of insert-kbd-macro in 2022 (c75f65442ddf):

    Also replace the old `kmacro-lambda-form` with a new `kmacro` constructor
    which takes a `key-parse` syntax, so that the code inserted with
    `insert-kbd-macro` is now more readable.

     [...]

    * lisp/macros.el (macro--string-to-vector): New function.
    (insert-kbd-macro): Use it.  Generate code using the `kmacro` constructor.