Subject: bug#60817: 28.2; `x-focus-frame' causes spurious input events causes edebug not showing intermediate results

From: Eli Zaretskii <[email-masked]>
Date: Sun, 15 Jan 2023 08:23:58 +0200

> Date: Sat, 14 Jan 2023 22:55:15 +0100
> From:  "Farblos" via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> This is Emacs 28.2 from the Debian emacs-lucid package running on fvwm3
> on Xorg.  I guess that either the lucid toolkit or fvwm3 is part of the
> problem here, since I cannot reproduce this on a gtk3 Emacs.
> 
> To demonstrate the problem:
> 
>   emacs -Q
>   M-x find-function fundamental-mode RET
>   C-u C-M-x                           ; instrument function
>   C-x 4 b *Messages* RET              ; show message buffer
>   C-x o
>   M-x fundamental-mode RET            ; call function
> 
> Then step through function `fundamental-mode' in edebug using SPC.
> While doing so I do not get the intermediate evaluation results
> displayed, but rather only the message "Stop".  My message buffer looks
> like this after stepping through the function:
> 
>   fundamental-mode
>   Stop
> 
>   Result: nil
>   Stop
> 
>   Stop
> 
>   Result: nil
>   Stop
> 
> Meaning: These permanent "Stop" messages hide the intermediate
> results.  I haven't found much information on this on the net,
> only the following thread on gnu.emacs.help:
> 
>   https://groups.google.com/g/gnu.emacs.help/c/ojSN-PgNvxg
> 
> The cause seems to be the call to function `x-focus-frame' in
> function `edebug--display-1':
> 
>   (if (not (memq (framep (selected-frame)) '(nil t pc)))
>       (x-focus-frame (selected-frame)))
> 
> When I instrument that snippet to
> 
>   (when (not (memq (framep (selected-frame)) '(nil t pc)))
>     (message "input-pending-pre: %S" (input-pending-p))
>     (x-focus-frame (selected-frame))
>     (message "input-pending-pst: %S" (input-pending-p)))
> 
> my message buffer looks like this instead:
> 
>   fundamental-mode
>   input-pending-pre: t
>   input-pending-pst: t
>   Stop
> 
>   input-pending-pre: nil
>   input-pending-pst: t
>   Result: nil
>   Stop
> 
>   input-pending-pre: nil
>   input-pending-pst: t
>   Stop
> 
>   input-pending-pre: nil
>   input-pending-pst: t
>   Result: nil
>   Stop
> 
> That spurious pending input caused by `x-focus-frame' triggers
> the following snippet later in function `edebug--display-1':
> 
>   (when (input-pending-p)
>     (setq edebug-stop t)
>     (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
>     (edebug-stop))
> 
> which causes the "Stop" messages.

AFAIU, input-pending-p should filter out focus events, so I don't
think I understand why you see this.  Can you try this in Emacs 29 or
the master branch of the Emacs Git repository, so we make sure this
problem was not fixed already?  If it still happens in those newer
versions, maybe you can step with GDB through get_input_pending, when
it's called from input-pending-p, and through readable_events called
by get_input_pending, and see why focus events aren't filtered out?