comparison lisp/easymenu.el @ 3230:012240027a21

[xemacs-hg @ 2006-02-05 19:20:44 by aidan] Address easymenu bug.
author aidan
date Sun, 05 Feb 2006 19:20:44 +0000
parents 263a354405ed
children cd167465bf69 308d34e9f07d
comparison
equal deleted inserted replaced
3229:5e523c853d06 3230:012240027a21
1 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs. 1 ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
2 2
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc. 3 ;; Copyright (C) 1992, 1993, 1994, 1995, 2005 Free Software Foundation, Inc.
4 4
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> 5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: XEmacs Development Team 6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: internal, extensions, dumped 7 ;; Keywords: internal, extensions, dumped
8 8
164 164
165 (defun easy-menu-change (&rest args) 165 (defun easy-menu-change (&rest args)
166 (when (featurep 'menubar) 166 (when (featurep 'menubar)
167 (apply 'add-menu args))) 167 (apply 'add-menu args)))
168 168
169 ;; This variable hold the easy-menu mode menus of all major and 169 (defvar easy-menu-all-popups nil
170 ;; minor modes currently in effect in the current buffer. 170 "This variable holds all the popup menus easy-menu knows about.
171 (defvar easy-menu-all-popups nil) 171 This includes any menu created with `easy-menu-add' and any
172 non-default value for `mode-popup-menu' that existed when
173 `easy-menu-add' was first called.")
172 (make-variable-buffer-local 'easy-menu-all-popups) 174 (make-variable-buffer-local 'easy-menu-all-popups)
173 175
174 (defun easy-menu-add (menu &optional map) 176 (defun easy-menu-add (menu &optional map)
175 "Add MENU to the current menu bar." 177 "Add MENU to the current menu bar."
176 (when (featurep 'menubar) 178 ;; If you uncomment the following, do an xemacs -vanilla, type M-x
177 (unless (member menu easy-menu-all-popups) 179 ;; folding-mode RET, you'll see that this code, which theoretically has
178 (push menu easy-menu-all-popups)) 180 ;; *scratch* as its buffer context, can't see *scratch*'s value for
179 (setq mode-popup-menu (if (> (length easy-menu-all-popups) 1) 181 ;; mode-popup-menu--the default overrides it.
180 (cons (easy-menu-title) 182 ;;
181 (reverse easy-menu-all-popups)) 183 ;; This is not specific to *scratch*--try it on ~/.xemacs/init.el--but it
182 (let ((same-as-menu 184 ;; does appear to be specific to the first time mode-popup-menu is
183 (car easy-menu-all-popups))) 185 ;; accessed as a buffer-local variable in non-interactive code (that is,
184 (cons (normalize-menu-text 186 ;; M-: mode-popup-menu RET gives the correct value).
185 (car same-as-menu)) 187 ;;
186 (cdr same-as-menu))))) 188 ;; My fixing this right now isn't going to happen. Aidan Kehoe, 2006-01-03
187 189 ; (message (concat "inside easy-menu-add, menu is %s, "
190 ; "mode-popup-menu is %s, current buffer is %s, "
191 ; "default-value mode-popup-menu is %s, "
192 ; "easy-menu-all-popups is %s")
193 ; menu mode-popup-menu (current-buffer)
194 ; (default-value 'mode-popup-menu) easy-menu-all-popups)
195 (when (featurep 'menubar)
196 ;; Save the existing mode-popup-menu, if it's been changed.
197 (when (and (zerop (length easy-menu-all-popups))
198 (not (equal (default-value 'mode-popup-menu) mode-popup-menu)))
199 (push mode-popup-menu easy-menu-all-popups))
200 ;; Add the menu to our list of all the popups for the buffer.
201 (pushnew menu easy-menu-all-popups :test 'equal)
202 ;; If there are multiple popup menus available, make the popup menu
203 ;; normally shown with button-3 a menu of them. If there is just one,
204 ;; make that button show it, and no super-menu.
205 (setq mode-popup-menu (if (= 1 (length easy-menu-all-popups))
206 (car easy-menu-all-popups)
207 (cons (easy-menu-title)
208 (reverse easy-menu-all-popups))))
188 (cond ((null current-menubar) 209 (cond ((null current-menubar)
189 ;; Don't add it to a non-existing menubar. 210 ;; Don't add it to a non-existing menubar.
190 nil) 211 nil)
191 ((assoc (car menu) current-menubar) 212 ((assoc (car menu) current-menubar)
192 ;; Already present. 213 ;; Already present.
200 (add-menu nil (car menu) (cdr menu)))))) 221 (add-menu nil (car menu) (cdr menu))))))
201 222
202 (defun easy-menu-remove (menu) 223 (defun easy-menu-remove (menu)
203 "Remove MENU from the current menu bar." 224 "Remove MENU from the current menu bar."
204 (when (featurep 'menubar) 225 (when (featurep 'menubar)
205 (setq easy-menu-all-popups (delq menu easy-menu-all-popups) 226 (setq
206 mode-popup-menu (if (> (length easy-menu-all-popups) 1) 227 ;; Remove this menu from the list of popups we know about.
207 (cons (easy-menu-title) 228 easy-menu-all-popups (delq menu easy-menu-all-popups)
208 (reverse easy-menu-all-popups)) 229 ;; If there are multiple popup menus available, make the popup menu
209 (let ((same-as-menu 230 ;; normally shown with button-3 a menu of them. If there is just one,
210 (car easy-menu-all-popups))) 231 ;; make that button show it, and no super-menu.
211 (cons (normalize-menu-text 232 mode-popup-menu (if (= 1 (length easy-menu-all-popups))
212 (car same-as-menu)) 233 (car easy-menu-all-popups)
213 (cdr same-as-menu))))) 234 (cons (easy-menu-title)
214 235 (reverse easy-menu-all-popups))))
236 ;; If we've just set mode-popup-menu to an empty menu, change that menu
237 ;; to its default value (without intervention from easy-menu).
238 (if (zerop (length easy-menu-all-popups))
239 (setq mode-popup-menu (default-value 'mode-popup-menu)))
215 (and current-menubar 240 (and current-menubar
216 (assoc (car menu) current-menubar) 241 (assoc (car menu) current-menubar)
217 (delete-menu-item (list (car menu)))))) 242 (delete-menu-item (list (car menu))))))
218 243
219 (defsubst easy-menu-normalize (menu) 244 (defsubst easy-menu-normalize (menu)
259 The return value can be used as an argument to `easy-menu-add-item'." 284 The return value can be used as an argument to `easy-menu-add-item'."
260 (when (featurep 'menubar) 285 (when (featurep 'menubar)
261 (delete-menu-item (append path (list name)) 286 (delete-menu-item (append path (list name))
262 (easy-menu-normalize menu)))) 287 (easy-menu-normalize menu))))
263 288
264 289 ;; Think up a good title for the menu. Take the major-mode of the buffer,
265 290 ;; strip the -mode part, convert hyphens to spaces, and capitalize it.
266
267 ;; Think up a good title for the menu. Take the major-mode of the
268 ;; buffer, strip the -mode part, convert hyphens to spaces, and
269 ;; capitalize it.
270 ;; 291 ;;
271 ;; If you can think of something smarter, feel free to replace it. 292 ;; In a more ideal world, we could use `mode-name' here, which see, but that
272 ;; Don't forget to mail the change to xemacs@xemacs.org where everyone 293 ;; turns out to be temporarily trashed by various minor modes, and this
273 ;; can flame, er, praise your changes. 294 ;; value is much more trustworthy.
295
274 (defun easy-menu-title () 296 (defun easy-menu-title ()
275 (capitalize (replace-in-string (replace-in-string 297 (capitalize (replace-in-string (replace-in-string
276 (symbol-name major-mode) "-mode$" "") 298 (symbol-name major-mode) "-mode$" "")
277 "-" " "))) 299 "-" " ")))
278 300