viper -- Update and prettify package-info.in provides.
[packages] / xemacs-packages / edit-utils / which-func.el
1 ;;; which-func.el --- print current function in mode line
2
3 ;; Copyright (C) 1994, 1997-1998, 2001-2013 Free Software Foundation,
4 ;; Inc.
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 3 of the License, or
15 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
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 (require 'timer)
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   "Display the current function name in the mode line."
75   :group 'tools
76   :version "20.3")
77
78 (defcustom which-func-modes t
79   ;; '(emacs-lisp-mode c-mode c++-mode objc-mode perl-mode cperl-mode python-mode
80   ;;                makefile-mode sh-mode fortran-mode f90-mode ada-mode
81   ;;                diff-mode)
82   "List of major modes for which Which Function mode should be used.
83 For other modes it is disabled.  If this is equal to t,
84 then Which Function mode is enabled in any major mode that supports it."
85   :group 'which-func
86   :version "24.3"                       ; explicit list -> t
87   :type '(choice (const :tag "All modes" t)
88                  (repeat (symbol :tag "Major mode"))))
89
90 (defcustom which-func-non-auto-modes nil
91   "List of major modes where Which Function mode is inactive till Imenu is used.
92 This means that Which Function mode won't really do anything
93 until you use Imenu, in these modes.  Note that files
94 larger than `which-func-maxout' behave in this way too;
95 Which Function mode doesn't do anything until you use Imenu."
96   :group 'which-func
97   :type '(repeat (symbol :tag "Major mode")))
98
99 (defcustom which-func-maxout 500000
100   "Don't automatically compute the Imenu menu if buffer is this big or bigger.
101 Zero means compute the Imenu menu regardless of size."
102   :group 'which-func
103   :type 'integer)
104
105 (defvar which-func-keymap
106   (let ((map (make-sparse-keymap)))
107     (define-key map [mode-line mouse-1] 'beginning-of-defun)
108     (define-key map [mode-line mouse-2]
109       (lambda ()
110         (interactive)
111         (if (eq (point-min) 1)
112             (narrow-to-defun)
113           (widen))))
114     (define-key map [mode-line mouse-3] 'end-of-defun)
115     map)
116   "Keymap to display on mode line which-func.")
117
118 (defface which-func
119   ;; Whether `font-lock-function-name-face' is an appropriate face to
120   ;; inherit depends on the mode-line face; define several variants based
121   ;; on the default mode-line face.
122   '(;; The default mode-line face on a high-color display is a relatively
123     ;; light color ("grey75"), and only the light-background variant of
124     ;; `font-lock-function-name-face' is visible against it.
125     (((class color) (min-colors 88) (background light))
126      (:inherit font-lock-function-name-face))
127     ;; The default mode-line face on other display types is inverse-video;
128     ;; it seems that only in the dark-background case is
129     ;; `font-lock-function-name-face' visible against it.
130     (((class grayscale mono) (background dark))
131      (:inherit font-lock-function-name-face))
132     (((class color) (background light))
133      (:inherit font-lock-function-name-face))
134     ;; If none of the above cases, use an explicit color chosen to contrast
135     ;; well with the default mode-line face.
136     (((class color) (min-colors 88) (background dark))
137      (:foreground "Blue1"))
138     (((background dark))
139      (:foreground "Blue1"))
140     (t
141      (:foreground "LightSkyBlue")))
142   "Face used to highlight mode line function names."
143   :group 'which-func)
144
145 (defcustom which-func-format
146   `("["
147     (:propertize which-func-current
148                  local-map ,which-func-keymap
149                  face which-func
150                  mouse-face mode-line-highlight
151                  help-echo "mouse-1: go to beginning\n\
152 mouse-2: toggle rest visibility\n\
153 mouse-3: go to end")
154     "]")
155   "Format for displaying the function in the mode line."
156   :version "24.2"                  ; added mouse-face; 24point2 is correct
157   :group 'which-func
158   :type 'sexp)
159 ;;;###autoload (put 'which-func-format 'risky-local-variable t)
160
161 (defvar which-func-imenu-joiner-function (lambda (x) (car (last x)))
162   "Function to join together multiple levels of imenu nomenclature.
163 Called with a single argument, a list of strings giving the names
164 of the menus we had to traverse to get to the item.  Returns a
165 single string, the new name of the item.")
166
167 (defvar which-func-cleanup-function nil
168   "Function to transform a string before displaying it in the mode line.
169 The function is called with one argument, the string to display.
170 Its return value is displayed in the mode line.
171 If nil, no function is called.  The default value is nil.
172
173 This feature can be useful if Imenu is set up to make more
174 detailed entries (e.g., containing the argument list of a function),
175 and you want to simplify them for the mode line
176 \(e.g., removing the parameter list to just have the function name.)")
177
178 ;;; Code, nothing to customize below here
179 ;;; -------------------------------------
180 ;;;
181 (require 'imenu)
182
183 (defvar which-func-table (make-hash-table :test 'eq :weakness 'key))
184
185 (defconst which-func-current
186   '(:eval (replace-regexp-in-string
187            "%" "%%"
188            (or (gethash (selected-window) which-func-table)
189                which-func-unknown))))
190 ;;;###autoload (put 'which-func-current 'risky-local-variable t)
191
192 (defvar which-func-mode nil
193   "Non-nil means display current function name in mode line.
194 This makes a difference only if `which-function-mode' is non-nil.")
195 (make-variable-buffer-local 'which-func-mode)
196 ;;(put 'which-func-mode 'permanent-local t)
197
198 (add-hook 'find-file-hook 'which-func-ff-hook t)
199
200 (defun which-func-ff-hook ()
201   "File find hook for Which Function mode.
202 It creates the Imenu index for the buffer, if necessary."
203   (setq which-func-mode
204         (and which-function-mode
205              (or (eq which-func-modes t)
206                  (member major-mode which-func-modes))))
207
208   (condition-case err
209       (if (and which-func-mode
210                (not (member major-mode which-func-non-auto-modes))
211                (or (null which-func-maxout)
212                    (< buffer-saved-size which-func-maxout)
213                    (= which-func-maxout 0)))
214           (setq imenu--index-alist
215                 (save-excursion (funcall imenu-create-index-function))))
216     (error
217      (unless (equal err
218                     '(user-error "This buffer cannot use `imenu-default-create-index-function'"))
219        (message "which-func-ff-hook error: %S" err))
220      (setq which-func-mode nil))))
221
222 (defun which-func-update ()
223   ;; "Update the Which-Function mode display for all windows."
224   ;; (walk-windows 'which-func-update-1 nil 'visible))
225   (which-func-update-1 (selected-window)))
226
227 (defun which-func-update-1 (window)
228   "Update the Which Function mode display for window WINDOW."
229   (with-selected-window window
230     (when which-func-mode
231       (condition-case info
232           (let ((current (which-function)))
233             (unless (equal current (gethash window which-func-table))
234               (puthash window current which-func-table)
235               (force-mode-line-update)))
236         (error
237          (setq which-func-mode nil)
238          (error "Error in which-func-update: %S" info))))))
239
240 ;;;###autoload
241 (define-obsolete-function-alias 'which-func-mode 'which-function-mode)
242
243 (defvar which-func-update-timer nil)
244
245 ;; This is the name people would normally expect.
246 ;;;###autoload
247 (define-minor-mode which-function-mode
248   "Toggle mode line display of current function (Which Function mode).
249 With a prefix argument ARG, enable Which Function mode if ARG is
250 positive, and disable it otherwise.  If called from Lisp, enable
251 the mode if ARG is omitted or nil.
252
253 Which Function mode is a global minor mode.  When enabled, the
254 current function name is continuously displayed in the mode line,
255 in certain major modes."
256   :global t :group 'which-func
257   (when (timerp which-func-update-timer)
258     (cancel-timer which-func-update-timer))
259   (setq which-func-update-timer nil)
260   (if which-function-mode
261       ;;Turn it on
262       (progn
263         (setq which-func-update-timer
264               (run-with-idle-timer 
265                (if (boundp 'idle-update-delay)
266                    idle-update-delay
267                  0.5)
268                t 'which-func-update))
269         (dolist (buf (buffer-list))
270           (with-current-buffer buf
271             (setq which-func-mode
272                   (or (eq which-func-modes t)
273                       (member major-mode which-func-modes))))))
274     ;; Turn it off
275     (dolist (buf (buffer-list))
276       (with-current-buffer buf (setq which-func-mode nil)))))
277
278 (defvar which-function-imenu-failed nil
279   "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
280
281 (defvar which-func-functions nil
282   "List of functions for `which-function' to call with no arguments.
283 It calls them sequentially, and if any returns non-nil,
284 `which-function' uses that name and stops looking for the name.")
285
286 (defun which-function ()
287   "Return current function name based on point.
288 Uses `which-func-functions', `imenu--index-alist'
289 or `add-log-current-defun'.
290 If no function name is found, return nil."
291   (let ((name
292          ;; Try the `which-func-functions' functions first.
293          (run-hook-with-args-until-success 'which-func-functions)))
294
295     ;; If Imenu is loaded, try to make an index alist with it.
296     (when (and (null name)
297                (boundp 'imenu--index-alist) (null imenu--index-alist)
298                (null which-function-imenu-failed))
299       (ignore-errors (imenu--make-index-alist t))
300       (unless imenu--index-alist
301         (set (make-local-variable 'which-function-imenu-failed) t)))
302     ;; If we have an index alist, use it.
303     (when (and (null name)
304                (boundp 'imenu--index-alist) imenu--index-alist)
305       (let ((alist imenu--index-alist)
306             (minoffset (point-max))
307             offset pair mark imstack namestack)
308         ;; Elements of alist are either ("name" . marker), or
309         ;; ("submenu" ("name" . marker) ... ). The list can be
310         ;; arbitrarily nested.
311         (while (or alist imstack)
312           (if (null alist)
313               (setq alist     (car imstack)
314                     namestack (cdr namestack)
315                     imstack   (cdr imstack))
316
317             (setq pair (car-safe alist)
318                   alist (cdr-safe alist))
319
320             (cond
321              ((atom pair))              ; Skip anything not a cons.
322
323              ((imenu--subalist-p pair)
324               (setq imstack   (cons alist imstack)
325                     namestack (cons (car pair) namestack)
326                     alist     (cdr pair)))
327
328              ((or (number-or-marker-p (setq mark (cdr pair)))
329                   (and (overlayp mark)
330                        (setq mark (overlay-start mark))))
331               (when (and (>= (setq offset (- (point) mark)) 0)
332                          (< offset minoffset)) ; Find the closest item.
333                 (setq minoffset offset
334                       name (if (null which-func-imenu-joiner-function)
335                                (car pair)
336                              (funcall
337                               which-func-imenu-joiner-function
338                               (reverse (cons (car pair) namestack))))))))))))
339
340     ;; Try using add-log support.
341     (when (null name)
342       (setq name (add-log-current-defun)))
343     ;; Filter the name if requested.
344     (when name
345       (if which-func-cleanup-function
346           (funcall which-func-cleanup-function name)
347         name))))
348
349 \f
350 ;;; Integration with other packages
351
352 (defun which-func-update-ediff-windows ()
353   "Update Which-Function mode display for Ediff windows.
354 This function is meant to be called from `ediff-select-hook'."
355   (when (eq major-mode 'ediff-mode)
356     (when ediff-window-A
357       (which-func-update-1 ediff-window-A))
358     (when ediff-window-B
359       (which-func-update-1 ediff-window-B))
360     (when ediff-window-C
361       (which-func-update-1 ediff-window-C))))
362
363 (add-hook 'ediff-select-hook 'which-func-update-ediff-windows)
364
365 (provide 'which-func)
366
367 ;;; which-func.el ends here