- (regexp (cadr pat))
- (index (caddr pat)))
- (if (and (not found) ; Only allow one entry;
- (looking-at regexp))
- (let ((beg (match-beginning index))
- (end (match-end index)))
- (setq found t)
- (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)
- (car (push
- (cons
- (imenu-create-submenu-name menu-title)
- '())
- index-alist))))))))))
- patterns))))
- (imenu-progress-message prev-pos 100 t)
- (delete 'dummy index-alist)))
+ (regexp (nth 1 pat))
+ (index (nth 2 pat))
+ (function (nth 3 pat))
+ (rest (nthcdr 4 pat)))
+ ;; Go backwards for convenience of adding items in order.
+ (goto-char (point-max))
+ (while (re-search-backward regexp nil t)
+ (imenu-progress-message prev-pos nil t)
+ (setq beg (match-beginning index))
+ ;; Add this sort of submenu only when we've found an
+ ;; item for it, avoiding empty, duff menus.
+ (unless (assoc menu-title index-alist)
+ (push (list menu-title) index-alist))
+ (if imenu-use-markers
+ (setq beg (set-marker (make-marker) beg)))
+ (let ((item
+ (if function
+ (nconc (list (match-string-no-properties index)
+ beg function)
+ rest)
+ (cons (match-string-no-properties index)
+ beg)))
+ ;; This is the desired submenu,
+ ;; starting with its title (or nil).
+ (menu (assoc menu-title index-alist)))
+ ;; Insert the item unless it is already present.
+ (unless (member item (cdr menu))
+ (setcdr menu
+ (cons item (cdr menu)))))))))
+ patterns)
+ (set-syntax-table old-table)))
+ (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)))
+ (nconc (delq main-element (delq 'dummy index-alist))
+ (cdr main-element)))))