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