WhichFuncMode

WhichFuncMode (also known as WhichFunctionMode) is a minor mode, that when activated displays the current function name in the mode line. It works under certain major modes, like CcMode or PerlMode. To activate it, type

  M-x which-function-mode RET

or enter the following piece of elisp into your `.emacs` file to make it permanent :

  (which-function-mode 1)

WhichFuncMode is based on ImenuMode, so if it doesn’t show correct current function name (and mode supports imenu), try

  M-x imenu RET *Rescan* RET RET

To show the function in the HeaderLine instead of the ModeLine,

(which-function-mode)

(setq mode-line-format (delete (assoc 'which-func-mode
                                      mode-line-format) mode-line-format)
      which-func-header-line-format '(which-func-mode ("" which-func-format)))
(defadvice which-func-ff-hook (after header-line activate)
  (when which-func-mode
    (setq mode-line-format (delete (assoc 'which-func-mode
                                          mode-line-format) mode-line-format)
          header-line-format which-func-header-line-format)))

(Note: Emacs 24.2.91 seems to put the which-func configuration in ‘mode-line-misc-info’ instead, so you may need to replace ‘mode-line-format’ with ‘mode-line-misc-info’ in the above snippet.)

By default, which-func mode automatically enables itself in all modes which have imenu support. To configure a subset of languages for which-func mode, set ‘which-func-modes’:

  (eval-after-load "which-func"
      '(setq which-func-modes '(java-mode c++-mode org-mode)))

To configure a language for which-func mode, you need to configure it for ImenuMode.

WhichFuncMode shows which function’s source code you are in, ElDoc shows where you are in a parameter list to a function call. They both are mode-configurable.

Non-standard languages (TL;DR;)

WhichFuncMode can be customized for non-standard language modes (including non-imenu supporting modes) using the hook ‘which-func-functions’, so adding an elisp function for a non-standard mode to this hook should work.

However, there is a gotcha. Internally, when imenu isn’t available, WhichFuncMode disables itself in the function ‘which-func-ff-hook’, which is called from the hook ‘find-file-hook’. An ugly workaround for this is to re-enable it by appending to ‘find-file-hook’. For example, for ‘foo-mode’ which provides the name of a context with `foo-whereami()’:

(defun foo-find-file-hook ()
  (when (derived-mode-p 'foo-mode)
    (which-function-mode t)
    (setq which-func-mode t)
    (add-hook 'which-func-functions 'foo-whereami t t)))

;; We need this to be run AFTER which-func-ff-hook - the "t" means append
(add-hook 'find-file-hook 'foo-find-file-hook t)

Alternatives

An alternative mode that offers similar functionality for several programming languages is SemanticIdleBreadcrumbsMode.

Another alternative mode is cfm. Cfm (display current function or method) will display class-qualified member function names (A::foo), particularly useful for C++ and Java classes that embed member function definitions (that is, without the three-box model).


CategoryProgrammerUtils CategoryNavigation