Guidance on Dynamically Generating Sub-menus #301
-
Asking for guidance on how one could dynamically generate sub-menus using Transient. Solving this would enable an Transient-based implementation of the In particular if the variable
Drawing from the example provided in transient-showcase, I was able to get this simple example code working for one level of dynamic menu generation. It is not clear to me what direction should be taken to extend this to support sub-menus. (transient-define-prefix cc/dummy-prefix ()
"Placeholder prefix to support dynamic Transient menu generation."
[])
(setq cc/temp-data
(list
'("a" "A" (lambda ()
(interactive)
(message "A okay!")))
'("b" "B" (lambda ()
(interactive)
(message "B okay!")))
))
(defun cc/option-generator (items)
(mapcar (lambda (x)
(transient-parse-suffix
#'cc/dummy-prefix
x))
items))
(transient-define-prefix cc/tsc-generated-child ()
"Prefix that uses `setup-children' to generate single child."
["Dynamic Menu Example"
:setup-children
(lambda (_)
(cc/option-generator cc/temp-data))])
Would appreciate any input on this matter. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
What exactly did you have in mind with "sub-menus"? Here's some ways this could be done (though not all of that is actually supported yet).
Most faithful port, which doesn't need any new functionality in Transient, would look like:
Or maybe you want real "submenus", starting with a "topmenu" like
and then after typing
Or the "as ..." commands could be hidden, similar to how the "common commands" are hidden in all menus, until the common prefix Starting with a menu looking like the one in the previous variant, and also after typing
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the response! I was actually thinking more in line with sub-menus being “real menus” (implemented as prefixes). For example using
Selecting
Arguably the sub-menu could support groups to support the layout above. IMHO, the flattened menu example doesn’t provide any benefit over the current Org implementation. I’d argue that sub-menus offer the benefit of constraining user focus to choices that are available as the user makes decisions. |
Beta Was this translation helpful? Give feedback.
So my second alternative.
Your question then is "how can I dynamically define prefix commands?", I suppose.
You shouldn't do that, not in
:setup-children
, or elsewhere. Probably.emoji-insert
used to do that, before I refactored it. The new implementation does not dynamically define new commands, instead it binds the same pre-defined commands many times and makes each instance behave differently by using a different value for the:scope
in the dynamically generated binding as suffix. (And that recursively, which won't be necessary here.)In this case you probably would only want all the "Ex…