1 ;;; easymenu.el --- support the easymenu interface for defining a menu.
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
5 ;; Keywords: emulations
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)
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.
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.
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.
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.
66 ENABLE is an expression; the item is enabled for selection
67 whenever this expression's value is non-nil.
71 NAME is a string; the name of an argument to CALLBACK.
75 STYLE is a symbol describing the type of menu item. The following are
79 Currently just prepend the name with the string \"Toggle \".
80 radio: A radio button.
81 nil: An ordinary menu item.
85 SELECTED is an expression; the checkbox or radio button is selected
86 whenever this expression's value is non-nil.
87 Currently just disable radio buttons, no effect on checkboxes.
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."
98 (defvar (, symbol
) nil
(, doc
))
99 (easy-menu-do-define (quote (, symbol
)) (, maps
) (, doc
) (, menu
)))))
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
107 (set symbol
(easy-menu-create-keymaps (car menu
) (cdr menu
)))
108 (fset symbol
(` (lambda (event) (, doc
) (interactive "@e")
109 (easy-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.
120 (defun easy-menu-create-keymaps (menu-name menu-items
)
121 (let ((menu (make-sparse-keymap menu-name
)))
122 ;; Process items in reverse order,
123 ;; since the define-key loop reverses them again.
124 (setq menu-items
(reverse menu-items
))
126 (let* ((item (car menu-items
))
127 (callback (if (vectorp item
) (aref item
1)))
128 command enabler name
)
129 (cond ((stringp item
)
131 (setq name
(if (string-match "^-+$" item
) "" item
)))
133 (setq command
(easy-menu-create-keymaps (car item
) (cdr item
)))
134 (setq name
(car item
)))
136 (setq command
(make-symbol (format "menu-function-%d"
137 easy-menu-item-count
)))
138 (setq easy-menu-item-count
(1+ easy-menu-item-count
))
139 (setq name
(aref item
0))
140 (let ((keyword (aref item
2)))
141 (if (and (symbolp keyword
)
142 (= ?
: (aref (symbol-name keyword
) 0)))
144 style selected active keys
146 (while (> (length item
) count
)
147 (setq keyword
(aref item count
))
148 (setq arg
(aref item
(1+ count
)))
149 (setq count
(+ 2 count
))
150 (cond ((eq keyword
':keys
)
152 ((eq keyword
':active
)
154 ((eq keyword
':suffix
)
155 (setq name
(concat name
" " arg
)))
156 ((eq keyword
':style
)
158 ((eq keyword
':selected
)
159 (setq selected arg
))))
161 (setq name
(concat name
" (" keys
")")))
162 (if (eq style
'toggle
)
163 ;; Simulate checkboxes.
164 (setq name
(concat "Toggle " name
)))
166 (put command
'menu-enable active
)
167 (and (eq style
'radio
)
169 ;; Simulate radio buttons with menu-enable.
170 (put command
'menu-enable
171 (list 'not selected
)))))
172 (put command
'menu-enable keyword
)))
173 (if (keymapp callback
)
174 (setq name
(concat name
" ...")))
175 (if (symbolp callback
)
176 (fset command callback
)
177 (fset command
(list 'lambda
() '(interactive) callback
)))))
179 ;; Handle inactive strings specially--allow any number
180 ;; of identical ones.
181 (setcdr menu
(cons (list nil name
) (cdr menu
)))
183 (define-key menu
(vector (intern name
)) (cons name command
)))))
184 (setq menu-items
(cdr menu-items
)))
187 (defun easy-menu-change (path name items
)
188 "Change menu found at PATH as item NAME to contain ITEMS.
189 PATH is a list of strings for locating the menu containing NAME in the
190 menu bar. ITEMS is a list of menu items, as in `easy-menu-define'.
191 These items entirely replace the previous items in that map.
193 Call this from `activate-menubar-hook' to implement dynamic menus."
194 (let ((map (key-binding (apply 'vector
196 (mapcar 'intern
(append path
(list name
)))))))
198 (setcdr map
(cdr (easy-menu-create-keymaps name items
)))
199 (error "Malformed menu in `easy-menu-change'"))))
201 (defun easy-menu-remove (menu))
203 (defun easy-menu-add (menu &optional map
))
207 ;;; easymenu.el ends here