Commit 07d00b56 authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* lisp/imenu.el: Misc cleanup. Make docstrings out of comments.

Use lexical-binding.
(imenu--index-alist, imenu--last-menubar-index-alist)
(imenu-menubar-modified-tick): Use defvar-local.
(imenu--split-menu): Remove unused var.
(imenu--cleanup-seen): Declare as global.
(imenu--cleanup): Use dolist.
parent 8bba5a75
2012-05-08 Stefan Monnier <monnier@iro.umontreal.ca> 2012-05-08 Stefan Monnier <monnier@iro.umontreal.ca>
* imenu.el: Misc cleanup. Make docstrings out of comments.
Use lexical-binding.
(imenu--index-alist, imenu--last-menubar-index-alist)
(imenu-menubar-modified-tick): Use defvar-local.
(imenu--split-menu): Remove unused var.
(imenu--cleanup-seen): Declare as global.
(imenu--cleanup): Use dolist.
* subr.el (defvar-local): Add debug spec and doc-string position. * subr.el (defvar-local): Add debug spec and doc-string position.
2012-05-08 Glenn Morris <rgm@gnu.org> 2012-05-08 Glenn Morris <rgm@gnu.org>
......
;;; imenu.el --- framework for mode-specific buffer indexes ;;; imenu.el --- framework for mode-specific buffer indexes -*- lexical-binding: t -*-
;; Copyright (C) 1994-1998, 2001-2012 Free Software Foundation, Inc. ;; Copyright (C) 1994-1998, 2001-2012 Free Software Foundation, Inc.
...@@ -266,12 +266,12 @@ The function in this variable is called when selecting a normal index-item.") ...@@ -266,12 +266,12 @@ The function in this variable is called when selecting a normal index-item.")
(and (consp (cdr item)) (listp (cadr item)) (and (consp (cdr item)) (listp (cadr item))
(not (eq (car (cadr item)) 'lambda)))) (not (eq (car (cadr item)) 'lambda))))
;; Macro to display a progress message. (defmacro imenu-progress-message (_prevpos &optional _relpos _reverse)
;; RELPOS is the relative position to display. "Macro to display a progress message.
;; If RELPOS is nil, then the relative position in the buffer RELPOS is the relative position to display.
;; is calculated. If RELPOS is nil, then the relative position in the buffer
;; PREVPOS is the variable in which we store the last position displayed. is calculated.
(defmacro imenu-progress-message (prevpos &optional relpos reverse) PREVPOS is the variable in which we store the last position displayed."
;; Made obsolete/empty, as computers are now faster than the eye, and ;; Made obsolete/empty, as computers are now faster than the eye, and
;; it had problems updating the messages correctly, and could shadow ;; it had problems updating the messages correctly, and could shadow
...@@ -419,8 +419,7 @@ Don't move point." ...@@ -419,8 +419,7 @@ Don't move point."
(defconst imenu--rescan-item '("*Rescan*" . -99)) (defconst imenu--rescan-item '("*Rescan*" . -99))
;; The latest buffer index. ;; The latest buffer index.
;; Buffer local. (defvar-local imenu--index-alist nil
(defvar imenu--index-alist nil
"The buffer index alist computed for this buffer in Imenu. "The buffer index alist computed for this buffer in Imenu.
Simple elements in the alist look like (INDEX-NAME . POSITION). Simple elements in the alist look like (INDEX-NAME . POSITION).
...@@ -439,16 +438,12 @@ There is one simple element with negative POSITION; selecting that ...@@ -439,16 +438,12 @@ There is one simple element with negative POSITION; selecting that
element recalculates the buffer's index alist.") element recalculates the buffer's index alist.")
;;;###autoload(put 'imenu--index-alist 'risky-local-variable t) ;;;###autoload(put 'imenu--index-alist 'risky-local-variable t)
(make-variable-buffer-local 'imenu--index-alist) (defvar-local imenu--last-menubar-index-alist nil
(defvar imenu--last-menubar-index-alist nil
"The latest buffer index alist used to update the menu bar menu.") "The latest buffer index alist used to update the menu bar menu.")
(make-variable-buffer-local 'imenu--last-menubar-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'.")
;; Making this buffer local caused it not to work!
(defvar imenu--history-list nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;
...@@ -456,21 +451,18 @@ element recalculates the buffer's index alist.") ...@@ -456,21 +451,18 @@ element recalculates the buffer's index alist.")
;;; ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Sort function
;;; Sorts the items depending on their index name.
;;; An item looks like (NAME . POSITION).
;;;
(defun imenu--sort-by-name (item1 item2) (defun imenu--sort-by-name (item1 item2)
"Comparison function to sort items depending on their index name.
An item looks like (NAME . POSITION)."
(string-lessp (car item1) (car item2))) (string-lessp (car item1) (car item2)))
(defun imenu--sort-by-position (item1 item2) (defun imenu--sort-by-position (item1 item2)
(< (cdr item1) (cdr item2))) (< (cdr item1) (cdr item2)))
(defun imenu--relative-position (&optional reverse) (defun imenu--relative-position (&optional reverse)
;; Support function to calculate relative position in buffer "Support function to calculate relative position in buffer.
;; Beginning of buffer is 0 and end of buffer is 100 Beginning of buffer is 0 and end of buffer is 100
;; If REVERSE is non-nil then the beginning is 100 and the end is 0. If REVERSE is non-nil then the beginning is 100 and the end is 0."
(let ((pos (point)) (let ((pos (point))
(total (buffer-size))) (total (buffer-size)))
(and reverse (setq pos (- total pos))) (and reverse (setq pos (- total pos)))
...@@ -479,11 +471,10 @@ element recalculates the buffer's index alist.") ...@@ -479,11 +471,10 @@ element recalculates the buffer's index alist.")
(/ (1- pos) (max (/ total 100) 1)) (/ (1- pos) (max (/ total 100) 1))
(/ (* 100 (1- pos)) (max total 1))))) (/ (* 100 (1- pos)) (max total 1)))))
;; 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))
;;
;; The returned list DOES NOT share structure with LIST.
(defun imenu--split (list n) (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))
The returned list DOES NOT share structure with LIST."
(let ((remain list) (let ((remain list)
(result '()) (result '())
(sublist '()) (sublist '())
...@@ -502,20 +493,18 @@ element recalculates the buffer's index alist.") ...@@ -502,20 +493,18 @@ element recalculates the buffer's index alist.")
(push (nreverse sublist) result)) (push (nreverse sublist) result))
(nreverse result))) (nreverse result)))
;;; Split the alist MENULIST into a nested alist, if it is long enough.
;;; In any case, add TITLE to the front of the alist.
;;; If IMENU--RESCAN-ITEM is present in MENULIST, it is moved to the
;;; beginning of the returned alist.
;;;
;;; The returned alist DOES NOT share structure with MENULIST.
(defun imenu--split-menu (menulist title) (defun imenu--split-menu (menulist title)
"Split the alist MENULIST into a nested alist, if it is long enough.
In any case, add TITLE to the front of the alist.
If IMENU--RESCAN-ITEM is present in MENULIST, it is moved to the
beginning of the returned alist.
The returned alist DOES NOT share structure with MENULIST."
(let ((menulist (copy-sequence menulist)) (let ((menulist (copy-sequence menulist))
keep-at-top tail) keep-at-top)
(if (memq imenu--rescan-item menulist) (if (memq imenu--rescan-item menulist)
(setq keep-at-top (list imenu--rescan-item) (setq keep-at-top (list imenu--rescan-item)
menulist (delq imenu--rescan-item menulist))) menulist (delq imenu--rescan-item menulist)))
(setq tail menulist) (dolist (item menulist)
(dolist (item tail)
(when (imenu--subalist-p item) (when (imenu--subalist-p item)
(push item keep-at-top) (push item keep-at-top)
(setq menulist (delq item menulist)))) (setq menulist (delq item menulist))))
...@@ -530,32 +519,28 @@ element recalculates the buffer's index alist.") ...@@ -530,32 +519,28 @@ element recalculates the buffer's index alist.")
(cons title (cons title
(nconc (nreverse keep-at-top) menulist)))) (nconc (nreverse keep-at-top) menulist))))
;;; Split up each long alist that are nested within ALIST
;;; into nested alists.
;;;
;;; Return a split and sorted copy of ALIST. The returned alist DOES
;;; NOT share structure with ALIST.
(defun imenu--split-submenus (alist) (defun imenu--split-submenus (alist)
(mapcar (function "Split up each long alist that are nested within ALIST into nested alists.
(lambda (elt) Return a split and sorted copy of ALIST. The returned alist DOES
(if (and (consp elt) NOT share structure with ALIST."
(stringp (car elt)) (mapcar (lambda (elt)
(listp (cdr elt))) (if (and (consp elt)
(imenu--split-menu (cdr elt) (car elt)) (stringp (car elt))
elt))) (listp (cdr elt)))
(imenu--split-menu (cdr elt) (car elt))
elt))
alist)) alist))
;;; Truncate all strings in MENULIST to imenu-max-item-length
(defun imenu--truncate-items (menulist) (defun imenu--truncate-items (menulist)
(mapcar (function "Truncate all strings in MENULIST to `imenu-max-item-length'."
(lambda (item) (mapcar (lambda (item)
(cond (cond
((consp (cdr item)) ((consp (cdr item))
(imenu--truncate-items (cdr item))) (imenu--truncate-items (cdr item)))
;; truncate if necessary ;; truncate if necessary
((and (numberp imenu-max-item-length) ((and (numberp imenu-max-item-length)
(> (length (car item)) imenu-max-item-length)) (> (length (car item)) imenu-max-item-length))
(setcar item (substring (car item) 0 imenu-max-item-length)))))) (setcar item (substring (car item) 0 imenu-max-item-length)))))
menulist)) menulist))
...@@ -585,13 +570,12 @@ See `imenu--index-alist' for the format of the index alist." ...@@ -585,13 +570,12 @@ See `imenu--index-alist' for the format of the index alist."
;; Add a rescan option to the index. ;; Add a rescan option to the index.
(cons imenu--rescan-item imenu--index-alist)) (cons imenu--rescan-item imenu--index-alist))
;;; Find all markers in alist and makes (defvar imenu--cleanup-seen nil)
;;; them point nowhere.
;;; The top-level call uses nil as the argument;
;;; non-nil arguments are in recursive calls.
(defvar imenu--cleanup-seen)
(defun imenu--cleanup (&optional alist) (defun imenu--cleanup (&optional alist)
"Find all markers in ALIST and make them point nowhere.
If ALIST is nil (the normal case), use `imenu--index-alist'.
Non-nil arguments are in recursive calls."
;; If alist is provided use that list. ;; If alist is provided use that list.
;; If not, empty the table of lists already seen ;; If not, empty the table of lists already seen
;; and use imenu--index-alist. ;; and use imenu--index-alist.
...@@ -599,18 +583,14 @@ See `imenu--index-alist' for the format of the index alist." ...@@ -599,18 +583,14 @@ See `imenu--index-alist' for the format of the index alist."
(setq imenu--cleanup-seen (cons alist imenu--cleanup-seen)) (setq imenu--cleanup-seen (cons alist imenu--cleanup-seen))
(setq alist imenu--index-alist imenu--cleanup-seen (list alist))) (setq alist imenu--index-alist imenu--cleanup-seen (list alist)))
(and alist (when alist
(mapc (dolist (item alist)
(lambda (item) (cond
(cond ((markerp (cdr item)) (set-marker (cdr item) nil))
((markerp (cdr item)) ;; Don't process one alist twice.
(set-marker (cdr item) nil)) ((memq (cdr item) imenu--cleanup-seen))
;; Don't process one alist twice. ((imenu--subalist-p item) (imenu--cleanup (cdr item)))))
((memq (cdr item) imenu--cleanup-seen)) t))
((imenu--subalist-p item)
(imenu--cleanup (cdr item)))))
alist)
t))
(defun imenu--create-keymap (title alist &optional cmd) (defun imenu--create-keymap (title alist &optional cmd)
(list* 'keymap title (list* 'keymap title
...@@ -981,10 +961,9 @@ A trivial interface to `imenu-add-to-menubar' suitable for use in a hook." ...@@ -981,10 +961,9 @@ A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
(defvar imenu-buffer-menubar nil) (defvar imenu-buffer-menubar nil)
(defvar imenu-menubar-modified-tick 0 (defvar-local imenu-menubar-modified-tick 0
"The value of (buffer-chars-modified-tick) as of the last call "The value of (buffer-chars-modified-tick) as of the last call
to `imenu-update-menubar'.") to `imenu-update-menubar'.")
(make-variable-buffer-local 'imenu-menubar-modified-tick)
(defun imenu-update-menubar () (defun imenu-update-menubar ()
(when (and (current-local-map) (when (and (current-local-map)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment