In reference to my previous question Unwanted Focus Shift when Clicking Menu Functions in Emacs Mode Line, I have found a possible solution:
(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))
(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-1* ()
(interactive)
(with-ignore-mouse-events
(mwe-function-1)))
(defun mwe-function-2 ()
"MWE function 3"
(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))
I defined a starred version of my mwe-function-1
wrapped with the with-ignore-mouse-events
macro. I made some test and this seems to fix my problem.
My question is: Is there a way to generate a starred version (other suffixes are also ok) of a given function without explicitly define it?
What I need is that SOME functions called using the menu must be wrapped with the with-ignore-mouse-events
macro. Using a lambda function
in the menu definition could also be good form me, but the best would be using a "suffix" without the need to define the "suffixed" function.
Is that possible?
I'm open to other solutions.
Note added. I mean I want to use the starred version of my mwe-function-1
but don't want to define it. I'm searching for a method to wrap SOME of my function with my macro when called from the menu. I though that a system to "autodefine" these functions could be a solution but I don't know if it is possible.
I mean when the "compiler" finds an undefined symbol with a given suffix it defines the function wrapping the "basename" function with the given macro.
I will follow Phils suggestion:
If you have a list of commands to create derivatives of, and the same macro is applicable in each case, then simply loop over the list, generating a derivative for each one via your macro.