]> code.delx.au - gnu-emacs/blobdiff - lisp/imenu.el
(rlogin): Call rlogin-mode after adding rlogin-carriage-filter to output
[gnu-emacs] / lisp / imenu.el
index 70a1b051956b8e19b445369acedef4cec898c7d8..166d2e10798cddb291758d5d9d3b3a3e43b89a06 100644 (file)
@@ -1,6 +1,6 @@
 ;;; imenu.el --- Framework for mode-specific buffer indexes.
 
-;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
 
 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
 ;;         Lars Lindberg <lli@sypro.cap.se>
@@ -98,7 +98,7 @@ element should come before the second.  The arguments are cons cells;
 \(NAME . POSITION).  Look at `imenu--sort-by-name' for an example.")
 
 (defvar imenu-max-items 25
-  "*Maximum number of elements in an index mouse-menu.")
+  "*Maximum number of elements in an mouse menu for Imenu.")
 
 (defvar imenu-scanning-message "Scanning buffer for index (%3d%%)"
   "*Progress message during the index scanning of the buffer.
@@ -117,9 +117,6 @@ names work as tokens.")
 Used for making mouse-menu titles and for flattening nested indexes
 with name concatenation.")
 
-(defvar imenu-submenu-name-format "%s..."
-  "*The format for making a submenu name.")
-
 ;;;###autoload
 (defvar imenu-generic-expression nil
   "The regex pattern to use for creating a buffer index.
@@ -149,6 +146,7 @@ For emacs-lisp-mode for example PATTERN would look like:
 
 The variable is buffer-local.")
 
+;;;###autoload
 (make-variable-buffer-local 'imenu-generic-expression)
 
 ;;;; Hooks
@@ -282,13 +280,13 @@ This function is called after the function pointed out by
                       index-unknown-alist)))))))
     (imenu-progress-message prev-pos 100)
     (and index-var-alist
-        (push (cons (imenu-create-submenu-name "Variables") index-var-alist)
+        (push (cons "Variables" index-var-alist)
               index-alist))
     (and index-type-alist
-        (push (cons (imenu-create-submenu-name "Types") index-type-alist)
+        (push (cons "Types" index-type-alist)
               index-alist))
     (and index-unknown-alist
-        (push (cons (imenu-create-submenu-name "Syntax-unknown") index-unknown-alist)
+        (push (cons "Syntax-unknown" index-unknown-alist)
               index-alist))
     index-alist))
 
@@ -372,14 +370,6 @@ This function is called after the function pointed out by
        (/ (1- pos) (max (/ total 100) 1))
       (/ (* 100 (1- pos)) (max total 1)))))
 
-;;;
-;;; Function for supporting general looking submenu names.
-;;; Uses `imenu-submenu-name-format' for creating the name.
-;;; NAME is the base of the new submenu name.
-;;;
-(defun imenu-create-submenu-name (name)
-   (format imenu-submenu-name-format name))
-
 ;; Split LIST into sublists of max length N.
 ;; Example (imenu--split '(1 2 3 4 5 6 7 8) 3)-> '((1 2 3) (4 5 6) (7 8))
 (defun imenu--split (list n)
@@ -401,16 +391,30 @@ This function is called after the function pointed out by
         (push (nreverse sublist) result))
     (nreverse result)))
 
-;;;
-;;; Split a menu in to several menus.
-;;;
+;;; Split the alist MENULIST into a nested alist, if it is long enough.
+;;; In any case, add TITLE to the front of the alist.
 (defun imenu--split-menu (menulist title)
-  (cons "Index menu"
-       (mapcar
-        (function
-         (lambda (menu)
-           (cons (format "(%s)" title) menu)))
-        (imenu--split menulist imenu-max-items))))
+  (if (> (length menulist) imenu-max-items)
+      (let ((count 0))
+       (cons title
+             (mapcar
+              (function
+               (lambda (menu)
+                 (cons (format "(%s-%d)" title (setq count (1+ count)))
+                       menu)))
+              (imenu--split menulist imenu-max-items))))
+    (cons title menulist)))
+
+;;; Split up each long alist that are nested within ALIST
+;;; into nested alists.
+(defun imenu--split-submenus (alist)
+  (mapcar (function (lambda (elt)
+                     (if (and (consp elt)
+                              (stringp (car elt))
+                              (listp (cdr elt)))
+                         (imenu--split-menu (cdr elt) (car elt))
+                       elt)))
+         alist))
 
 ;;;
 ;;; Find all items in this buffer that should be in the index.
@@ -629,18 +633,14 @@ pattern.
                          (push 
                           (cons (buffer-substring-no-properties beg end) beg)
                           (cdr 
-                           (or (if (not (stringp menu-title)) index-alist) 
-                               (assoc 
-                                (imenu-create-submenu-name menu-title) 
-                                index-alist)
+                           (or (assoc menu-title index-alist)
                                (car (push 
-                                     (cons 
-                                      (imenu-create-submenu-name menu-title) 
-                                      '()) 
+                                     (cons menu-title '()) 
                                      index-alist))))))))))
-           patterns))))
-      (imenu-progress-message prev-pos 100 t)
-      (delete 'dummy index-alist)))
+          patterns))))
+    (imenu-progress-message prev-pos 100 t)
+    (let ((main-element (assq nil index-alist)))
+      (nconc (delq main-element (delq 'dummy index-alist)) main-element))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;
@@ -696,6 +696,7 @@ Returns t for rescan and otherwise a position number."
 INDEX-ALIST is the buffer index and EVENT is a mouse event.
 
 Returns t for rescan and otherwise a position number."
+  (setq index-alist (imenu--split-submenus index-alist))
   (let* ((menu         (imenu--split-menu
                 (if imenu-sort-function
                     (sort
@@ -713,6 +714,9 @@ Returns t for rescan and otherwise a position number."
                                            (if (< 1 (length (cdr menu)))
                                                (cdr menu)
                                              (cdr (cadr menu))))))
+
+    (or imenu-use-keymap-menu
+       (setq menu (list "Imenu" (delq nil menu))))
     (setq position (x-popup-menu event menu))
     (if imenu-use-keymap-menu
        (progn
@@ -785,13 +789,18 @@ The returned value is on the form (INDEX-NAME . INDEX-POSITION)."
 
 ;;;###autoload
 (defun imenu-add-to-menubar (name)
-  "Adds an \"imenu\" entry to the menu bar for the current major mode.
+  "Adds an `imenu' entry to the menu bar for the current buffer.
 NAME is a string used to name the menu bar item.
-See `imenu' for more information."
+See the command `imenu' for more information."
   (interactive "sImenu menu item name: ")
-  (define-key (current-local-map) [menu-bar index]
-    (cons name (nconc (make-sparse-keymap "Imenu")
-                     (make-sparse-keymap))))
+  (let ((newmap (make-sparse-keymap))
+       (menu-bar (lookup-key (current-local-map) [menu-bar])))
+    (define-key newmap [menu-bar]
+      (append (make-sparse-keymap) menu-bar))
+    (define-key newmap [menu-bar index]
+      (cons name (nconc (make-sparse-keymap "Imenu")
+                       (make-sparse-keymap))))
+    (use-local-map (append newmap (current-local-map))))
   (add-hook 'menu-bar-update-hook 'imenu-update-menubar))
 
 (defvar imenu-buffer-menubar nil)
@@ -799,23 +808,13 @@ See `imenu' for more information."
 (defun imenu-update-menubar ()
   (and (current-local-map)
        (keymapp (lookup-key (current-local-map) [menu-bar index]))
-       (progn
-        (or (local-variable-p 'imenu-buffer-menubar)
-            ;; Make a buffer-specific copy of the local map
-            ;; so that we don't affect other buffers with the same major mode.
-            (let ((newmap (make-sparse-keymap)))
-              (define-key (current-local-map) [menu-bar index]
-                (cons name (nconc (make-sparse-keymap "Imenu")
-                                  (make-sparse-keymap))))
-              (use-local-map (append newmap (current-local-map)))))
-        (make-local-variable 'imenu-buffer-menubar)
-        t)
        (let ((index-alist (imenu--make-index-alist t)))
         ;; Don't bother updating if the index-alist has not changed
         ;; since the last time we did it.
         (or (equal index-alist imenu--last-menubar-index-alist)
             (let (menu menu1 old)
               (setq imenu--last-menubar-index-alist index-alist)
+              (setq index-alist (imenu--split-submenus index-alist))
               (setq menu (imenu--split-menu
                           (if imenu-sort-function
                               (sort
@@ -838,8 +837,12 @@ See `imenu' for more information."
 
 (defun imenu--menubar-select (item)
   "Use Imenu to select the function or variable named in this menu item."
-  (interactive)
-  (imenu item))
+  (if (equal item '("*Rescan*" . -99))
+      (progn
+       (imenu--cleanup)
+       (setq imenu--index-alist nil)
+       (imenu-update-menubar))
+    (imenu item)))
 
 ;;;###autoload
 (defun imenu (index-item)