Mercurial > hg > xemacs-beta
annotate lisp/menubar.el @ 5818:15b0715c204d
Avoid passing patterns to with charset property to FcNameUnparse.
Prevents crash reported by Raymond Toy.
| author | Stephen J. Turnbull <stephen@xemacs.org> |
|---|---|
| date | Sat, 18 Oct 2014 21:20:42 +0900 |
| parents | 580ebed3500a |
| children |
| rev | line source |
|---|---|
| 428 | 1 ;;; menubar.el --- Menubar support for XEmacs |
| 2 | |
| 3 ;; Copyright (C) 1991-4, 1997-1998 Free Software Foundation, Inc. | |
| 4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp. | |
| 2545 | 5 ;; Copyright (C) 1995, 1996, 2003 Ben Wing. |
| 428 | 6 |
| 7 ;; Maintainer: XEmacs Development Team | |
| 8 ;; Keywords: internal, extensions, dumped | |
| 9 | |
| 10 ;; This file is part of XEmacs. | |
| 11 | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
12 ;; XEmacs is free software: you can redistribute it and/or modify it |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
13 ;; under the terms of the GNU General Public License as published by the |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
14 ;; Free Software Foundation, either version 3 of the License, or (at your |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
15 ;; option) any later version. |
| 428 | 16 |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
20 ;; for more details. |
| 428 | 21 |
| 22 ;; You should have received a copy of the GNU General Public License | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4310
diff
changeset
|
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
| 428 | 24 |
| 25 ;;; Synched up with: Not in FSF. (Completely divergent from FSF menu-bar.el) | |
| 26 | |
| 27 ;;; Commentary: | |
| 28 | |
| 29 ;; This file is dumped with XEmacs (when menubar support is compiled in). | |
| 30 | |
| 442 | 31 ;; Some stuff in FSF menu-bar.el is in menubar-items.el |
| 428 | 32 |
| 33 ;;; Code: | |
| 34 | |
| 35 (defgroup menu nil | |
| 36 "Input from the menus." | |
| 37 :group 'environment) | |
| 38 | |
| 39 (defvar default-menubar nil) | |
| 40 | |
| 41 ;; this function is considered "part of the lexicon" by many, | |
| 42 ;; so we'll leave it here. | |
| 43 (defun kill-this-buffer () ; for the menubar | |
| 44 "Kill the current buffer." | |
| 45 (interactive) | |
| 46 (kill-buffer (current-buffer))) | |
| 47 | |
| 48 (defun set-menubar-dirty-flag () | |
| 49 "Tell XEmacs that the menubar has to be updated. | |
| 50 NOTE: XEmacs now recognizes when you set a different value for | |
| 51 `current-menubar'. You *only* need to call this function if you | |
| 52 destructively modify a part of the menubar and don't set `current-menubar'. | |
| 53 Note that all the functions that modify a menu call this automatically." | |
| 54 (setq-default current-menubar (default-value 'current-menubar))) | |
| 55 | |
| 56 ;; #### shouldn't this perhaps be `copy-tree'? | |
| 57 (defun set-menubar (menubar) | |
| 58 "Set the default menubar to be MENUBAR. | |
| 59 See `current-menubar' for a description of the syntax of a menubar." | |
| 60 (check-menu-syntax menubar t) | |
| 61 (setq-default current-menubar (copy-sequence menubar))) | |
| 62 | |
| 63 (defun set-buffer-menubar (menubar) | |
| 64 "Set the buffer-local menubar to be MENUBAR. | |
| 65 See `current-menubar' for a description of the syntax of a menubar." | |
| 66 (check-menu-syntax menubar t) | |
| 67 (make-local-variable 'current-menubar) | |
| 68 (setq current-menubar (copy-sequence menubar))) | |
| 69 | |
| 70 (defun check-menu-syntax (menu &optional menubar-p) | |
| 71 ;; The C code does syntax checking on the value of `current-menubar', | |
| 72 ;; but it's better to do it early, before things have gotten messed up. | |
| 73 (if menubar-p | |
| 74 nil | |
| 75 (or (stringp (car menu)) | |
| 76 (signal 'error | |
| 77 (list "menu name (first element) must be a string" menu))) | |
| 78 ;;(or (cdr menu) (signal 'error (list "menu is empty" menu))) | |
| 79 (setq menu (cdr menu))) | |
| 80 (let (menuitem item) | |
| 81 (while (keywordp (setq item (car menu))) | |
|
5715
68f8d295be49
Support :visible in menu specifications.
Jerry James <james@xemacs.org>
parents:
5706
diff
changeset
|
82 (or (memq item '(:config :included :visible :filter :accelerator :active)) |
| 428 | 83 (signal 'error |
|
5715
68f8d295be49
Support :visible in menu specifications.
Jerry James <james@xemacs.org>
parents:
5706
diff
changeset
|
84 (list "menu keyword must be :config, :included, :visible, :accelerator, :active or :filter" |
| 428 | 85 item))) |
| 86 (if (or (not (cdr menu)) | |
| 87 (vectorp (nth 1 menu)) | |
| 88 (keywordp (nth 1 menu))) | |
| 89 (signal 'error (list "strange keyword value" item (nth 1 menu)))) | |
| 90 (setq menu (nthcdr 2 menu))) | |
| 91 (while menu | |
| 92 (setq menuitem (car menu)) | |
| 93 (cond | |
| 94 ((stringp menuitem) | |
| 95 (and (string-match "^\\(-+\\|=+\\):\\(.*\\)" menuitem) | |
| 96 (setq item (match-string 2 menuitem)) | |
| 444 | 97 (or (member item '(;; Motif-compatible |
| 428 | 98 "singleLine" |
| 99 "doubleLine" | |
| 100 "singleDashedLine" | |
| 101 "doubleDashedLine" | |
| 102 "noLine" | |
| 103 "shadowEtchedIn" | |
| 104 "shadowEtchedOut" | |
| 105 "shadowEtchedInDash" | |
| 106 "shadowEtchedOutDash" | |
| 107 ;; non-Motif (Lucid menubar widget only) | |
| 108 "shadowDoubleEtchedIn" | |
| 109 "shadowDoubleEtchedOut" | |
| 110 "shadowDoubleEtchedInDash" | |
| 111 "shadowDoubleEtchedOutDash" | |
| 112 )) | |
| 113 (signal 'error (list "bogus separator style in menu item" item))) | |
| 114 )) | |
| 115 ((null menuitem) | |
| 116 (or menubar-p | |
| 117 (signal 'error (list "nil is only permitted in the top level of menubars")))) | |
| 118 ((consp menuitem) | |
| 119 (check-menu-syntax menuitem)) | |
| 120 ((vectorp menuitem) | |
| 121 (let ((L (length menuitem)) | |
| 122 plistp) | |
| 123 (and (< L 2) | |
| 124 (signal 'error | |
| 125 (list "button descriptors must be at least 2 long" | |
| 126 menuitem))) | |
| 127 (setq plistp (or (>= L 5) | |
| 128 (and (> L 2) (keywordp (aref menuitem 2))))) | |
| 129 (if plistp | |
| 130 (let ((i 2) | |
| 131 selp | |
| 132 style | |
| 133 item) | |
| 134 (while (< i L) | |
| 135 (setq item (aref menuitem i)) | |
| 136 (cond ((not (memq item '(:active :suffix :keys :style | |
| 137 :full :included :selected | |
|
5715
68f8d295be49
Support :visible in menu specifications.
Jerry James <james@xemacs.org>
parents:
5706
diff
changeset
|
138 :visible :accelerator))) |
| 428 | 139 (signal 'error |
| 140 (list (if (keywordp item) | |
| 141 "unknown menu item keyword" | |
| 142 "not a keyword") | |
| 143 item menuitem))) | |
| 144 ((eq item :style) | |
| 145 (setq style (aref menuitem (1+ i))) | |
| 146 (or (memq style '(nil toggle radio button text)) | |
| 147 (signal 'error (list "unknown style" style | |
| 148 menuitem)))) | |
| 149 ((eq item :selected) (setq selp t)) | |
| 150 ) | |
| 151 (setq i (+ i (if (eq item :full) 1 2)))) | |
| 152 (if (and selp (not (memq style '(toggle button radio)))) | |
| 153 (signal 'error | |
| 154 (list | |
| 155 ":selected only makes sense with :style toggle, radio, or button" | |
| 156 menuitem))) | |
| 157 ))) | |
| 158 ) | |
| 159 ;; (t (signal 'error (list "unrecognized menu descriptor" menuitem)))) | |
| 160 (t (message "unrecognized menu descriptor %s" (prin1-to-string menuitem)))) | |
| 161 (setq menu (cdr menu))))) | |
| 162 | |
| 163 | |
| 2545 | 164 ;;; basic menu manipulation functions |
| 428 | 165 |
| 2545 | 166 (defun menu-item-text (item &optional normalize) |
| 167 "Return the text that is displayed for a menu item. | |
| 168 If ITEM is a string (unselectable text), it is returned; otherwise, | |
| 169 the first element of the cons or vector is returned. | |
| 170 If NORMALIZE is non-nil, pass the text through `normalize-menu-text' | |
| 171 before being returned, to remove accelerator specs and convert %% to %." | |
| 172 (let ((val (if (stringp item) item (elt item 0)))) | |
| 173 (if normalize (normalize-menu-text val) val))) | |
| 174 | |
| 175 (defun find-menu-item (menubar item-path-list) | |
| 176 "Search MENUBAR for item given by ITEM-PATH-LIST. | |
| 428 | 177 Returns (ITEM . PARENT), where PARENT is the immediate parent of |
| 178 the item found. | |
| 179 If the item does not exist, the car of the returned value is nil. | |
| 180 If some menu in the ITEM-PATH-LIST does not exist, an error is signalled." | |
|
5645
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
181 (labels |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
182 ((find-menu-item-1 (menubar item-path-list &optional parent) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
183 (check-argument-type 'listp item-path-list) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
184 (if (not (consp menubar)) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
185 nil |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
186 (let ((rest menubar) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
187 result) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
188 (when (stringp (car rest)) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
189 (setq rest (cdr rest))) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
190 (while (keywordp (car rest)) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
191 (setq rest (cddr rest))) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
192 (while rest |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
193 (if (and (car rest) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
194 (stringp (car item-path-list)) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
195 (= 0 (compare-menu-text (car item-path-list) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
196 (menu-item-text (car rest))))) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
197 (setq result (car rest) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
198 rest nil) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
199 (setq rest (cdr rest)))) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
200 (if (cdr item-path-list) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
201 (cond ((consp result) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
202 (find-menu-item-1 (cdr result) (cdr item-path-list) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
203 result)) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
204 (result |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
205 (signal 'error (list (gettext "not a submenu") result))) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
206 (t |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
207 (signal 'error (list (gettext "no such submenu") |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
208 (car item-path-list))))) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
209 (cons result parent)))))) |
|
5d3bb1100832
Remove some utility functions from the global namespace, lisp/
Aidan Kehoe <kehoea@parhasard.net>
parents:
5490
diff
changeset
|
210 (find-menu-item-1 menubar item-path-list))) |
| 428 | 211 |
|
4310
a6d7e031a10b
Fix two Tailor glitches.
Mike Sperber <sperber@deinprogramm.de>
parents:
4164
diff
changeset
|
212 (defun add-menu-item-1 (leaf-p menu-path new-item before in-menu) |
| 428 | 213 ;; This code looks like it could be cleaned up some more |
| 214 ;; Do we really need 6 calls to find-menu-item? | |
| 215 (let* ((item-name | |
| 216 (cond ((vectorp new-item) (aref new-item 0)) | |
| 217 ((consp new-item) (car new-item)) | |
| 218 (t nil))) | |
| 219 (menubar (or in-menu current-menubar)) | |
| 220 (menu (condition-case () | |
| 221 (car (find-menu-item menubar menu-path)) | |
| 222 (error nil))) | |
| 223 (item-found (cond | |
| 224 ((null item-name) | |
| 225 nil) | |
| 226 ((not (listp menu)) | |
| 227 (signal 'error (list (gettext "not a submenu") | |
| 228 menu-path))) | |
| 229 (menu | |
| 230 (find-menu-item (cdr menu) (list item-name))) | |
| 231 (t | |
| 232 (find-menu-item menubar (list item-name))) | |
| 233 ))) | |
| 234 (unless menubar | |
| 235 (error "`current-menubar' is nil: can't add menus to it.")) | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
236 (unless menu ; If we don't have all intervening submenus needed by menu-path, add them. |
| 428 | 237 (let ((rest menu-path) |
| 238 (so-far menubar)) | |
| 239 (while rest | |
| 240 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest))))) | |
| 241 (setq menu | |
| 242 (if (eq so-far menubar) | |
| 243 (car (find-menu-item so-far (list (car rest)))) | |
| 244 (car (find-menu-item (cdr so-far) (list (car rest)))))) | |
| 245 (unless menu | |
| 246 (let ((rest2 so-far)) | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
247 (while (and (cdr rest2) (car (cdr rest2))) ; Walk rest2 down so-far till rest2 is the last item before divider or end of list. |
| 428 | 248 (setq rest2 (cdr rest2))) |
| 249 (setcdr rest2 | |
| 250 (nconc (list (setq menu (list (car rest)))) | |
| 251 (cdr rest2))))) | |
| 252 (setq so-far menu) | |
| 253 (setq rest (cdr rest))))) | |
| 254 (if (and item-found (car item-found)) | |
| 255 ;; hack the item in place. | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
256 (if (or menu (not (eq (car item-found) (car menubar)))) ;If either replacing in submenu, or replacing non-initial top-level item. |
| 428 | 257 ;; Isn't it very bad form to use nsubstitute for side effects? |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
258 (nsubstitute new-item (car item-found) (or menu menubar)) |
|
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
259 (setcar menubar new-item)) |
| 428 | 260 ;; OK, we have to add the whole thing... |
| 261 ;; if BEFORE is specified, try to add it there. | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
262 (unless menu (setq menu menubar)) |
| 428 | 263 (when before |
| 264 (setq before (car (find-menu-item menu (list before))))) | |
| 265 (let ((rest menu) | |
| 266 (added-before nil)) | |
| 267 (while rest | |
| 268 (if (eq before (car (cdr rest))) | |
| 269 (progn | |
| 270 (setcdr rest (cons new-item (cdr rest))) | |
| 271 (setq rest nil added-before t)) | |
| 272 (setq rest (cdr rest)))) | |
| 273 (when (not added-before) | |
| 274 ;; adding before the first item on the menubar itself is harder | |
| 275 (if (and (eq menu menubar) (eq before (car menu))) | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
276 (let ((old-car (cons (car menubar) (cdr menubar)))) |
|
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
277 (setcar menubar new-item) |
|
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
278 (setcdr menubar old-car)) |
| 428 | 279 ;; otherwise, add the item to the end. |
| 280 (nconc menu (list new-item)))))) | |
| 281 (set-menubar-dirty-flag) | |
| 282 new-item)) | |
| 283 | |
|
4310
a6d7e031a10b
Fix two Tailor glitches.
Mike Sperber <sperber@deinprogramm.de>
parents:
4164
diff
changeset
|
284 (defun add-menu-button (menu-path menu-leaf &optional before in-menu) |
| 428 | 285 "Add a menu item to some menu, creating the menu first if necessary. |
| 286 If the named item exists already, it is changed. | |
| 287 MENU-PATH identifies the menu under which the new menu item should be inserted. | |
| 288 It is a list of strings; for example, (\"File\") names the top-level \"File\" | |
| 289 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\". | |
|
4310
a6d7e031a10b
Fix two Tailor glitches.
Mike Sperber <sperber@deinprogramm.de>
parents:
4164
diff
changeset
|
290 MENU-LEAF is a menubar leaf node. See the documentation of `current-menubar'. |
| 428 | 291 BEFORE, if provided, is the name of a menu item before which this item should |
| 292 be added, if this item is not on the menu already. If the item is already | |
| 293 present, it will not be moved. | |
| 444 | 294 IN-MENU, if provided, means use that instead of `current-menubar' as the |
| 295 menu to change." | |
| 428 | 296 ;; Note easymenu.el uses the fact that menu-leaf can be a submenu. |
|
4310
a6d7e031a10b
Fix two Tailor glitches.
Mike Sperber <sperber@deinprogramm.de>
parents:
4164
diff
changeset
|
297 (add-menu-item-1 t menu-path menu-leaf before in-menu)) |
| 428 | 298 |
| 299 ;; I actually liked the old name better, but the interface has changed too | |
| 444 | 300 ;; drastically to keep it. --Stig |
| 428 | 301 (defun add-submenu (menu-path submenu &optional before in-menu) |
| 302 "Add a menu to the menubar or one of its submenus. | |
| 303 If the named menu exists already, it is changed. | |
| 304 MENU-PATH identifies the menu under which the new menu should be inserted. | |
| 305 It is a list of strings; for example, (\"File\") names the top-level \"File\" | |
| 306 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\". | |
| 307 If MENU-PATH is nil, then the menu will be added to the menubar itself. | |
| 308 SUBMENU is the new menu to add. | |
| 309 See the documentation of `current-menubar' for the syntax. | |
| 310 BEFORE, if provided, is the name of a menu before which this menu should | |
| 311 be added, if this menu is not on its parent already. If the menu is already | |
| 444 | 312 present, it will not be moved. |
| 313 IN-MENU, if provided, means use that instead of `current-menubar' as the | |
| 314 menu to change." | |
| 428 | 315 (check-menu-syntax submenu nil) |
|
4310
a6d7e031a10b
Fix two Tailor glitches.
Mike Sperber <sperber@deinprogramm.de>
parents:
4164
diff
changeset
|
316 (add-menu-item-1 nil menu-path submenu before in-menu)) |
| 444 | 317 ;; purespace is no more, so this function is unnecessary |
| 318 ;(defun purecopy-menubar (x) | |
| 319 ; ;; this calls purecopy on the strings, and the contents of the vectors, | |
| 320 ; ;; but not on the vectors themselves, or the conses - those must be | |
| 321 ; ;; writable. | |
| 322 ; (cond ((vectorp x) | |
| 323 ; (let ((i (length x))) | |
| 324 ; (while (> i 0) | |
| 325 ; (aset x (1- i) (purecopy (aref x (1- i)))) | |
| 326 ; (setq i (1- i)))) | |
| 327 ; x) | |
| 328 ; ((consp x) | |
| 329 ; (let ((rest x)) | |
| 330 ; (while rest | |
| 331 ; (setcar rest (purecopy-menubar (car rest))) | |
| 332 ; (setq rest (cdr rest)))) | |
| 333 ; x) | |
| 334 ; (t | |
| 335 ; (purecopy x)))) | |
| 428 | 336 |
| 337 (defun delete-menu-item (path &optional from-menu) | |
| 338 "Remove the named menu item from the menu hierarchy. | |
| 444 | 339 PATH is a list of strings which identify the position of the menu item |
| 340 in the menu hierarchy. The documentation of `add-submenu' describes | |
| 341 menu paths. | |
| 342 FROM-MENU, if provided, means use that instead of `current-menubar' | |
| 343 as the menu to change." | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
344 (let* ((menubar (or from-menu current-menubar)) |
|
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
345 (pair (condition-case nil (find-menu-item menubar path) |
| 428 | 346 (error nil))) |
| 347 (item (car pair)) | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
348 (parent (or (cdr pair) menubar))) |
| 428 | 349 (if (not item) |
| 350 nil | |
|
5780
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
351 (if (eq item (car menubar)) ; Deleting first item from a top-level menubar |
|
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
352 (progn |
|
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
353 (setcar menubar (car (cdr menubar))) |
|
580ebed3500a
Bug fix in menubar.el
Mike Sperber <sperber@deinprogramm.de>
parents:
5715
diff
changeset
|
354 (setcdr menubar (cdr (cdr menubar)))) |
|
5652
cc6f0266bc36
Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents:
5645
diff
changeset
|
355 (delete* item parent)) |
| 428 | 356 (set-menubar-dirty-flag) |
| 357 item))) | |
| 358 | |
| 359 (defun relabel-menu-item (path new-name) | |
| 360 "Change the string of the specified menu item. | |
| 444 | 361 PATH is a list of strings which identify the position of the menu item in |
| 428 | 362 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" |
| 444 | 363 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the |
| 428 | 364 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\". |
| 365 NEW-NAME is the string that the menu item will be printed as from now on." | |
| 446 | 366 (check-type new-name string) |
| 428 | 367 (let* ((menubar current-menubar) |
| 368 (pair (find-menu-item menubar path)) | |
| 369 (item (car pair)) | |
| 370 (menu (cdr pair))) | |
| 371 (or item | |
| 372 (signal 'error (list (if menu (gettext "No such menu item") | |
| 373 (gettext "No such menu")) | |
| 374 path))) | |
| 375 (if (and (consp item) | |
| 376 (stringp (car item))) | |
| 377 (setcar item new-name) | |
| 378 (aset item 0 new-name)) | |
| 379 (set-menubar-dirty-flag) | |
| 380 item)) | |
| 381 | |
| 382 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
| 383 ;; | |
| 384 ;; these are all bad style. Why in the world would we put evaluable forms | |
| 385 ;; into the menubar if we didn't want people to use 'em? | |
| 386 ;; x-font-menu.el is the only known offender right now and that ought to be | |
| 387 ;; rehashed a bit. | |
| 444 | 388 ;; |
| 428 | 389 |
| 390 (defun enable-menu-item-1 (path toggle-p on-p) | |
| 391 (let (menu item) | |
| 392 (if (and (vectorp path) (> (length path) 2)) ; limited syntax checking... | |
| 393 (setq item path) | |
| 394 (let* ((menubar current-menubar) | |
| 395 (pair (find-menu-item menubar path))) | |
| 396 (setq item (car pair) | |
| 397 menu (cdr pair)) | |
| 398 (or item | |
| 399 (signal 'error (list (if menu | |
| 400 "No such menu item" | |
| 401 "No such menu") | |
| 402 path))) | |
| 403 (if (consp item) | |
| 404 (error "%S is a menu, not a menu item" path)))) | |
| 405 (if (or (> (length item) 4) | |
| 406 (and (symbolp (aref item 2)) | |
| 407 (= ?: (aref (symbol-name (aref item 2)) 0)))) | |
| 408 ;; plist-like syntax | |
| 409 (let ((i 2) | |
| 410 (keyword (if toggle-p :selected :active)) | |
| 411 (ok nil)) | |
| 412 (while (< i (length item)) | |
| 413 (cond ((eq (aref item i) keyword) | |
| 414 (aset item (1+ i) on-p) | |
| 415 (setq ok t))) | |
| 416 (setq i (+ i 2))) | |
| 417 (cond (ok nil) | |
| 418 (toggle-p | |
| 419 (signal 'error (list "not a toggle menu item" item))) | |
| 420 (t | |
| 421 ;; Need to copy the item to extend it, sigh... | |
| 422 (let ((cons (memq item menu)) | |
| 423 (new-item (vconcat item (list keyword on-p)))) | |
| 424 (if cons | |
| 425 (setcar cons (setq item new-item)) | |
| 426 (if menu | |
| 427 (error "couldn't find %S on its parent?" item) | |
| 428 (error "no %S slot to set: %S" keyword item))))))) | |
| 429 ;; positional syntax | |
| 430 (if toggle-p | |
| 431 (signal 'error (list "not a toggle menu item" item)) | |
| 432 (aset item 2 on-p))) | |
| 433 (set-menubar-dirty-flag) | |
| 434 item)) | |
| 435 | |
| 436 (defun enable-menu-item (path) | |
| 437 "Make the named menu item be selectable. | |
| 444 | 438 PATH is a list of strings which identify the position of the menu item in |
| 428 | 439 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" |
| 444 | 440 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the |
| 428 | 441 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"." |
| 442 (enable-menu-item-1 path nil t)) | |
| 443 | |
| 444 (defun disable-menu-item (path) | |
| 445 "Make the named menu item be unselectable. | |
| 444 | 446 PATH is a list of strings which identify the position of the menu item in |
| 428 | 447 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" |
| 444 | 448 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the |
| 428 | 449 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"." |
| 450 (enable-menu-item-1 path nil nil)) | |
| 451 | |
| 452 (defun select-toggle-menu-item (path) | |
| 453 "Make the named toggle- or radio-style menu item be in the `selected' state. | |
| 444 | 454 PATH is a list of strings which identify the position of the menu item in |
| 428 | 455 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" |
| 444 | 456 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the |
| 428 | 457 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"." |
| 458 (enable-menu-item-1 path t t)) | |
| 459 | |
| 460 (defun deselect-toggle-menu-item (path) | |
| 461 "Make the named toggle- or radio-style menu item be in the `unselected' state. | |
| 444 | 462 PATH is a list of strings which identify the position of the menu item in |
| 428 | 463 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" |
| 444 | 464 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the |
| 428 | 465 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"." |
| 466 (enable-menu-item-1 path t nil)) | |
| 467 | |
| 468 | |
| 2545 | 469 ;;; functions for manipulating whole menus -- adding accelerators, sorting, |
| 470 ;;; splitting long menus, etc. | |
| 471 | |
| 472 (defun submenu-generate-accelerator-spec (list &optional omit-chars-list) | |
| 473 "Add auto-generated accelerator specifications to a submenu. | |
| 474 This can be used to add accelerators to the return value of a menu filter | |
| 475 function. It correctly ignores unselectable items. It will destructively | |
| 476 modify the list passed to it. If an item already has an auto-generated | |
| 477 accelerator spec, this will be removed before the new one is added, making | |
| 478 this function idempotent. | |
| 479 | |
| 480 If OMIT-CHARS-LIST is given, it should be a list of lowercase characters, | |
| 481 which will not be used as accelerators." | |
| 482 (let ((n 0)) | |
| 483 (dolist (item list list) | |
| 484 (cond | |
| 485 ((or (vectorp item) (consp item)) | |
| 486 (incf n) | |
| 487 (setf (elt item 0) | |
| 488 (concat | |
| 489 (menu-item-generate-accelerator-spec n omit-chars-list) | |
| 490 (menu-item-strip-accelerator-spec (elt item 0))))))))) | |
| 491 | |
| 492 (defun menu-item-strip-accelerator-spec (item) | |
| 493 "Strip an auto-generated accelerator spec off of ITEM. | |
| 494 ITEM should be a string. This removes specs added by | |
| 495 `menu-item-generate-accelerator-spec' and `submenu-generate-accelerator-spec'." | |
| 496 (if (string-match "%_. " item) | |
| 497 (substring item 4) | |
| 498 item)) | |
| 499 | |
| 500 (defun menu-item-generate-accelerator-spec (n &optional omit-chars-list) | |
| 501 "Return an accelerator specification for use with auto-generated menus. | |
| 502 This should be concat'd onto the beginning of each menu line. The spec | |
| 503 allows the Nth line to be selected by the number N. '0' is used for the | |
| 504 10th line, and 'a' through 'z' are used for the following 26 lines. | |
| 505 | |
| 506 If OMIT-CHARS-LIST is given, it should be a list of lowercase characters, | |
| 507 which will not be used as accelerators." | |
| 508 (cond ((< n 10) (concat "%_" (int-to-string n) " ")) | |
| 509 ((= n 10) "%_0 ") | |
| 510 ((<= n 36) | |
| 511 (setq n (- n 10)) | |
| 512 (let ((m 0)) | |
| 513 (while (> n 0) | |
| 514 (setq m (1+ m)) | |
| 515 (while (memq (int-to-char (+ m (- (char-to-int ?a) 1))) | |
| 516 omit-chars-list) | |
| 517 (setq m (1+ m))) | |
| 518 (setq n (1- n))) | |
| 519 (if (<= m 26) | |
| 520 (concat | |
| 521 "%_" | |
| 522 (char-to-string (int-to-char (+ m (- (char-to-int ?a) 1)))) | |
| 523 " ") | |
| 524 ""))) | |
| 525 (t ""))) | |
| 526 | |
| 527 (defcustom menu-max-items 25 | |
| 528 "*Maximum number of items in generated menus. | |
| 529 If number of entries in such a menu is larger than this value, split menu | |
| 530 into submenus of nearly equal length (see `menu-submenu-max-items'). If | |
| 531 nil, never split menu into submenus." | |
| 532 :group 'menu | |
| 533 :type '(choice (const :tag "no submenus" nil) | |
| 534 (integer))) | |
| 535 | |
| 536 (defcustom menu-submenu-max-items 20 | |
| 537 "*Maximum number of items in submenus when splitting menus. | |
| 538 We split large menus into submenus of this many items, and then balance | |
| 539 them out as much as possible (otherwise the last submenu may have very few | |
| 540 items)." | |
| 541 :group 'menu | |
| 542 :type 'integer) | |
| 543 | |
| 544 (defcustom menu-submenu-name-format "%-12.12s ... %.12s" | |
| 545 "*Format specification of the submenu name when splitting menus. | |
| 546 Used by `menu-split-long-menu' if the number of entries in a menu is | |
| 547 larger than `menu-menu-max-items'. | |
| 548 This string should contain one %s for the name of the first entry and | |
| 549 one %s for the name of the last entry in the submenu. | |
| 550 If the value is a function, it should return the submenu name. The | |
| 551 function is be called with two arguments, the names of the first and | |
| 552 the last entry in the menu." | |
| 553 :group 'menu | |
| 554 :type '(choice (string :tag "Format string") | |
| 555 (function))) | |
| 556 | |
| 557 (defun menu-split-long-menu-and-sort (menu) | |
| 558 "Sort MENU, split according to `menu-max-items' and add accelerator specs. | |
| 559 This is useful for menus generated by filter functions, to make them look | |
| 560 nice. This is equivalent to | |
| 561 | |
| 562 \(menu-split-long-menu (menu-sort-menu menu)) | |
| 563 | |
| 564 and you can call those functions individually if necessary. | |
| 565 You can also call `submenu-generate-accelerator-spec' yourself to add | |
| 566 accelerator specs -- this works even if the specs have already been added." | |
| 567 (menu-split-long-menu (menu-sort-menu menu))) | |
| 568 | |
| 569 (defun menu-split-long-menu (menu) | |
| 570 "Split MENU according to `menu-max-items' and add accelerator specs. | |
| 571 If MENU already has accelerator specs, they will be removed and new ones | |
| 572 generated. You should normally use `menu-split-long-menu-and-sort' instead. | |
| 573 The menu should already be sorted to get meaningful results when it is | |
| 574 split, since the outer menus are of the format `FROM ... TO'." | |
| 575 (let ((len (length menu))) | |
| 576 (if (or (null menu-max-items) | |
| 577 (<= len menu-max-items)) | |
| 578 (submenu-generate-accelerator-spec menu) | |
| 579 (let* ((outer (/ (+ len (1- menu-submenu-max-items)) | |
| 580 menu-submenu-max-items)) | |
| 581 (inner (/ (+ len (1- outer)) outer)) | |
| 582 (result nil)) | |
| 583 (while menu | |
| 584 (let ((sub nil) | |
| 585 (from (car menu))) | |
| 586 (dotimes (foo (min inner len)) | |
| 587 (setq sub (cons (car menu) sub) | |
| 588 menu (cdr menu))) | |
| 589 (setq len (- len inner)) | |
| 590 (let* ((to (car sub)) | |
| 591 (ftext (menu-item-strip-accelerator-spec | |
| 592 (menu-item-text from))) | |
| 593 (ttext (menu-item-strip-accelerator-spec | |
| 594 (menu-item-text to)))) | |
| 595 (setq sub (nreverse sub)) | |
| 596 (setq result | |
| 597 (cons (cons (if (stringp menu-submenu-name-format) | |
| 598 (format menu-submenu-name-format | |
| 599 ftext ttext) | |
| 600 (funcall menu-submenu-name-format | |
| 601 ftext ttext)) | |
| 602 (submenu-generate-accelerator-spec sub)) | |
| 603 result))))) | |
| 604 (submenu-generate-accelerator-spec (nreverse result)))))) | |
| 605 | |
| 606 (defun menu-sort-menu (menu) | |
| 607 "Sort MENU alphabetically. | |
| 608 You should normally use `menu-split-long-menu-and-sort' instead." | |
| 609 (sort menu | |
| 610 #'(lambda (a b) (< (compare-menu-text | |
| 611 (menu-item-text a) (menu-item-text b)) | |
| 612 0)))) | |
| 613 | |
| 442 | 614 |
| 615 ;;;;;;; popup menus | |
| 616 | |
| 617 (defvar global-popup-menu nil | |
| 618 "The global popup menu. This is present in all modes. | |
| 619 See the function `popup-menu' for a description of menu syntax.") | |
| 620 | |
| 621 (defvar mode-popup-menu nil | |
| 622 "The mode-specific popup menu. Automatically buffer local. | |
| 623 This is appended to the default items in `global-popup-menu'. | |
| 624 See the function `popup-menu' for a description of menu syntax.") | |
| 625 (make-variable-buffer-local 'mode-popup-menu) | |
| 626 | |
| 627 (defvar activate-popup-menu-hook nil | |
| 628 "Function or functions run before a mode-specific popup menu is made visible. | |
| 629 These functions are called with no arguments, and should interrogate and | |
| 630 modify the value of `global-popup-menu' or `mode-popup-menu' as desired. | |
| 631 Note: this hook is only run if you use `popup-mode-menu' for activating the | |
| 632 global and mode-specific commands; if you have your own binding for button3, | |
| 633 this hook won't be run.") | |
| 634 | |
| 635 (defvar last-popup-menu-event nil | |
| 636 "The mouse event that invoked the last popup menu. | |
| 637 NOTE: This is EXPERIMENTAL and may change at any time.") | |
| 638 | |
| 639 (defun popup-mode-menu (&optional event) | |
| 640 "Pop up a menu of global and mode-specific commands. | |
| 641 The menu is computed by combining `global-popup-menu' and `mode-popup-menu' | |
| 642 with any items derived from the `context-menu' property of the extent where the | |
| 643 button was clicked." | |
| 644 (interactive "_e") | |
| 645 (setq last-popup-menu-event | |
| 646 (or (and event (button-event-p event) event) | |
| 647 (let* ((mouse-pos (mouse-position)) | |
| 648 (win (car mouse-pos)) | |
| 649 (x (cadr mouse-pos)) | |
| 650 (y (cddr mouse-pos)) | |
| 651 (edges (window-pixel-edges win)) | |
| 652 (winx (first edges)) | |
| 653 (winy (second edges)) | |
| 654 (x (+ x winx)) | |
| 655 (y (+ y winy))) | |
| 656 (make-event 'button-press | |
| 657 `(button 3 x ,x y ,y channel ,(window-frame win) | |
| 658 timestamp ,(current-event-timestamp | |
| 659 (cdfw-console win))))))) | |
| 660 (run-hooks 'activate-popup-menu-hook) | |
| 661 (let* ((context-window (and event (event-window event))) | |
| 662 (context-point (and event (event-point event))) | |
| 663 (context-extents (and context-window | |
| 664 context-point | |
| 665 (extents-at context-point | |
| 666 (window-buffer context-window) | |
| 667 'context-menu))) | |
| 668 (context-menu-items | |
| 669 (apply 'append (mapcar #'(lambda (extent) | |
| 670 (extent-property extent 'context-menu)) | |
| 671 context-extents)))) | |
| 672 (popup-menu | |
| 462 | 673 (progn |
| 442 | 674 ;; Merge global-popup-menu and mode-popup-menu |
| 462 | 675 (and mode-popup-menu (check-menu-syntax mode-popup-menu)) |
| 676 (let* ((mode-title (and (stringp (car mode-popup-menu)) | |
| 677 (car mode-popup-menu))) | |
| 678 (mode-items (if mode-title (cdr mode-popup-menu) | |
| 679 mode-popup-menu)) | |
| 680 (global-title (and (stringp (car global-popup-menu)) | |
| 681 (car global-popup-menu))) | |
| 682 (global-items (if global-title (cdr global-popup-menu) | |
| 683 global-popup-menu)) | |
| 442 | 684 mode-filters) |
| 685 ;; Strip keywords from local menu for attaching them at the top | |
| 462 | 686 (while (and mode-items |
| 687 (keywordp (car mode-items))) | |
| 442 | 688 ;; Push both keyword and its argument. |
| 462 | 689 (push (pop mode-items) mode-filters) |
| 690 (push (pop mode-items) mode-filters)) | |
| 442 | 691 (setq mode-filters (nreverse mode-filters)) |
| 692 ;; If mode-filters contains a keyword already present in | |
| 693 ;; `global-popup-menu', you will probably lose. | |
| 462 | 694 (append (and popup-menu-titles |
| 695 (cond (mode-title (list mode-title)) | |
| 696 (global-title (list global-title)) | |
| 697 (t ""))) | |
| 442 | 698 mode-filters |
| 462 | 699 context-menu-items |
| 700 (and context-menu-items mode-items '("---")) | |
| 701 mode-items | |
| 702 (and (or context-menu-items mode-items) | |
| 703 global-items '("---" "---")) | |
| 704 (and global-title (list global-title)) | |
| 705 global-items | |
| 706 )))) | |
| 442 | 707 |
| 708 (while (popup-up-p) | |
| 709 (dispatch-event (next-event))) | |
| 710 | |
| 711 )) | |
| 444 | 712 |
| 442 | 713 (defun popup-buffer-menu (event) |
| 502 | 714 "Pop up a copy of the menubar Buffers menu where the mouse is clicked." |
| 442 | 715 (interactive "e") |
| 716 (let ((window (and (event-over-text-area-p event) (event-window event))) | |
| 717 (bmenu nil)) | |
| 718 (or window | |
| 719 (error "Pointer must be in a normal window")) | |
| 720 (select-window window) | |
| 721 (if current-menubar | |
| 722 (setq bmenu (assoc "%_Buffers" current-menubar))) | |
| 723 (if (null bmenu) | |
| 724 (setq bmenu (assoc "%_Buffers" default-menubar))) | |
| 725 (if (null bmenu) | |
| 726 (error "Can't find the Buffers menu")) | |
| 727 (popup-menu bmenu))) | |
| 728 | |
| 729 (defun popup-menubar-menu (event) | |
| 730 "Pop up a copy of menu that also appears in the menubar." | |
| 731 (interactive "e") | |
| 732 (let ((window (and (event-over-text-area-p event) (event-window event))) | |
| 733 popup-menubar) | |
| 734 (or window | |
| 735 (error "Pointer must be in a normal window")) | |
| 736 (select-window window) | |
| 737 (and current-menubar (run-hooks 'activate-menubar-hook)) | |
| 738 ;; #### Instead of having to copy this just to safely get rid of | |
| 739 ;; any nil what we should really do is fix up the internal menubar | |
| 740 ;; code to just ignore nil if generating a popup menu | |
| 741 (setq popup-menubar (delete nil (copy-sequence (or current-menubar | |
| 742 default-menubar)))) | |
| 743 (popup-menu (cons "%_Menubar Menu" popup-menubar)) | |
| 744 )) | |
| 745 | |
| 746 (defun menu-call-at-event (form &optional event default-behavior-fallback) | |
| 747 "Call FORM while temporarily setting point to the position in EVENT. | |
| 748 NOTE: This is EXPERIMENTAL and may change at any time. | |
| 749 | |
| 750 FORM is called the way forms in menu specs are: i.e. if a symbol, it's called | |
| 751 with `call-interactively', otherwise with `eval'. EVENT defaults to | |
| 752 `last-popup-menu-event', making this function especially useful in popup | |
| 753 menus. The buffer and point are set temporarily within a `save-excursion'. | |
| 754 If EVENT is not a mouse event, or was not over a buffer, nothing | |
| 755 happens unless DEFAULT-BEHAVIOR-FALLBACK is non-nil, in which case the | |
| 756 FORM is called normally." | |
| 757 (or event (setq event last-popup-menu-event)) | |
| 758 (let ((buf (event-buffer event)) | |
| 759 (p (event-closest-point event))) | |
| 760 (cond ((and buf p (> p 0)) | |
| 761 (save-excursion | |
| 762 (set-buffer buf) | |
| 763 (goto-char p) | |
| 764 (if (symbolp form) | |
| 765 (call-interactively form) | |
| 766 (eval form)))) | |
| 767 (default-behavior-fallback | |
| 768 (if (symbolp form) | |
| 769 (call-interactively form) | |
| 770 (eval form)))))) | |
| 771 | |
| 772 (global-set-key 'button3 'popup-mode-menu) | |
| 773 ;; shift button3 and shift button2 are reserved for Hyperbole | |
| 774 (global-set-key '(meta control button3) 'popup-buffer-menu) | |
| 775 ;; The following command is way too dangerous with Custom. | |
| 776 ;; (global-set-key '(meta shift button3) 'popup-menubar-menu) | |
| 777 | |
| 778 ;; Here's a test of the cool new menu features (from Stig). | |
| 779 | |
| 780 ;;(setq mode-popup-menu | |
| 781 ;; '("Test Popup Menu" | |
| 782 ;; :filter cdr | |
| 783 ;; ["this item won't appear because of the menu filter" ding t] | |
| 784 ;; "--:singleLine" | |
| 785 ;; "singleLine" | |
| 786 ;; "--:doubleLine" | |
| 787 ;; "doubleLine" | |
| 788 ;; "--:singleDashedLine" | |
| 789 ;; "singleDashedLine" | |
| 790 ;; "--:doubleDashedLine" | |
| 791 ;; "doubleDashedLine" | |
| 792 ;; "--:noLine" | |
| 793 ;; "noLine" | |
| 794 ;; "--:shadowEtchedIn" | |
| 795 ;; "shadowEtchedIn" | |
| 796 ;; "--:shadowEtchedOut" | |
| 797 ;; "shadowEtchedOut" | |
| 798 ;; "--:shadowDoubleEtchedIn" | |
| 799 ;; "shadowDoubleEtchedIn" | |
| 800 ;; "--:shadowDoubleEtchedOut" | |
| 801 ;; "shadowDoubleEtchedOut" | |
| 802 ;; "--:shadowEtchedInDash" | |
| 803 ;; "shadowEtchedInDash" | |
| 804 ;; "--:shadowEtchedOutDash" | |
| 805 ;; "shadowEtchedOutDash" | |
| 806 ;; "--:shadowDoubleEtchedInDash" | |
| 807 ;; "shadowDoubleEtchedInDash" | |
| 808 ;; "--:shadowDoubleEtchedOutDash" | |
| 809 ;; "shadowDoubleEtchedOutDash" | |
| 810 ;; )) | |
| 811 | |
| 428 | 812 (defun get-popup-menu-response (menu-desc &optional event) |
| 813 "Pop up the given menu and wait for a response. | |
| 814 This blocks until the response is received, and returns the misc-user | |
| 815 event that encapsulates the response. To execute it, you can do | |
| 816 (funcall (event-function response) (event-object response)) | |
| 817 If no response was received, nil is returned. | |
| 818 | |
| 819 MENU-DESC and EVENT are as in the call to `popup-menu'." | |
| 820 ;; partially stolen from w3 | |
| 707 | 821 |
| 822 ;; This function is way gross and assumes to much about menu | |
| 823 ;; processing that is X specific. Under mswindows popup menus behave | |
| 824 ;; in reasonable ways that you can't obstruct. | |
| 428 | 825 (let ((echo-keystrokes 0) |
| 826 new-event) | |
| 827 (popup-menu menu-desc event) | |
| 828 (catch 'popup-done | |
| 829 (while t | |
| 830 (setq new-event (next-command-event new-event)) | |
| 831 (cond ((misc-user-event-p new-event) | |
| 832 (throw 'popup-done new-event)) | |
| 707 | 833 ((button-release-event-p new-event);; don't beep twice |
| 834 nil) | |
| 835 ;; It shows how bogus this function is that the event | |
| 836 ;; arg could be missing and no-one noticed ... | |
| 837 ((event-matches-key-specifier-p new-event (quit-char)) | |
| 838 (signal 'quit nil)) | |
| 839 ;; mswindows has no pop-down processing (selection is | |
| 840 ;; atomic) so doing anything more makes no sense. Since | |
| 841 ;; popup-up-p is always false under mswindows, this | |
| 842 ;; function has been ordered to do essentially X-specifc | |
| 843 ;; processing after this check. | |
| 844 ((not (popup-up-p)) | |
| 428 | 845 (setq unread-command-events (cons new-event |
| 846 unread-command-events)) | |
| 847 (throw 'popup-done nil)) | |
| 707 | 848 ;; mswindows never gets here |
| 428 | 849 (t |
| 850 (beep) | |
| 851 (message "please make a choice from the menu."))))))) | |
| 852 | |
| 853 (defun popup-menu-and-execute-in-window (menu-desc event) | |
| 854 "Pop up the given menu and execute its response in EVENT's window. | |
| 855 This blocks until the response is received, temporarily selects | |
| 856 EVENT's window, and executes the command specified in the response. | |
| 857 EVENT can also be a window. See `popup-menu' for the semantics of | |
| 858 MENU-DESC." | |
| 859 (let ((response | |
| 860 (get-popup-menu-response menu-desc | |
| 861 (and (eventp event) event)))) | |
| 862 (and (misc-user-event-p response) | |
| 863 (save-selected-window | |
| 864 (select-window (if (windowp event) event | |
| 865 (event-window event))) | |
| 866 (funcall (event-function response) | |
| 867 (event-object response)))))) | |
| 868 | |
| 869 ;; provide default bindings for menu accelerator map | |
| 870 (and (boundp 'menu-accelerator-map) | |
| 871 (keymapp menu-accelerator-map) | |
| 872 (progn | |
| 873 (define-key menu-accelerator-map "\e" 'menu-escape) | |
| 874 (define-key menu-accelerator-map [left] 'menu-left) | |
| 875 (define-key menu-accelerator-map [right] 'menu-right) | |
| 876 (define-key menu-accelerator-map [up] 'menu-up) | |
| 877 (define-key menu-accelerator-map [down] 'menu-down) | |
| 878 (define-key menu-accelerator-map [return] 'menu-select) | |
| 502 | 879 (define-key menu-accelerator-map [kp-down] 'menu-down) |
| 880 (define-key menu-accelerator-map [kp-up] 'menu-down) | |
| 881 (define-key menu-accelerator-map [kp-left] 'menu-left) | |
| 882 (define-key menu-accelerator-map [kp-right] 'menu-right) | |
| 883 (define-key menu-accelerator-map [kp-enter] 'menu-select) | |
| 428 | 884 (define-key menu-accelerator-map "\C-g" 'menu-quit))) |
| 885 | |
| 886 | |
| 887 (provide 'menubar) | |
| 888 | |
| 889 ;;; menubar.el ends here |
