1 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
3 ;; $Id: x-easymenu.el,v 1.1 1996/05/29 05:36:45 steve Exp $
6 ;; easymenu|Per Abrahamsen|abraham@iesd.auc.dk|
7 ;; Easy menu support for XEmacs|
8 ;; $Date: 1996/05/29 05:36:45 $|$Revision: 1.1 $|~/misc/easymenu.el.gz|
10 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, write to the Free Software
24 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;; Easymenu allows you to define menus for both Emacs 19 and XEmacs.
31 ;; The advantages of using easymenu are:
33 ;; - Easier to use than either the Emacs 19 and XEmacs menu syntax.
35 ;; - Common interface for Emacs 18, Emacs 19, and XEmacs.
36 ;; (The code does nothing when run under Emacs 18).
38 ;; The public functions are:
40 ;; - Function: easy-menu-define SYMBOL MAPS DOC MENU
41 ;; SYMBOL is both the name of the variable that holds the menu and
42 ;; the name of a function that will present a the menu.
43 ;; MAPS is a list of keymaps where the menu should appear in the menubar.
44 ;; DOC is the documentation string for the variable.
45 ;; MENU is an XEmacs style menu description.
47 ;; See the documentation for easy-menu-define for details.
49 ;; - Function: easy-menu-change PATH NAME ITEMS
50 ;; Change an existing menu.
51 ;; The menu must already exist an be visible on the menu bar.
52 ;; PATH is a list of strings used for locating the menu on the menu bar.
53 ;; NAME is the name of the menu.
54 ;; ITEMS is a list of menu items, as defined in `easy-menu-define'.
56 ;; - Function: easy-menu-add MENU [ MAP ]
57 ;; Add MENU to the current menubar in MAP.
59 ;; - Function: easy-menu-remove MENU
60 ;; Remove MENU from the current menubar.
62 ;; Emacs 19 never uses `easy-menu-add' or `easy-menu-remove', menus
63 ;; automatically appear and disappear when the keymaps specified by
64 ;; the MAPS argument to `easy-menu-define' are activated.
66 ;; XEmacs will bind the map to button3 in each MAPS, but you must
67 ;; explicitly call `easy-menu-add' and `easy-menu-remove' to add and
68 ;; remove menus from the menu bar.
73 (defmacro easy-menu-define (symbol maps doc menu)
74 "Define a menu bar submenu in maps MAPS, according to MENU.
75 The arguments SYMBOL and DOC are ignored; they are present for
76 compatibility only. SYMBOL is not evaluated. In other Emacs versions
77 these arguments may be used as a variable to hold the menu data, and a
78 doc string for that variable.
80 The first element of MENU must be a string. It is the menu bar item name.
81 The rest of the elements are menu items.
83 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
85 NAME is a string--the menu item name.
87 CALLBACK is a command to run when the item is chosen,
88 or a list to evaluate when the item is chosen.
90 ENABLE is an expression; the item is enabled for selection
91 whenever this expression's value is non-nil.
93 Alternatively, a menu item may have the form:
95 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
97 Where KEYWORD is one of the symbol defined below.
101 KEYS is a string; a complex keyboard equivalent to this menu item.
105 ENABLE is an expression; the item is enabled for selection
106 whenever this expression's value is non-nil.
110 NAME is a string; the name of an argument to CALLBACK.
114 STYLE is a symbol describing the type of menu item. The following are
118 Currently just prepend the name with the string \"Toggle \".
119 radio: A radio button.
120 nil: An ordinary menu item.
124 SELECTED is an expression; the checkbox or radio button is selected
125 whenever this expression's value is non-nil.
126 Currently just disable radio buttons, no effect on checkboxes.
128 A menu item can be a string. Then that string appears in the menu as
129 unselectable text. A string consisting solely of hyphens is displayed
130 as a solid horizontal line.
132 A menu item can be a list. It is treated as a submenu.
133 The first element should be the submenu name. That's used as the
134 menu item in the top-level menu. The cdr of the submenu list
135 is a list of menu items, as above."
137 (defvar (, symbol) nil (, doc))
138 (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
140 (defun easy-menu-do-define (symbol maps doc menu)
142 (fset symbol (list 'lambda '(e)
145 '(run-hooks 'activate-menubar-hook)
146 '(setq zmacs-region-stays 't)
147 (list 'popup-menu symbol)))
148 (mapcar (function (lambda (map) (define-key map 'button3 symbol)))
149 (if (keymapp maps) (list maps) maps)))
151 (fset 'easy-menu-change (symbol-function 'add-menu))
153 (defun easy-menu-add (menu &optional map)
154 "Add MENU to the current menu bar."
155 (cond ((null current-menubar)
156 ;; Don't add it to a non-existing menubar.
158 ((assoc (car menu) current-menubar)
161 ((equal current-menubar '(nil))
162 ;; Set at left if only contains right marker.
163 (set-buffer-menubar (list menu nil)))
166 (set-buffer-menubar (copy-sequence current-menubar))
167 (add-menu nil (car menu) (cdr menu)))))
169 (defun easy-menu-remove (menu)
170 "Remove MENU from the current menu bar."
172 (assoc (car menu) current-menubar)
173 (delete-menu-item (list (car menu)))))
177 ;;; easymenu.el ends here