elisp: Menu Bar
21.17.5 The Menu Bar
--------------------
Emacs usually shows a “menu bar” at the top of each frame.
(emacs)Menu Bars. Menu bar items are subcommands of the fake function
key ‘menu-bar’, as defined in the active keymaps.
To add an item to the menu bar, invent a fake function key of your
own (let’s call it KEY), and make a binding for the key sequence
‘[menu-bar KEY]’. Most often, the binding is a menu keymap, so that
pressing a button on the menu bar item leads to another menu.
When more than one active keymap defines the same function key for
the menu bar, the item appears just once. If the user clicks on that
menu bar item, it brings up a single, combined menu containing all the
subcommands of that item—the global subcommands, the local subcommands,
and the minor mode subcommands.
The variable ‘overriding-local-map’ is normally ignored when
determining the menu bar contents. That is, the menu bar is computed
from the keymaps that would be active if ‘overriding-local-map’ were
‘nil’. Active Keymaps.
Here’s an example of setting up a menu bar item:
;; Make a menu keymap (with a prompt string)
;; and make it the menu bar item’s definition.
(define-key global-map [menu-bar words]
(cons "Words" (make-sparse-keymap "Words")))
;; Define specific subcommands in this menu.
(define-key global-map
[menu-bar words forward]
'("Forward word" . forward-word))
(define-key global-map
[menu-bar words backward]
'("Backward word" . backward-word))
A local keymap can cancel a menu bar item made by the global keymap
by rebinding the same fake function key with ‘undefined’ as the binding.
For example, this is how Dired suppresses the ‘Edit’ menu bar item:
(define-key dired-mode-map [menu-bar edit] 'undefined)
Here, ‘edit’ is the fake function key used by the global map for the
‘Edit’ menu bar item. The main reason to suppress a global menu bar
item is to regain space for mode-specific items.
-- Variable: menu-bar-final-items
Normally the menu bar shows global items followed by items defined
by the local maps.
This variable holds a list of fake function keys for items to
display at the end of the menu bar rather than in normal sequence.
The default value is ‘(help-menu)’; thus, the ‘Help’ menu item
normally appears at the end of the menu bar, following local menu
items.
-- Variable: menu-bar-update-hook
This normal hook is run by redisplay to update the menu bar
contents, before redisplaying the menu bar. You can use it to
update menus whose contents should vary. Since this hook is run
frequently, we advise you to ensure that the functions it calls do
not take much time in the usual case.
Next to every menu bar item, Emacs displays a key binding that runs
the same command (if such a key binding exists). This serves as a
convenient hint for users who do not know the key binding. If a command
has multiple bindings, Emacs normally displays the first one it finds.
You can specify one particular key binding by assigning an
‘:advertised-binding’ symbol property to the command. Keys in
Documentation.