]> code.delx.au - gnu-emacs/blob - lisp/progmodes/which-func.el
Updated to IDLWAVE v5.7 (see idlwave.org), and variable cleanup
[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, 2003, 2005
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 2, 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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 makefile-mode
80 sh-mode fortran-mode f90-mode ada-mode)
81 "List of major modes for which Which Function mode should be used.
82 For other modes it is disabled. If this is equal to t,
83 then Which Function mode is enabled in any major mode that supports it."
84 :group 'which-func
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.
90 This means that Which Function mode won't really do anything
91 until you use Imenu, in these modes. Note that files
92 larger than `which-func-maxout' behave in this way too;
93 Which 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.
99 Zero means compute the Imenu menu regardless of size."
100 :group 'which-func
101 :type 'integer)
102
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
116 (defface which-func
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."
141 :group 'which-func)
142 ;; backward-compatibility alias
143 (put 'which-func-face 'face-alias 'which-func)
144
145 (defcustom which-func-format
146 `("["
147 (:propertize which-func-current
148 local-map ,which-func-keymap
149 face which-func
150 ;;mouse-face highlight ; currently not evaluated :-(
151 help-echo "mouse-1: go to beginning, mouse-2: toggle rest visibility, mouse-3: go to end")
152 "]")
153 "Format for displaying the function in the mode line."
154 :group 'which-func
155 :type 'sexp)
156 ;;;###autoload (put 'which-func-format 'risky-local-variable t)
157
158 (defvar which-func-cleanup-function nil
159 "Function to transform a string before displaying it in the mode line.
160 The function is called with one argument, the string to display.
161 Its return value is displayed in the modeline.
162 If nil, no function is called. The default value is nil.
163
164 This feature can be useful if Imenu is set up to make more
165 detailed entries (e.g., containing the argument list of a function),
166 and you want to simplify them for the mode line
167 \(e.g., removing the parameter list to just have the function name.)")
168
169 ;;; Code, nothing to customize below here
170 ;;; -------------------------------------
171 ;;;
172 (require 'imenu)
173
174 (defvar which-func-table (make-hash-table :test 'eq :weakness 'key))
175
176 (defconst which-func-current
177 '(:eval (gethash (selected-window) which-func-table which-func-unknown)))
178 ;;;###autoload (put 'which-func-current 'risky-local-variable t)
179
180 (defvar which-func-mode nil
181 "Non-nil means display current function name in mode line.
182 This makes a difference only if `which-function-mode' is non-nil.")
183 (make-variable-buffer-local 'which-func-mode)
184 ;;(put 'which-func-mode 'permanent-local t)
185
186 (add-hook 'find-file-hook 'which-func-ff-hook t)
187
188 (defun which-func-ff-hook ()
189 "File find hook for Which Function mode.
190 It creates the Imenu index for the buffer, if necessary."
191 (setq which-func-mode
192 (and which-function-mode
193 (or (eq which-func-modes t)
194 (member major-mode which-func-modes))))
195
196 (condition-case nil
197 (if (and which-func-mode
198 (not (member major-mode which-func-non-auto-modes))
199 (or (null which-func-maxout)
200 (< buffer-saved-size which-func-maxout)
201 (= which-func-maxout 0)))
202 (setq imenu--index-alist
203 (save-excursion (funcall imenu-create-index-function))))
204 (error
205 (setq which-func-mode nil))))
206
207 (defun which-func-update ()
208 ;; "Update the Which-Function mode display for all windows."
209 ;; (walk-windows 'which-func-update-1 nil 'visible))
210 (which-func-update-1 (selected-window)))
211
212 (defun which-func-update-1 (window)
213 "Update the Which Function mode display for window WINDOW."
214 (with-selected-window window
215 (when which-func-mode
216 (condition-case info
217 (let ((current (which-function)))
218 (unless (equal current (gethash window which-func-table))
219 (puthash window current which-func-table)
220 (force-mode-line-update)))
221 (error
222 (setq which-func-mode nil)
223 (error "Error in which-func-update: %s" info))))))
224
225 ;;;###autoload
226 (defalias 'which-func-mode 'which-function-mode)
227
228 (defvar which-func-update-timer nil)
229
230 ;; This is the name people would normally expect.
231 ;;;###autoload
232 (define-minor-mode which-function-mode
233 "Toggle Which Function mode, globally.
234 When Which Function mode is enabled, the current function name is
235 continuously displayed in the mode line, in certain major modes.
236
237 With prefix ARG, turn Which Function mode on iff arg is positive,
238 and off otherwise."
239 :global t :group 'which-func
240 (if which-function-mode
241 ;;Turn it on
242 (progn
243 (setq which-func-update-timer
244 (run-with-idle-timer idle-update-delay t 'which-func-update))
245 (dolist (buf (buffer-list))
246 (with-current-buffer buf
247 (setq which-func-mode
248 (or (eq which-func-modes t)
249 (member major-mode which-func-modes))))))
250 ;; Turn it off
251 (when (timerp which-func-update-timer)
252 (cancel-timer which-func-update-timer))
253 (setq which-func-update-timer nil)
254 (dolist (buf (buffer-list))
255 (with-current-buffer buf (setq which-func-mode nil)))))
256
257 (defvar which-function-imenu-failed nil
258 "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
259
260 (defvar which-func-functions nil
261 "List of functions for `which-function' to call with no arguments.
262 It calls them sequentially, and if any returns non-nil,
263 `which-function' uses that name and stops looking for the name.")
264
265 (defun which-function ()
266 "Return current function name based on point.
267 Uses `which-function-functions', `imenu--index-alist'
268 or `add-log-current-defun-function'.
269 If no function name is found, return nil."
270 (let ((name
271 ;; Try the `which-function-functions' functions first.
272 (run-hook-with-args-until-success 'which-func-functions)))
273
274 ;; If Imenu is loaded, try to make an index alist with it.
275 (when (and (null name)
276 (boundp 'imenu--index-alist) (null imenu--index-alist)
277 (null which-function-imenu-failed))
278 (imenu--make-index-alist t)
279 (unless imenu--index-alist
280 (make-local-variable 'which-function-imenu-failed)
281 (setq which-function-imenu-failed t)))
282 ;; If we have an index alist, use it.
283 (when (and (null name)
284 (boundp 'imenu--index-alist) imenu--index-alist)
285 (let ((alist imenu--index-alist)
286 (minoffset (point-max))
287 offset elem pair mark)
288 (while alist
289 (setq elem (car-safe alist)
290 alist (cdr-safe alist))
291 ;; Elements of alist are either ("name" . marker), or
292 ;; ("submenu" ("name" . marker) ... ).
293 (unless (listp (cdr elem))
294 (setq elem (list elem)))
295 (while elem
296 (setq pair (car elem)
297 elem (cdr elem))
298 (and (consp pair)
299 (number-or-marker-p (setq mark (cdr pair)))
300 (if (>= (setq offset (- (point) mark)) 0)
301 (if (< offset minoffset) ; find the closest item
302 (setq minoffset offset
303 name (car pair)))
304 ;; Entries in order, so can skip all those after point.
305 (setq elem nil)))))))
306 ;; Try using add-log support.
307 (when (and (null name) (boundp 'add-log-current-defun-function)
308 add-log-current-defun-function)
309 (setq name (funcall add-log-current-defun-function)))
310 ;; Filter the name if requested.
311 (when name
312 (if which-func-cleanup-function
313 (funcall which-func-cleanup-function name)
314 name))))
315
316 (provide 'which-func)
317
318 ;; arch-tag: fa8a55c7-bfe3-4ffc-95ab-01bf21796827
319 ;;; which-func.el ends here