Trailing whitespace deleted.
[bpt/emacs.git] / lisp / which-func.el
CommitLineData
e8af40ee 1;;; which-func.el --- print current function in mode line
d6d2fa1f 2
e0f82ad8 3;; Copyright (C) 1994, 1997, 1998, 2001 Free Software Foundation, Inc.
d6d2fa1f
RS
4
5;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
b6d95ba5 6;; (doesn't seem to be responsive any more)
0211dda3 7;; Keywords: mode-line, imenu, tools
d6d2fa1f
RS
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
0211dda3
DL
26;;; Commentary:
27
28;; This package prints name of function where your current point is
29;; located in mode line. It assumes that you work with imenu package
30;; and imenu--index-alist is up to date.
31
32;; KNOWN BUGS
33;; ----------
34;; Really this package shows not "function where the current point is
35;; located now", but "nearest function which defined above the current
36;; point". So if your current point is located after end of function
37;; FOO but before begin of function BAR, FOO will be displayed in mode
38;; line.
d530d358
SM
39;; - if two windows display the same buffer, both windows
40;; show the same `which-func' information.
0211dda3
DL
41
42;; TODO LIST
43;; ---------
44;; 1. Dependence on imenu package should be removed. Separate
45;; function determination mechanism should be used to determine the end
46;; of a function as well as the beginning of a function.
47;; 2. This package should be realized with the help of overlay
48;; properties instead of imenu--index-alist variable.
49
50;;; History:
51
52;; THANKS TO
53;; ---------
54;; Per Abrahamsen <abraham@iesd.auc.dk>
55;; Some ideas (inserting in mode-line, using of post-command hook
56;; and toggling this mode) have been borrowed from his package
57;; column.el
58;; Peter Eisenhauer <pipe@fzi.de>
59;; Bug fixing in case nested indexes.
60;; Terry Tateyama <ttt@ursa0.cs.utah.edu>
1a4914f3 61;; Suggestion to use find-file-hook for first imenu
0211dda3
DL
62;; index building.
63
64;;; Code:
65
66;; Variables for customization
67;; ---------------------------
f1180544 68;;
d6d2fa1f
RS
69(defvar which-func-unknown "???"
70 "String to display in the mode line when current function is unknown.")
71
daa02ea5
DN
72(defgroup which-func nil
73 "Mode to display the current function name in the modeline."
74 :group 'tools
75 :version "20.3")
76
7a513c47 77(defcustom which-func-modes
d530d358
SM
78 '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode makefile-mode
79 sh-mode fortran-mode)
5686b9d4 80 "List of major modes for which Which Function mode should be used.
d6d2fa1f 81For other modes it is disabled. If this is equal to t,
5686b9d4 82then Which Function mode is enabled in any major mode that supports it."
d6d2fa1f
RS
83 :group 'which-func
84 :type '(choice (const :tag "All modes" t)
5686b9d4 85 (repeat (symbol :tag "Major mode"))))
d6d2fa1f 86
4962cb1a
RS
87(defcustom which-func-non-auto-modes nil
88 "List of major modes where Which Function mode is inactive till Imenu is used.
5686b9d4
RS
89This means that Which Function mode won't really do anything
90until you use Imenu, in these modes. Note that files
91larger than `which-func-maxout' behave in this way too;
92Which Function mode doesn't do anything until you use Imenu."
d6d2fa1f 93 :group 'which-func
5686b9d4 94 :type '(repeat (symbol :tag "Major mode")))
d6d2fa1f 95
10e65e41 96(defcustom which-func-maxout 500000
d6d2fa1f
RS
97 "Don't automatically compute the Imenu menu if buffer is this big or bigger.
98Zero means compute the Imenu menu regardless of size."
99 :group 'which-func
100 :type 'integer)
101
7a513c47 102(defcustom which-func-format '("[" which-func-current "]")
d6d2fa1f
RS
103 "Format for displaying the function in the mode line."
104 :group 'which-func
105 :type 'sexp)
106
1e68f8d0
RS
107(defvar which-func-cleanup-function nil
108 "Function to transform a string before displaying it in the mode line.
109The function is called with one argument, the string to display.
110Its return value is displayed in the modeline.
111If nil, no function is called. The default value is nil.
112
113This feature can be useful if Imenu is set up to make more
114detailed entries (e.g., containing the argument list of a function),
115and you want to simplify them for the mode line
116\(e.g., removing the parameter list to just have the function name.)")
117
d6d2fa1f
RS
118;;; Code, nothing to customize below here
119;;; -------------------------------------
120;;;
121(require 'imenu)
122
123(defvar which-func-current which-func-unknown)
124(defvar which-func-previous which-func-unknown)
125(make-variable-buffer-local 'which-func-current)
126(make-variable-buffer-local 'which-func-previous)
127
d6d2fa1f
RS
128(defvar which-func-mode nil
129 "Non-nil means display current function name in mode line.
3c3e43ef 130This makes a difference only if `which-function-mode' is non-nil.")
d6d2fa1f 131(make-variable-buffer-local 'which-func-mode)
7a513c47 132;;(put 'which-func-mode 'permanent-local t)
d6d2fa1f 133
1a4914f3 134(add-hook 'find-file-hook 'which-func-ff-hook t)
d6d2fa1f
RS
135
136(defun which-func-ff-hook ()
137 "File find hook for Which Function mode.
138It creates the Imenu index for the buffer, if necessary."
7a513c47 139 (setq which-func-mode
e0f82ad8
GM
140 (and which-function-mode
141 (or (eq which-func-modes t)
142 (member major-mode which-func-modes))))
d6d2fa1f 143
2576f5d2
KH
144 (condition-case nil
145 (if (and which-func-mode
146 (not (member major-mode which-func-non-auto-modes))
7a513c47
SM
147 (or (null which-func-maxout)
148 (< buffer-saved-size which-func-maxout)
2576f5d2
KH
149 (= which-func-maxout 0)))
150 (setq imenu--index-alist
151 (save-excursion (funcall imenu-create-index-function))))
152 (error
153 (setq which-func-mode nil))))
d6d2fa1f
RS
154
155(defun which-func-update ()
ecafbba2
RS
156 "Update the Which-Function mode display for all windows."
157 (walk-windows 'which-func-update-1 nil 'visible))
158
159(defun which-func-update-1 (window)
160 "Update the Which-Function mode display for window WINDOW."
161 (save-selected-window
162 (select-window window)
163 ;; Update the string containing the current function.
164 (when which-func-mode
165 (condition-case info
166 (progn
167 (setq which-func-current (or (which-function) which-func-unknown))
168 (unless (string= which-func-current which-func-previous)
169 (force-mode-line-update)
170 (setq which-func-previous which-func-current)))
171 (error
172 (which-func-mode -1)
173 (error "Error in which-func-update: %s" info))))))
d6d2fa1f 174
d530d358 175;;;###autoload
10e65e41
RS
176(defalias 'which-func-mode 'which-function-mode)
177
178;; This is the name people would normally expect.
0211dda3 179;;;###autoload
10e65e41 180(define-minor-mode which-function-mode
d6d2fa1f
RS
181 "Toggle Which Function mode, globally.
182When Which Function mode is enabled, the current function name is
183continuously displayed in the mode line, in certain major modes.
184
7a513c47 185With prefix ARG, turn Which Function mode on iff arg is positive,
d6d2fa1f 186and off otherwise."
d530d358 187 :global t :group 'which-func
e0f82ad8 188 (if which-function-mode
532f5197 189 ;;Turn it on
7a513c47 190 (progn
532f5197
SM
191 (add-hook 'post-command-idle-hook 'which-func-update)
192 (dolist (buf (buffer-list))
193 (with-current-buffer buf
194 (setq which-func-mode
195 (or (eq which-func-modes t)
196 (member major-mode which-func-modes))))))
197 ;; Turn it off
198 (remove-hook 'post-command-idle-hook 'which-func-update)
7a513c47 199 (dolist (buf (buffer-list))
532f5197 200 (with-current-buffer buf (setq which-func-mode nil)))))
d6d2fa1f 201
ecafbba2
RS
202(defvar which-function-imenu-failed nil
203 "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
204
d6d2fa1f 205(defun which-function ()
0211dda3 206 "Return current function name based on point.
d530d358
SM
207Uses `imenu--index-alist' or `add-log-current-defun-function'.
208If no function name is found, return nil."
7a513c47 209 (let (name)
ecafbba2
RS
210 ;; If Imenu is loaded, try to make an index alist with it.
211 (when (and (boundp 'imenu--index-alist) (null imenu--index-alist)
212 (null which-function-imenu-failed))
213 (imenu--make-index-alist)
214 (unless imenu--index-alist
215 (make-local-variable 'which-function-imenu-failed)
216 (setq which-function-imenu-failed t)))
217 ;; If we have an index alist, use it.
7a513c47
SM
218 (when (and (boundp 'imenu--index-alist) imenu--index-alist)
219 (let ((pair (car-safe imenu--index-alist))
220 (rest (cdr-safe imenu--index-alist)))
221 (while (and (or rest pair)
222 (or (not (number-or-marker-p (cdr pair)))
223 (> (point) (cdr pair))))
224 (setq name (car pair))
225 (setq pair (car-safe rest))
226 (setq rest (cdr-safe rest)))))
227 ;; Try using add-log support.
228 (when (and (null name) (boundp 'add-log-current-defun-function)
229 add-log-current-defun-function)
230 (setq name (funcall add-log-current-defun-function)))
231 ;; Filter the name if requested.
232 (when name
233 (if which-func-cleanup-function
234 (funcall which-func-cleanup-function name)
235 name))))
d6d2fa1f
RS
236
237(provide 'which-func)
238
e8af40ee 239;;; which-func.el ends here