diff options
| author | Richard M. Stallman | 1996-01-02 06:35:43 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1996-01-02 06:35:43 +0000 |
| commit | bdbc7685292cabc7e79ec5ec26689cd3aca32734 (patch) | |
| tree | 1f6f84a60753caec16e3278aa7bd483be95650dd | |
| parent | 77cc5db0c39e120c048b1eb30c6caf67c029fce1 (diff) | |
| download | emacs-bdbc7685292cabc7e79ec5ec26689cd3aca32734.tar.gz emacs-bdbc7685292cabc7e79ec5ec26689cd3aca32734.zip | |
(tmm-prompt): Major cleanups. Handle pop-menu case nicely.
Arg BIND renamed to MENU.
Look at MENU to decide whether it is a keymap.
Arg IN-POPUP now used only in recursive call.
Use "Menu bar" as the default menu name.
Delete some debugging code.
| -rw-r--r-- | lisp/tmm.el | 173 |
1 files changed, 98 insertions, 75 deletions
diff --git a/lisp/tmm.el b/lisp/tmm.el index 8ad75e03751..868b07b98a4 100644 --- a/lisp/tmm.el +++ b/lisp/tmm.el | |||
| @@ -105,91 +105,114 @@ marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel. | |||
| 105 | "What insert on top of completion buffer.") | 105 | "What insert on top of completion buffer.") |
| 106 | 106 | ||
| 107 | ;;;###autoload | 107 | ;;;###autoload |
| 108 | (defun tmm-prompt (bind &optional in-popup default-item) | 108 | (defun tmm-prompt (menu &optional in-popup default-item) |
| 109 | "Text-mode emulation of calling the bindings in keymap. | 109 | "Text-mode emulation of calling the bindings in keymap. |
| 110 | Creates a text-mode menu of possible choices. You can access the elements | 110 | Creates a text-mode menu of possible choices. You can access the elements |
| 111 | in the menu in two ways: | 111 | in the menu in two ways: |
| 112 | *) via history mechanism from minibuffer; | 112 | *) via history mechanism from minibuffer; |
| 113 | *) Or via completion-buffer that is automatically shown. | 113 | *) Or via completion-buffer that is automatically shown. |
| 114 | The last alternative is currently a hack, you cannot use mouse reliably. | 114 | The last alternative is currently a hack, you cannot use mouse reliably. |
| 115 | If the optional argument IN-POPUP is non-nil, it should compatible with | 115 | |
| 116 | `x-popup-menu', otherwise the argument BIND should be keymap." | 116 | MENU is like the MENU argument to `x-popup-menu': either a |
| 117 | (if in-popup (if bind (setq bind in-popup) (x-popup-menu nil in-popup))) | 117 | keymap or an alist of alists. |
| 118 | (let (gl-str tmm-km-list out history history-len tmm-table-undef tmm-c-prompt | 118 | DEFAULT-ITEM, if non-nil, specifies an initial default choice. |
| 119 | tmm-old-mb-map tmm-old-comp-map tmm-short-cuts) | 119 | Its value should be an event that has a binding in MENU." |
| 120 | ;; If the optional argument IN-POPUP is t, | ||
| 121 | ;; then MENU is an alist of elements of the form (STRING . VALUE). | ||
| 122 | ;; That is used for recursive calls only. | ||
| 123 | (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap | ||
| 124 | ; so it doesn't have a name. | ||
| 125 | tmm-km-list out history history-len tmm-table-undef tmm-c-prompt | ||
| 126 | tmm-old-mb-map tmm-old-comp-map tmm-short-cuts | ||
| 127 | chosen-string choice | ||
| 128 | (not-menu (not (keymapp menu)))) | ||
| 120 | (run-hooks 'activate-menubar-hook) | 129 | (run-hooks 'activate-menubar-hook) |
| 130 | ;; Compute tmm-km-list from MENU. | ||
| 131 | ;; tmm-km-list is an alist of (STRING . MEANING). | ||
| 132 | ;; It has no other elements. | ||
| 133 | ;; The order of elements in tmm-km-list is the order of the menu bar. | ||
| 121 | (mapcar (function (lambda (elt) | 134 | (mapcar (function (lambda (elt) |
| 122 | (if (stringp elt) | 135 | (if (stringp elt) |
| 123 | (setq gl-str elt) | 136 | (setq gl-str elt) |
| 124 | (and (listp elt) (tmm-get-keymap elt in-popup))))) | 137 | (and (listp elt) (tmm-get-keymap elt not-menu))))) |
| 125 | bind) | 138 | menu) |
| 126 | (setq foo default-item foo1 bind) | 139 | ;; Choose an element of tmm-km-list; put it in choice. |
| 127 | (and tmm-km-list | 140 | (if (and not-menu (= 1 (length tmm-km-list))) |
| 128 | (let ((index-of-default 0)) | 141 | ;; If this is the top-level of an x-popup-menu menu, |
| 129 | (if tmm-mid-prompt | 142 | ;; and there is just one pane, choose that one silently. |
| 130 | (setq tmm-km-list (tmm-add-shortcuts tmm-km-list)) | 143 | ;; This way we only ask the user one question, |
| 131 | t) | 144 | ;; for which element of that pane. |
| 132 | ;; Find the default item's index within the menu bar. | 145 | (setq choice (cdr (car tmm-km-list))) |
| 133 | ;; We use this to decide the initial minibuffer contents | 146 | (and tmm-km-list |
| 134 | ;; and initial history position. | 147 | (let ((index-of-default 0)) |
| 135 | (if default-item | 148 | (if tmm-mid-prompt |
| 136 | (let ((tail bind)) | 149 | (setq tmm-km-list (tmm-add-shortcuts tmm-km-list)) |
| 137 | (while (and tail | 150 | t) |
| 138 | (not (eq (car-safe (car tail)) default-item))) | 151 | ;; Find the default item's index within the menu bar. |
| 139 | ;; Be careful to count only the elements of BIND | 152 | ;; We use this to decide the initial minibuffer contents |
| 140 | ;; that actually constitute menu bar items. | 153 | ;; and initial history position. |
| 141 | (if (and (consp (car tail)) | 154 | (if default-item |
| 142 | (stringp (car-safe (cdr (car tail))))) | 155 | (let ((tail menu)) |
| 143 | (setq index-of-default (1+ index-of-default))) | 156 | (while (and tail |
| 144 | (setq tail (cdr tail))))) | 157 | (not (eq (car-safe (car tail)) default-item))) |
| 145 | (setq history (reverse (mapcar 'car tmm-km-list))) | 158 | ;; Be careful to count only the elements of MENU |
| 146 | (setq history-len (length history)) | 159 | ;; that actually constitute menu bar items. |
| 147 | (setq history (append history history history history)) | 160 | (if (and (consp (car tail)) |
| 148 | (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history)) | 161 | (stringp (car-safe (cdr (car tail))))) |
| 149 | (add-hook 'minibuffer-setup-hook 'tmm-add-prompt) | 162 | (setq index-of-default (1+ index-of-default))) |
| 150 | (unwind-protect | 163 | (setq tail (cdr tail))))) |
| 151 | (setq out | 164 | (setq history (reverse (mapcar 'car tmm-km-list))) |
| 152 | (completing-read | 165 | (setq history-len (length history)) |
| 153 | (concat gl-str " (up/down to change, PgUp to menu): ") | 166 | (setq history (append history history history history)) |
| 154 | tmm-km-list nil t nil | 167 | (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history)) |
| 155 | (cons 'history (- (* 2 history-len) index-of-default)))) | 168 | (add-hook 'minibuffer-setup-hook 'tmm-add-prompt) |
| 156 | (save-excursion | 169 | (unwind-protect |
| 157 | (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt) | 170 | (setq out |
| 158 | (if (get-buffer "*Completions*") | 171 | (completing-read |
| 159 | (progn | 172 | (concat gl-str " (up/down to change, PgUp to menu): ") |
| 160 | (set-buffer "*Completions*") | 173 | tmm-km-list nil t nil |
| 161 | (use-local-map tmm-old-comp-map) | 174 | (cons 'history (- (* 2 history-len) index-of-default)))) |
| 162 | (bury-buffer (current-buffer))))) | 175 | (save-excursion |
| 163 | ))) | 176 | (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt) |
| 164 | (setq bind (cdr (assoc out tmm-km-list))) | 177 | (if (get-buffer "*Completions*") |
| 165 | (and (null bind) | 178 | (progn |
| 166 | (> (length out) (length tmm-c-prompt)) | 179 | (set-buffer "*Completions*") |
| 167 | (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt) | 180 | (use-local-map tmm-old-comp-map) |
| 168 | (setq out (substring out (length tmm-c-prompt)) | 181 | (bury-buffer (current-buffer))))) |
| 169 | bind (cdr (assoc out tmm-km-list)))) | 182 | ))) |
| 170 | (and (null bind) | 183 | (setq choice (cdr (assoc out tmm-km-list))) |
| 171 | (setq out (try-completion out tmm-km-list) | 184 | (and (null choice) |
| 172 | bind (cdr (assoc out tmm-km-list)))) | 185 | (> (length out) (length tmm-c-prompt)) |
| 173 | (setq last-command-event (car bind)) | 186 | (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt) |
| 174 | (setq bind (cdr bind)) | 187 | (setq out (substring out (length tmm-c-prompt)) |
| 175 | (if bind | 188 | choice (cdr (assoc out tmm-km-list)))) |
| 176 | (if in-popup (tmm-prompt t bind) | 189 | (and (null choice) |
| 177 | (if (keymapp bind) | 190 | (setq out (try-completion out tmm-km-list) |
| 178 | (if (listp bind) | 191 | choice (cdr (assoc out tmm-km-list))))) |
| 179 | (progn | 192 | ;; CHOICE is now (STRING . MEANING). Separate the two parts. |
| 180 | (condition-case nil | 193 | (setq chosen-string (car choice)) |
| 181 | (require 'mouse) | 194 | (setq choice (cdr choice)) |
| 182 | (error nil)) | 195 | (cond (in-popup |
| 183 | (condition-case nil | 196 | ;; We just did the inner level of a -popup menu. |
| 184 | (x-popup-menu nil bind) ; Get the shortcuts | 197 | choice) |
| 185 | (error nil)) | 198 | ;; We just did the outer level. Do the inner level now. |
| 186 | (tmm-prompt bind)) | 199 | (not-menu (tmm-prompt choice t)) |
| 187 | (tmm-prompt (symbol-value bind)) | 200 | ;; We just handled a menu keymap and found another keymap. |
| 188 | ) | 201 | ((keymapp choice) |
| 189 | (if last-command-event | 202 | (if (symbolp choice) |
| 190 | (call-interactively bind) | 203 | (setq choice (indirect-function choice))) |
| 191 | bind))) | 204 | (condition-case nil |
| 192 | gl-str))) | 205 | (require 'mouse) |
| 206 | (error nil)) | ||
| 207 | (condition-case nil | ||
| 208 | (x-popup-menu nil choice) ; Get the shortcuts | ||
| 209 | (error nil)) | ||
| 210 | (tmm-prompt choice)) | ||
| 211 | ;; We just handled a menu keymap and found a command. | ||
| 212 | (choice | ||
| 213 | (if chosen-string | ||
| 214 | (call-interactively choice) | ||
| 215 | choice))))) | ||
| 193 | 216 | ||
| 194 | 217 | ||
| 195 | (defun tmm-add-shortcuts (list) | 218 | (defun tmm-add-shortcuts (list) |