*** empty log message ***
[gnus] / lisp / x-easymenu.el
1 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
2 ;; 
3 ;; $Id: easymenu.el,v 5.9 1995/02/14 19:44:00 amanda Exp $
4 ;;
5 ;; LCD Archive Entry:
6 ;; easymenu|Per Abrahamsen|abraham@iesd.auc.dk|
7 ;; Easy menu support for XEmacs|
8 ;; $Date: 1995/02/14 19:44:00 $|$Revision: 5.9 $|~/misc/easymenu.el.gz|
9
10 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
11 ;;
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)
15 ;; any later version.
16 ;; 
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.
21 ;; 
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.
25
26 ;; Commentary:
27 ;;
28 ;; Easymenu allows you to define menus for both Emacs 19 and XEmacs.
29 ;;
30 ;; This file 
31 ;; The advantages of using easymenu are:
32 ;;
33 ;; - Easier to use than either the Emacs 19 and XEmacs menu syntax.
34 ;;
35 ;; - Common interface for Emacs 18, Emacs 19, and XEmacs.  
36 ;;   (The code does nothing when run under Emacs 18).
37 ;;
38 ;; The public functions are:
39 ;; 
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.  
46 ;;
47 ;;     See the documentation for easy-menu-define for details.
48 ;;
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'.
55 ;;
56 ;; - Function: easy-menu-add MENU [ MAP ]
57 ;;     Add MENU to the current menubar in MAP.
58 ;;
59 ;; - Function: easy-menu-remove MENU
60 ;;     Remove MENU from the current menubar.
61 ;;
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.
65 ;;
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.
69
70 ;;; Code:
71
72 ;;;###autoload
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.
79
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.
82
83 A menu item is usually a vector of three elements:  [NAME CALLBACK ENABLE]
84
85 NAME is a string--the menu item name.
86
87 CALLBACK is a command to run when the item is chosen,
88 or a list to evaluate when the item is chosen.
89
90 ENABLE is an expression; the item is enabled for selection
91 whenever this expression's value is non-nil.
92
93 Alternatively, a menu item may have the form: 
94
95    [ NAME CALLBACK [ KEYWORD ARG ] ... ]
96
97 Where KEYWORD is one of the symbol defined below.
98
99    :keys KEYS
100
101 KEYS is a string; a complex keyboard equivalent to this menu item.
102
103    :active ENABLE
104
105 ENABLE is an expression; the item is enabled for selection
106 whenever this expression's value is non-nil.
107
108    :suffix NAME
109
110 NAME is a string; the name of an argument to CALLBACK.
111
112    :style STYLE
113    
114 STYLE is a symbol describing the type of menu item.  The following are
115 defined:  
116
117 toggle: A checkbox.  
118         Currently just prepend the name with the string \"Toggle \".
119 radio: A radio button. 
120 nil: An ordinary menu item.
121
122    :selected SELECTED
123
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.
127
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.
131
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."
136   (` (progn
137        (defvar (, symbol) nil (, doc))
138        (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
139
140 (defun easy-menu-do-define (symbol maps doc menu)
141   (set symbol menu)
142   (fset symbol (list 'lambda '(e)
143                      doc
144                      '(interactive "@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)))
150
151 (fset 'easy-menu-change (symbol-function 'add-menu))
152
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.
157          nil)
158         ((assoc (car menu) current-menubar)
159          ;; Already present.
160          nil)
161         ((equal current-menubar '(nil))
162          ;; Set at left if only contains right marker.
163          (set-buffer-menubar (list menu nil)))
164         (t
165          ;; Add at right.
166          (set-buffer-menubar (copy-sequence current-menubar))
167          (add-menu nil (car menu) (cdr menu)))))
168
169 (defun easy-menu-remove (menu)
170   "Remove MENU from the current menu bar."
171   (and current-menubar
172        (assoc (car menu) current-menubar)
173        (delete-menu-item (list (car menu)))))
174
175 (provide 'easymenu)
176
177 ;;; easymenu.el ends here
178 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
179 ;; 
180 ;; $Id: easymenu.el,v 5.9 1995/02/14 19:44:00 amanda Exp $
181 ;;
182 ;; LCD Archive Entry:
183 ;; easymenu|Per Abrahamsen|abraham@iesd.auc.dk|
184 ;; Easy menu support for XEmacs|
185 ;; $Date: 1995/02/14 19:44:00 $|$Revision: 5.9 $|~/misc/easymenu.el.gz|
186
187 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
188 ;;
189 ;; This program is free software; you can redistribute it and/or modify
190 ;; it under the terms of the GNU General Public License as published by
191 ;; the Free Software Foundation; either version 2, or (at your option)
192 ;; any later version.
193 ;; 
194 ;; This program is distributed in the hope that it will be useful,
195 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
196 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
197 ;; GNU General Public License for more details.
198 ;; 
199 ;; You should have received a copy of the GNU General Public License
200 ;; along with this program; if not, write to the Free Software
201 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
202
203 ;; Commentary:
204 ;;
205 ;; Easymenu allows you to define menus for both Emacs 19 and XEmacs.
206 ;;
207 ;; This file 
208 ;; The advantages of using easymenu are:
209 ;;
210 ;; - Easier to use than either the Emacs 19 and XEmacs menu syntax.
211 ;;
212 ;; - Common interface for Emacs 18, Emacs 19, and XEmacs.  
213 ;;   (The code does nothing when run under Emacs 18).
214 ;;
215 ;; The public functions are:
216 ;; 
217 ;; - Function: easy-menu-define SYMBOL MAPS DOC MENU
218 ;;     SYMBOL is both the name of the variable that holds the menu and
219 ;;            the name of a function that will present a the menu.
220 ;;     MAPS is a list of keymaps where the menu should appear in the menubar.
221 ;;     DOC is the documentation string for the variable.
222 ;;     MENU is an XEmacs style menu description.  
223 ;;
224 ;;     See the documentation for easy-menu-define for details.
225 ;;
226 ;; - Function: easy-menu-change PATH NAME ITEMS
227 ;;     Change an existing menu.
228 ;;     The menu must already exist an be visible on the menu bar.
229 ;;     PATH is a list of strings used for locating the menu on the menu bar. 
230 ;;     NAME is the name of the menu.  
231 ;;     ITEMS is a list of menu items, as defined in `easy-menu-define'.
232 ;;
233 ;; - Function: easy-menu-add MENU [ MAP ]
234 ;;     Add MENU to the current menubar in MAP.
235 ;;
236 ;; - Function: easy-menu-remove MENU
237 ;;     Remove MENU from the current menubar.
238 ;;
239 ;; Emacs 19 never uses `easy-menu-add' or `easy-menu-remove', menus
240 ;; automatically appear and disappear when the keymaps specified by
241 ;; the MAPS argument to `easy-menu-define' are activated.
242 ;;
243 ;; XEmacs will bind the map to button3 in each MAPS, but you must
244 ;; explicitly call `easy-menu-add' and `easy-menu-remove' to add and
245 ;; remove menus from the menu bar.
246
247 ;;; Code:
248
249 ;;;###autoload
250 (defmacro easy-menu-define (symbol maps doc menu)
251   "Define a menu bar submenu in maps MAPS, according to MENU.
252 The arguments SYMBOL and DOC are ignored; they are present for
253 compatibility only.  SYMBOL is not evaluated.  In other Emacs versions
254 these arguments may be used as a variable to hold the menu data, and a
255 doc string for that variable.
256
257 The first element of MENU must be a string.  It is the menu bar item name.
258 The rest of the elements are menu items.
259
260 A menu item is usually a vector of three elements:  [NAME CALLBACK ENABLE]
261
262 NAME is a string--the menu item name.
263
264 CALLBACK is a command to run when the item is chosen,
265 or a list to evaluate when the item is chosen.
266
267 ENABLE is an expression; the item is enabled for selection
268 whenever this expression's value is non-nil.
269
270 Alternatively, a menu item may have the form: 
271
272    [ NAME CALLBACK [ KEYWORD ARG ] ... ]
273
274 Where KEYWORD is one of the symbol defined below.
275
276    :keys KEYS
277
278 KEYS is a string; a complex keyboard equivalent to this menu item.
279
280    :active ENABLE
281
282 ENABLE is an expression; the item is enabled for selection
283 whenever this expression's value is non-nil.
284
285    :suffix NAME
286
287 NAME is a string; the name of an argument to CALLBACK.
288
289    :style STYLE
290    
291 STYLE is a symbol describing the type of menu item.  The following are
292 defined:  
293
294 toggle: A checkbox.  
295         Currently just prepend the name with the string \"Toggle \".
296 radio: A radio button. 
297 nil: An ordinary menu item.
298
299    :selected SELECTED
300
301 SELECTED is an expression; the checkbox or radio button is selected
302 whenever this expression's value is non-nil.
303 Currently just disable radio buttons, no effect on checkboxes.
304
305 A menu item can be a string.  Then that string appears in the menu as
306 unselectable text.  A string consisting solely of hyphens is displayed
307 as a solid horizontal line.
308
309 A menu item can be a list.  It is treated as a submenu.
310 The first element should be the submenu name.  That's used as the
311 menu item in the top-level menu.  The cdr of the submenu list
312 is a list of menu items, as above."
313   (` (progn
314        (defvar (, symbol) nil (, doc))
315        (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
316
317 (defun easy-menu-do-define (symbol maps doc menu)
318   (set symbol menu)
319   (fset symbol (list 'lambda '(e)
320                      doc
321                      '(interactive "@e")
322                      '(run-hooks 'activate-menubar-hook)
323                      '(setq zmacs-region-stays 't)
324                      (list 'popup-menu symbol)))
325   (mapcar (function (lambda (map) (define-key map 'button3 symbol)))
326           (if (keymapp maps) (list maps) maps)))
327
328 (fset 'easy-menu-change (symbol-function 'add-menu))
329
330 (defun easy-menu-add (menu &optional map)
331   "Add MENU to the current menu bar."
332   (cond ((null current-menubar)
333          ;; Don't add it to a non-existing menubar.
334          nil)
335         ((assoc (car menu) current-menubar)
336          ;; Already present.
337          nil)
338         ((equal current-menubar '(nil))
339          ;; Set at left if only contains right marker.
340          (set-buffer-menubar (list menu nil)))
341         (t
342          ;; Add at right.
343          (set-buffer-menubar (copy-sequence current-menubar))
344          (add-menu nil (car menu) (cdr menu)))))
345
346 (defun easy-menu-remove (menu)
347   "Remove MENU from the current menu bar."
348   (and current-menubar
349        (assoc (car menu) current-menubar)
350        (delete-menu-item (list (car menu)))))
351
352 (provide 'easymenu)
353
354 ;;; easymenu.el ends here