Subject: bug#61700: 30.0.50; insert-kbd-macro fails for named macro but not last macro

From: Josh Moller-Mara <[email-masked]>
Date: Tue, 21 Feb 2023 17:18:16 -0800

Using “M-x insert-kbd-macro” can insert an incorrect kmacro form for a
named macro, while still correctly inserting the “last-kbd-macro”.

To see this, we’ll define a macro that simply inserts the HTML string
“<b>hello</b>” (press F3, type the previous string, then press F4).
We’ll name this macro “bold-hello” using “C-x C-k n”.

Inserting the definition to the last macro (using “M-x insert-kbd-macro”
and leaving the prompt blank) returns:

(setq last-kbd-macro
   (kmacro "< b > H e l l o < / b >"))

Inserting the definition to “bold-hello” (using “M-x insert-kbd-macro”
and typing “bold-hello” at the prompt) returns:

(defalias 'bold-hello
   (kmacro "<b> H e l l o < / b >"))

Notice the difference in “< b >” vs “<b>”.  The “bold-hello” command
doesn’t work, printing the message:

“After 0 kbd macro iterations: Keyboard macro terminated by a command ringing the bell”

I think this may have to do with the use of “key-parse” in “kmacro” (see
“(find-function 'kmacro)”).

(key-parse "<b>hello</b>") returns
“[b 104 101 108 108 111 60 47 98 62]”
but
(macro--string-to-vector "<b>hello</b>") returns
“[60 98 62 104 101 108 108 111 60 47 98 62]”

The latter form is used in “insert-kbd-macro” when inserting
“last-kbd-macro”.

You can also quickly replicate the issue by evaluating the following
form:

(progn
  (setq last-kbd-macro "<b>hello</b>")
  (kmacro-name-last-macro 'bold-hello)
  (let ((temp-buffer (generate-new-buffer "*macros*")))
    (with-current-buffer temp-buffer
      (insert-kbd-macro (intern ""))
      (insert-kbd-macro 'bold-hello))
    (display-buffer temp-buffer)))

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.35, cairo version 1.16.0)
Repository revision: 2c7e87c73a90effab66fa7c783855199880315d3
Repository branch: master
System Description: NixOS 22.11 (Raccoon)

Configured using:
 'configure
 --prefix=/nix/store/bdg6d4gm3ma2hja63hbxwrkaap04j2c6-emacs-pgtk-20230217.0
 --disable-build-details --with-modules --with-pgtk
 --with-native-compilation'

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON
LIBSELINUX LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER
PGTK PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS
TREE_SITTER WEBP XIM GTK3 ZLIB