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.
Creating a Minimal Working Example (MWE) for the menus, as they were generated using minor modes and easy-menu-define, would be challenging.
I would appreciate any insights or suggestions on how to diagnose and resolve this issue. If you've encountered a similar problem or have experience with focus shifts when using menus in the mode line, please share your insights. Thank you!
I thought that introducing a delay, let's say half a second, between the moment I click on the function name in the menu and its actual execution might solve the problem. Unfortunately, I have no idea how to implement this delay.
Edit. This is my MWE:
(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))
emacs --version
GNU Emacs 29.1
Development version 28fb02492c24 on master branch; build date 2023-12-10.
$ uname -a
Linux ---- 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ echo $XDG_CURRENT_DESKTOP
MATE
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.
I would try to add a dalay between the mouse event on the menu and the function execution.
Edit 2. I updated my MWE adding an UNWINDFORM to my functions and my Emacs
version. Now I am on Emacs 29.1
.
It continues to happen, randomly, that query-replace
gets interrupted by mouse capture within the buffer window.
Indeed (from the manual):
Aside from this, any other character exits the
query-replace
, and is then reread as part of a key sequence. Thus, if you typeC-k
, it exits thequery-replace
and then kills to end of line. In particular,C-g
simply exits thequery-replace
.
However, the UNWIND-FORM is always executed.

Emacs
to 29.1. – Gabriele Feb 15 '24 at 21:31query-replace
finishes, then hit enter when I see the final “function 1 executed” prompt. It is hard to tell what your gif is doing, but whatever it is must be caused by some other part of your Emacs config, or by the system you are running on, your hardware, etc. – db48x Feb 16 '24 at 08:49C-g
to abort the function when it works in the right way. In the MWE the issue is "rare" but in the my normal setting the issue happens more frequently. – Gabriele Feb 16 '24 at 08:59