diff options
| author | Richard M. Stallman | 1997-01-02 20:20:22 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1997-01-02 20:20:22 +0000 |
| commit | e6a6d6979915d526e72d2590dbf70cd742acb134 (patch) | |
| tree | 31ef322650dd713546cc2c5314de1846573b0df9 | |
| parent | ec3fac5e7899603f762782c7b08932e83724a515 (diff) | |
| download | emacs-e6a6d6979915d526e72d2590dbf70cd742acb134.tar.gz emacs-e6a6d6979915d526e72d2590dbf70cd742acb134.zip | |
(easy-menu-create-keymaps): Menu item STYLE toggle (checkbox)
and radio (radio button) are prefixed by "[X] " or
"(*) " respectively, when selected and "[ ] " or "( ) ", when not
selected. In a menu that contain these prefixes, " " is used
as prefix for items that have no other prefix.
(easy-menu-update-button): New function used as `menu-enable'
property for checkboxes and radio buttons to update the prefixes.
(easy-menu-define): Change documentation string to describe the
new prefixes.
| -rw-r--r-- | lisp/emacs-lisp/easymenu.el | 92 |
1 files changed, 64 insertions, 28 deletions
diff --git a/lisp/emacs-lisp/easymenu.el b/lisp/emacs-lisp/easymenu.el index 624dd0b8363..b172e131763 100644 --- a/lisp/emacs-lisp/easymenu.el +++ b/lisp/emacs-lisp/easymenu.el | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | ;;; easymenu.el --- support the easymenu interface for defining a menu. | 1 | ;;; easymenu.el --- support the easymenu interface for defining a menu. |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1994 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 1994, 1996 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | ;; Keywords: emulations | 5 | ;; Keywords: emulations |
| 6 | ;; Author: rms | 6 | ;; Author: rms |
| @@ -75,16 +75,16 @@ 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 | 75 | STYLE is a symbol describing the type of menu item. The following are |
| 76 | defined: | 76 | defined: |
| 77 | 77 | ||
| 78 | toggle: A checkbox. | 78 | toggle: A checkbox. |
| 79 | Currently just prepend the name with the string \"Toggle \". | 79 | Prepend the name with '(*) ' or '( ) ' depending on if selected or not. |
| 80 | radio: A radio button. | 80 | radio: A radio button. |
| 81 | Prepend the name with '[X] ' or '[ ] ' depending on if selected or not. | ||
| 81 | nil: An ordinary menu item. | 82 | nil: An ordinary menu item. |
| 82 | 83 | ||
| 83 | :selected SELECTED | 84 | :selected SELECTED |
| 84 | 85 | ||
| 85 | SELECTED is an expression; the checkbox or radio button is selected | 86 | SELECTED is an expression; the checkbox or radio button is selected |
| 86 | whenever this expression's value is non-nil. | 87 | whenever this expression's value is non-nil. |
| 87 | Currently just disable radio buttons, no effect on checkboxes. | ||
| 88 | 88 | ||
| 89 | A menu item can be a string. Then that string appears in the menu as | 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 | 90 | unselectable text. A string consisting solely of hyphens is displayed |
| @@ -118,25 +118,26 @@ is a list of menu items, as above." | |||
| 118 | ;; MENU-ITEMS, and with name MENU-NAME. | 118 | ;; MENU-ITEMS, and with name MENU-NAME. |
| 119 | ;;;###autoload | 119 | ;;;###autoload |
| 120 | (defun easy-menu-create-keymaps (menu-name menu-items) | 120 | (defun easy-menu-create-keymaps (menu-name menu-items) |
| 121 | (let ((menu (make-sparse-keymap menu-name))) | 121 | (let ((menu (make-sparse-keymap menu-name)) old-items have-buttons) |
| 122 | ;; Process items in reverse order, | 122 | ;; Process items in reverse order, |
| 123 | ;; since the define-key loop reverses them again. | 123 | ;; since the define-key loop reverses them again. |
| 124 | (setq menu-items (reverse menu-items)) | 124 | (setq menu-items (reverse menu-items)) |
| 125 | (while menu-items | 125 | (while menu-items |
| 126 | (let* ((item (car menu-items)) | 126 | (let* ((item (car menu-items)) |
| 127 | (callback (if (vectorp item) (aref item 1))) | 127 | (callback (if (vectorp item) (aref item 1))) |
| 128 | command enabler name) | 128 | (not-button t) |
| 129 | command enabler item-string name) | ||
| 129 | (cond ((stringp item) | 130 | (cond ((stringp item) |
| 130 | (setq command nil) | 131 | (setq command nil) |
| 131 | (setq name (if (string-match "^-+$" item) "" item))) | 132 | (setq item-string (if (string-match "^-+$" item) "" item))) |
| 132 | ((consp item) | 133 | ((consp item) |
| 133 | (setq command (easy-menu-create-keymaps (car item) (cdr item))) | 134 | (setq command (easy-menu-create-keymaps (car item) (cdr item))) |
| 134 | (setq name (car item))) | 135 | (setq name (setq item-string (car item)))) |
| 135 | ((vectorp item) | 136 | ((vectorp item) |
| 136 | (setq command (make-symbol (format "menu-function-%d" | 137 | (setq command (make-symbol (format "menu-function-%d" |
| 137 | easy-menu-item-count))) | 138 | easy-menu-item-count))) |
| 138 | (setq easy-menu-item-count (1+ easy-menu-item-count)) | 139 | (setq easy-menu-item-count (1+ easy-menu-item-count)) |
| 139 | (setq name (aref item 0)) | 140 | (setq name (setq item-string (aref item 0))) |
| 140 | (let ((keyword (aref item 2))) | 141 | (let ((keyword (aref item 2))) |
| 141 | (if (and (symbolp keyword) | 142 | (if (and (symbolp keyword) |
| 142 | (= ?: (aref (symbol-name keyword) 0))) | 143 | (= ?: (aref (symbol-name keyword) 0))) |
| @@ -152,26 +153,40 @@ is a list of menu items, as above." | |||
| 152 | ((eq keyword ':active) | 153 | ((eq keyword ':active) |
| 153 | (setq active arg)) | 154 | (setq active arg)) |
| 154 | ((eq keyword ':suffix) | 155 | ((eq keyword ':suffix) |
| 155 | (setq name (concat name " " arg))) | 156 | (setq item-string |
| 157 | (concat item-string " " arg))) | ||
| 156 | ((eq keyword ':style) | 158 | ((eq keyword ':style) |
| 157 | (setq style arg)) | 159 | (setq style arg)) |
| 158 | ((eq keyword ':selected) | 160 | ((eq keyword ':selected) |
| 159 | (setq selected arg)))) | 161 | (setq selected arg)))) |
| 160 | (if keys | 162 | (if keys |
| 161 | (setq name (concat name " (" keys ")"))) | 163 | (setq item-string |
| 162 | (if (eq style 'toggle) | 164 | (concat item-string " (" keys ")"))) |
| 163 | ;; Simulate checkboxes. | 165 | (if (and selected |
| 164 | (setq name (concat "Toggle " name))) | 166 | (or (eq style 'radio) (eq style 'toggle))) |
| 165 | (if active | 167 | ;; Simulate checkboxes and radio buttons. |
| 166 | (put command 'menu-enable active) | 168 | (progn |
| 167 | (and (eq style 'radio) | 169 | (setq item-string |
| 168 | selected | 170 | (concat |
| 169 | ;; Simulate radio buttons with menu-enable. | 171 | (if (eval selected) |
| 170 | (put command 'menu-enable | 172 | (if (eq style 'radio) "(*) " "[X] ") |
| 171 | (list 'not selected))))) | 173 | (if (eq style 'radio) "( ) " "[ ] ")) |
| 174 | item-string)) | ||
| 175 | (put command 'menu-enable | ||
| 176 | (list 'easy-menu-update-button | ||
| 177 | item-string | ||
| 178 | (if (eq style 'radio) ?* ?X) | ||
| 179 | selected | ||
| 180 | (or active t))) | ||
| 181 | (setq not-button nil | ||
| 182 | active nil | ||
| 183 | have-buttons t) | ||
| 184 | (while old-items ; Fix items aleady defined. | ||
| 185 | (setcar (car old-items) | ||
| 186 | (concat " " (car (car old-items)))) | ||
| 187 | (setq old-items (cdr old-items))))) | ||
| 188 | (if active (put command 'menu-enable active))) | ||
| 172 | (put command 'menu-enable keyword))) | 189 | (put command 'menu-enable keyword))) |
| 173 | (if (keymapp callback) | ||
| 174 | (setq name (concat name " ..."))) | ||
| 175 | (if (symbolp callback) | 190 | (if (symbolp callback) |
| 176 | (fset command callback) | 191 | (fset command callback) |
| 177 | (fset command (list 'lambda () '(interactive) callback))) | 192 | (fset command (list 'lambda () '(interactive) callback))) |
| @@ -179,19 +194,40 @@ is a list of menu items, as above." | |||
| 179 | (if (null command) | 194 | (if (null command) |
| 180 | ;; Handle inactive strings specially--allow any number | 195 | ;; Handle inactive strings specially--allow any number |
| 181 | ;; of identical ones. | 196 | ;; of identical ones. |
| 182 | (setcdr menu (cons (list nil name) (cdr menu))) | 197 | (setcdr menu (cons (list nil item-string) (cdr menu))) |
| 183 | (if name | 198 | (if (and not-button have-buttons) |
| 184 | (define-key menu (vector (intern name)) (cons name command))))) | 199 | (setq item-string (concat " " item-string))) |
| 200 | (setq command (cons item-string command)) | ||
| 201 | (if (not have-buttons) ; Save all items so that we can fix | ||
| 202 | (setq old-items (cons command old-items))) ; if we have buttons. | ||
| 203 | (if name (define-key menu (vector (intern name)) command)))) | ||
| 185 | (setq menu-items (cdr menu-items))) | 204 | (setq menu-items (cdr menu-items))) |
| 186 | menu)) | 205 | menu)) |
| 187 | 206 | ||
| 207 | (defun easy-menu-update-button (item ch selected active) | ||
| 208 | "Used as menu-enable property to update buttons. | ||
| 209 | A call to this function is used as the menu-enable property for buttons. | ||
| 210 | ITEM is the item-string into wich CH or ` ' is inserted depending on if | ||
| 211 | SELECTED is true or not. The menu entry in enabled iff ACTIVE is true." | ||
| 212 | (let ((new (if selected ch ? )) | ||
| 213 | (old (aref item 1))) | ||
| 214 | (if (eq new old) | ||
| 215 | ;; No change, just use the active value. | ||
| 216 | active | ||
| 217 | ;; It has changed. Update the entry. | ||
| 218 | (aset item 1 new) | ||
| 219 | ;; If the entry is active, make sure the menu gets updated by | ||
| 220 | ;; returning a different value than last time to cheat the cache. | ||
| 221 | (and active | ||
| 222 | (random))))) | ||
| 223 | |||
| 188 | (defun easy-menu-change (path name items) | 224 | (defun easy-menu-change (path name items) |
| 189 | "Change menu found at PATH as item NAME to contain ITEMS. | 225 | "Change menu found at PATH as item NAME to contain ITEMS. |
| 190 | PATH is a list of strings for locating the menu containing NAME in the | 226 | PATH is a list of strings for locating the menu containing NAME in the |
| 191 | menu bar. ITEMS is a list of menu items, as in `easy-menu-define'. | 227 | menu bar. ITEMS is a list of menu items, as in `easy-menu-define'. |
| 192 | These items entirely replace the previous items in that map. | 228 | These items entirely replace the previous items in that map. |
| 193 | 229 | ||
| 194 | Call this from `activate-menubar-hook' to implement dynamic menus." | 230 | Call this from `menu-bar-update-hook' to implement dynamic menus." |
| 195 | (let ((map (key-binding (apply 'vector | 231 | (let ((map (key-binding (apply 'vector |
| 196 | 'menu-bar | 232 | 'menu-bar |
| 197 | (mapcar 'intern (append path (list name))))))) | 233 | (mapcar 'intern (append path (list name))))))) |