Subject: Re: bug#72419: 31.0.50; Macro editing can't deal with event appliers

From: Stefan Monnier <[email-masked]>
Date: Tue, 20 Aug 2024 15:04:37 -0400

> Thanks.  This seems to be a very old bug.  Looks like indeed macro
> editing was not adapted to support event modifier-applying commands,
> or maybe it's a problem with function-key-map.

AFAICT the problem is not specific to event modifier-applying thingies
but to any use of a function binding (i.e. a dynamic remapping) in
`(local-)function-key-map`.

The ugly hack below seems to work for the given recipe, but I tried
a related recipe:

    emacs -Q --nw -f xterm-mouse-mode
    C-x (
    click mouse-1 somewhere to move point
    C-x )
    M-x kmacro-edit-macro RET

and the resulting macro still doesn't look right at all.

        Stefan

diff --git a/lisp/edmacro.el b/lisp/edmacro.el
index c63f7f30c5e..28f74e03b4a 100644
--- a/lisp/edmacro.el
+++ b/lisp/edmacro.el
@@ -593,16 +593,26 @@ edmacro-format-keys
 			      (prog1 (vconcat "C-u " (cl-subseq rest-mac 1 i) " ")
 				(cl-callf cl-subseq rest-mac i)))))))
 	     (bind-len (apply #'max 1
+			      ;; FIXME: Pass `maps' to `lookup-key' directly!
 			      (cl-loop for map in maps
                                        for b = (lookup-key map rest-mac)
                                        when b collect b)))
 	     (key (cl-subseq rest-mac 0 bind-len))
 	     (fkey nil) tlen tkey
+	     ;; FIXME: Pass `maps' to `lookup-key' directly!
 	     (bind (or (cl-loop for map in maps for b = (lookup-key map key)
                                 thereis (and (not (integerp b)) b))
 		       (and (setq fkey (lookup-key local-function-key-map rest-mac))
 			    (setq tlen fkey tkey (cl-subseq rest-mac 0 tlen)
 				  fkey (lookup-key local-function-key-map tkey))
+                            (progn
+                              (when (functionp fkey)
+                                (let ((unread-command-events
+                                       (append (cl-subseq rest-mac tlen) nil)))
+                                  (setq fkey (funcall fkey ""))
+                                  (setq rest-mac (vconcat tkey unread-command-events))))
+                              t)
+			    ;; FIXME: Pass `maps' to `lookup-key' directly!
 			    (cl-loop for map in maps
                                      for b = (lookup-key map fkey)
                                      when (and (not (integerp b)) b)