A place to keep aliases to built-in constants when needed
[sxemacs] / lisp / easymenu.el
1 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: SXEmacs Development Team
7 ;; Keywords: internal, extensions, dumped
8
9 ;; This file is part of SXEmacs.
10
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.
15
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.
20
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/>.
23
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>
27
28 ;; Commentary:
29
30 ;; This file is dumped with SXEmacs.
31
32 ;; Easymenu allows you to define menus for both Emacs 19 and XEmacs.
33
34 ;; This file
35 ;; The advantages of using easymenu are:
36
37 ;; - Easier to use than either the Emacs 19 and XEmacs menu syntax.
38
39 ;; - Common interface for Emacs 18, Emacs 19, and XEmacs.
40 ;;   (The code does nothing when run under Emacs 18).
41
42 ;; The public functions are:
43
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.
50
51 ;;     See the documentation for easy-menu-define for details.
52
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'.
59
60 ;; - Function: easy-menu-add MENU [ MAP ]
61 ;;     Add MENU to the current menubar in MAP.
62
63 ;; - Function: easy-menu-remove MENU
64 ;;     Remove MENU from the current menubar.
65
66 ;; - Function: easy-menu-add-item
67 ;;     Add item or submenu to existing menu
68
69 ;; - Function: easy-menu-item-present-p
70 ;;     Locate item
71
72 ;; - Function: easy-menu-remove-item
73 ;;     Delete item from menu.
74
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.
78
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.
82
83 ;;; Code:
84
85 ;; ;;;###autoload
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.
92
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.
95
96 A menu item is usually a vector of three elements:  [NAME CALLBACK ENABLE]
97
98 NAME is a string--the menu item name.
99
100 CALLBACK is a command to run when the item is chosen,
101 or a list to evaluate when the item is chosen.
102
103 ENABLE is an expression; the item is enabled for selection
104 whenever this expression's value is non-nil.
105
106 Alternatively, a menu item may have the form:
107
108    [ NAME CALLBACK [ KEYWORD ARG ] ... ]
109
110 Where KEYWORD is one of the symbol defined below.
111
112    :keys KEYS
113
114 KEYS is a string; a complex keyboard equivalent to this menu item.
115
116    :active ENABLE
117
118 ENABLE is an expression; the item is enabled for selection
119 whenever this expression's value is non-nil.
120
121    :suffix NAME
122
123 NAME is a string; the name of an argument to CALLBACK.
124
125    :style STYLE
126
127 STYLE is a symbol describing the type of menu item.  The following are
128 defined:
129
130 toggle: A checkbox.
131         Currently just prepend the name with the string \"Toggle \".
132 radio: A radio button.
133 nil: An ordinary menu item.
134
135    :selected SELECTED
136
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.
140
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.
144
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."
149   `(progn
150      (defvar ,symbol nil ,doc)
151      (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
152
153 (defun easy-menu-do-define (symbol maps doc menu)
154   (when (featurep 'menubar)
155     (set symbol menu)
156     (fset symbol `(lambda (e)
157                     ,doc
158                     (interactive "@e")
159                     (run-hooks 'activate-menubar-hook)
160                     (setq zmacs-region-stays 't)
161                     (popup-menu ,symbol)))))
162
163 (defun easy-menu-change (&rest args)
164   (when (featurep 'menubar)
165     (apply 'add-menu args)))
166
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)
171
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))
180                             (let ((same-as-menu
181                                    (car easy-menu-all-popups)))
182                               (cons (normalize-menu-item-name
183                                      (car same-as-menu))
184                                     (cdr same-as-menu)))))
185
186     (cond ((null current-menubar)
187            ;; Don't add it to a non-existing menubar.
188            nil)
189           ((assoc (car menu) current-menubar)
190            ;; Already present.
191            nil)
192           ((equal current-menubar '(nil))
193            ;; Set at left if only contains right marker.
194            (set-buffer-menubar (list menu nil)))
195           (t
196            ;; Add at right.
197            (set-buffer-menubar (copy-sequence current-menubar))
198            (add-menu nil (car menu) (cdr menu))))))
199
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))
207                             (let ((same-as-menu
208                                    (car easy-menu-all-popups)))
209                               (cons (normalize-menu-item-name
210                                      (car same-as-menu))
211                                     (cdr same-as-menu)))))
212
213     (and current-menubar
214          (assoc (car menu) current-menubar)
215          (delete-menu-item (list (car menu))))))
216
217 (defsubst easy-menu-normalize (menu)
218   (if (symbolp menu)
219       (symbol-value menu)
220     menu))
221
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))))
240
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.
245
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))))
250     nil))
251
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.
256
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))))
261
262
263
264
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
267 ;; capitalize it.
268 ;;
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$" "")
275                                  "-" " ")))
276
277 (provide 'easymenu)
278
279 ;;; easymenu.el ends here