1 ;;; which-func.el --- Print current function in mode line
3 ;; Copyright (C) 1994, 1997 Free Software Foundation, Inc.
5 ;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
6 ;; Keywords: mode-line imenu
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;;; This package prints name of function where your current point is
28 ;;; located in mode line. It assumes that you work with imenu package
29 ;;; and imenu--index-alist is up to date.
33 ;;; Really this package shows not "function where the current point
34 ;;; is located now", but "nearest function which defined above the
35 ;;; current point". So if your current point is located after end of
36 ;;; function FOO but before begin of function BAR, FOO will be
37 ;;; displayed in mode line.
41 ;;; 1. Dependence on imenu package should be removed. Separate
42 ;;; function determination mechanism should be used to determine the end
43 ;;; of a function as well as the beginning of a function.
44 ;;; 2. This package should be realized with the help of overlay
45 ;;; properties instead of imenu--index-alist variable.
49 ;;; Per Abrahamsen <abraham@iesd.auc.dk>
50 ;;; Some ideas (inserting in mode-line, using of post-command hook
51 ;;; and toggling this mode) have been borrowed from his package
53 ;;; Peter Eisenhauer <pipe@fzi.de>
54 ;;; Bug fixing in case nested indexes.
55 ;;; Terry Tateyama <ttt@ursa0.cs.utah.edu>
56 ;;; Suggestion to use find-file-hooks for first imenu
59 ;;; Variables for customization
60 ;;; ---------------------------
62 (defvar which-func-unknown "???"
63 "String to display in the mode line when current function is unknown.")
65 (defcustom which-func-modes
66 '(emacs-lisp-mode c-mode c++-mode perl-mode makefile-mode sh-mode)
67 "List of major modes for which `which-func-mode' should be used.
68 For other modes it is disabled. If this is equal to t,
69 then current-function recognition is enabled in any mode."
71 :type '(choice (const :tag "All modes" t)
72 (list (symbol :tag "Major mode"))))
74 (defcustom which-func-amodes '(emacs-lisp-mode c-mode c++-mode)
75 "List of major modes for which the Imenu menu should be computed automatically.
76 It is computed when you visit a file in one of these major modes.
77 If the variable's value is nil, then the Imenu menu is not computed
78 automatically in any mode. Note that the menu is never computed
79 automatically if the buffer size exceeds `which-func-maxout'."
81 :type '(list (symbol :tag "Major mode")))
83 (defcustom which-func-maxout 100000
84 "Don't automatically compute the Imenu menu if buffer is this big or bigger.
85 Zero means compute the Imenu menu regardless of size."
89 (defcustom which-func-format '(" [" which-func-current "]")
90 "Format for displaying the function in the mode line."
94 ;;; Code, nothing to customize below here
95 ;;; -------------------------------------
99 (defvar which-func-current which-func-unknown)
100 (defvar which-func-previous which-func-unknown)
101 (make-variable-buffer-local 'which-func-current)
102 (make-variable-buffer-local 'which-func-previous)
104 (defvar which-func-mode-global nil
105 "Non-nil means display current function name in mode line.")
107 (defvar which-func-mode nil
108 "Non-nil means display current function name in mode line.
109 This makes a difference only if which-func-mode-global is non-nil")
110 (make-variable-buffer-local 'which-func-mode)
111 (put 'which-func-mode 'permanent-local t)
113 (add-hook 'find-file-hooks 'which-func-ff-hook t)
115 (defun which-func-ff-hook ()
116 "File find hook for Which Function mode.
117 It creates the Imenu index for the buffer, if necessary."
118 (if (or (eq which-func-modes t) (member major-mode which-func-modes))
119 (setq which-func-mode which-func-mode-global)
120 (setq which-func-mode nil))
122 (if (and which-func-mode
123 (member major-mode which-func-amodes)
124 (or (< buffer-saved-size which-func-maxout)
125 (= which-func-maxout 0)))
126 (setq imenu--index-alist
127 (save-excursion (funcall imenu-create-index-function)))))
129 (defun which-func-update ()
130 ;; Update the string containing the current function.
133 (if (not (setq which-func-current (which-function)))
134 (setq which-func-current which-func-unknown))
135 (if (not (string= which-func-current which-func-previous))
137 (force-mode-line-update)
138 (setq which-func-previous which-func-current))))
141 (remove-hook 'post-command-hook 'which-func-update)
142 (which-func-mode -1) ; Function mode off
143 (message "Error in which-func-update: %s" info)))
145 (defun which-func-mode (&optional arg)
146 "Toggle Which Function mode, globally.
147 When Which Function mode is enabled, the current function name is
148 continuously displayed in the mode line, in certain major modes.
150 With prefix arg, turn Which Function mode on iff arg is positive,
153 (if (or (and (null arg) which-func-mode-global)
154 (<= (prefix-numeric-value arg) 0))
156 (if which-func-mode-global
158 (remove-hook 'post-command-hook 'which-func-update)
159 (setq which-func-mode-global nil)
160 (setq which-func-mode nil)
161 (force-mode-line-update)))
163 (if which-func-mode-global
165 (add-hook 'post-command-hook 'which-func-update)
166 (setq which-func-mode-global t)
167 (setq which-func-mode
168 (or (eq which-func-modes t)
169 (member major-mode which-func-modes))))))
171 (defun which-function ()
172 "Returns current function name based on point.
173 If imenu--index-alist does not exist, or is empty or if point
174 is located before first function, returns nil."
176 (boundp 'imenu--index-alist)
178 (let ((pair (car-safe imenu--index-alist))
179 (rest (cdr-safe imenu--index-alist))
181 (while (and pair (or (not (number-or-marker-p (cdr pair)))
182 (> (point) (cdr pair))))
183 (setq name (car pair))
184 (setq pair (car-safe rest))
185 (setq rest (cdr-safe rest)))
188 (provide 'which-func)
190 ;; which-func.el ends here