How to dynamically define a menu item - what's in square brackets?
It's not clear about some fundamental syntax here. define-key
takes a set of inputs, one of which is inside square brackets. What is this construction? How can I dynamically generate what is included in the square brackets?
In a simple case, I can display a one-way menu like this:
(flet ((ok (&optional p1 &rest args) t))
(setq menu-1 (make-sparse-keymap "Title"))
(define-key menu-1 [menu-1-ok-event]
`(menu-item "OK"
ok
:keys ""
:visible t
:enable t))
(x-popup-menu t menu-1))
I can insert additional menu items like:
(flet ((ok (&optional p1 &rest args) t))
(setq menu-1 (make-sparse-keymap "Title"))
(define-key menu-1 [menu-1-event-ok]
`(menu-item "OK"
ok
:keys ""
:visible t
:enable t))
(define-key menu-1 [menu-1-event-1]
`(menu-item "This is line 1"
nil
:keys ""
:visible t
:enable t))
(x-popup-menu t menu-1))
But what if I want to dynamically generate the thing inside the square brackets? What if I want something like this:
(while (< n 5)
(define-key menu-1 [(dynamic-thing n)]
`(menu-item (format "This is line %d" n)
nil
:keys ""
:visible t
:enable t)))
I tried
(define-key menu-1 [(intern (format "menu-1-event-%d" n))]
...
.. but it didn't work. The result is always "trainee". ???
What are square curly braces? The syntax is unfamiliar to me.
source to share
These are vectors . [foo bar]
- syntactic sugar for (quote (vector foo bar))
; it's literal. To plot the vector in which elements are to be evaluated, use the built-in function vector
; it works like list
.
(define-key menu-1 (vector (format "menu-1-event-%d" n)) …
The chapter on menu keyboards can also help .
source to share