Commit | Line | Data |
---|---|---|
5e046f6d JB |
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 | |
6b61353c | 79 | sh-mode fortran-mode f90-mode ada-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 | ||
102 | (defcustom which-func-format '("[" which-func-current "]") | |
103 | "Format for displaying the function in the mode line." | |
104 | :group 'which-func | |
105 | :type 'sexp) | |
106 | ;;;###autoload (put 'which-func-format 'risky-local-variable t) | |
107 | ||
108 | (defvar which-func-cleanup-function nil | |
109 | "Function to transform a string before displaying it in the mode line. | |
110 | The function is called with one argument, the string to display. | |
111 | Its return value is displayed in the modeline. | |
112 | If nil, no function is called. The default value is nil. | |
113 | ||
114 | This feature can be useful if Imenu is set up to make more | |
115 | detailed entries (e.g., containing the argument list of a function), | |
116 | and you want to simplify them for the mode line | |
117 | \(e.g., removing the parameter list to just have the function name.)") | |
118 | ||
119 | ;;; Code, nothing to customize below here | |
120 | ;;; ------------------------------------- | |
121 | ;;; | |
122 | (require 'imenu) | |
123 | ||
124 | (defvar which-func-table (make-hash-table :test 'eq :weakness 'key)) | |
125 | ||
126 | (defconst which-func-current | |
127 | '(:eval (gethash (selected-window) which-func-table which-func-unknown))) | |
128 | ;;;###autoload (put 'which-func-current 'risky-local-variable t) | |
129 | ||
130 | (defvar which-func-mode nil | |
131 | "Non-nil means display current function name in mode line. | |
132 | This makes a difference only if `which-function-mode' is non-nil.") | |
133 | (make-variable-buffer-local 'which-func-mode) | |
134 | ;;(put 'which-func-mode 'permanent-local t) | |
135 | ||
136 | (add-hook 'find-file-hook 'which-func-ff-hook t) | |
137 | ||
138 | (defun which-func-ff-hook () | |
139 | "File find hook for Which Function mode. | |
140 | It creates the Imenu index for the buffer, if necessary." | |
141 | (setq which-func-mode | |
142 | (and which-function-mode | |
143 | (or (eq which-func-modes t) | |
144 | (member major-mode which-func-modes)))) | |
145 | ||
146 | (condition-case nil | |
147 | (if (and which-func-mode | |
148 | (not (member major-mode which-func-non-auto-modes)) | |
149 | (or (null which-func-maxout) | |
150 | (< buffer-saved-size which-func-maxout) | |
151 | (= which-func-maxout 0))) | |
152 | (setq imenu--index-alist | |
153 | (save-excursion (funcall imenu-create-index-function)))) | |
154 | (error | |
155 | (setq which-func-mode nil)))) | |
156 | ||
157 | (defun which-func-update () | |
158 | ;; "Update the Which-Function mode display for all windows." | |
159 | ;; (walk-windows 'which-func-update-1 nil 'visible)) | |
160 | (which-func-update-1 (selected-window))) | |
161 | ||
162 | (defun which-func-update-1 (window) | |
163 | "Update the Which-Function mode display for window WINDOW." | |
164 | (with-selected-window window | |
165 | (when which-func-mode | |
166 | (condition-case info | |
167 | (let ((current (which-function))) | |
168 | (unless (equal current (gethash window which-func-table)) | |
169 | (puthash window current which-func-table) | |
170 | (force-mode-line-update))) | |
171 | (error | |
172 | (which-func-mode -1) | |
173 | (error "Error in which-func-update: %s" info)))))) | |
174 | ||
175 | ;;;###autoload | |
176 | (defalias 'which-func-mode 'which-function-mode) | |
177 | ||
178 | (defvar which-func-update-timer nil) | |
179 | ||
180 | ;; This is the name people would normally expect. | |
181 | ;;;###autoload | |
182 | (define-minor-mode which-function-mode | |
183 | "Toggle Which Function mode, globally. | |
184 | When Which Function mode is enabled, the current function name is | |
185 | continuously displayed in the mode line, in certain major modes. | |
186 | ||
187 | With prefix ARG, turn Which Function mode on iff arg is positive, | |
188 | and off otherwise." | |
189 | :global t :group 'which-func | |
190 | (if which-function-mode | |
191 | ;;Turn it on | |
192 | (progn | |
193 | (setq which-func-update-timer | |
194 | (run-with-idle-timer idle-update-delay t 'which-func-update)) | |
195 | (dolist (buf (buffer-list)) | |
196 | (with-current-buffer buf | |
197 | (setq which-func-mode | |
198 | (or (eq which-func-modes t) | |
199 | (member major-mode which-func-modes)))))) | |
200 | ;; Turn it off | |
6b61353c KH |
201 | (when (timerp which-func-update-timer) |
202 | (cancel-timer which-func-update-timer)) | |
5e046f6d JB |
203 | (setq which-func-update-timer nil) |
204 | (dolist (buf (buffer-list)) | |
205 | (with-current-buffer buf (setq which-func-mode nil))))) | |
206 | ||
207 | (defvar which-function-imenu-failed nil | |
208 | "Locally t in a buffer if `imenu--make-index-alist' found nothing there.") | |
209 | ||
6b61353c KH |
210 | (defvar which-func-functions nil |
211 | "List of functions for `which-function' to call with no arguments. | |
212 | It calls them sequentially, and if any returns non-nil, | |
213 | `which-function' uses that name and stops looking for the name.") | |
214 | ||
5e046f6d JB |
215 | (defun which-function () |
216 | "Return current function name based on point. | |
6b61353c KH |
217 | Uses `which-function-functions', `imenu--index-alist' |
218 | or `add-log-current-defun-function'. | |
5e046f6d JB |
219 | If no function name is found, return nil." |
220 | (let (name) | |
6b61353c KH |
221 | ;; Try the which-function-functions functions first. |
222 | (let ((hooks which-func-functions)) | |
223 | (while hooks | |
224 | (let ((value (funcall (car hooks)))) | |
225 | (when value | |
226 | (setq name value | |
227 | hooks nil))) | |
228 | (setq hooks (cdr hooks)))) | |
229 | ||
5e046f6d | 230 | ;; If Imenu is loaded, try to make an index alist with it. |
6b61353c KH |
231 | (when (and (null name) |
232 | (boundp 'imenu--index-alist) (null imenu--index-alist) | |
5e046f6d JB |
233 | (null which-function-imenu-failed)) |
234 | (imenu--make-index-alist) | |
235 | (unless imenu--index-alist | |
236 | (make-local-variable 'which-function-imenu-failed) | |
237 | (setq which-function-imenu-failed t))) | |
238 | ;; If we have an index alist, use it. | |
6b61353c KH |
239 | (when (and (null name) |
240 | (boundp 'imenu--index-alist) imenu--index-alist) | |
5e046f6d JB |
241 | (let ((alist imenu--index-alist) |
242 | (minoffset (point-max)) | |
243 | offset elem pair mark) | |
244 | (while alist | |
245 | (setq elem (car-safe alist) | |
246 | alist (cdr-safe alist)) | |
247 | ;; Elements of alist are either ("name" . marker), or | |
248 | ;; ("submenu" ("name" . marker) ... ). | |
249 | (unless (listp (cdr elem)) | |
250 | (setq elem (list elem))) | |
251 | (while elem | |
252 | (setq pair (car elem) | |
253 | elem (cdr elem)) | |
254 | (and (consp pair) | |
255 | (number-or-marker-p (setq mark (cdr pair))) | |
256 | (if (>= (setq offset (- (point) mark)) 0) | |
257 | (if (< offset minoffset) ; find the closest item | |
258 | (setq minoffset offset | |
259 | name (car pair))) | |
260 | ;; Entries in order, so can skip all those after point. | |
261 | (setq elem nil))))))) | |
262 | ;; Try using add-log support. | |
263 | (when (and (null name) (boundp 'add-log-current-defun-function) | |
264 | add-log-current-defun-function) | |
265 | (setq name (funcall add-log-current-defun-function))) | |
266 | ;; Filter the name if requested. | |
267 | (when name | |
268 | (if which-func-cleanup-function | |
269 | (funcall which-func-cleanup-function name) | |
270 | name)))) | |
271 | ||
272 | (provide 'which-func) | |
273 | ||
6b61353c | 274 | ;;; arch-tag: fa8a55c7-bfe3-4ffc-95ab-01bf21796827 |
5e046f6d | 275 | ;;; which-func.el ends here |