www.delorie.com/gnu/docs/elisp-manual-21/elisp_343.html   search  
Buy the book!

GNU Emacs Lisp Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.12.4 Menu Example

Here is a complete example of defining a menu keymap. It is the definition of the `Print' submenu in the `Tools' menu in the menu bar, and it uses the simple menu item format (see section Simple Menu Items). First we create the keymap, and give it a name:

(defvar menu-bar-print-menu (make-sparse-keymap "Print"))

Next we define the menu items:

(define-key menu-bar-print-menu [ps-print-region]
  '("Postscript Print Region" . ps-print-region-with-faces))
(define-key menu-bar-print-menu [ps-print-buffer]
  '("Postscript Print Buffer" . ps-print-buffer-with-faces))
(define-key menu-bar-print-menu [separator-ps-print]
(define-key menu-bar-print-menu [print-region]
  '("Print Region" . print-region))
(define-key menu-bar-print-menu [print-buffer]
  '("Print Buffer" . print-buffer))

Note the symbols which the bindings are "made for"; these appear inside square brackets, in the key sequence being defined. In some cases, this symbol is the same as the command name; sometimes it is different. These symbols are treated as "function keys", but they are not real function keys on the keyboard. They do not affect the functioning of the menu itself, but they are "echoed" in the echo area when the user selects from the menu, and they appear in the output of where-is and apropos.

The binding whose definition is ("--") is a separator line. Like a real menu item, the separator has a key symbol, in this case separator-ps-print. If one menu has two separators, they must have two different key symbols.

Here is code to define enable conditions for two of the commands in the menu:

(put 'print-region 'menu-enable 'mark-active)
(put 'ps-print-region-with-faces 'menu-enable 'mark-active)

Here is how we make this menu appear as an item in the parent menu:

(define-key menu-bar-tools-menu [print]
  (cons "Print" menu-bar-print-menu))

Note that this incorporates the submenu keymap, which is the value of the variable menu-bar-print-menu, rather than the symbol menu-bar-print-menu itself. Using that symbol in the parent menu item would be meaningless because menu-bar-print-menu is not a command.

If you wanted to attach the same print menu to a mouse click, you can do it this way:

(define-key global-map [C-S-down-mouse-1]

We could equally well use an extended menu item (see section Extended Menu Items) for print-region, like this:

(define-key menu-bar-print-menu [print-region]
  '(menu-item "Print Region" print-region
              :enable mark-active))

With the extended menu item, the enable condition is specified inside the menu item itself. If we wanted to make this item disappear from the menu entirely when the mark is inactive, we could do it this way:

(define-key menu-bar-print-menu [print-region]
  '(menu-item "Print Region" print-region
              :visible mark-active))

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003