Merge branch 'master' into comment-cache
[emacs.git] / lisp / progmodes / which-func.el
blob41513340e128092322721ac422dd44b86dd45c47
1 ;;; which-func.el --- print current function in mode line -*- lexical-binding:t -*-
3 ;; Copyright (C) 1994, 1997-1998, 2001-2017 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
7 ;; (doesn't seem to be responsive any more)
8 ;; Keywords: mode-line, imenu, tools
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
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.
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.
49 ;;; History:
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.
63 ;;; Code:
65 ;; Variables for customization
66 ;; ---------------------------
68 (defvar which-func-unknown "???"
69 "String to display in the mode line when current function is unknown.")
71 (defgroup which-func nil
72 "Display the current function name in the mode line."
73 :group 'tools
74 :version "20.3")
76 (defcustom which-func-modes t
77 ;; '(emacs-lisp-mode c-mode c++-mode objc-mode perl-mode cperl-mode python-mode
78 ;; makefile-mode sh-mode fortran-mode f90-mode ada-mode
79 ;; diff-mode)
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 :version "24.3" ; explicit list -> t
84 :type '(choice (const :tag "All modes" t)
85 (repeat (symbol :tag "Major mode"))))
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 :type '(repeat (symbol :tag "Major mode")))
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 :type 'integer)
100 (defvar which-func-keymap
101 (let ((map (make-sparse-keymap)))
102 (define-key map [mode-line mouse-1] 'beginning-of-defun)
103 (define-key map [mode-line mouse-2]
104 (lambda ()
105 (interactive)
106 (if (eq (point-min) 1)
107 (narrow-to-defun)
108 (widen))))
109 (define-key map [mode-line mouse-3] 'end-of-defun)
110 map)
111 "Keymap to display on mode line which-func.")
113 (defface which-func
114 ;; Whether `font-lock-function-name-face' is an appropriate face to
115 ;; inherit depends on the mode-line face; define several variants based
116 ;; on the default mode-line face.
117 '(;; The default mode-line face on a high-color display is a relatively
118 ;; light color ("grey75"), and only the light-background variant of
119 ;; `font-lock-function-name-face' is visible against it.
120 (((class color) (min-colors 88) (background light))
121 :inherit font-lock-function-name-face)
122 ;; The default mode-line face on other display types is inverse-video;
123 ;; it seems that only in the dark-background case is
124 ;; `font-lock-function-name-face' visible against it.
125 (((class grayscale mono) (background dark))
126 :inherit font-lock-function-name-face)
127 (((class color) (background light))
128 :inherit font-lock-function-name-face)
129 ;; If none of the above cases, use an explicit color chosen to contrast
130 ;; well with the default mode-line face.
131 (((class color) (min-colors 88) (background dark))
132 :foreground "Blue1")
133 (((background dark))
134 :foreground "Blue1")
136 :foreground "LightSkyBlue"))
137 "Face used to highlight mode line function names.")
139 (defcustom which-func-format
140 `("["
141 (:propertize which-func-current
142 local-map ,which-func-keymap
143 face which-func
144 mouse-face mode-line-highlight
145 help-echo "mouse-1: go to beginning\n\
146 mouse-2: toggle rest visibility\n\
147 mouse-3: go to end")
148 "]")
149 "Format for displaying the function in the mode line."
150 :version "24.2" ; added mouse-face; 24point2 is correct
151 :type 'sexp)
152 ;;;###autoload (put 'which-func-format 'risky-local-variable t)
154 (defvar which-func-imenu-joiner-function (lambda (x) (car (last x)))
155 "Function to join together multiple levels of imenu nomenclature.
156 Called with a single argument, a list of strings giving the names
157 of the menus we had to traverse to get to the item. Returns a
158 single string, the new name of the item.")
160 (defvar which-func-cleanup-function nil
161 "Function to transform a string before displaying it in the mode line.
162 The function is called with one argument, the string to display.
163 Its return value is displayed in the mode line.
164 If nil, no function is called. The default value is nil.
166 This feature can be useful if Imenu is set up to make more
167 detailed entries (e.g., containing the argument list of a function),
168 and you want to simplify them for the mode line
169 \(e.g., removing the parameter list to just have the function name.)")
171 ;;; Code, nothing to customize below here
172 ;;; -------------------------------------
174 (require 'imenu)
176 (defvar which-func-table (make-hash-table :test 'eq :weakness 'key))
178 (defconst which-func-current
179 '(:eval (replace-regexp-in-string
180 "%" "%%"
181 (or (gethash (selected-window) which-func-table)
182 which-func-unknown))))
183 ;;;###autoload (put 'which-func-current 'risky-local-variable t)
185 (defvar-local which-func-mode nil
186 "Non-nil means display current function name in mode line.
187 This makes a difference only if `which-function-mode' is non-nil.")
189 (add-hook 'find-file-hook 'which-func-ff-hook t)
191 (defun which-func-try-to-enable ()
192 (unless (or (not which-function-mode)
193 (local-variable-p 'which-func-mode))
194 (setq which-func-mode (or (eq which-func-modes t)
195 (member major-mode which-func-modes)))))
197 (defun which-func-ff-hook ()
198 "File find hook for Which Function mode.
199 It creates the Imenu index for the buffer, if necessary."
200 (which-func-try-to-enable)
202 (condition-case err
203 (if (and which-func-mode
204 (not (member major-mode which-func-non-auto-modes))
205 (or (null which-func-maxout)
206 (< buffer-saved-size which-func-maxout)
207 (= which-func-maxout 0)))
208 (setq imenu--index-alist
209 (save-excursion (funcall imenu-create-index-function))))
210 (imenu-unavailable
211 (setq which-func-mode nil))
212 (error
213 (message "which-func-ff-hook error: %S" err)
214 (setq which-func-mode nil))))
216 (defun which-func-update ()
217 ;; "Update the Which-Function mode display for all windows."
218 ;; (walk-windows 'which-func-update-1 nil 'visible))
219 (which-func-update-1 (selected-window)))
221 (defun which-func-update-1 (window)
222 "Update the Which Function mode display for window WINDOW."
223 (with-selected-window window
224 (when which-func-mode
225 (condition-case info
226 (let ((current (which-function)))
227 (unless (equal current (gethash window which-func-table))
228 (puthash window current which-func-table)
229 (force-mode-line-update)))
230 (error
231 (setq which-func-mode nil)
232 (error "Error in which-func-update: %S" info))))))
234 ;;;###autoload
235 (define-obsolete-function-alias 'which-func-mode 'which-function-mode "24.1")
237 (defvar which-func-update-timer nil)
239 (unless (or (assq 'which-func-mode mode-line-misc-info)
240 (assq 'which-function-mode mode-line-misc-info))
241 (add-to-list 'mode-line-misc-info
242 '(which-function-mode ;Only display if mode is enabled.
243 (which-func-mode ;Only display if buffer supports it.
244 ("" which-func-format " ")))))
246 ;; This is the name people would normally expect.
247 ;;;###autoload
248 (define-minor-mode which-function-mode
249 "Toggle mode line display of current function (Which Function mode).
250 With a prefix argument ARG, enable Which Function mode if ARG is
251 positive, and disable it otherwise. If called from Lisp, enable
252 the mode if ARG is omitted or nil.
254 Which Function mode is a global minor mode. When enabled, the
255 current function name is continuously displayed in the mode line,
256 in certain major modes."
257 :global t :group 'which-func
258 (when (timerp which-func-update-timer)
259 (cancel-timer which-func-update-timer))
260 (setq which-func-update-timer nil)
261 (when which-function-mode
262 ;;Turn it on.
263 (setq which-func-update-timer
264 (run-with-idle-timer idle-update-delay t #'which-func-update))
265 (dolist (buf (buffer-list))
266 (with-current-buffer buf (which-func-try-to-enable)))))
268 (defvar which-function-imenu-failed nil
269 "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
271 (defvar which-func-functions nil
272 "List of functions for `which-function' to call with no arguments.
273 It calls them sequentially, and if any returns non-nil,
274 `which-function' uses that name and stops looking for the name.")
276 (defun which-function ()
277 "Return current function name based on point.
278 Uses `which-func-functions', `imenu--index-alist'
279 or `add-log-current-defun'.
280 If no function name is found, return nil."
281 (let ((name
282 ;; Try the `which-func-functions' functions first.
283 (run-hook-with-args-until-success 'which-func-functions)))
285 ;; If Imenu is loaded, try to make an index alist with it.
286 (when (and (null name)
287 (boundp 'imenu--index-alist) (null imenu--index-alist)
288 (null which-function-imenu-failed))
289 (ignore-errors (imenu--make-index-alist t))
290 (unless imenu--index-alist
291 (set (make-local-variable 'which-function-imenu-failed) t)))
292 ;; If we have an index alist, use it.
293 (when (and (null name)
294 (boundp 'imenu--index-alist) imenu--index-alist)
295 (let ((alist imenu--index-alist)
296 (minoffset (point-max))
297 offset pair mark imstack namestack)
298 ;; Elements of alist are either ("name" . marker), or
299 ;; ("submenu" ("name" . marker) ... ). The list can be
300 ;; arbitrarily nested.
301 (while (or alist imstack)
302 (if (null alist)
303 (setq alist (car imstack)
304 namestack (cdr namestack)
305 imstack (cdr imstack))
307 (setq pair (car-safe alist)
308 alist (cdr-safe alist))
310 (cond
311 ((atom pair)) ; Skip anything not a cons.
313 ((imenu--subalist-p pair)
314 (setq imstack (cons alist imstack)
315 namestack (cons (car pair) namestack)
316 alist (cdr pair)))
318 ((or (number-or-marker-p (setq mark (cdr pair)))
319 (and (overlayp mark)
320 (setq mark (overlay-start mark))))
321 (when (and (>= (setq offset (- (point) mark)) 0)
322 (< offset minoffset)) ; Find the closest item.
323 (setq minoffset offset
324 name (if (null which-func-imenu-joiner-function)
325 (car pair)
326 (funcall
327 which-func-imenu-joiner-function
328 (reverse (cons (car pair) namestack))))))))))))
330 ;; Try using add-log support.
331 (when (null name)
332 (setq name (add-log-current-defun)))
333 ;; Filter the name if requested.
334 (when name
335 (if which-func-cleanup-function
336 (funcall which-func-cleanup-function name)
337 name))))
340 ;;; Integration with other packages
342 (defvar ediff-window-A)
343 (defvar ediff-window-B)
344 (defvar ediff-window-C)
346 ;; FIXME: Why does ediff require special support?
347 (defun which-func-update-ediff-windows ()
348 "Update Which-Function mode display for Ediff windows.
349 This function is meant to be called from `ediff-select-hook'."
350 (when (and (derived-mode-p 'ediff-mode) which-function-mode)
351 (when ediff-window-A
352 (which-func-update-1 ediff-window-A))
353 (when ediff-window-B
354 (which-func-update-1 ediff-window-B))
355 (when ediff-window-C
356 (which-func-update-1 ediff-window-C))))
358 (add-hook 'ediff-select-hook 'which-func-update-ediff-windows)
360 (provide 'which-func)
362 ;;; which-func.el ends here