1

I'm facing an issue (Unwanted Focus Shift when Clicking Menu Functions in Emacs Mode Line) regarding mouse event handling in buffer windows during menus opening. To address this, I'm attempting to temporarily inhibit mouse event handling in buffer windows when menus are opened.

Please note that by I mean 'inhibit', not 'unbind' (unset-key). During menu opening (and for a short period after its closure, probably), windows should be completely unaware of mouse events.

This issue is crucial to me as I'm building tools for a team that uses Emacs for editing LaTeX documents, and I'm trying to make the system more user-friendly by organizing the many available functions into minor modes and related menus. The problem of not being able to reliably launch functions using the menus undermines my work.

The functions that give me trouble are those containing query-replace* (the mouse event closes the function and subsequent ones) or those that use the minibuffer like read-string (the focus switches from the minibuffer to the main window).

I have verified that, in the case of query-replace, modifying the query-replace-map in this way:

(define-key query-replace-map [mouse-1] 'recenter)
(define-key query-replace-map [down-mouse-1] 'recenter)
etc.

partially resolves the issue.

I believe that a real solution could be to make the windows completely unaware of the mouse events during menu usage (and for a short moment after their closure).

Maybe some hooks at the opening and exiting of a menu could be helpful.

Does anyone have experience with this issue or suggestions on how to tackle it?

Note. I use easy-menu-define to build my menus.

2 Answers 2

0

The workaround I found is to intercept and "unread" the mouse-1 event using this macro:

(defmacro with-ignore-mouse-events (&rest body)
  "Macro to ignore mouse events before evaluating BODY."
  `(progn
     (when (input-pending-p)
       (let ((event (read-event)))
         (if (mouse-event-p event)
             (let ((button (event-basic-type event)))
               (if (eq button 'mouse-1)
                   (ignore)
                 (setq unread-command-events (list event))))
           (setq unread-command-events (list event)))))
     ,@body))

The complete solution is explained here: https://emacs.stackexchange.com/a/80427/15606

A better solution would have been to automatically wrap all functions, when launched from the menu, with my macro, but I have no idea how to accomplish that.

0

Probably overkill for your situation, but you might consider installing package disable-mouse and toggling global-disable-mouse-mode as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.