Subject: Re: bug#21329: 25.0.50; Flyspell minor mode produces weird effects on keyboard macros

From: Eli Zaretskii <[email-masked]>
Date: Mon, 24 Aug 2015 17:39:09 +0300

> From: Mark Karpov <[email-masked]>
> Date: Sun, 23 Aug 2015 17:57:44 +0600
> 
> This is really strange. Here is how to reproduce it in development
> version of Emacs:
> 
> 1. Start Emacs with ‘-Q’ flag.
> 
> 2. Enable ‘flyspell-mode’ (M-x flyspell-mode RET).
> 
> 3. Start recording keyboard macro with F3.
> 
> 4. Type (pick one of these, they all produce different effects):
> 
>    * abc
>    * prop_
> 
> 5. Finish recording of the keyboard macro with F4.
> 
> 6. Hit F4 again to run the macro.
> 
> If you chose “abc”, you will get:
> 
> abcabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbccabbcca
> 
> And in ‘*Messages*’:
> 
> For information about GNU Emacs and the GNU system, type C-h C-a.
> completing-read-default: Command attempted to use minibuffer while in minibuffer
> Quit

This is a very old problem, I see it in Emacs 22.

Here's a simple recipe to demonstrate what happens:

  emacs -Q

  Evaluate in *scratch*:

    (defun my-hook ()
      ""
      (sit-for 1))
    (add-hook 'post-command-hook 'my-hook t t)

  Now type "F3 abc F4 F4", and you will get the same problem as in the
  original report.  Type "C-x C-k n foo RET" followed by "M-x
  insert-kbd-macro RET", and you will see that the macro is defined as
  a vector that includes F4 at its end, which is the cause of the
  unexpected recursive invocation of the macro by F4.

The problem is that 'sit-for' calls 'read-event', and then "undoes"
that by pushing the events it read onto unread-command-events.  This
evidently messes up keyboard macro recording.  (It also runs afoul of
the recent changes in keyboard.c related to recording events, which
causes the characters to be duplicated in the macro.)

The following band-aid fixes the problem, but I will wait for The
Powers That Be to comment on this before pushing.  Or maybe we should
introduce an "unrecord-key" facility for use by code such as we have
in sit-for?

--- lisp/subr.el~	2015-08-05 12:26:16.000000000 +0300
+++ lisp/subr.el	2015-08-24 13:53:09.858014100 +0300
@@ -2276,7 +2276,8 @@
     t)
    ((input-pending-p t)
     nil)
-   ((<= seconds 0)
+   ((or (<= seconds 0)
+        defining-kbd-macro)
     (or nodisp (redisplay)))
    (t
     (or nodisp (redisplay))