together.)
@findex imenu
+@vindex imenu-flatten
If you type @kbd{M-g i} (@code{imenu}), it reads the name of a
definition using the minibuffer, then moves point to that definition.
You can use completion to specify the name; the command always
-displays the whole list of valid names.
+displays the whole list of valid names. If you set @code{imenu-flatten}
+to a non-@code{nil} value, then instead of the nested menu
+you can select a completion candidate from the flat list.
@findex imenu-add-menubar-index
Alternatively, you can bind the command @code{imenu} to a mouse
(defcustom imenu-level-separator ":"
"The separator between index names of different levels.
-Used for making mouse-menu titles and for flattening nested indexes
-with name concatenation."
+Used for flattening nested indexes with name concatenation."
:type 'string)
+(defcustom imenu-flatten nil
+ "Whether to flatten the list of sections in an imenu or show it nested.
+If non-nil, popup the completion buffer with a flattened menu.
+The string from `imenu-level-separator' is used to separate names of
+nested levels while flattening nested indexes with name concatenation."
+ :type 'boolean
+ :version "30.1")
+
(defcustom imenu-generic-skip-comments-and-strings t
"When non-nil, ignore text inside comments and strings.
Only affects `imenu-default-create-index-function' (and any
menu)))))
(popup-menu map event)))
+(defun imenu--flatten-index-alist (index-alist &optional concat-names prefix)
+ ;; Takes a nested INDEX-ALIST and returns a flat index alist.
+ ;; If optional CONCAT-NAMES is non-nil, then a nested index has its
+ ;; name and a space concatenated to the names of the children.
+ ;; Third argument PREFIX is for internal use only.
+ (mapcan
+ (lambda (item)
+ (let* ((name (car item))
+ (pos (cdr item))
+ (new-prefix (and concat-names
+ (if prefix
+ (concat prefix imenu-level-separator name)
+ name))))
+ (cond
+ ((or (markerp pos) (numberp pos))
+ (list (cons new-prefix pos)))
+ (t
+ (imenu--flatten-index-alist pos concat-names new-prefix)))))
+ index-alist))
+
(defun imenu-choose-buffer-index (&optional prompt alist)
"Let the user select from a buffer index and return the chosen index.
;; Create a list for this buffer only when needed.
(while (eq result t)
(setq index-alist (if alist alist (imenu--make-index-alist)))
+ (when imenu-flatten
+ (setq index-alist (imenu--flatten-index-alist index-alist t)))
(setq result
(if (and imenu-use-popup-menu
(or (eq imenu-use-popup-menu t) mouse-triggered))
(interactive)
(imenu-add-to-menubar "Index"))
-(defvar imenu-buffer-menubar nil)
-
(defvar-local imenu-menubar-modified-tick 0
"Value of (buffer-chars-modified-tick) when `imenu-update-menubar' was called.")