Subject: bug#25860: 25.1; Double macro execution

From: Tino Calancha <[email-masked]>
Date: Sat, 25 Feb 2017 19:10:20 +0900

Tino Calancha <[email-masked]> writes:

> II) ;;; Using macro `insert-a' we insert "a" _twice_; so you need to remove
>     ;;; <<insert-a>> line by hand:
> F3 M-x insert-a RET F4
> C-x C-k e RET
> ;; Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.
> ;; Original keys: <<insert-a>> a RET
>
> Command: last-kbd-macro
> Key: none
>
> Macro:
>
> <<insert-a>>		;; insert-a
> a			;; self-insert-command
> RET			;; newline

How about introduce a function doing such clean up for us?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>From 9c3b20f573b3f448ba98fa4f1f5c950bd2e9f39a Mon Sep 17 00:00:00 2001
From: Tino Calancha <[email-masked]>
Date: Sat, 25 Feb 2017 19:05:43 +0900
Subject: [PATCH] Prevent from unintentional duplicate kbd calls

lisp/kmacro.el (kmacro-avoid-double-kbd-calls): New defun.
(kmacro-end-macro): Use it (Bug#25860).
---
 lisp/kmacro.el | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/lisp/kmacro.el b/lisp/kmacro.el
index 838a492b6c..baa1c4bc7a 100644
--- a/lisp/kmacro.el
+++ b/lisp/kmacro.el
@@ -592,6 +592,16 @@ kmacro-start-macro
       (if (and defining-kbd-macro append)
 	  (setq defining-kbd-macro 'append)))))
 
+(declare-function edmacro-finish-edit "edmacro")
+
+;; Prevent from unintentional duplicate kbd calls (Bug#25860).
+(defun kmacro--avoid-double-kbd-calls ()
+  (edit-kbd-macro [f4] t)
+  (while (re-search-forward "<<\\([^>]+\\)>>" nil t)
+    (let ((sym (intern (match-string 1))))
+      (when (get sym 'kmacro)
+        (delete-region (match-beginning 0) (match-end 0)))))
+  (edmacro-finish-edit))
 
 ;;;###autoload
 (defun kmacro-end-macro (arg)
@@ -609,6 +619,7 @@ kmacro-end-macro
    ;; Just ignore it when executing the macro.
   (unless executing-kbd-macro
     (end-kbd-macro arg #'kmacro-loop-setup-function)
+    (kmacro--avoid-double-kbd-calls)
     (when (and last-kbd-macro (= (length last-kbd-macro) 0))
       (setq last-kbd-macro nil)
       (message "Ignore empty macro")
-- 
2.11.0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In GNU Emacs 26.0.50 (build 8, x86_64-pc-linux-gnu, GTK+ Version 3.22.7)
 of 2017-02-25
Repository revision: ecbd5f9ac6eb2d31241657bbb3e3f9b860391054