84002eee7f14b96e09ff58180d83ce1836a1bcaf
[gnus] / lisp / x-easymenu.el
1 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
2 ;; 
3 ;; $Id: x-easymenu.el,v 1.1 1996/05/29 05:36:45 steve Exp $
4 ;;
5 ;; LCD Archive Entry:
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|
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