1 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: SXEmacs Development Team
7 ;; Keywords: internal, extensions, dumped
9 ;; This file is part of SXEmacs.
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Synched up with: Not synched with FSF but coordinated with the FSF
25 ;;; easymenu maintainer for compatibility with FSF 20.4.
26 ;;; Please: Coordinate changes with Inge Frick <inge@nada.kth.se>
30 ;; This file is dumped with SXEmacs.
32 ;; Easymenu allows you to define menus for both Emacs 19 and XEmacs.
35 ;; The advantages of using easymenu are:
37 ;; - Easier to use than either the Emacs 19 and XEmacs menu syntax.
39 ;; - Common interface for Emacs 18, Emacs 19, and XEmacs.
40 ;; (The code does nothing when run under Emacs 18).
42 ;; The public functions are:
44 ;; - Function: easy-menu-define SYMBOL MAPS DOC MENU
45 ;; SYMBOL is both the name of the variable that holds the menu and
46 ;; the name of a function that will present the menu.
47 ;; MAPS is a list of keymaps where the menu should appear in the menubar.
48 ;; DOC is the documentation string for the variable.
49 ;; MENU is an XEmacs style menu description.
51 ;; See the documentation for easy-menu-define for details.
53 ;; - Function: easy-menu-change PATH NAME ITEMS
54 ;; Change an existing menu.
55 ;; The menu must already exist and be visible on the menu bar.
56 ;; PATH is a list of strings used for locating the menu on the menu bar.
57 ;; NAME is the name of the menu.
58 ;; ITEMS is a list of menu items, as defined in `easy-menu-define'.
60 ;; - Function: easy-menu-add MENU [ MAP ]
61 ;; Add MENU to the current menubar in MAP.
63 ;; - Function: easy-menu-remove MENU
64 ;; Remove MENU from the current menubar.
66 ;; - Function: easy-menu-add-item
67 ;; Add item or submenu to existing menu
69 ;; - Function: easy-menu-item-present-p
72 ;; - Function: easy-menu-remove-item
73 ;; Delete item from menu.
75 ;; Emacs 19 never uses `easy-menu-add' or `easy-menu-remove', menus
76 ;; automatically appear and disappear when the keymaps specified by
77 ;; the MAPS argument to `easy-menu-define' are activated.
79 ;; XEmacs will bind the map to button3 in each MAPS, but you must
80 ;; explicitly call `easy-menu-add' and `easy-menu-remove' to add and
81 ;; remove menus from the menu bar.
86 (defmacro easy-menu-define (symbol maps doc menu)
87 "Define a menu bar submenu in maps MAPS, according to MENU.
88 The arguments SYMBOL and DOC are ignored; they are present for
89 compatibility only. SYMBOL is not evaluated. In other Emacs versions
90 these arguments may be used as a variable to hold the menu data, and a
91 doc string for that variable.
93 The first element of MENU must be a string. It is the menu bar item name.
94 The rest of the elements are menu items.
96 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
98 NAME is a string--the menu item name.
100 CALLBACK is a command to run when the item is chosen,
101 or a list to evaluate when the item is chosen.
103 ENABLE is an expression; the item is enabled for selection
104 whenever this expression's value is non-nil.
106 Alternatively, a menu item may have the form:
108 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
110 Where KEYWORD is one of the symbol defined below.
114 KEYS is a string; a complex keyboard equivalent to this menu item.
118 ENABLE is an expression; the item is enabled for selection
119 whenever this expression's value is non-nil.
123 NAME is a string; the name of an argument to CALLBACK.
127 STYLE is a symbol describing the type of menu item. The following are
131 Currently just prepend the name with the string \"Toggle \".
132 radio: A radio button.
133 nil: An ordinary menu item.
137 SELECTED is an expression; the checkbox or radio button is selected
138 whenever this expression's value is non-nil.
139 Currently just disable radio buttons, no effect on checkboxes.
141 A menu item can be a string. Then that string appears in the menu as
142 unselectable text. A string consisting solely of hyphens is displayed
143 as a solid horizontal line.
145 A menu item can be a list. It is treated as a submenu.
146 The first element should be the submenu name. That's used as the
147 menu item in the top-level menu. The cdr of the submenu list
148 is a list of menu items, as above."
150 (defvar ,symbol nil ,doc)
151 (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
153 (defun easy-menu-do-define (symbol maps doc menu)
154 (when (featurep 'menubar)
156 (fset symbol `(lambda (e)
159 (run-hooks 'activate-menubar-hook)
160 (setq zmacs-region-stays 't)
161 (popup-menu ,symbol)))))
163 (defun easy-menu-change (&rest args)
164 (when (featurep 'menubar)
165 (apply 'add-menu args)))
167 ;; This variable hold the easy-menu mode menus of all major and
168 ;; minor modes currently in effect in the current buffer.
169 (defvar easy-menu-all-popups nil)
170 (make-variable-buffer-local 'easy-menu-all-popups)
172 (defun easy-menu-add (menu &optional map)
173 "Add MENU to the current menu bar."
174 (when (featurep 'menubar)
175 (unless (member menu easy-menu-all-popups)
176 (push menu easy-menu-all-popups))
177 (setq mode-popup-menu (if (> (length easy-menu-all-popups) 1)
178 (cons (easy-menu-title)
179 (reverse easy-menu-all-popups))
181 (car easy-menu-all-popups)))
182 (cons (normalize-menu-item-name
184 (cdr same-as-menu)))))
186 (cond ((null current-menubar)
187 ;; Don't add it to a non-existing menubar.
189 ((assoc (car menu) current-menubar)
192 ((equal current-menubar '(nil))
193 ;; Set at left if only contains right marker.
194 (set-buffer-menubar (list menu nil)))
197 (set-buffer-menubar (copy-sequence current-menubar))
198 (add-menu nil (car menu) (cdr menu))))))
200 (defun easy-menu-remove (menu)
201 "Remove MENU from the current menu bar."
202 (when (featurep 'menubar)
203 (setq easy-menu-all-popups (delq menu easy-menu-all-popups)
204 mode-popup-menu (if (< (length easy-menu-all-popups) 1)
205 (cons (easy-menu-title)
206 (reverse easy-menu-all-popups))
208 (car easy-menu-all-popups)))
209 (cons (normalize-menu-item-name
211 (cdr same-as-menu)))))
214 (assoc (car menu) current-menubar)
215 (delete-menu-item (list (car menu))))))
217 (defsubst easy-menu-normalize (menu)
222 (defun easy-menu-add-item (menu path item &optional before)
223 "At the end of the submenu of MENU with path PATH, add ITEM.
224 If ITEM is already present in this submenu, then this item will be changed.
225 otherwise ITEM will be added at the end of the submenu, unless the optional
226 argument BEFORE is present, in which case ITEM will instead be added
227 before the item named BEFORE.
228 MENU is either a symbol, which have earlier been used as the first
229 argument in a call to `easy-menu-define', or the value of such a symbol
230 i.e. a menu, or nil, which stands for the current menubar.
231 PATH is a list of strings for locating the submenu where ITEM is to be
232 added. If PATH is nil, MENU itself is used. Otherwise, the first
233 element should be the name of a submenu directly under MENU. This
234 submenu is then traversed recursively with the remaining elements of PATH.
235 ITEM is either defined as in `easy-menu-define', a menu defined earlier
236 by `easy-menu-define' or `easy-menu-create-menu' or an item returned
237 from `easy-menu-item-present-p' or `easy-menu-remove-item'."
238 (when (featurep 'menubar)
239 (add-menu-button path item before (easy-menu-normalize menu))))
241 (defun easy-menu-item-present-p (menu path name)
242 "In submenu of MENU with path PATH, return true iff item NAME is present.
243 MENU and PATH are defined as in `easy-menu-add-item'.
244 NAME should be a string, the name of the element to be looked for.
246 The return value can be used as an argument to `easy-menu-add-item'."
247 (if (featurep 'menubar)
248 (car (find-menu-item (or (easy-menu-normalize menu) current-menubar)
249 (append path (list name))))
252 (defun easy-menu-remove-item (menu path name)
253 "From submenu of MENU with path PATH, remove item NAME.
254 MENU and PATH are defined as in `easy-menu-add-item'.
255 NAME should be a string, the name of the element to be removed.
257 The return value can be used as an argument to `easy-menu-add-item'."
258 (when (featurep 'menubar)
259 (delete-menu-item (append path (list name))
260 (easy-menu-normalize menu))))
265 ;; Think up a good title for the menu. Take the major-mode of the
266 ;; buffer, strip the -mode part, convert hyphens to spaces, and
269 ;; If you can think of something smarter, feel free to replace it.
270 ;; Don't forget to mail the change to xemacs@xemacs.org where everyone
271 ;; can flame, er, praise your changes.
272 (defun easy-menu-title ()
273 (capitalize (replace-in-string (replace-in-string
274 (symbol-name major-mode) "-mode$" "")
279 ;;; easymenu.el ends here