(speedbar-timer-fn): Disable updating if the frame
[bpt/emacs.git] / lisp / which-func.el
1 ;;; which-func.el --- Print current function in mode line
2
3 ;; Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
6 ;; Keywords: mode-line, imenu, tools
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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
39 ;; TODO LIST
40 ;; ---------
41 ;; 1. Dependence on imenu package should be removed. Separate
42 ;; function determination mechanism should be used to determine the end
43 ;; of a function as well as the beginning of a function.
44 ;; 2. This package should be realized with the help of overlay
45 ;; properties instead of imenu--index-alist variable.
46
47 ;;; History:
48
49 ;; THANKS TO
50 ;; ---------
51 ;; Per Abrahamsen <abraham@iesd.auc.dk>
52 ;; Some ideas (inserting in mode-line, using of post-command hook
53 ;; and toggling this mode) have been borrowed from his package
54 ;; column.el
55 ;; Peter Eisenhauer <pipe@fzi.de>
56 ;; Bug fixing in case nested indexes.
57 ;; Terry Tateyama <ttt@ursa0.cs.utah.edu>
58 ;; Suggestion to use find-file-hooks for first imenu
59 ;; index building.
60
61 ;;; Code:
62
63 ;; Variables for customization
64 ;; ---------------------------
65 ;;
66 (defvar which-func-unknown "???"
67 "String to display in the mode line when current function is unknown.")
68
69 (defgroup which-func nil
70 "Mode to display the current function name in the modeline."
71 :group 'tools
72 :version "20.3")
73
74 (defcustom which-func-modes
75 '(emacs-lisp-mode c-mode c++-mode perl-mode makefile-mode sh-mode
76 fortran-mode)
77 "List of major modes for which Which Function mode should be used.
78 For other modes it is disabled. If this is equal to t,
79 then Which Function mode is enabled in any major mode that supports it."
80 :group 'which-func
81 :type '(choice (const :tag "All modes" t)
82 (repeat (symbol :tag "Major mode"))))
83
84 (defcustom which-func-non-auto-modes nil
85 "List of major modes where Which Function mode is inactive till Imenu is used.
86 This means that Which Function mode won't really do anything
87 until you use Imenu, in these modes. Note that files
88 larger than `which-func-maxout' behave in this way too;
89 Which Function mode doesn't do anything until you use Imenu."
90 :group 'which-func
91 :type '(repeat (symbol :tag "Major mode")))
92
93 (defcustom which-func-maxout 100000
94 "Don't automatically compute the Imenu menu if buffer is this big or bigger.
95 Zero means compute the Imenu menu regardless of size."
96 :group 'which-func
97 :type 'integer)
98
99 (defcustom which-func-format '(" [" which-func-current "]")
100 "Format for displaying the function in the mode line."
101 :group 'which-func
102 :type 'sexp)
103
104 ;;;###autoload
105 (defcustom which-func-mode-global nil
106 "*Toggle `which-func-mode'.
107 You must modify via \\[customize] for this variable to have an effect."
108 :set #'(lambda (symbol value)
109 (which-func-mode (or value 0)))
110 :initialize 'custom-initialize-default
111 :type 'boolean
112 :group 'which-func
113 :require 'which-func)
114
115 ;;; Code, nothing to customize below here
116 ;;; -------------------------------------
117 ;;;
118 (require 'imenu)
119
120 (defvar which-func-current which-func-unknown)
121 (defvar which-func-previous which-func-unknown)
122 (make-variable-buffer-local 'which-func-current)
123 (make-variable-buffer-local 'which-func-previous)
124
125 (defvar which-func-mode nil
126 "Non-nil means display current function name in mode line.
127 This makes a difference only if `which-func-mode-global' is non-nil")
128 (make-variable-buffer-local 'which-func-mode)
129 (put 'which-func-mode 'permanent-local t)
130
131 (add-hook 'find-file-hooks 'which-func-ff-hook t)
132
133 (defun which-func-ff-hook ()
134 "File find hook for Which Function mode.
135 It creates the Imenu index for the buffer, if necessary."
136 (if (or (eq which-func-modes t) (member major-mode which-func-modes))
137 (setq which-func-mode which-func-mode-global)
138 (setq which-func-mode nil))
139
140 (condition-case nil
141 (if (and which-func-mode
142 (not (member major-mode which-func-non-auto-modes))
143 (or (< buffer-saved-size which-func-maxout)
144 (= which-func-maxout 0)))
145 (setq imenu--index-alist
146 (save-excursion (funcall imenu-create-index-function))))
147 (error
148 (setq which-func-mode nil))))
149
150 (defun which-func-update ()
151 ;; Update the string containing the current function.
152 (condition-case info
153 (progn
154 (if (not (setq which-func-current (which-function)))
155 (setq which-func-current which-func-unknown))
156 (if (not (string= which-func-current which-func-previous))
157 (progn
158 (force-mode-line-update)
159 (setq which-func-previous which-func-current))))
160 (error
161 (ding)
162 (remove-hook 'post-command-hook 'which-func-update)
163 (which-func-mode -1) ; Function mode off
164 (message "Error in which-func-update: %s" info))))
165
166 ;; This is the name people would normally expect.
167 ;;;###autoload
168 (defalias 'which-function-mode 'which-func-mode)
169
170 ;;;###autoload
171 (defun which-func-mode (&optional arg)
172 "Toggle Which Function mode, globally.
173 When Which Function mode is enabled, the current function name is
174 continuously displayed in the mode line, in certain major modes.
175
176 With prefix arg, turn Which Function mode on iff arg is positive,
177 and off otherwise."
178 (interactive "P")
179 (if (or (and (null arg) which-func-mode-global)
180 (<= (prefix-numeric-value arg) 0))
181 ;; Turn it off
182 (if which-func-mode-global
183 (progn
184 (remove-hook 'post-command-hook 'which-func-update)
185 (setq which-func-mode-global nil)
186 (setq which-func-mode nil)
187 (force-mode-line-update)))
188 ;;Turn it on
189 (if which-func-mode-global
190 ()
191 (add-hook 'post-command-hook 'which-func-update)
192 (setq which-func-mode-global t)
193 (setq which-func-mode
194 (or (eq which-func-modes t)
195 (member major-mode which-func-modes))))))
196
197 (defun which-function ()
198 "Return current function name based on point.
199 If `imenu--index-alist' does not exist, or is empty or if point
200 is located before first function, returns nil."
201 (and
202 (boundp 'imenu--index-alist)
203 imenu--index-alist
204 (let ((pair (car-safe imenu--index-alist))
205 (rest (cdr-safe imenu--index-alist))
206 (name nil))
207 (while (and pair (or (not (number-or-marker-p (cdr pair)))
208 (> (point) (cdr pair))))
209 (setq name (car pair))
210 (setq pair (car-safe rest))
211 (setq rest (cdr-safe rest)))
212 name)))
213
214 (provide 'which-func)
215
216 ;; which-func.el ends here