Not clear on some fundamental syntax here. define-key accepts a set of inputs, one of which is inside square braces. What is that construct? How can I dynamically generate what goes inside the square braces?
In the simple case, I can display a one-item 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 this:
(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 square braces? 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 that did not work. The result is always “intern”. ???
What are the square braces? The syntax is unfamiliar to me.
These are vectors.
[foo bar]is syntactic sugar for(quote (vector foo bar)); it’s a literal. To construct a vector where the elements need to be evaluated, use thevectorbuilt-in function explicitly; it works likelist.The chapter on menu keymaps may help as well.