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