(easy-menu-create-keymaps): If nil is
[emacs.git] / lisp / emacs-lisp / easymenu.el
blob081780bd4652006172a3abd1d6ca3baf031266ad
1 ;;; easymenu.el --- support the easymenu interface for defining a menu.
3 ;; Copyright (C) 1994, 1996 Free Software Foundation, Inc.
5 ;; Keywords: emulations
6 ;; Author: rms
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This is compatible with easymenu.el by Per Abrahamsen
28 ;; but it is much simpler as it doesn't try to support other Emacs versions.
29 ;; The code was mostly derived from lmenu.el.
31 ;;; Code:
33 ;;;###autoload
34 (defmacro easy-menu-define (symbol maps doc menu)
35 "Define a menu bar submenu in maps MAPS, according to MENU.
36 The menu keymap is stored in symbol SYMBOL, both as its value
37 and as its function definition. DOC is used as the doc string for SYMBOL.
39 The first element of MENU must be a string. It is the menu bar item name.
40 The rest of the elements are menu items.
42 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
44 NAME is a string--the menu item name.
46 CALLBACK is a command to run when the item is chosen,
47 or a list to evaluate when the item is chosen.
49 ENABLE is an expression; the item is enabled for selection
50 whenever this expression's value is non-nil.
52 Alternatively, a menu item may have the form:
54 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
56 Where KEYWORD is one of the symbol defined below.
58 :keys KEYS
60 KEYS is a string; a complex keyboard equivalent to this menu item.
61 This is normally not needed because keyboard equivalents are usually
62 computed automatically.
64 :active ENABLE
66 ENABLE is an expression; the item is enabled for selection
67 whenever this expression's value is non-nil.
69 :suffix NAME
71 NAME is a string; the name of an argument to CALLBACK.
73 :style STYLE
75 STYLE is a symbol describing the type of menu item. The following are
76 defined:
78 toggle: A checkbox.
79 Prepend the name with '(*) ' or '( ) ' depending on if selected or not.
80 radio: A radio button.
81 Prepend the name with '[X] ' or '[ ] ' depending on if selected or not.
82 nil: An ordinary menu item.
84 :selected SELECTED
86 SELECTED is an expression; the checkbox or radio button is selected
87 whenever this expression's value is non-nil.
89 A menu item can be a string. Then that string appears in the menu as
90 unselectable text. A string consisting solely of hyphens is displayed
91 as a solid horizontal line.
93 A menu item can be a list. It is treated as a submenu.
94 The first element should be the submenu name. That's used as the
95 menu item in the top-level menu. The cdr of the submenu list
96 is a list of menu items, as above."
97 (` (progn
98 (defvar (, symbol) nil (, doc))
99 (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
101 ;;;###autoload
102 (defun easy-menu-do-define (symbol maps doc menu)
103 ;; We can't do anything that might differ between Emacs dialects in
104 ;; `easy-menu-define' in order to make byte compiled files
105 ;; compatible. Therefore everything interesting is done in this
106 ;; function.
107 (set symbol (easy-menu-create-keymaps (car menu) (cdr menu)))
108 (fset symbol (` (lambda (event) (, doc) (interactive "@e")
109 (x-popup-menu event (, symbol)))))
110 (mapcar (function (lambda (map)
111 (define-key map (vector 'menu-bar (intern (car menu)))
112 (cons (car menu) (symbol-value symbol)))))
113 (if (keymapp maps) (list maps) maps)))
115 (defvar easy-menu-item-count 0)
117 ;; Return a menu keymap corresponding to a Lucid-style menu list
118 ;; MENU-ITEMS, and with name MENU-NAME.
119 ;;;###autoload
120 (defun easy-menu-create-keymaps (menu-name menu-items)
121 (let ((menu (make-sparse-keymap menu-name)) old-items have-buttons)
122 ;; Process items in reverse order,
123 ;; since the define-key loop reverses them again.
124 (setq menu-items (reverse menu-items))
125 (while menu-items
126 (let* ((item (car menu-items))
127 (callback (if (vectorp item) (aref item 1)))
128 (not-button t)
129 command enabler item-string name)
130 (cond ((stringp item)
131 (setq command nil)
132 (setq item-string (if (string-match "^-+$" item) "" item)))
133 ((consp item)
134 (setq command (easy-menu-create-keymaps (car item) (cdr item)))
135 (setq name (setq item-string (car item))))
136 ((vectorp item)
137 (setq command (make-symbol (format "menu-function-%d"
138 easy-menu-item-count)))
139 (setq easy-menu-item-count (1+ easy-menu-item-count))
140 (setq name (setq item-string (aref item 0)))
141 (let ((keyword (aref item 2)))
142 (if (and (symbolp keyword)
143 (= ?: (aref (symbol-name keyword) 0)))
144 (let ((count 2)
145 style selected active keys active-specified
146 arg)
147 (while (> (length item) count)
148 (setq keyword (aref item count))
149 (setq arg (aref item (1+ count)))
150 (setq count (+ 2 count))
151 (cond ((eq keyword ':keys)
152 (setq keys arg))
153 ((eq keyword ':active)
154 (setq active (or arg ''nil)
155 active-specified t))
156 ((eq keyword ':suffix)
157 (setq item-string
158 (concat item-string " " arg)))
159 ((eq keyword ':style)
160 (setq style arg))
161 ((eq keyword ':selected)
162 (setq selected arg))))
163 (if keys
164 (setq item-string
165 (concat item-string " (" keys ")")))
166 (if (and selected
167 (or (eq style 'radio) (eq style 'toggle)))
168 ;; Simulate checkboxes and radio buttons.
169 (progn
170 (setq item-string
171 (concat
172 (if (eval selected)
173 (if (eq style 'radio) "(*) " "[X] ")
174 (if (eq style 'radio) "( ) " "[ ] "))
175 item-string))
176 (put command 'menu-enable
177 (list 'easy-menu-update-button
178 item-string
179 (if (eq style 'radio) ?* ?X)
180 selected
181 (or active t)))
182 (setq not-button nil
183 active nil
184 have-buttons t)
185 (while old-items ; Fix items aleady defined.
186 (setcar (car old-items)
187 (concat " " (car (car old-items))))
188 (setq old-items (cdr old-items)))))
189 (if active-specified (put command 'menu-enable active)))
190 ;; If the third element is nil,
191 ;; make this command always disabled.
192 (put command 'menu-enable (or keyword ''nil))))
193 (if (symbolp callback)
194 (fset command callback)
195 (fset command (list 'lambda () '(interactive) callback)))
196 (put command 'menu-alias t)))
197 (if (null command)
198 ;; Handle inactive strings specially--allow any number
199 ;; of identical ones.
200 (setcdr menu (cons (list nil item-string) (cdr menu)))
201 (if (and not-button have-buttons)
202 (setq item-string (concat " " item-string)))
203 (setq command (cons item-string command))
204 (if (not have-buttons) ; Save all items so that we can fix
205 (setq old-items (cons command old-items))) ; if we have buttons.
206 (if name (define-key menu (vector (intern name)) command))))
207 (setq menu-items (cdr menu-items)))
208 menu))
210 (defun easy-menu-update-button (item ch selected active)
211 "Used as menu-enable property to update buttons.
212 A call to this function is used as the menu-enable property for buttons.
213 ITEM is the item-string into wich CH or ` ' is inserted depending on if
214 SELECTED is true or not. The menu entry in enabled iff ACTIVE is true."
215 (let ((new (if selected ch ? ))
216 (old (aref item 1)))
217 (if (eq new old)
218 ;; No change, just use the active value.
219 active
220 ;; It has changed. Update the entry.
221 (aset item 1 new)
222 ;; If the entry is active, make sure the menu gets updated by
223 ;; returning a different value than last time to cheat the cache.
224 (and active
225 (random)))))
227 (defun easy-menu-change (path name items)
228 "Change menu found at PATH as item NAME to contain ITEMS.
229 PATH is a list of strings for locating the menu containing NAME in the
230 menu bar. ITEMS is a list of menu items, as in `easy-menu-define'.
231 These items entirely replace the previous items in that map.
233 Call this from `menu-bar-update-hook' to implement dynamic menus."
234 (let ((map (key-binding (apply 'vector
235 'menu-bar
236 (mapcar 'intern (append path (list name)))))))
237 (if (keymapp map)
238 (setcdr map (cdr (easy-menu-create-keymaps name items)))
239 (error "Malformed menu in `easy-menu-change'"))))
241 (defun easy-menu-remove (menu))
243 (defun easy-menu-add (menu &optional map))
245 (provide 'easymenu)
247 ;;; easymenu.el ends here