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

From: Robert Pluim <[email-masked]>
Date: Thu, 23 Feb 2023 13:59:40 +0100

>>>>> On Thu, 23 Feb 2023 10:21:05 +0100, Robert Pluim <[email-masked]> said:

    Robert> This fails in emacs-29 as well. Stefan, you rewrote this code to
    Robert> use oclosures, any ideas? I guess we could just use
    Robert> macro--string-to-vector in both cases, but youʼre the expert here :-)

The following passes 'make bootstrap' and 'make check'.

Iʼve probably committed a heinous crime by autoloading an internal
function, but that seemed cleaner than "(require 'macros)" in two
places.

Robert
-- 

diff --git c/lisp/kmacro.el i/lisp/kmacro.el
index 94d8794bd23..411f981d47b 100644
--- c/lisp/kmacro.el
+++ i/lisp/kmacro.el
@@ -380,7 +380,10 @@ kmacro-view-item-no
 (defun kmacro-ring-head ()
   "Return pseudo head element in macro ring."
   (and last-kbd-macro
-       (kmacro last-kbd-macro kmacro-counter kmacro-counter-format-start)))
+       (kmacro (if (stringp last-kbd-macro)
+                   (macro--string-to-vector last-kbd-macro)
+                 last-kbd-macro)
+               kmacro-counter kmacro-counter-format-start)))

 (defun kmacro-push-ring (&optional elt)
@@ -841,8 +844,6 @@ kmacro-lambda-form
       (setq mac     (nth 0 mac)))
     (when (stringp mac)
       ;; `kmacro' interprets a string according to `key-parse'.
-      (require 'macros)
-      (declare-function macro--string-to-vector "macros")
       (setq mac (macro--string-to-vector mac)))
     (kmacro mac counter format)))
 
diff --git c/lisp/macros.el i/lisp/macros.el
index 59c7796551f..e00aedc82b0 100644
--- c/lisp/macros.el
+++ i/lisp/macros.el
@@ -46,6 +46,7 @@ macros--insert-vector-macro
                      " ")
           ?\]))
 
+;;;###autoload
 (defun macro--string-to-vector (str)
   "Convert an old-style string key sequence to the vector form."
   (let ((vec (string-to-vector str)))
diff --git c/test/lisp/kmacro-tests.el i/test/lisp/kmacro-tests.el
index 551fd8b60fc..a325220e8d9 100644
--- c/test/lisp/kmacro-tests.el
+++ i/test/lisp/kmacro-tests.el
@@ -614,6 +614,20 @@ kmacro-tests-name-last-macro-bind-and-rebind
   (kmacro-tests-should-insert "bb"
     (kmacro-tests-simulate-command '(kmacro-tests-symbol-for-test))))
 
+;; Bug#61700 inserting named macro when the definition contains things
+;; that `key-parse' thinks are named keys
+(kmacro-tests-deftest kmacro-tests-name-last-macro-key-parse-syntax ()
+  "Name last macro can rebind a symbol it binds."
+  ;; Make sure our symbol is unbound.
+  (when (fboundp 'kmacro-tests-symbol-for-test)
+    (fmakunbound 'kmacro-tests-symbol-for-test))
+  (setplist 'kmacro-tests-symbol-for-test nil)
+  (kmacro-tests-define-macro "<b> hello </>")
+  (kmacro-name-last-macro 'kmacro-tests-symbol-for-test)
+  ;; Now run the function bound to the symbol.
+  (kmacro-tests-should-insert "<b> hello </>"
+    (kmacro-tests-simulate-command '(kmacro-tests-symbol-for-test))))
+
 (kmacro-tests-deftest kmacro-tests-store-in-register ()
   "Macro can be stored in and retrieved from a register."
   (use-local-map kmacro-tests-keymap)