]> code.delx.au - gnu-emacs/blobdiff - lisp/tmm.el
(Abbrevs): A @node line without explicit Prev, Next, and Up links.
[gnu-emacs] / lisp / tmm.el
index 0db56d4c1d468bc7d78daefd247425704285e0cc..86c326616aac120cde8c9681e0dc4260a17cf038 100644 (file)
@@ -1,9 +1,11 @@
 ;;; tmm.el --- text mode access to menu-bar
 
-;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002, 2003,
+;;   2004, 2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
 ;; Maintainer: FSF
+;; Keywords: convenience
 
 ;; This file is part of GNU Emacs.
 
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
-;; To use this package add 
-
-;; (autoload 'tmm-menubar 'tmm "Text mode substitute for menubar" t) 
-;; (global-set-key [f10] 'tmm-menubar)
-;; to your .emacs file. You can also add your own access to different
-;; menus available in Window System Emacs modeling definition after
-;; tmm-menubar.
+;; This package provides text mode access to the menu bar.
 
 ;;; Code:
 
@@ -100,7 +96,7 @@ See the documentation for `tmm-prompt'."
   (tmm-menubar (car (posn-x-y (event-start event)))))
 
 (defcustom tmm-mid-prompt "==>"
-  "*String to insert between shortcut and menu item. 
+  "*String to insert between shortcut and menu item.
 If nil, there will be no shortcuts. It should not consist only of spaces,
 or else the correct item might not be found in the `*Completions*' buffer."
   :type 'string
@@ -109,10 +105,10 @@ or else the correct item might not be found in the `*Completions*' buffer."
 (defvar tmm-mb-map nil
   "A place to store minibuffer map.")
 
-(defcustom tmm-completion-prompt 
-  "Press PageUp Key to reach this buffer from the minibuffer.
+(defcustom tmm-completion-prompt
+  "Press PageUp key to reach this buffer from the minibuffer.
 Alternatively, you can use Up/Down keys (or your History keys) to change
-the item in the minibuffer, and press RET when you are done, or press the 
+the item in the minibuffer, and press RET when you are done, or press the
 marked letters to pick up your choice.  Type C-g or ESC ESC ESC to cancel.
 "
   "*Help text to insert on the top of the completion buffer.
@@ -122,8 +118,8 @@ in which case the standard introduction text is deleted too."
   :group 'tmm)
 
 (defcustom tmm-shortcut-style '(downcase upcase)
-  "*What letters to use as menu shortcuts. 
-Must be either one of the symbols `downcase' or `upcase', 
+  "*What letters to use as menu shortcuts.
+Must be either one of the symbols `downcase' or `upcase',
 or else a list of the two in the order you prefer."
   :type '(choice (const downcase)
                 (const upcase)
@@ -132,11 +128,16 @@ or else a list of the two in the order you prefer."
 
 (defcustom tmm-shortcut-words 2
   "*How many successive words to try for shortcuts, nil means all.
-If you use only one of `downcase' or `upcase' for `tmm-shortcut-style', 
+If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
 specify nil for this variable."
   :type '(choice integer (const nil))
   :group 'tmm)
 
+(defface tmm-inactive
+  '((t :inherit shadow))
+  "Face used for inactive menu items."
+  :group 'tmm)
+
 ;;;###autoload
 (defun tmm-prompt (menu &optional in-popup default-item)
   "Text-mode emulation of calling the bindings in keymap.
@@ -164,10 +165,10 @@ Its value should be an event that has a binding in MENU."
     ;; tmm-km-list is an alist of (STRING . MEANING).
     ;; It has no other elements.
     ;; The order of elements in tmm-km-list is the order of the menu bar.
-    (mapcar (function (lambda (elt)
-                       (if (stringp elt)
-                           (setq gl-str elt)
-                         (and (listp elt) (tmm-get-keymap elt not-menu)))))
+    (mapc (lambda (elt)
+           (if (stringp elt)
+               (setq gl-str elt)
+             (and (listp elt) (tmm-get-keymap elt not-menu))))
            menu)
     ;; Choose an element of tmm-km-list; put it in choice.
     (if (and not-menu (= 1 (length tmm-km-list)))
@@ -193,10 +194,18 @@ Its value should be an event that has a binding in MENU."
                     ;; Be careful to count only the elements of MENU
                     ;; that actually constitute menu bar items.
                     (if (and (consp (car tail))
-                             (stringp (car-safe (cdr (car tail)))))
+                             (or (stringp (car-safe (cdr (car tail))))
+                                 (eq (car-safe (cdr (car tail))) 'menu-item)))
                         (setq index-of-default (1+ index-of-default)))
                     (setq tail (cdr tail)))))
-            (setq history (reverse (mapcar 'car tmm-km-list)))
+             (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
+               (setq history
+                     (reverse (delq nil
+                                    (mapcar
+                                     (lambda (elt)
+                                       (if (string-match prompt (car elt))
+                                           (car elt)))
+                                     tmm-km-list)))))
             (setq history-len (length history))
             (setq history (append history history history history))
             (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
@@ -232,7 +241,7 @@ Its value should be an event that has a binding in MENU."
           ;; We just did the inner level of a -popup menu.
           choice)
          ;; We just did the outer level.  Do the inner level now.
-         (not-menu (tmm-prompt choice t)) 
+         (not-menu (tmm-prompt choice t))
          ;; We just handled a menu keymap and found another keymap.
          ((keymapp choice)
           (if (symbolp choice)
@@ -262,51 +271,56 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
 
 (defsubst tmm-add-one-shortcut (elt)
 ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
-  (let* ((str (car elt))
-        (paren (string-match "(" str))  
-        (pos 0) (word 0) char)
-    (catch 'done                        ; ??? is this slow?
-      (while (and (or (not tmm-shortcut-words) ; no limit on words
-                      (< word tmm-shortcut-words)) ; try n words
-                  (setq pos (string-match "\\w+" str pos)) ; get next word
-                  (not (and paren (> pos paren)))) ; don't go past "(binding.."
-        (if (or (= pos 0)
-                (/= (aref str (1- pos)) ?.)) ; avoid file extensions
-            (let ((shortcut-style                 
-                   (if (listp tmm-shortcut-style) ; convert to list
-                       tmm-shortcut-style
-                     (list tmm-shortcut-style))))
-              (while shortcut-style     ; try upcase and downcase variants
-                (setq char (funcall (car shortcut-style) (aref str pos)))
-                (if (not (memq char tmm-short-cuts)) (throw 'done char))
-                (setq shortcut-style (cdr shortcut-style)))))
-        (setq word (1+ word))
-        (setq pos (match-end 0)))
-      (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
-        (setq char tmm-next-shortcut-digit)
-        (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
-        (if (not (memq char tmm-short-cuts)) (throw 'done char)))
-      (setq char nil))
-    (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
-    (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
-                    ;; keep them lined up in columns
-                    (make-string (1+ (length tmm-mid-prompt)) ?\ ))
-                  str)
-          (cdr elt))))
+  (cond
+   ((eq (cddr elt) 'ignore)
+    (cons (concat " " (make-string (length tmm-mid-prompt) ?\-)
+                  (car elt))
+          (cdr elt)))
+   (t
+    (let* ((str (car elt))
+           (paren (string-match "(" str))
+           (pos 0) (word 0) char)
+      (catch 'done                             ; ??? is this slow?
+        (while (and (or (not tmm-shortcut-words)   ; no limit on words
+                        (< word tmm-shortcut-words)) ; try n words
+                    (setq pos (string-match "\\w+" str pos)) ; get next word
+                    (not (and paren (> pos paren)))) ; don't go past "(binding.."
+          (if (or (= pos 0)
+                  (/= (aref str (1- pos)) ?.)) ; avoid file extensions
+              (let ((shortcut-style
+                     (if (listp tmm-shortcut-style) ; convert to list
+                         tmm-shortcut-style
+                       (list tmm-shortcut-style))))
+                (while shortcut-style ; try upcase and downcase variants
+                  (setq char (funcall (car shortcut-style) (aref str pos)))
+                  (if (not (memq char tmm-short-cuts)) (throw 'done char))
+                  (setq shortcut-style (cdr shortcut-style)))))
+          (setq word (1+ word))
+          (setq pos (match-end 0)))
+        (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
+          (setq char tmm-next-shortcut-digit)
+          (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
+          (if (not (memq char tmm-short-cuts)) (throw 'done char)))
+        (setq char nil))
+      (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
+      (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
+                      ;; keep them lined up in columns
+                      (make-string (1+ (length tmm-mid-prompt)) ?\ ))
+                    str)
+            (cdr elt))))))
 
 ;; This returns the old map.
 (defun tmm-define-keys (minibuffer)
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map t)
-    (mapcar
-     (function
-      (lambda (c)
-        (if (listp tmm-shortcut-style)
-            (define-key map (char-to-string c) 'tmm-shortcut)
-          ;; only one kind of letters are shortcuts, so map both upcase and
-          ;; downcase input to the same
-          (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
-          (define-key map (char-to-string (upcase c)) 'tmm-shortcut))))
+    (mapc
+     (lambda (c)
+       (if (listp tmm-shortcut-style)
+          (define-key map (char-to-string c) 'tmm-shortcut)
+        ;; only one kind of letters are shortcuts, so map both upcase and
+        ;; downcase input to the same
+        (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
+        (define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
      tmm-short-cuts)
     (if minibuffer
        (progn
@@ -323,14 +337,31 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
   (goto-char 1)
   (delete-region 1 (search-forward "Possible completions are:\n")))
 
+(defun tmm-remove-inactive-mouse-face ()
+  "Remove the mouse-face property from inactive menu items."
+  (let ((inhibit-read-only t)
+        (inactive-string
+         (concat " " (make-string (length tmm-mid-prompt) ?\-)))
+        next)
+    (save-excursion
+      (goto-char (point-min))
+      (while (not (eobp))
+        (setq next (next-single-char-property-change (point) 'mouse-face))
+        (when (looking-at inactive-string)
+          (remove-text-properties (point) next '(mouse-face))
+          (add-text-properties (point) next '(face tmm-inactive)))
+        (goto-char next)))
+    (set-buffer-modified-p nil)))
+
 (defun tmm-add-prompt ()
   (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
-  (make-local-hook 'minibuffer-exit-hook)
   (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
+  (unless tmm-c-prompt
+    (error "No active menu entries"))
   (let ((win (selected-window)))
     (setq tmm-old-mb-map (tmm-define-keys t))
     ;; Get window and hide it for electric mode to get correct size
-    (save-window-excursion 
+    (save-window-excursion
       (let ((completions
             (mapcar 'car minibuffer-completion-table)))
         (or tmm-completion-prompt
@@ -339,19 +370,18 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
        (with-output-to-temp-buffer "*Completions*"
          (display-completion-list completions))
         (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
-      (if tmm-completion-prompt
-          (progn
-           (set-buffer "*Completions*")
-           (goto-char 1)
-            (insert tmm-completion-prompt)))
-      )
+      (set-buffer "*Completions*")
+      (tmm-remove-inactive-mouse-face)
+      (when tmm-completion-prompt
+       (let ((buffer-read-only nil))
+         (goto-char (point-min))
+         (insert tmm-completion-prompt))))
     (save-selected-window
       (other-window 1)                 ; Electric-pop-up-window does
                                        ; not work in minibuffer
       (Electric-pop-up-window "*Completions*")
       (with-current-buffer "*Completions*"
        (setq tmm-old-comp-map (tmm-define-keys nil))))
-
     (insert tmm-c-prompt)))
 
 (defun tmm-delete-map ()
@@ -368,19 +398,19 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
     (if (memq c tmm-short-cuts)
        (if (equal (buffer-name) "*Completions*")
            (progn
-             (beginning-of-buffer)
+             (goto-char (point-min))
              (re-search-forward
               (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
              (choose-completion))
          ;; In minibuffer
          (delete-region (minibuffer-prompt-end) (point-max))
-         (mapcar (lambda (elt)
-                   (if (string=
-                        (substring (car elt) 0 
-                                   (min (1+ (length tmm-mid-prompt))
-                                        (length (car elt))))
-                        (concat (char-to-string c) tmm-mid-prompt))
-                       (setq s (car elt))))
+         (mapc (lambda (elt)
+                 (if (string=
+                      (substring (car elt) 0
+                                 (min (1+ (length tmm-mid-prompt))
+                                      (length (car elt))))
+                      (concat (char-to-string c) tmm-mid-prompt))
+                     (setq s (car elt))))
                  tmm-km-list)
          (insert s)
          (exit-minibuffer)))))
@@ -394,14 +424,14 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
   (search-forward tmm-c-prompt)
   (search-backward tmm-c-prompt))
 
-(defun tmm-get-keymap (elt &optional in-x-menu) 
+(defun tmm-get-keymap (elt &optional in-x-menu)
   "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
 The values are deduced from the argument ELT, that should be an
 element of keymap, an `x-popup-menu' argument, or an element of
 `x-popup-menu' argument (when IN-X-MENU is not-nil).
 This function adds the element only if it is not already present.
 It uses the free variable `tmm-table-undef' to keep undefined keys."
-  (let (km str cache plist filter (event (car elt)))
+  (let (km str cache plist filter visible enable (event (car elt)))
     (setq elt (cdr elt))
     (if (eq elt 'undefined)
        (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
@@ -410,12 +440,14 @@ It uses the free variable `tmm-table-undef' to keep undefined keys."
                   (or (keymapp elt) (eq (car elt) 'lambda))
                 (fboundp elt))
               (setq km elt))
+
              ((if (listp (cdr-safe elt))
                   (or (keymapp (cdr-safe elt))
                       (eq (car (cdr-safe elt)) 'lambda))
                 (fboundp (cdr-safe elt)))
               (setq km (cdr elt))
               (and (stringp (car elt)) (setq str (car elt))))
+
              ((if (listp (cdr-safe (cdr-safe elt)))
                   (or (keymapp (cdr-safe (cdr-safe elt)))
                       (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
@@ -426,19 +458,30 @@ It uses the free variable `tmm-table-undef' to keep undefined keys."
                    (stringp (cdr (car (cdr elt)))) ; keyseq cache
                    (setq cache (cdr (car (cdr elt))))
                    cache (setq str (concat str cache))))
+
              ((eq (car-safe elt) 'menu-item)
+              ;; (menu-item TITLE COMMAND KEY ...)
               (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
+              (when (consp (car-safe plist))
+                (setq plist (cdr-safe plist)))
               (setq km (nth 2 elt))
-              (setq str (nth 1 elt))
+              (setq str (eval (nth 1 elt)))
               (setq filter (plist-get plist :filter))
               (if filter
                   (setq km (funcall filter km)))
+              (setq visible (plist-get plist :visible))
+              (if visible
+                  (setq km (and (eval visible) km)))
+              (setq enable (plist-get plist :enable))
+              (if enable
+                   (setq km (if (eval enable) km 'ignore)))
               (and str
                    (consp (nth 3 elt))
                    (stringp (cdr (nth 3 elt))) ; keyseq cache
                    (setq cache (cdr (nth 3 elt)))
                    cache
                    (setq str (concat str cache))))
+
              ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
                   (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
                       (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
@@ -450,6 +493,7 @@ It uses the free variable `tmm-table-undef' to keep undefined keys."
                    (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
                    (setq cache (cdr (car (cdr (cdr elt)))))
                    cache (setq str (concat str cache))))
+
              ((stringp event)          ; x-popup or x-popup element
               (if (or in-x-menu (stringp (car-safe elt)))
                   (setq str event event nil km elt)
@@ -459,13 +503,10 @@ It uses the free variable `tmm-table-undef' to keep undefined keys."
       ;; Verify that the command is enabled;
       ;; if not, don't mention it.
       (when (and km (symbolp km) (get km 'menu-enable))
-       (unless (eval (get km 'menu-enable))
-         (setq km nil)))
+         (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
       (and km str
           (or (assoc str tmm-km-list)
-              (setq tmm-km-list 
-                    (cons (cons str (cons event km)) tmm-km-list)))
-          ))))
+              (push (cons str (cons event km)) tmm-km-list))))))
 
 (defun tmm-get-keybind (keyseq)
   "Return the current binding of KEYSEQ, merging prefix definitions.
@@ -484,17 +525,17 @@ of `menu-bar-final-items'."
        (progn
          ;; Otherwise, it is a prefix, so make a list of the subcommands.
          ;; Make a list of all the bindings in all the keymaps.
-         (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq))) 
+         (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
          (setq allbind (cons (local-key-binding keyseq) allbind))
          (setq allbind (cons (global-key-binding keyseq) allbind))
          ;; Merge all the elements of ALLBIND into one keymap.
-         (mapcar (lambda (in)
-                   (if (and (symbolp in) (keymapp in))
-                       (setq in (symbol-function in)))
-                   (and in (keymapp in)
-                        (if (keymapp bind)
-                            (setq bind (nconc bind (copy-sequence (cdr in))))
-                          (setq bind (copy-sequence in)))))
+         (mapc (lambda (in)
+                 (if (and (symbolp in) (keymapp in))
+                     (setq in (symbol-function in)))
+                 (and in (keymapp in)
+                      (if (keymapp bind)
+                          (setq bind (nconc bind (copy-sequence (cdr in))))
+                        (setq bind (copy-sequence in)))))
                  allbind)
          ;; Return that keymap.
          bind))))
@@ -503,4 +544,5 @@ of `menu-bar-final-items'."
 
 (provide 'tmm)
 
+;;; arch-tag: e7ddbdb6-4b95-4da3-afbe-ad6063d112f4
 ;;; tmm.el ends here