Subject: RE: bug#12717: 24.2.50; [PATCH] `imenu--split-submenus' incorrectly distinguishes submenus

From: "Drew Adams" <[email-masked]>
Date: Mon, 29 Oct 2012 23:18:30 -0700

> This looks eminently reasonable, except that I don't 
> understand the code enough to be sure (e.g. the current test
> includes (consp elt) whereas imenu--split-menu starts right
> off by calling cdr).  Do you happen to have some kind of test
> case?

I ran into the bug when trying to make use of so-called "special" menu items.

I add two general toggle items to the menus, along with the general item
*Rescan*.  In doing that I discovered the bug.

This is the redefinition of `imenu--make-index-alist' that I use.  It is here
that I add two menu items: one to toggle sorting on/off, and one to toggle
case-sensitivity of sorting when sorting by name.

;; REPLACE ORIGINAL in `imenu.el'.
;;
;; 1. Respect `ignore-comments-flag', if defined:
;;    use `with-comments-hidden'.
;; 2. Add Imenu+ toggle commands to menu.
;;
(defun imenu--make-index-alist (&optional noerror)
  "Create an index alist for the definitions in the current buffer.
This works by using the hook function `imenu-create-index-function'.
Report an error if the list is empty unless NOERROR is supplied and
non-nil.

If `ignore-comments-flag' is defined and non-nil, then respect it,
ignoring hidden comments.

See `imenu--index-alist' for the format of the index alist."
  (or (and imenu--index-alist
           (or (not imenu-auto-rescan)
               (and imenu-auto-rescan
                    (> (buffer-size) imenu-auto-rescan-maxout))))
      ;; Get the index; truncate if necessary
      (progn (setq imenu--index-alist
                   (save-excursion
                     (save-restriction
                       (widen)
                       (if (and (> emacs-major-version 20)
                                (require 'hide-comnt nil t))
                           (let ((search-invisible  nil))
                             (with-comments-hidden
                                 (point-min) (point-max)
                                 (funcall imenu-create-index-function)))
                         (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"))
  (or imenu--index-alist  (setq imenu--index-alist  (list nil)))
  (cons imenu--rescan-item              ; `*Rescan*'.
        (cons '("Toggle Case-Sensitive Name-Sort"
                IGNORE
                (lambda (&rest _ignore)
                  (imenup-toggle-case-sensitive-sorting)))
              (cons '("Toggle Sorting"
                      IGNORE
                      (lambda (&rest _ignore)
                        (imenup-toggle-sort)))
                    imenu--index-alist))))

Clearly, adding general menu items this way is a make-do hack.  I don't use the
POSITION, and the functions invoked don't use any of the args.

And I don't know how, using this kind of menu creation, to, for example, make
the case-sensitivity toggle item inactive when the sort method is not by name.
But I didn't find a better way to add ordinary, non-indexing menu items.

Anyway, that's how I ran into the bug.

The full code is here, FWIW:
http://www.emacswiki.org/emacs-en/download/imenu%2b.el