c2ce12b6ad4b5515ddd7de08c65e146812414d0f
[bpt/emacs.git] / lisp / progmodes / which-func.el
1 ;;; which-func.el --- print current function in mode line
2
3 ;; Copyright (C) 1994, 1997-1998, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
6 ;; (doesn't seem to be responsive any more)
7 ;; Keywords: mode-line, imenu, tools
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This package prints name of function where your current point is
27 ;; located in mode line. It assumes that you work with imenu package
28 ;; and imenu--index-alist is up to date.
29
30 ;; KNOWN BUGS
31 ;; ----------
32 ;; Really this package shows not "function where the current point is
33 ;; located now", but "nearest function which defined above the current
34 ;; point". So if your current point is located after end of function
35 ;; FOO but before begin of function BAR, FOO will be displayed in mode
36 ;; line.
37 ;; - if two windows display the same buffer, both windows
38 ;; show the same `which-func' information.
39
40 ;; TODO LIST
41 ;; ---------
42 ;; 1. Dependence on imenu package should be removed. Separate
43 ;; function determination mechanism should be used to determine the end
44 ;; of a function as well as the beginning of a function.
45 ;; 2. This package should be realized with the help of overlay
46 ;; properties instead of imenu--index-alist variable.
47
48 ;;; History:
49
50 ;; THANKS TO
51 ;; ---------
52 ;; Per Abrahamsen <abraham@iesd.auc.dk>
53 ;; Some ideas (inserting in mode-line, using of post-command hook
54 ;; and toggling this mode) have been borrowed from his package
55 ;; column.el
56 ;; Peter Eisenhauer <pipe@fzi.de>
57 ;; Bug fixing in case nested indexes.
58 ;; Terry Tateyama <ttt@ursa0.cs.utah.edu>
59 ;; Suggestion to use find-file-hook for first imenu
60 ;; index building.
61
62 ;;; Code:
63
64 ;; Variables for customization
65 ;; ---------------------------
66 ;;
67 (defvar which-func-unknown "???"
68 "String to display in the mode line when current function is unknown.")
69
70 (defgroup which-func nil
71 "Display the current function name in the mode line."
72 :group 'tools
73 :version "20.3")
74
75 (defcustom which-func-modes t
76 ;; '(emacs-lisp-mode c-mode c++-mode objc-mode perl-mode cperl-mode python-mode
77 ;; makefile-mode sh-mode fortran-mode f90-mode ada-mode
78 ;; diff-mode)
79 "List of major modes for which Which Function mode should be used.
80 For other modes it is disabled. If this is equal to t,
81 then Which Function mode is enabled in any major mode that supports it."
82 :group 'which-func
83 :version "24.3" ; explicit list -> t
84 :type '(choice (const :tag "All modes" t)
85 (repeat (symbol :tag "Major mode"))))
86
87 (defcustom which-func-non-auto-modes nil
88 "List of major modes where Which Function mode is inactive till Imenu is used.
89 This means that Which Function mode won't really do anything
90 until you use Imenu, in these modes. Note that files
91 larger than `which-func-maxout' behave in this way too;
92 Which Function mode doesn't do anything until you use Imenu."
93 :group 'which-func
94 :type '(repeat (symbol :tag "Major mode")))
95
96 (defcustom which-func-maxout 500000
97 "Don't automatically compute the Imenu menu if buffer is this big or bigger.
98 Zero means compute the Imenu menu regardless of size."
99 :group 'which-func
100 :type 'integer)
101
102 (defvar which-func-keymap
103 (let ((map (make-sparse-keymap)))
104 (define-key map [mode-line mouse-1] 'beginning-of-defun)
105 (define-key map [mode-line mouse-2]
106 (lambda ()
107 (interactive)
108 (if (eq (point-min) 1)
109 (narrow-to-defun)
110 (widen))))
111 (define-key map [mode-line mouse-3] 'end-of-defun)
112 map)
113 "Keymap to display on mode line which-func.")
114
115 (defface which-func
116 ;; Whether `font-lock-function-name-face' is an appropriate face to
117 ;; inherit depends on the mode-line face; define several variants based
118 ;; on the default mode-line face.
119 '(;; The default mode-line face on a high-color display is a relatively
120 ;; light color ("grey75"), and only the light-background variant of
121 ;; `font-lock-function-name-face' is visible against it.
122 (((class color) (min-colors 88) (background light))
123 :inherit font-lock-function-name-face)
124 ;; The default mode-line face on other display types is inverse-video;
125 ;; it seems that only in the dark-background case is
126 ;; `font-lock-function-name-face' visible against it.
127 (((class grayscale mono) (background dark))
128 :inherit font-lock-function-name-face)
129 (((class color) (background light))
130 :inherit font-lock-function-name-face)
131 ;; If none of the above cases, use an explicit color chosen to contrast
132 ;; well with the default mode-line face.
133 (((class color) (min-colors 88) (background dark))
134 :foreground "Blue1")
135 (((background dark))
136 :foreground "Blue1")
137 (t
138 :foreground "LightSkyBlue"))
139 "Face used to highlight mode line function names."
140 :group 'which-func)
141
142 (defcustom which-func-format
143 `("["
144 (:propertize which-func-current
145 local-map ,which-func-keymap
146 face which-func
147 mouse-face mode-line-highlight
148 help-echo "mouse-1: go to beginning\n\
149 mouse-2: toggle rest visibility\n\
150 mouse-3: go to end")
151 "]")
152 "Format for displaying the function in the mode line."
153 :version "24.2" ; added mouse-face; 24point2 is correct
154 :group 'which-func
155 :type 'sexp)
156 ;;;###autoload (put 'which-func-format 'risky-local-variable t)
157
158 (defvar which-func-imenu-joiner-function (lambda (x) (car (last x)))
159 "Function to join together multiple levels of imenu nomenclature.
160 Called with a single argument, a list of strings giving the names
161 of the menus we had to traverse to get to the item. Returns a
162 single string, the new name of the item.")
163
164 (defvar which-func-cleanup-function nil
165 "Function to transform a string before displaying it in the mode line.
166 The function is called with one argument, the string to display.
167 Its return value is displayed in the mode line.
168 If nil, no function is called. The default value is nil.
169
170 This feature can be useful if Imenu is set up to make more
171 detailed entries (e.g., containing the argument list of a function),
172 and you want to simplify them for the mode line
173 \(e.g., removing the parameter list to just have the function name.)")
174
175 ;;; Code, nothing to customize below here
176 ;;; -------------------------------------
177 ;;;
178 (require 'imenu)
179
180 (defvar which-func-table (make-hash-table :test 'eq :weakness 'key))
181
182 (defconst which-func-current
183 '(:eval (replace-regexp-in-string
184 "%" "%%"
185 (or (gethash (selected-window) which-func-table)
186 which-func-unknown))))
187 ;;;###autoload (put 'which-func-current 'risky-local-variable t)
188
189 (defvar which-func-mode nil
190 "Non-nil means display current function name in mode line.
191 This makes a difference only if `which-function-mode' is non-nil.")
192 (make-variable-buffer-local 'which-func-mode)
193 ;;(put 'which-func-mode 'permanent-local t)
194
195 (add-hook 'find-file-hook 'which-func-ff-hook t)
196
197 (defun which-func-ff-hook ()
198 "File find hook for Which Function mode.
199 It creates the Imenu index for the buffer, if necessary."
200 (setq which-func-mode
201 (and which-function-mode
202 (or (eq which-func-modes t)
203 (member major-mode which-func-modes))))
204
205 (condition-case err
206 (if (and which-func-mode
207 (not (member major-mode which-func-non-auto-modes))
208 (or (null which-func-maxout)
209 (< buffer-saved-size which-func-maxout)
210 (= which-func-maxout 0)))
211 (setq imenu--index-alist
212 (save-excursion (funcall imenu-create-index-function))))
213 (error
214 (unless (equal err
215 '(user-error "This buffer cannot use `imenu-default-create-index-function'"))
216 (message "which-func-ff-hook error: %S" err))
217 (setq which-func-mode nil))))
218
219 (defun which-func-update ()
220 ;; "Update the Which-Function mode display for all windows."
221 ;; (walk-windows 'which-func-update-1 nil 'visible))
222 (which-func-update-1 (selected-window)))
223
224 (defun which-func-update-1 (window)
225 "Update the Which Function mode display for window WINDOW."
226 (with-selected-window window
227 (when which-func-mode
228 (condition-case info
229 (let ((current (which-function)))
230 (unless (equal current (gethash window which-func-table))
231 (puthash window current which-func-table)
232 (force-mode-line-update)))
233 (error
234 (setq which-func-mode nil)
235 (error "Error in which-func-update: %S" info))))))
236
237 ;;;###autoload
238 (define-obsolete-function-alias 'which-func-mode 'which-function-mode "24.1")
239
240 (defvar which-func-update-timer nil)
241
242 ;; This is the name people would normally expect.
243 ;;;###autoload
244 (define-minor-mode which-function-mode
245 "Toggle mode line display of current function (Which Function mode).
246 With a prefix argument ARG, enable Which Function mode if ARG is
247 positive, and disable it otherwise. If called from Lisp, enable
248 the mode if ARG is omitted or nil.
249
250 Which Function mode is a global minor mode. When enabled, the
251 current function name is continuously displayed in the mode line,
252 in certain major modes."
253 :global t :group 'which-func
254 (when (timerp which-func-update-timer)
255 (cancel-timer which-func-update-timer))
256 (setq which-func-update-timer nil)
257 (if which-function-mode
258 ;;Turn it on
259 (progn
260 (setq which-func-update-timer
261 (run-with-idle-timer idle-update-delay t 'which-func-update))
262 (dolist (buf (buffer-list))
263 (with-current-buffer buf
264 (setq which-func-mode
265 (or (eq which-func-modes t)
266 (member major-mode which-func-modes))))))
267 ;; Turn it off
268 (dolist (buf (buffer-list))
269 (with-current-buffer buf (setq which-func-mode nil)))))
270
271 (defvar which-function-imenu-failed nil
272 "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
273
274 (defvar which-func-functions nil
275 "List of functions for `which-function' to call with no arguments.
276 It calls them sequentially, and if any returns non-nil,
277 `which-function' uses that name and stops looking for the name.")
278
279 (defun which-function ()
280 "Return current function name based on point.
281 Uses `which-func-functions', `imenu--index-alist'
282 or `add-log-current-defun'.
283 If no function name is found, return nil."
284 (let ((name
285 ;; Try the `which-func-functions' functions first.
286 (run-hook-with-args-until-success 'which-func-functions)))
287
288 ;; If Imenu is loaded, try to make an index alist with it.
289 (when (and (null name)
290 (boundp 'imenu--index-alist) (null imenu--index-alist)
291 (null which-function-imenu-failed))
292 (imenu--make-index-alist t)
293 (unless imenu--index-alist
294 (set (make-local-variable 'which-function-imenu-failed) t)))
295 ;; If we have an index alist, use it.
296 (when (and (null name)
297 (boundp 'imenu--index-alist) imenu--index-alist)
298 (let ((alist imenu--index-alist)
299 (minoffset (point-max))
300 offset pair mark imstack namestack)
301 ;; Elements of alist are either ("name" . marker), or
302 ;; ("submenu" ("name" . marker) ... ). The list can be
303 ;; arbitrarily nested.
304 (while (or alist imstack)
305 (if (null alist)
306 (setq alist (car imstack)
307 namestack (cdr namestack)
308 imstack (cdr imstack))
309
310 (setq pair (car-safe alist)
311 alist (cdr-safe alist))
312
313 (cond
314 ((atom pair)) ; Skip anything not a cons.
315
316 ((imenu--subalist-p pair)
317 (setq imstack (cons alist imstack)
318 namestack (cons (car pair) namestack)
319 alist (cdr pair)))
320
321 ((number-or-marker-p (setq mark (cdr pair)))
322 (when (and (>= (setq offset (- (point) mark)) 0)
323 (< offset minoffset)) ; Find the closest item.
324 (setq minoffset offset
325 name (if (null which-func-imenu-joiner-function)
326 (car pair)
327 (funcall
328 which-func-imenu-joiner-function
329 (reverse (cons (car pair) namestack))))))))))))
330
331 ;; Try using add-log support.
332 (when (null name)
333 (setq name (add-log-current-defun)))
334 ;; Filter the name if requested.
335 (when name
336 (if which-func-cleanup-function
337 (funcall which-func-cleanup-function name)
338 name))))
339
340 \f
341 ;;; Integration with other packages
342
343 (defun which-func-update-ediff-windows ()
344 "Update Which-Function mode display for Ediff windows.
345 This function is meant to be called from `ediff-select-hook'."
346 (when (eq major-mode 'ediff-mode)
347 (when ediff-window-A
348 (which-func-update-1 ediff-window-A))
349 (when ediff-window-B
350 (which-func-update-1 ediff-window-B))
351 (when ediff-window-C
352 (which-func-update-1 ediff-window-C))))
353
354 (add-hook 'ediff-select-hook 'which-func-update-ediff-windows)
355
356 (provide 'which-func)
357
358 ;;; which-func.el ends here