Subject: bug#8725: imenu--split-menu should not deliver surprises

From: Hui Liu <[email-masked]>
Date: Sat, 12 Apr 2014 21:52:30 +0800

Hi. This bug still exists in 24.4.50. An example:

    emacs -Q
    M-x org-mode
    ;;; type the following text
    * section 1
    * section 2
    ** 2.1
    ** 2.2
    * section 3
    * section 4
    ** 4.1
    ** 4.2
    ;;; end
    M-x imenu-add-menubar-index

Then Imenu produces a confusing menu like this:

    section 2
    |—2.1
    |—2.2
    section 4
    |—4.1
    |—4.2
    section 1
    section 3

and the reason is Imenu put items with submenus at the top. Such
behaviour may be reasonable for programming modes, but is clearly
unnecessary for text modes.

How about making a discrimination in imenu--split-menu?

--- a/lisp/imenu.el
+++ b/lisp/imenu.el
@@ -537,10 +537,11 @@ The returned alist DOES NOT share structure with
MENULIST."
     (if (memq imenu--rescan-item menulist)
        (setq keep-at-top (list imenu--rescan-item)
              menulist (delq imenu--rescan-item menulist)))
-    (dolist (item menulist)
+    (unless (derived-mode-p 'text-mode)
+     (dolist (item menulist)
       (when (imenu--subalist-p item)
        (push item keep-at-top)
-       (setq menulist (delq item menulist))))
+       (setq menulist (delq item menulist)))))
     (if imenu-sort-function
        (setq menulist (sort menulist imenu-sort-function)))
     (if (> (length menulist) imenu-max-items)