Subject: Re: bug#69236: 29.1; Unwanted Focus Shift when Clicking Menu Functions in Emacs Mode Line

From: Eli Zaretskii <[email-masked]>
Date: Sun, 18 Feb 2024 20:59:01 +0200

> Date: Thu, 15 Feb 2024 22:55:12 +0100
> From: Gabriele Nicolardi <[email-masked]>
> 
> I have this MWE code:
> 
> (defun mwe-function-1 ()
>   "MWE function 1"
>   (interactive)
>   (unwind-protect
>       (query-replace "foo" "bar" nil (point-min) (point-max))
>     ;; UNWINDFORMS
>     (read-string "function 1 executed")))
> 
> (defun mwe-function-2 ()
>   "MWE function 2"
>   (interactive)
>   (unwind-protect
>       (query-replace "foo" "bar" nil (point-min) (point-max))
>     ;; UNWINDFORMS
>     (read-string "function 2 executed")))
> 
> (defun mwe-function-3 ()
>   "MWE function 3"
>   (interactive)
>   (unwind-protect
>       (query-replace "foo" "bar" nil (point-min) (point-max))
>     ;; UNWINDFORMS
>     (read-string "function 3 executed")))
> 
> (define-minor-mode mwe-mode
>   "MWE mode"
>   :init-value nil
>   :lighter (:eval (propertize " MWE "
>                               'face '(:foreground "RoyalBlue" :background "DarkGoldenrod1")))
> 
>   :keymap
>   `(
>     (,(kbd "<C-kp-1>") . mwe-function)
>     )
> 
>   (if mwe-mode
>       (easy-menu-define mwe-menu mwe-mode-map
>         "MWE"
>         '("MWE mode"
>           ;; I want the menu on mode-line only:
>           :visible (not (eq (framep (selected-frame)) 'x))
>           ["mwe-function-1" mwe-function-1
>            :help "mwe-function 1"]
>           ["mwe-function-2" mwe-function-2
>            :help "mwe-function 2"]
>           ["mwe-function-3" mwe-function-3
>            :help "mwe-function 3"]))
>     t))
> 
> that builds a minor mode where I have the mwe-menu on the mode line.
> 
> It happens, very randomly but quite often, that I am experiencing an
> issue in Emacs where clicking on a function name in a menu displayed on
> the mode line not only triggers the desired function but also has an
> unintended effect on the window or buffer below, interfering with the
> intended functionality.
> 
> When I click on the function mwe-function-* in the menu, it doesn't get
> executed because the mouse event interrupts `query-replace'. The issue
> doesn't occur consistently and is quite random.

Please show the output of "C-h l" in both cases, i.e. when things work
and when they don't.  This should give some ideas about what goes
wrong.

Thanks.