]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/easymenu.el
(byte-compile-from-buffer): Bind print-level.
[gnu-emacs] / lisp / emacs-lisp / easymenu.el
1 ;;; easymenu.el --- support the easymenu interface for defining a menu.
2
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
4
5 ;; Keywords: emulations
6 ;; Author: rms
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; This is compatible with easymenu.el by Per Abrahamsen
25 ;;; but it is much simpler as it doesn't try to support other Emacs versions.
26 ;;; The code was mostly derived from lmenu.el.
27
28 ;;; Code:
29
30 ;;;###autoload
31 (defmacro easy-menu-define (symbol maps doc menu)
32 "Define a menu bar submenu in maps MAPS, according to MENU.
33 The menu keymap is stored in symbol SYMBOL, both as its value
34 and as its function definition. DOC is used as the doc string for SYMBOL.
35
36 The first element of MENU must be a string. It is the menu bar item name.
37 The rest of the elements are menu items.
38
39 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
40
41 NAME is a string--the menu item name.
42
43 CALLBACK is a command to run when the item is chosen,
44 or a list to evaluate when the item is chosen.
45
46 ENABLE is an expression; the item is enabled for selection
47 whenever this expression's value is non-nil.
48
49 Alternatively, a menu item may have the form:
50
51 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
52
53 Where KEYWORD is one of the symbol defined below.
54
55 :keys KEYS
56
57 KEYS is a string; a complex keyboard equivalent to this menu item.
58 This is normally not needed because keyboard equivalents are usually
59 computed automatically.
60
61 :active ENABLE
62
63 ENABLE is an expression; the item is enabled for selection
64 whenever this expression's value is non-nil.
65
66 :suffix NAME
67
68 NAME is a string; the name of an argument to CALLBACK.
69
70 :style
71
72 STYLE is a symbol describing the type of menu item. The following are
73 defined:
74
75 toggle: A checkbox.
76 Currently just prepend the name with the string \"Toggle \".
77 radio: A radio button.
78 nil: An ordinary menu item.
79
80 :selected SELECTED
81
82 SELECTED is an expression; the checkbox or radio button is selected
83 whenever this expression's value is non-nil.
84 Currently just disable radio buttons, no effect on checkboxes.
85
86 A menu item can be a string. Then that string appears in the menu as
87 unselectable text. A string consisting solely of hyphens is displayed
88 as a solid horizontal line.
89
90 A menu item can be a list. It is treated as a submenu.
91 The first element should be the submenu name. That's used as the
92 menu item in the top-level menu. The cdr of the submenu list
93 is a list of menu items, as above."
94 (` (progn
95 (defvar (, symbol) nil (, doc))
96 (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
97
98 ;;;###autoload
99 (defun easy-menu-do-define (symbol maps doc menu)
100 ;; We can't do anything that might differ between Emacs dialects in
101 ;; `easy-menu-define' in order to make byte compiled files
102 ;; compatible. Therefore everything interesting is done in this
103 ;; function.
104 (set symbol (easy-menu-create-keymaps (car menu) (cdr menu)))
105 (fset symbol (` (lambda (event) (, doc) (interactive "@e")
106 (easy-popup-menu event (, symbol)))))
107 (mapcar (function (lambda (map)
108 (define-key map (vector 'menu-bar (intern (car menu)))
109 (cons (car menu) (symbol-value symbol)))))
110 (if (keymapp maps) (list maps) maps)))
111
112 (defvar easy-menu-item-count 0)
113
114 ;; Return a menu keymap corresponding to a Lucid-style menu list
115 ;; MENU-ITEMS, and with name MENU-NAME.
116 ;;;###autoload
117 (defun easy-menu-create-keymaps (menu-name menu-items)
118 (let ((menu (make-sparse-keymap menu-name)))
119 ;; Process items in reverse order,
120 ;; since the define-key loop reverses them again.
121 (setq menu-items (reverse menu-items))
122 (while menu-items
123 (let* ((item (car menu-items))
124 (callback (if (vectorp item) (aref item 1)))
125 command enabler name)
126 (cond ((stringp item)
127 (setq command nil)
128 (setq name (if (string-match "^-+$" item) "" item)))
129 ((consp item)
130 (setq command (easy-menu-create-keymaps (car item) (cdr item)))
131 (setq name (car item)))
132 ((vectorp item)
133 (setq command (make-symbol (format "menu-function-%d"
134 easy-menu-item-count)))
135 (setq easy-menu-item-count (1+ easy-menu-item-count))
136 (setq name (aref item 0))
137 (let ((keyword (aref item 2)))
138 (if (and (symbolp keyword)
139 (= ?: (aref (symbol-name keyword) 0)))
140 (let ((count 2)
141 style selected active keys
142 arg)
143 (while (> (length item) count)
144 (setq keyword (aref item count))
145 (setq arg (aref item (1+ count)))
146 (setq count (+ 2 count))
147 (cond ((eq keyword ':keys)
148 (setq keys arg))
149 ((eq keyword ':active)
150 (setq active arg))
151 ((eq keyword ':suffix)
152 (setq name (concat name " " arg)))
153 ((eq keyword ':style)
154 (setq style arg))
155 ((eq keyword ':selected)
156 (setq selected arg))))
157 (if keys
158 (setq name (concat name " (" keys ")")))
159 (if (eq style 'toggle)
160 ;; Simulate checkboxes.
161 (setq name (concat "Toggle " name)))
162 (if active
163 (put command 'menu-enable active)
164 (and (eq style 'radio)
165 selected
166 ;; Simulate radio buttons with menu-enable.
167 (put command 'menu-enable
168 (list 'not selected)))))
169 (put command 'menu-enable keyword)))
170 (if (keymapp callback)
171 (setq name (concat name " ...")))
172 (if (symbolp callback)
173 (fset command callback)
174 (fset command (list 'lambda () '(interactive) callback)))))
175 (if (null command)
176 ;; Handle inactive strings specially--allow any number
177 ;; of identical ones.
178 (setcdr menu (cons (list nil name) (cdr menu)))
179 (if name
180 (define-key menu (vector (intern name)) (cons name command)))))
181 (setq menu-items (cdr menu-items)))
182 menu))
183
184 (defun easy-menu-change (path name items)
185 "Change menu found at PATH as item NAME to contain ITEMS.
186 PATH is a list of strings for locating the menu containing NAME in the
187 menu bar. ITEMS is a list of menu items, as in `easy-menu-define'.
188 These items entirely replace the previous items in that map.
189
190 Call this from `activate-menubar-hook' to implement dynamic menus."
191 (let ((map (key-binding (apply 'vector
192 'menu-bar
193 (mapcar 'intern (append path (list name)))))))
194 (if (keymapp map)
195 (setcdr map (cdr (easy-menu-create-keymaps name items)))
196 (error "Malformed menu in `easy-menu-change'"))))
197
198 (defun easy-menu-remove (menu))
199
200 (defun easy-menu-add (menu &optional map))
201
202 (provide 'easymenu)
203
204 ;;; easymenu.el ends here