]> code.delx.au - gnu-emacs/blobdiff - lisp/imenu.el
Fill some imenu--index-alist doc lines
[gnu-emacs] / lisp / imenu.el
index 316905fd0fd4357b5da54345a7b4059092b15907..48257b892db19f4008b1dd830e62206792ff150c 100644 (file)
@@ -1,6 +1,6 @@
 ;;; imenu.el --- framework for mode-specific buffer indexes  -*- lexical-binding: t -*-
 
-;; Copyright (C) 1994-1998, 2001-2015 Free Software Foundation, Inc.
+;; Copyright (C) 1994-1998, 2001-2016 Free Software Foundation, Inc.
 
 ;; Author: Ake Stenhoff <etxaksf@aom.ericsson.se>
 ;;         Lars Lindberg <lli@sypro.cap.se>
@@ -348,6 +348,12 @@ Don't move point."
 ;;; Lisp
 ;;;
 
+(define-error 'imenu-unavailable "imenu unavailable")
+
+(defun imenu-unavailable-error (format &rest args)
+  (signal 'imenu-unavailable
+          (list (apply #'format-message format args))))
+
 (defun imenu-example--lisp-extract-index-name ()
   ;; Example of a candidate for `imenu-extract-index-name-function'.
   ;; This will generate a flat index of definitions in a lisp file.
@@ -456,12 +462,15 @@ Don't move point."
 Simple elements in the alist look like (INDEX-NAME . POSITION).
 POSITION is the buffer position of the item; to go to the item
 is simply to move point to that position.
-POSITION is passed to `imenu-default-goto-function', so it can be a non-number
-if that variable has been changed (e.g. Semantic uses overlays for POSITIONs).
 
-Special elements look like (INDEX-NAME POSITION FUNCTION ARGUMENTS...).
-To \"go to\" a special element means applying FUNCTION
-to INDEX-NAME, POSITION, and the ARGUMENTS.
+POSITION is passed to `imenu-default-goto-function', so it can be
+a non-number if that variable has been changed (e.g. Semantic
+uses overlays for POSITIONs).
+
+Special elements look like
+\(INDEX-NAME POSITION FUNCTION ARGUMENTS...).
+To \"go to\" a special element means applying FUNCTION to
+INDEX-NAME, POSITION, and the ARGUMENTS.
 
 A nested sub-alist element looks like (INDEX-NAME . SUB-ALIST).
 The function `imenu--subalist-p' tests an element and returns t
@@ -476,7 +485,7 @@ element recalculates the buffer's index alist.")
 
 (defvar imenu--history-list nil
   ;; Making this buffer local caused it not to work!
-  "History list for 'jump-to-function-in-buffer'.")
+  "History list for `jump-to-function-in-buffer'.")
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;
@@ -499,14 +508,11 @@ If REVERSE is non-nil then the beginning is 100 and the end is 0."
   (let ((pos (point))
        (total (buffer-size)))
     (and reverse (setq pos (- total pos)))
-    (if (> total 50000)
-       ;; Avoid overflow from multiplying by 100!
-       (/ (1- pos) (max (/ total 100) 1))
-      (/ (* 100 (1- pos)) (max total 1)))))
+    (floor (* 100.0 (1- pos)) (max total 1))))
 
 (defun imenu--split (list n)
   "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))
+Example (imenu--split \\='(1 2 3 4 5 6 7 8) 3) => ((1 2 3) (4 5 6) (7 8))
 The returned list DOES NOT share structure with LIST."
   (let ((remain list)
        (result '())
@@ -593,7 +599,8 @@ See `imenu--index-alist' for the format of the index alist."
                  (funcall imenu-create-index-function))))
        (imenu--truncate-items imenu--index-alist)))
   (or imenu--index-alist noerror
-      (user-error "No items suitable for an index found in this buffer"))
+      (imenu-unavailable-error
+       "No items suitable for an index found in this buffer"))
   (or imenu--index-alist
       (setq imenu--index-alist (list nil)))
   ;; Add a rescan option to the index.
@@ -707,7 +714,7 @@ The alternate method, which is the one most often used, is to call
        ((and imenu-generic-expression)
         (imenu--generic-function imenu-generic-expression))
        (t
-        (user-error "This buffer cannot use `imenu-default-create-index-function'"))))
+         (imenu-unavailable-error "This buffer cannot use `imenu-default-create-index-function'"))))
 
 ;;;
 ;;; Generic index gathering function.
@@ -936,6 +943,8 @@ The returned value is of the form (INDEX-NAME . INDEX-POSITION)."
           (setq result t imenu--index-alist nil)))
     result))
 
+(defvar-local imenu--menubar-keymap nil)
+
 ;;;###autoload
 (defun imenu-add-to-menubar (name)
   "Add an `imenu' entry to the menu bar for the current buffer.
@@ -952,12 +961,13 @@ See the command `imenu' for more information."
        (let ((newmap (make-sparse-keymap)))
          (set-keymap-parent newmap (current-local-map))
          (setq imenu--last-menubar-index-alist nil)
+          (setq imenu--menubar-keymap (make-sparse-keymap "Imenu"))
          (define-key newmap [menu-bar index]
-           `(menu-item ,name ,(make-sparse-keymap "Imenu")))
+           `(menu-item ,name ,imenu--menubar-keymap))
          (use-local-map newmap)
          (add-hook 'menu-bar-update-hook 'imenu-update-menubar)))
-    (user-error "The mode `%s' does not support Imenu"
-                (format-mode-line mode-name))))
+    (imenu-unavailable-error "The mode `%s' does not support Imenu"
+                             (format-mode-line mode-name))))
 
 ;;;###autoload
 (defun imenu-add-menubar-index ()
@@ -975,28 +985,23 @@ to `imenu-update-menubar'.")
 
 (defun imenu-update-menubar ()
   (when (and (current-local-map)
-            (keymapp (lookup-key (current-local-map) [menu-bar index]))
+             imenu--menubar-keymap
             (/= (buffer-chars-modified-tick) imenu-menubar-modified-tick))
     (setq imenu-menubar-modified-tick (buffer-chars-modified-tick))
     (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.
       (unless (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 index-alist
-                                       (buffer-name)))
-         (setq menu1 (imenu--create-keymap (car menu)
+        (setq imenu--last-menubar-index-alist index-alist)
+        (setq index-alist (imenu--split-submenus index-alist))
+       (let* ((menu (imenu--split-menu index-alist
+                                        (buffer-name)))
+               (menu1 (imenu--create-keymap (car menu)
                                            (cdr (if (< 1 (length (cdr menu)))
                                                     menu
                                                   (car (cdr menu))))
-                                           'imenu--menubar-select))
-         (setq old (lookup-key (current-local-map) [menu-bar index]))
-         ;; This should never happen, but in some odd cases, potentially,
-         ;; lookup-key may return a dynamically composed keymap.
-         (if (keymapp (cadr old)) (setq old (cadr old)))
-         (setcdr old (cdr menu1)))))))
+                                           'imenu--menubar-select)))
+         (setcdr imenu--menubar-keymap (cdr menu1)))))))
 
 (defun imenu--menubar-select (item)
   "Use Imenu to select the function or variable named in this menu ITEM."
@@ -1034,16 +1039,13 @@ for more information."
   (if (stringp index-item)
       (setq index-item (assoc index-item (imenu--make-index-alist))))
   (when index-item
-    (push-mark nil t)
-    (let* ((is-special-item (listp (cdr index-item)))
-          (function
-           (if is-special-item
-               (nth 2 index-item) imenu-default-goto-function))
-          (position (if is-special-item
-                        (cadr index-item) (cdr index-item)))
-          (args (if is-special-item (cdr (cddr index-item)))))
-      (apply function (car index-item) position args))
-    (run-hooks 'imenu-after-jump-hook)))
+    (pcase index-item
+      (`(,name ,pos ,fn . ,args)
+       (push-mark nil t)
+       (apply fn name pos args)
+       (run-hooks 'imenu-after-jump-hook))
+      (`(,name . ,pos) (imenu (list name pos imenu-default-goto-function)))
+      (_ (error "Unknown imenu item: %S" index-item)))))
 
 (provide 'imenu)