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