Question
I want to show a popup menu similarwould like to that usedcreate user interface in Magitform of popup menu, popup menu similar to that used in Magit. It seems like Magit has separate package
Features
Definition of Popup
Popup in context of this question means little temporary window that manages all contains collection of menu items so that user can select one and only one of these items.
Position on the Screen
The popup businessis allowed to appear in any part of the screen, so Ibut it's desirable started reading sourcethat it should be quite obvious and thus it should appear beside currently active window.
Contents of Popup Buffer
Items should be displayed in form of pretty table. Pretty is context of
the question means visually appealing, this effect can be most easily
achieved by putting menu items into straight rows, see
magitcomplete-popup.el-insert-string
for example. It's not big, but not smallThis paragraph serves for additional
eitherclarification, 1k+ lines of code. That's not obvious how to write something muchyou can do it in your own way, this won't render your answer
simpler using the same conceptincorrect.
Selection of Menu Item
I see that magit-popup.el
uses button.el
builtThe selection is expected to be performed by a single key-in packagepressing or, so I started
to read documentation of that package toooptionally with a mouse (I'malthough it's not familiar with itthat important so answers
containing propositions that don't support mouse are legal). RightIf you propose
now I don't know wheresolution that supports mouse, please note that user should be able to startselect
a menu item in an intuitive way, I just wantthat is, by left-button clicking on desired
choice.
N.B. mouse can be used in many ways and alternative ways to show a little window withindicate buttonsa choice are also welcomed.
Elimination of Popup
Once user has selected a menu item in a way described above, therethe buffer and thus its window should be only one section with several rowseliminated from view as well as killed. The window that has been active before invocation of the popup menu should get focus (that is, like this:become active) again.
Some title with my project-specific info.
Commands:
a one command e ...
b another one f ...
c ... g ...
d ... h ...
Returned Value and Arguments
So the solutionPreferably, this consequence of actions should have the following specific featuresresult in a Lisp object returned. The Lisp object can be either:
- it should display all items to choose from at once;
- every item should be selectable with one key pressing (user enters character corresponding to desired menu item);
nil
— this indicates that user has aborted the popup menu either by pressing C-g or in some other way †. - buffer itself should be shown in separate window at the bottom of currently active window.
string
— string (it's allowed to use a symbol) should bestring-equal
to one of the strings supplied to the popup menu as collection of actual items.
Ideally I would likeAlternative ways to get nil
(if user abortslet the popup withrest of program know user's choice, or,
C-gpossibly, its absence, are acceptable. However, if it's not clear how else
it can be performed I ask all answerers improvise and don't ask me for example)
further clarification of this aspect.
This is all for returned value. As for input parameters, or namethey should at least include collection of commandstrings which represent possible choices (this is probably differentthat from normal usage where something is automatically calledis, as I understandmenu items).
Acceptable Answers
Can you tell me how do I write something like this? Should I use
define-button-type
, then somehow define all the buttons and then insert
them into newly created buffer? DoesExpected answer can be of the buffer just read one key press or
does it handle it via some key map?following forms:
Sufficient code snippet that allows educated reader to write function like that described above; it's not expected or necessary to write entire working function. However, to avoid uncertainty (can considerable parts of code be omitted?), I should note that missing parts of the snippet should be described in textual component of answer.
A link to existing library that implements similar functionality. To avoid uncertainty, I should note that similar in our case means that the library can be used to create popup (see definition above) that has at least 2 or 3 features described above. If proposed library is different to the point where the previously stated condition cannot be met, each such case will be judged independently and will be always upvoted if OP deems it useful.
Description of built-in Emacs functions or third-party ones that can be used to implement any feature described in section «Features», see above. To avoid uncertainty, please state clearly how your answer can be useful for future readers who want to implement popup, popup menu similar to that used in Magit.
This question doesn't beg for code, it asks for tools (functions, libraries) that can be used to simplify creating of such an interface element. † Alternative ways to abort the popup menu may include the following (but not limited to these):
clicking outside of popup menu window;
killing of buffer containing the popup without making a choice.