XEmacs 21.2-b1
[chise/xemacs-chise.git.1] / 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: XEmacs Development Team
7 ;; Keywords: internal, extensions, dumped
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs 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 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs 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 XEmacs; if not, write to the Free Software
23 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ;; 02111-1307, USA.
25
26 ;;; Synched up with: Not synched with FSF.
27
28 ;; Commentary:
29
30 ;; This file is dumped with XEmacs.
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 a 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 ;; Emacs 19 never uses `easy-menu-add' or `easy-menu-remove', menus
67 ;; automatically appear and disappear when the keymaps specified by
68 ;; the MAPS argument to `easy-menu-define' are activated.
69
70 ;; XEmacs will bind the map to button3 in each MAPS, but you must
71 ;; explicitly call `easy-menu-add' and `easy-menu-remove' to add and
72 ;; remove menus from the menu bar.
73
74 ;;; Code:
75
76 ;; ;;;###autoload
77 (defmacro easy-menu-define (symbol maps doc menu)
78   "Define a menu bar submenu in maps MAPS, according to MENU.
79 The arguments SYMBOL and DOC are ignored; they are present for
80 compatibility only.  SYMBOL is not evaluated.  In other Emacs versions
81 these arguments may be used as a variable to hold the menu data, and a
82 doc string for that variable.
83
84 The first element of MENU must be a string.  It is the menu bar item name.
85 The rest of the elements are menu items.
86
87 A menu item is usually a vector of three elements:  [NAME CALLBACK ENABLE]
88
89 NAME is a string--the menu item name.
90
91 CALLBACK is a command to run when the item is chosen,
92 or a list to evaluate when the item is chosen.
93
94 ENABLE is an expression; the item is enabled for selection
95 whenever this expression's value is non-nil.
96
97 Alternatively, a menu item may have the form: 
98
99    [ NAME CALLBACK [ KEYWORD ARG ] ... ]
100
101 Where KEYWORD is one of the symbol defined below.
102
103    :keys KEYS
104
105 KEYS is a string; a complex keyboard equivalent to this menu item.
106
107    :active ENABLE
108
109 ENABLE is an expression; the item is enabled for selection
110 whenever this expression's value is non-nil.
111
112    :suffix NAME
113
114 NAME is a string; the name of an argument to CALLBACK.
115
116    :style STYLE
117    
118 STYLE is a symbol describing the type of menu item.  The following are
119 defined:  
120
121 toggle: A checkbox.  
122         Currently just prepend the name with the string \"Toggle \".
123 radio: A radio button. 
124 nil: An ordinary menu item.
125
126    :selected SELECTED
127
128 SELECTED is an expression; the checkbox or radio button is selected
129 whenever this expression's value is non-nil.
130 Currently just disable radio buttons, no effect on checkboxes.
131
132 A menu item can be a string.  Then that string appears in the menu as
133 unselectable text.  A string consisting solely of hyphens is displayed
134 as a solid horizontal line.
135
136 A menu item can be a list.  It is treated as a submenu.
137 The first element should be the submenu name.  That's used as the
138 menu item in the top-level menu.  The cdr of the submenu list
139 is a list of menu items, as above."
140   (` (progn
141        (defvar (, symbol) nil (, doc))
142        (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
143
144 (defun easy-menu-do-define (symbol maps doc menu)
145   (if (featurep 'menubar)
146       (progn
147         (set symbol menu)
148         (fset symbol (list 'lambda '(e)
149                            doc
150                            '(interactive "@e")
151                            '(run-hooks 'activate-menubar-hook)
152                            '(setq zmacs-region-stays 't)
153                            (list 'popup-menu symbol))))))
154
155 (defun easy-menu-change (&rest args)
156   (when (featurep 'menubar)
157     (apply 'add-menu args)))
158
159 ;; This variable hold the easy-menu mode menus of all major and
160 ;; minor modes currently in effect in the current buffer.
161 (defvar easy-menu-all-popups nil)
162 (make-variable-buffer-local 'easy-menu-all-popups)
163
164 (defun easy-menu-add (menu &optional map)
165   "Add MENU to the current menu bar."
166   (if (featurep 'menubar)
167       (progn
168         (unless (member menu easy-menu-all-popups)
169           (push menu easy-menu-all-popups))
170         (setq mode-popup-menu (if (> (length easy-menu-all-popups) 1)
171                                   (cons (easy-menu-title)
172                                         (reverse easy-menu-all-popups))
173                                 (car easy-menu-all-popups)))
174
175         (cond ((null current-menubar)
176                ;; Don't add it to a non-existing menubar.
177                nil)
178               ((assoc (car menu) current-menubar)
179                ;; Already present.
180                nil)
181               ((equal current-menubar '(nil))
182                ;; Set at left if only contains right marker.
183                (set-buffer-menubar (list menu nil)))
184               (t
185                ;; Add at right.
186                (set-buffer-menubar (copy-sequence current-menubar))
187                (add-menu nil (car menu) (cdr menu)))))))
188
189 (defun easy-menu-remove (menu)
190   "Remove MENU from the current menu bar."
191   (if (featurep 'menubar)
192       (progn
193         (setq easy-menu-all-popups (delq menu easy-menu-all-popups)
194               mode-popup-menu (if (< (length easy-menu-all-popups) 1)
195                                   (cons (easy-menu-title)
196                                         (reverse easy-menu-all-popups))
197                                 (car easy-menu-all-popups)))
198
199         (and current-menubar
200              (assoc (car menu) current-menubar)
201              (delete-menu-item (list (car menu)))))))
202
203 ;; Think up a good title for the menu.  Take the major-mode of the
204 ;; buffer, strip the -mode part, convert hyphens to spaces, and
205 ;; capitalize it.
206 ;;
207 ;; If you can think of something smarter, feel free to replace it.
208 ;; Don't forget to mail the change to xemacs@xemacs.org where everyone
209 ;; can flame, er, praise your changes.
210 (defun easy-menu-title ()
211   (capitalize (replace-in-string (replace-in-string
212                                   (symbol-name major-mode) "-mode$" "")
213                                  "-" " ")))
214
215 (provide 'easymenu)
216
217 ;;; easymenu.el ends here