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