(imenu--generic-function): Sort each submenu by position.

(imenu--sort-by-position): New function.
This commit is contained in:
Karl Heuer 1998-09-30 19:21:01 +00:00
parent 2a1c4b9034
commit c01ee596b5

View file

@ -128,7 +128,7 @@ in the buffer.
Set it to `imenu--sort-by-name' if you want alphabetic sorting. Set it to `imenu--sort-by-name' if you want alphabetic sorting.
The function should take two arguments and return T if the first The function should take two arguments and return t if the first
element should come before the second. The arguments are cons cells; element should come before the second. The arguments are cons cells;
\(NAME . POSITION). Look at `imenu--sort-by-name' for an example." \(NAME . POSITION). Look at `imenu--sort-by-name' for an example."
:type '(choice (const :tag "No sorting" nil) :type '(choice (const :tag "No sorting" nil)
@ -443,11 +443,14 @@ This variable is local in all buffers, once set.")
;;; ;;;
;;; Sort function ;;; Sort function
;;; Sorts the items depending on their index name. ;;; Sorts the items depending on their index name.
;;; An item look like (NAME . POSITION). ;;; An item looks like (NAME . POSITION).
;;; ;;;
(defun imenu--sort-by-name (item1 item2) (defun imenu--sort-by-name (item1 item2)
(string-lessp (car item1) (car item2))) (string-lessp (car item1) (car item2)))
(defun imenu--sort-by-position (item1 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
@ -814,15 +817,24 @@ PATTERNS."
rest) rest)
(cons (match-string-no-properties index) (cons (match-string-no-properties index)
beg))) beg)))
(menu (cdr (assoc menu-title index-alist)))) ;; This is the desired submenu,
;; avoid duplicates from, e.g. cc-mode patterns ;; starting with its title (or nil).
(unless (member item menu) (menu (assoc menu-title index-alist)))
;; insert the item after the (sub-)menu title ;; Insert the item unless it is already present.
(setcdr (assoc menu-title index-alist) (unless (member item (cdr menu))
(cons item menu)))))))) (setcdr menu
(cons item (cdr menu)))))))))
patterns) patterns)
(set-syntax-table old-table))) (set-syntax-table old-table)))
(imenu-progress-message prev-pos 100 t) (imenu-progress-message prev-pos 100 t)
;; Sort each submenu by position.
;; This is in case one submenu gets items from two different regexps.
(let ((tail index-alist))
(while tail
(if (listp (car tail))
(setcdr (car tail)
(sort (cdr (car tail)) 'imenu--sort-by-position)))
(setq tail (cdr tail))))
(let ((main-element (assq nil index-alist))) (let ((main-element (assq nil index-alist)))
(nconc (delq main-element (delq 'dummy index-alist)) (nconc (delq main-element (delq 'dummy index-alist))
(cdr main-element))))) (cdr main-element)))))