+ "Adds an `imenu' entry to the menu bar for the current buffer.
+NAME is a string used to name the menu bar item.
+See the command `imenu' for more information."
+ (interactive "sImenu menu item name: ")
+ (if (or (and (fboundp imenu-prev-index-position-function)
+ (fboundp imenu-extract-index-name-function))
+ (and imenu-generic-expression))
+ (let ((newmap (make-sparse-keymap))
+ (menu-bar (lookup-key (current-local-map) [menu-bar])))
+ (define-key newmap [menu-bar]
+ (append (make-sparse-keymap) menu-bar))
+ (define-key newmap [menu-bar index]
+ (cons name (nconc (make-sparse-keymap "Imenu")
+ (make-sparse-keymap))))
+ (use-local-map (append newmap (current-local-map)))
+ (add-hook 'menu-bar-update-hook 'imenu-update-menubar))
+ (error "The mode `%s' does not support Imenu" mode-name)))
+
+(defvar imenu-buffer-menubar nil)
+
+(defun imenu-update-menubar ()
+ (and (current-local-map)
+ (keymapp (lookup-key (current-local-map) [menu-bar index]))
+ (let ((index-alist (imenu--make-index-alist t)))
+ ;; Don't bother updating if the index-alist has not changed
+ ;; since the last time we did it.
+ (or (equal index-alist imenu--last-menubar-index-alist)
+ (let (menu menu1 old)
+ (setq imenu--last-menubar-index-alist index-alist)
+ (setq index-alist (imenu--split-submenus index-alist))
+ (setq menu (imenu--split-menu index-alist
+ (buffer-name)))
+ (setq menu1 (imenu--create-keymap-1 (car menu)
+ (if (< 1 (length (cdr menu)))
+ (cdr menu)
+ (cdr (car (cdr menu))))
+ t))
+ (setq old (lookup-key (current-local-map) [menu-bar index]))
+ (setcdr old (cdr menu1)))))))
+
+(defun imenu--menubar-select (item)
+ "Use Imenu to select the function or variable named in this menu item."
+ (if (equal item imenu--rescan-item)
+ (progn
+ (imenu--cleanup)
+ (setq imenu--index-alist nil)
+ (imenu-update-menubar))
+ (imenu item)))
+
+(defun imenu-default-goto-function (name position &optional rest)
+ "This function is used for moving the point to POSITION.
+The NAME and REST parameters are not used, they are here just to make
+this function have the same interface as a function placed in a special
+index-item."
+ (if (or (< position (point-min))
+ (> position (point-max)))
+ ;; widen if outside narrowing
+ (widen))
+ (goto-char position))