]> code.delx.au - gnu-emacs/blobdiff - lisp/tmm.el
Add 2008 to copyright years.
[gnu-emacs] / lisp / tmm.el
index 3e1ba6c88986f7285aced262203a686a09935dd2..5248d0cda733a3f9c80b0b009d533178846da1f5 100644 (file)
@@ -1,7 +1,7 @@
 ;;; tmm.el --- text mode access to menu-bar
 
-;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002, 2003,
+;;   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
 ;; Maintainer: FSF
@@ -11,7 +11,7 @@
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -21,8 +21,8 @@
 
 ;; 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:
 
@@ -41,7 +41,7 @@
 (defvar tmm-short-cuts)
 (defvar tmm-old-mb-map nil)
 (defvar tmm-old-comp-map)
-(defvar tmm-c-prompt)
+(defvar tmm-c-prompt nil)
 (defvar tmm-km-list)
 (defvar tmm-next-shortcut-digit)
 (defvar tmm-table-undef)
@@ -71,17 +71,22 @@ we make that menu bar item (the one at that position) the default choice."
                                   (list this-one)))))
        (setq list (cdr list))))
     (if x-position
-       (let ((tail menu-bar)
-             this-one
-             (column 0))
-         (while (and tail (< column x-position))
+       (let ((tail menu-bar) (column 0)
+             this-one name visible)
+         (while (and tail (<= column x-position))
            (setq this-one (car tail))
-           (if (and (consp (car tail))
-                    (consp (cdr (car tail)))
-                    (stringp (nth 1 (car tail))))
-               (setq column (+ column
-                               (length (nth 1 (car tail)))
-                               1)))
+           (if (and (consp this-one)
+                    (consp (cdr this-one))
+                    (setq name  ;simple menu
+                          (cond ((stringp (nth 1  this-one))
+                                 (nth 1  this-one))
+                                ;extended menu
+                                ((stringp (nth 2 this-one))
+                                 (setq visible (plist-get
+                                                (nthcdr 4 this-one) :visible))
+                                 (unless (and visible (not (eval visible)))
+                                   (nth 2 this-one))))))
+               (setq column (+ column (length name) 1)))
            (setq tail (cdr tail)))
          (setq menu-bar-item (car this-one))))
     (tmm-prompt menu-bar nil menu-bar-item)))
@@ -106,7 +111,7 @@ or else the correct item might not be found in the `*Completions*' buffer."
   "A place to store minibuffer map.")
 
 (defcustom tmm-completion-prompt
-  "Press PageUp Key to reach this buffer from the minibuffer.
+  "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
 marked letters to pick up your choice.  Type C-g or ESC ESC ESC to cancel.
@@ -133,6 +138,11 @@ 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.
@@ -183,36 +193,52 @@ Its value should be an event that has a binding in MENU."
             ;; We use this to decide the initial minibuffer contents
             ;; and initial history position.
             (if default-item
-                (let ((tail menu))
+                (let ((tail menu) visible)
                   (while (and tail
                               (not (eq (car-safe (car tail)) default-item)))
                     ;; Be careful to count only the elements of MENU
                     ;; that actually constitute menu bar items.
                     (if (and (consp (car tail))
                              (or (stringp (car-safe (cdr (car tail))))
-                                 (eq (car-safe (cdr (car tail))) 'menu-item)))
+                                 (and
+                                  (eq (car-safe (cdr (car tail))) 'menu-item)
+                                  (progn
+                                    (setq visible
+                                          (plist-get
+                                           (nthcdr 4 (car tail)) :visible))
+                                    (or (not visible) (eval visible))))))
                         (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))
             (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
-            (save-excursion
-              (unwind-protect
-                  (setq out
-                        (completing-read
-                         (concat gl-str " (up/down to change, PgUp to menu): ")
-                         tmm-km-list nil t nil
-                         (cons 'history (- (* 2 history-len) index-of-default))))
-                (save-excursion
-                  (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
-                  (if (get-buffer "*Completions*")
-                      (progn
-                        (set-buffer "*Completions*")
-                        (use-local-map tmm-old-comp-map)
-                        (bury-buffer (current-buffer)))))
-                ))))
+            (if default-item
+                (setq out (car (nth index-of-default tmm-km-list)))
+              (save-excursion
+                (unwind-protect
+                    (setq out
+                          (completing-read
+                           (concat gl-str
+                                   " (up/down to change, PgUp to menu): ")
+                           tmm-km-list nil t nil
+                           (cons 'history
+                                 (- (* 2 history-len) index-of-default))))
+                  (save-excursion
+                    (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
+                    (if (get-buffer "*Completions*")
+                        (progn
+                          (set-buffer "*Completions*")
+                          (use-local-map tmm-old-comp-map)
+                          (bury-buffer (current-buffer))))))))))
       (setq choice (cdr (assoc out tmm-km-list)))
       (and (null choice)
           (> (length out) (length tmm-c-prompt))
@@ -259,37 +285,43 @@ 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)) ?\s))
+                    str)
+            (cdr elt))))))
 
 ;; This returns the old map.
 (defun tmm-define-keys (minibuffer)
@@ -319,9 +351,27 @@ 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)
   (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
@@ -334,8 +384,9 @@ 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))
+      (set-buffer "*Completions*")
+      (tmm-remove-inactive-mouse-face)
       (when tmm-completion-prompt
-       (set-buffer "*Completions*")
        (let ((buffer-read-only nil))
          (goto-char (point-min))
          (insert tmm-completion-prompt))))
@@ -345,7 +396,6 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
       (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 ()
@@ -362,7 +412,7 @@ 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))
@@ -395,7 +445,7 @@ 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 visible (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))
@@ -436,6 +486,9 @@ It uses the free variable `tmm-table-undef' to keep undefined keys."
               (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
@@ -464,8 +517,7 @@ 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)
               (push (cons str (cons event km)) tmm-km-list))))))
@@ -476,7 +528,7 @@ If KEYSEQ is a prefix key that has local and global bindings,
 we merge them into a single keymap which shows the proper order of the menu.
 However, for the menu bar itself, the value does not take account
 of `menu-bar-final-items'."
-  (let (allbind bind)
+  (let (allbind bind minorbind localbind globalbind)
     (setq bind (key-binding keyseq))
     ;; If KEYSEQ is a prefix key, then BIND is either nil
     ;; or a symbol defined as a keymap (which satisfies keymapp).
@@ -487,9 +539,21 @@ 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 (cons (local-key-binding keyseq) allbind))
-         (setq allbind (cons (global-key-binding keyseq) allbind))
+         (setq minorbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
+         (setq localbind (local-key-binding keyseq))
+         (setq globalbind (copy-sequence (cdr (global-key-binding keyseq))))
+
+         ;; If items have been redefined/undefined locally, remove them from
+         ;; the global list.
+         (dolist (minor minorbind)
+           (dolist (item (cdr minor))
+             (setq globalbind (assq-delete-all (car-safe item) globalbind))))
+         (dolist (item (cdr localbind))
+           (setq globalbind (assq-delete-all (car-safe item) globalbind)))
+
+         (setq globalbind (cons 'keymap globalbind))
+         (setq allbind (cons globalbind (cons localbind minorbind)))
+
          ;; Merge all the elements of ALLBIND into one keymap.
          (mapc (lambda (in)
                  (if (and (symbolp in) (keymapp in))
@@ -506,4 +570,5 @@ of `menu-bar-final-items'."
 
 (provide 'tmm)
 
+;;; arch-tag: e7ddbdb6-4b95-4da3-afbe-ad6063d112f4
 ;;; tmm.el ends here