Mercurial > emacs
annotate lisp/progmodes/which-func.el @ 57401:b1bdc11085c9
(lisp-fill-paragraph): Fix backslashes.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Sat, 09 Oct 2004 14:28:12 +0000 |
parents | f54e121491eb |
children | 212ad195d26f d8411455de48 |
rev | line source |
---|---|
51349 | 1 ;;; which-func.el --- print current function in mode line |
2 | |
3 ;; Copyright (C) 1994, 1997, 1998, 2001, 2003 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 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 | |
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. | |
39 ;; - if two windows display the same buffer, both windows | |
40 ;; show the same `which-func' information. | |
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> | |
61 ;; Suggestion to use find-file-hook for first imenu | |
62 ;; index building. | |
63 | |
64 ;;; Code: | |
65 | |
66 ;; Variables for customization | |
67 ;; --------------------------- | |
68 ;; | |
69 (defvar which-func-unknown "???" | |
70 "String to display in the mode line when current function is unknown.") | |
71 | |
72 (defgroup which-func nil | |
73 "Mode to display the current function name in the modeline." | |
74 :group 'tools | |
75 :version "20.3") | |
76 | |
77 (defcustom which-func-modes | |
78 '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode makefile-mode | |
52691
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
79 sh-mode fortran-mode f90-mode ada-mode) |
51349 | 80 "List of major modes for which Which Function mode should be used. |
81 For other modes it is disabled. If this is equal to t, | |
82 then Which Function mode is enabled in any major mode that supports it." | |
83 :group 'which-func | |
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 | |
56442
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
102 (defvar which-func-keymap |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
103 (let ((map (make-sparse-keymap))) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
104 (define-key map [mode-line mouse-1] 'beginning-of-defun) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
105 (define-key map [mode-line mouse-2] |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
106 (lambda () |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
107 (interactive) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
108 (if (eq (point-min) 1) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
109 (narrow-to-defun) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
110 (widen)))) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
111 (define-key map [mode-line mouse-3] 'end-of-defun) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
112 map) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
113 "Keymap to display on mode line which-func.") |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
114 |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
115 (defface which-func-face |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
116 '((t (:inherit font-lock-function-name-face))) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
117 "Face used to highlight mode line function names. |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
118 Defaults to `font-lock-function-name-face' if font-lock is loaded." |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
119 :group 'which-func) |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
120 |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
121 (defcustom which-func-format |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
122 `("[" |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
123 (:propertize which-func-current |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
124 local-map ,which-func-keymap |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
125 face which-func-face |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
126 ;;mouse-face highlight ; currently not evaluated :-( |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
127 help-echo "mouse-1: go to beginning, mouse-2: toggle rest visibility, mouse-3: go to end") |
e8032355ca23
(which-func-keymap): New var.
Daniel Pfeiffer <occitan@esperanto.org>
parents:
53618
diff
changeset
|
128 "]") |
51349 | 129 "Format for displaying the function in the mode line." |
130 :group 'which-func | |
131 :type 'sexp) | |
132 ;;;###autoload (put 'which-func-format 'risky-local-variable t) | |
133 | |
134 (defvar which-func-cleanup-function nil | |
135 "Function to transform a string before displaying it in the mode line. | |
136 The function is called with one argument, the string to display. | |
137 Its return value is displayed in the modeline. | |
138 If nil, no function is called. The default value is nil. | |
139 | |
140 This feature can be useful if Imenu is set up to make more | |
141 detailed entries (e.g., containing the argument list of a function), | |
142 and you want to simplify them for the mode line | |
143 \(e.g., removing the parameter list to just have the function name.)") | |
144 | |
145 ;;; Code, nothing to customize below here | |
146 ;;; ------------------------------------- | |
147 ;;; | |
148 (require 'imenu) | |
149 | |
150 (defvar which-func-table (make-hash-table :test 'eq :weakness 'key)) | |
151 | |
152 (defconst which-func-current | |
153 '(:eval (gethash (selected-window) which-func-table which-func-unknown))) | |
154 ;;;###autoload (put 'which-func-current 'risky-local-variable t) | |
155 | |
156 (defvar which-func-mode nil | |
157 "Non-nil means display current function name in mode line. | |
158 This makes a difference only if `which-function-mode' is non-nil.") | |
159 (make-variable-buffer-local 'which-func-mode) | |
160 ;;(put 'which-func-mode 'permanent-local t) | |
161 | |
162 (add-hook 'find-file-hook 'which-func-ff-hook t) | |
163 | |
164 (defun which-func-ff-hook () | |
165 "File find hook for Which Function mode. | |
166 It creates the Imenu index for the buffer, if necessary." | |
167 (setq which-func-mode | |
168 (and which-function-mode | |
169 (or (eq which-func-modes t) | |
170 (member major-mode which-func-modes)))) | |
171 | |
172 (condition-case nil | |
173 (if (and which-func-mode | |
174 (not (member major-mode which-func-non-auto-modes)) | |
175 (or (null which-func-maxout) | |
176 (< buffer-saved-size which-func-maxout) | |
177 (= which-func-maxout 0))) | |
178 (setq imenu--index-alist | |
179 (save-excursion (funcall imenu-create-index-function)))) | |
180 (error | |
181 (setq which-func-mode nil)))) | |
182 | |
183 (defun which-func-update () | |
184 ;; "Update the Which-Function mode display for all windows." | |
185 ;; (walk-windows 'which-func-update-1 nil 'visible)) | |
186 (which-func-update-1 (selected-window))) | |
187 | |
188 (defun which-func-update-1 (window) | |
56689
f54e121491eb
(which-func-update-1): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
56442
diff
changeset
|
189 "Update the Which Function mode display for window WINDOW." |
51349 | 190 (with-selected-window window |
191 (when which-func-mode | |
192 (condition-case info | |
193 (let ((current (which-function))) | |
194 (unless (equal current (gethash window which-func-table)) | |
195 (puthash window current which-func-table) | |
196 (force-mode-line-update))) | |
197 (error | |
198 (which-func-mode -1) | |
199 (error "Error in which-func-update: %s" info)))))) | |
200 | |
201 ;;;###autoload | |
202 (defalias 'which-func-mode 'which-function-mode) | |
203 | |
204 (defvar which-func-update-timer nil) | |
205 | |
206 ;; This is the name people would normally expect. | |
207 ;;;###autoload | |
208 (define-minor-mode which-function-mode | |
209 "Toggle Which Function mode, globally. | |
210 When Which Function mode is enabled, the current function name is | |
211 continuously displayed in the mode line, in certain major modes. | |
212 | |
213 With prefix ARG, turn Which Function mode on iff arg is positive, | |
214 and off otherwise." | |
215 :global t :group 'which-func | |
216 (if which-function-mode | |
217 ;;Turn it on | |
218 (progn | |
219 (setq which-func-update-timer | |
220 (run-with-idle-timer idle-update-delay t 'which-func-update)) | |
221 (dolist (buf (buffer-list)) | |
222 (with-current-buffer buf | |
223 (setq which-func-mode | |
224 (or (eq which-func-modes t) | |
225 (member major-mode which-func-modes)))))) | |
226 ;; Turn it off | |
53618
bd715b02e44e
David Ponce <david@dponce.com>
Glenn Morris <rgm@gnu.org>
parents:
52691
diff
changeset
|
227 (when (timerp which-func-update-timer) |
bd715b02e44e
David Ponce <david@dponce.com>
Glenn Morris <rgm@gnu.org>
parents:
52691
diff
changeset
|
228 (cancel-timer which-func-update-timer)) |
51349 | 229 (setq which-func-update-timer nil) |
230 (dolist (buf (buffer-list)) | |
231 (with-current-buffer buf (setq which-func-mode nil))))) | |
232 | |
233 (defvar which-function-imenu-failed nil | |
234 "Locally t in a buffer if `imenu--make-index-alist' found nothing there.") | |
235 | |
52691
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
236 (defvar which-func-functions nil |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
237 "List of functions for `which-function' to call with no arguments. |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
238 It calls them sequentially, and if any returns non-nil, |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
239 `which-function' uses that name and stops looking for the name.") |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
240 |
51349 | 241 (defun which-function () |
242 "Return current function name based on point. | |
52691
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
243 Uses `which-function-functions', `imenu--index-alist' |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
244 or `add-log-current-defun-function'. |
51349 | 245 If no function name is found, return nil." |
246 (let (name) | |
52691
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
247 ;; Try the which-function-functions functions first. |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
248 (let ((hooks which-func-functions)) |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
249 (while hooks |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
250 (let ((value (funcall (car hooks)))) |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
251 (when value |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
252 (setq name value |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
253 hooks nil))) |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
254 (setq hooks (cdr hooks)))) |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
255 |
51349 | 256 ;; If Imenu is loaded, try to make an index alist with it. |
52691
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
257 (when (and (null name) |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
258 (boundp 'imenu--index-alist) (null imenu--index-alist) |
51349 | 259 (null which-function-imenu-failed)) |
260 (imenu--make-index-alist) | |
261 (unless imenu--index-alist | |
262 (make-local-variable 'which-function-imenu-failed) | |
263 (setq which-function-imenu-failed t))) | |
264 ;; If we have an index alist, use it. | |
52691
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
265 (when (and (null name) |
ccda1b53b035
(which-func-modes): Add ada-mode.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
266 (boundp 'imenu--index-alist) imenu--index-alist) |
51349 | 267 (let ((alist imenu--index-alist) |
268 (minoffset (point-max)) | |
269 offset elem pair mark) | |
270 (while alist | |
271 (setq elem (car-safe alist) | |
272 alist (cdr-safe alist)) | |
273 ;; Elements of alist are either ("name" . marker), or | |
274 ;; ("submenu" ("name" . marker) ... ). | |
275 (unless (listp (cdr elem)) | |
276 (setq elem (list elem))) | |
277 (while elem | |
278 (setq pair (car elem) | |
279 elem (cdr elem)) | |
280 (and (consp pair) | |
281 (number-or-marker-p (setq mark (cdr pair))) | |
282 (if (>= (setq offset (- (point) mark)) 0) | |
283 (if (< offset minoffset) ; find the closest item | |
284 (setq minoffset offset | |
285 name (car pair))) | |
286 ;; Entries in order, so can skip all those after point. | |
287 (setq elem nil))))))) | |
288 ;; Try using add-log support. | |
289 (when (and (null name) (boundp 'add-log-current-defun-function) | |
290 add-log-current-defun-function) | |
291 (setq name (funcall add-log-current-defun-function))) | |
292 ;; Filter the name if requested. | |
293 (when name | |
294 (if which-func-cleanup-function | |
295 (funcall which-func-cleanup-function name) | |
296 name)))) | |
297 | |
298 (provide 'which-func) | |
299 | |
52401 | 300 ;;; arch-tag: fa8a55c7-bfe3-4ffc-95ab-01bf21796827 |
51349 | 301 ;;; which-func.el ends here |