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