comparison lisp/easymenu.el @ 371:cc15677e0335 r21-2b1

Import from CVS: tag r21-2b1
author cvs
date Mon, 13 Aug 2007 11:03:08 +0200
parents a4f53d9b3154
children d883f39b8495
comparison
equal deleted inserted replaced
370:bd866891f083 371:cc15677e0335
21 ;; You should have received a copy of the GNU General Public License 21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; if not, write to the Free Software 22 ;; along with XEmacs; if not, write to the Free Software
23 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ;; 02111-1307, USA. 24 ;; 02111-1307, USA.
25 25
26 ;;; Synched up with: Not synched with FSF but coordinated with the FSF 26 ;;; Synched up with: Not synched with FSF.
27 ;;; easymenu maintainer for compatability with FSF 20.4.
28 ;;; Please: Coordinate changes with Inge Frick <inge@nada.kth.se>
29 27
30 ;; Commentary: 28 ;; Commentary:
31 29
32 ;; This file is dumped with XEmacs. 30 ;; This file is dumped with XEmacs.
33 31
43 41
44 ;; The public functions are: 42 ;; The public functions are:
45 43
46 ;; - Function: easy-menu-define SYMBOL MAPS DOC MENU 44 ;; - Function: easy-menu-define SYMBOL MAPS DOC MENU
47 ;; SYMBOL is both the name of the variable that holds the menu and 45 ;; SYMBOL is both the name of the variable that holds the menu and
48 ;; the name of a function that will present the menu. 46 ;; the name of a function that will present a the menu.
49 ;; MAPS is a list of keymaps where the menu should appear in the menubar. 47 ;; MAPS is a list of keymaps where the menu should appear in the menubar.
50 ;; DOC is the documentation string for the variable. 48 ;; DOC is the documentation string for the variable.
51 ;; MENU is an XEmacs style menu description. 49 ;; MENU is an XEmacs style menu description.
52 50
53 ;; See the documentation for easy-menu-define for details. 51 ;; See the documentation for easy-menu-define for details.
62 ;; - Function: easy-menu-add MENU [ MAP ] 60 ;; - Function: easy-menu-add MENU [ MAP ]
63 ;; Add MENU to the current menubar in MAP. 61 ;; Add MENU to the current menubar in MAP.
64 62
65 ;; - Function: easy-menu-remove MENU 63 ;; - Function: easy-menu-remove MENU
66 ;; Remove MENU from the current menubar. 64 ;; Remove MENU from the current menubar.
67
68 ;; - Function: easy-menu-add-item
69 ;; Add item or submenu to existing menu
70
71 ;; - Function: easy-menu-item-present-p
72 ;; Locate item
73
74 ;; - Function: easy-menu-remove-item
75 ;; Delete item from menu.
76 65
77 ;; Emacs 19 never uses `easy-menu-add' or `easy-menu-remove', menus 66 ;; Emacs 19 never uses `easy-menu-add' or `easy-menu-remove', menus
78 ;; automatically appear and disappear when the keymaps specified by 67 ;; automatically appear and disappear when the keymaps specified by
79 ;; the MAPS argument to `easy-menu-define' are activated. 68 ;; the MAPS argument to `easy-menu-define' are activated.
80 69
151 (` (progn 140 (` (progn
152 (defvar (, symbol) nil (, doc)) 141 (defvar (, symbol) nil (, doc))
153 (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu))))) 142 (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
154 143
155 (defun easy-menu-do-define (symbol maps doc menu) 144 (defun easy-menu-do-define (symbol maps doc menu)
156 (when (featurep 'menubar) 145 (if (featurep 'menubar)
146 (progn
157 (set symbol menu) 147 (set symbol menu)
158 (fset symbol `(lambda (e) 148 (fset symbol (list 'lambda '(e)
159 ,doc 149 doc
160 (interactive "@e") 150 '(interactive "@e")
161 (run-hooks 'activate-menubar-hook) 151 '(run-hooks 'activate-menubar-hook)
162 (setq zmacs-region-stays 't) 152 '(setq zmacs-region-stays 't)
163 (popup-menu ,symbol))))) 153 (list 'popup-menu symbol))))))
164 154
165 (defun easy-menu-change (&rest args) 155 (defun easy-menu-change (&rest args)
166 (when (featurep 'menubar) 156 (when (featurep 'menubar)
167 (apply 'add-menu args))) 157 (apply 'add-menu args)))
168 158
171 (defvar easy-menu-all-popups nil) 161 (defvar easy-menu-all-popups nil)
172 (make-variable-buffer-local 'easy-menu-all-popups) 162 (make-variable-buffer-local 'easy-menu-all-popups)
173 163
174 (defun easy-menu-add (menu &optional map) 164 (defun easy-menu-add (menu &optional map)
175 "Add MENU to the current menu bar." 165 "Add MENU to the current menu bar."
176 (when (featurep 'menubar) 166 (if (featurep 'menubar)
167 (progn
177 (unless (member menu easy-menu-all-popups) 168 (unless (member menu easy-menu-all-popups)
178 (push menu easy-menu-all-popups)) 169 (push menu easy-menu-all-popups))
179 (setq mode-popup-menu (if (> (length easy-menu-all-popups) 1) 170 (setq mode-popup-menu (if (> (length easy-menu-all-popups) 1)
180 (cons (easy-menu-title) 171 (cons (easy-menu-title)
181 (reverse easy-menu-all-popups)) 172 (reverse easy-menu-all-popups))
191 ;; Set at left if only contains right marker. 182 ;; Set at left if only contains right marker.
192 (set-buffer-menubar (list menu nil))) 183 (set-buffer-menubar (list menu nil)))
193 (t 184 (t
194 ;; Add at right. 185 ;; Add at right.
195 (set-buffer-menubar (copy-sequence current-menubar)) 186 (set-buffer-menubar (copy-sequence current-menubar))
196 (add-menu nil (car menu) (cdr menu)))))) 187 (add-menu nil (car menu) (cdr menu)))))))
197 188
198 (defun easy-menu-remove (menu) 189 (defun easy-menu-remove (menu)
199 "Remove MENU from the current menu bar." 190 "Remove MENU from the current menu bar."
200 (when (featurep 'menubar) 191 (if (featurep 'menubar)
192 (progn
201 (setq easy-menu-all-popups (delq menu easy-menu-all-popups) 193 (setq easy-menu-all-popups (delq menu easy-menu-all-popups)
202 mode-popup-menu (if (< (length easy-menu-all-popups) 1) 194 mode-popup-menu (if (< (length easy-menu-all-popups) 1)
203 (cons (easy-menu-title) 195 (cons (easy-menu-title)
204 (reverse easy-menu-all-popups)) 196 (reverse easy-menu-all-popups))
205 (car easy-menu-all-popups))) 197 (car easy-menu-all-popups)))
206 198
207 (and current-menubar 199 (and current-menubar
208 (assoc (car menu) current-menubar) 200 (assoc (car menu) current-menubar)
209 (delete-menu-item (list (car menu)))))) 201 (delete-menu-item (list (car menu)))))))
210
211 (defsubst easy-menu-normalize (menu)
212 (if (symbolp menu)
213 (symbol-value menu)
214 menu))
215
216 (defun easy-menu-add-item (menu path item &optional before)
217 "At the end of the submenu of MENU with path PATH, add ITEM.
218 If ITEM is already present in this submenu, then this item will be changed.
219 otherwise ITEM will be added at the end of the submenu, unless the optional
220 argument BEFORE is present, in which case ITEM will instead be added
221 before the item named BEFORE.
222 MENU is either a symbol, which have earlier been used as the first
223 argument in a call to `easy-menu-define', or the value of such a symbol
224 i.e. a menu, or nil, which stands for the current menubar.
225 PATH is a list of strings for locating the submenu where ITEM is to be
226 added. If PATH is nil, MENU itself is used. Otherwise, the first
227 element should be the name of a submenu directly under MENU. This
228 submenu is then traversed recursively with the remaining elements of PATH.
229 ITEM is either defined as in `easy-menu-define', a menu defined earlier
230 by `easy-menu-define' or `easy-menu-create-menu' or an item returned
231 from `easy-menu-item-present-p' or `easy-menu-remove-item'."
232 (when (featurep 'menubar)
233 (add-menu-button path item before (easy-menu-normalize menu))))
234
235 (defun easy-menu-item-present-p (menu path name)
236 "In submenu of MENU with path PATH, return true iff item NAME is present.
237 MENU and PATH are defined as in `easy-menu-add-item'.
238 NAME should be a string, the name of the element to be looked for.
239
240 The return value can be used as an argument to `easy-menu-add-item'."
241 (if (featurep 'menubar)
242 (car (find-menu-item (or (easy-menu-normalize menu) current-menubar)
243 (append path (list name))))
244 nil))
245
246 (defun easy-menu-remove-item (menu path name)
247 "From submenu of MENU with path PATH, remove item NAME.
248 MENU and PATH are defined as in `easy-menu-add-item'.
249 NAME should be a string, the name of the element to be removed.
250
251 The return value can be used as an argument to `easy-menu-add-item'."
252 (when (featurep 'menubar)
253 (delete-menu-item (append path (list name))
254 (easy-menu-normalize menu))))
255
256
257
258 202
259 ;; Think up a good title for the menu. Take the major-mode of the 203 ;; Think up a good title for the menu. Take the major-mode of the
260 ;; buffer, strip the -mode part, convert hyphens to spaces, and 204 ;; buffer, strip the -mode part, convert hyphens to spaces, and
261 ;; capitalize it. 205 ;; capitalize it.
262 ;; 206 ;;