- (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))
+ (if (or (and imenu-prev-index-position-function
+ imenu-extract-index-name-function)
+ imenu-generic-expression
+ (not (eq imenu-create-index-function
+ 'imenu-default-create-index-function)))
+ (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)))
+
+;;;###autoload
+(defun imenu-add-menubar-index ()
+ "Add an Imenu \"Index\" entry on the menu bar for the current buffer.
+
+A trivial interface to `imenu-add-to-menubar' suitable for use in a hook."
+ (interactive)
+ (imenu-add-to-menubar "Index"))