(diary-face, holiday-face): Add dark-background variants.
[bpt/emacs.git] / lisp / which-func.el
CommitLineData
95044490 1;;; which-func.el --- Print current function in mode line
d6d2fa1f 2
0211dda3 3;; Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc.
d6d2fa1f
RS
4
5;; Author: Alex Rezinsky <alexr@msil.sps.mot.com>
0211dda3 6;; Keywords: mode-line, imenu, tools
d6d2fa1f
RS
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
0211dda3
DL
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;;
d6d2fa1f
RS
66(defvar which-func-unknown "???"
67 "String to display in the mode line when current function is unknown.")
68
daa02ea5
DN
69(defgroup which-func nil
70 "Mode to display the current function name in the modeline."
71 :group 'tools
72 :version "20.3")
73
7a513c47 74(defcustom which-func-modes
70066cfd
RS
75 '(emacs-lisp-mode c-mode c++-mode perl-mode makefile-mode sh-mode
76 fortran-mode)
5686b9d4 77 "List of major modes for which Which Function mode should be used.
d6d2fa1f 78For other modes it is disabled. If this is equal to t,
5686b9d4 79then Which Function mode is enabled in any major mode that supports it."
d6d2fa1f
RS
80 :group 'which-func
81 :type '(choice (const :tag "All modes" t)
5686b9d4 82 (repeat (symbol :tag "Major mode"))))
d6d2fa1f 83
4962cb1a
RS
84(defcustom which-func-non-auto-modes nil
85 "List of major modes where Which Function mode is inactive till Imenu is used.
5686b9d4
RS
86This means that Which Function mode won't really do anything
87until you use Imenu, in these modes. Note that files
88larger than `which-func-maxout' behave in this way too;
89Which Function mode doesn't do anything until you use Imenu."
d6d2fa1f 90 :group 'which-func
5686b9d4 91 :type '(repeat (symbol :tag "Major mode")))
d6d2fa1f
RS
92
93(defcustom which-func-maxout 100000
94 "Don't automatically compute the Imenu menu if buffer is this big or bigger.
95Zero means compute the Imenu menu regardless of size."
96 :group 'which-func
97 :type 'integer)
98
7a513c47 99(defcustom which-func-format '("[" which-func-current "]")
d6d2fa1f
RS
100 "Format for displaying the function in the mode line."
101 :group 'which-func
102 :type 'sexp)
103
0211dda3
DL
104;;;###autoload
105(defcustom which-func-mode-global nil
739db504
DL
106 "*Toggle `which-func-mode' globally.
107Setting this variable directly does not take effect;
108use either \\[customize] or the function `which-func-mode'."
0211dda3 109 :set #'(lambda (symbol value)
2b7b0d15 110 (which-func-mode (if value 1 0)))
0211dda3
DL
111 :initialize 'custom-initialize-default
112 :type 'boolean
113 :group 'which-func
114 :require 'which-func)
115
1e68f8d0
RS
116(defvar which-func-cleanup-function nil
117 "Function to transform a string before displaying it in the mode line.
118The function is called with one argument, the string to display.
119Its return value is displayed in the modeline.
120If nil, no function is called. The default value is nil.
121
122This feature can be useful if Imenu is set up to make more
123detailed entries (e.g., containing the argument list of a function),
124and you want to simplify them for the mode line
125\(e.g., removing the parameter list to just have the function name.)")
126
d6d2fa1f
RS
127;;; Code, nothing to customize below here
128;;; -------------------------------------
129;;;
130(require 'imenu)
131
132(defvar which-func-current which-func-unknown)
133(defvar which-func-previous which-func-unknown)
134(make-variable-buffer-local 'which-func-current)
135(make-variable-buffer-local 'which-func-previous)
136
d6d2fa1f
RS
137(defvar which-func-mode nil
138 "Non-nil means display current function name in mode line.
0211dda3 139This makes a difference only if `which-func-mode-global' is non-nil")
d6d2fa1f 140(make-variable-buffer-local 'which-func-mode)
7a513c47 141;;(put 'which-func-mode 'permanent-local t)
d6d2fa1f
RS
142
143(add-hook 'find-file-hooks 'which-func-ff-hook t)
144
145(defun which-func-ff-hook ()
146 "File find hook for Which Function mode.
147It creates the Imenu index for the buffer, if necessary."
7a513c47
SM
148 (setq which-func-mode
149 (and which-func-mode-global
150 (or (eq which-func-modes t) (member major-mode which-func-modes))))
d6d2fa1f 151
2576f5d2
KH
152 (condition-case nil
153 (if (and which-func-mode
154 (not (member major-mode which-func-non-auto-modes))
7a513c47
SM
155 (or (null which-func-maxout)
156 (< buffer-saved-size which-func-maxout)
2576f5d2
KH
157 (= which-func-maxout 0)))
158 (setq imenu--index-alist
159 (save-excursion (funcall imenu-create-index-function))))
160 (error
161 (setq which-func-mode nil))))
d6d2fa1f
RS
162
163(defun which-func-update ()
164 ;; Update the string containing the current function.
7a513c47
SM
165 (when which-func-mode
166 (condition-case info
167 (progn
168 (setq which-func-current (or (which-function) which-func-unknown))
169 (unless (string= which-func-current which-func-previous)
c5059c2c 170 (force-mode-line-update)
7a513c47
SM
171 (setq which-func-previous which-func-current)))
172 (error
173 (which-func-mode -1)
174 (error "Error in which-func-update: %s" info)))))
d6d2fa1f 175
4ed0c28d
RS
176;; This is the name people would normally expect.
177;;;###autoload
178(defalias 'which-function-mode 'which-func-mode)
179
0211dda3 180;;;###autoload
d6d2fa1f
RS
181(defun which-func-mode (&optional arg)
182 "Toggle Which Function mode, globally.
183When Which Function mode is enabled, the current function name is
184continuously displayed in the mode line, in certain major modes.
185
7a513c47 186With prefix ARG, turn Which Function mode on iff arg is positive,
d6d2fa1f
RS
187and off otherwise."
188 (interactive "P")
7a513c47
SM
189 (setq which-func-mode-global
190 (or (and (null arg) which-func-mode-global)
191 (<= (prefix-numeric-value arg) 0)))
192 (if which-func-mode-global
d6d2fa1f 193 ;; Turn it off
7a513c47
SM
194 (progn
195 (remove-hook 'post-command-idle-hook 'which-func-update)
196 (dolist (buf (buffer-list))
197 (with-current-buffer buf (setq which-func-mode nil))))
d6d2fa1f 198 ;;Turn it on
7a513c47
SM
199 (add-hook 'post-command-idle-hook 'which-func-update)
200 (dolist (buf (buffer-list))
201 (with-current-buffer buf
202 (setq which-func-mode
203 (or (eq which-func-modes t)
204 (member major-mode which-func-modes)))))))
d6d2fa1f
RS
205
206(defun which-function ()
0211dda3
DL
207 "Return current function name based on point.
208If `imenu--index-alist' does not exist, or is empty or if point
d6d2fa1f 209is located before first function, returns nil."
7a513c47
SM
210 (let (name)
211 ;; First try using imenu support.
212 (when (and (boundp 'imenu--index-alist) imenu--index-alist)
213 (let ((pair (car-safe imenu--index-alist))
214 (rest (cdr-safe imenu--index-alist)))
215 (while (and (or rest pair)
216 (or (not (number-or-marker-p (cdr pair)))
217 (> (point) (cdr pair))))
218 (setq name (car pair))
219 (setq pair (car-safe rest))
220 (setq rest (cdr-safe rest)))))
221 ;; Try using add-log support.
222 (when (and (null name) (boundp 'add-log-current-defun-function)
223 add-log-current-defun-function)
224 (setq name (funcall add-log-current-defun-function)))
225 ;; Filter the name if requested.
226 (when name
227 (if which-func-cleanup-function
228 (funcall which-func-cleanup-function name)
229 name))))
d6d2fa1f
RS
230
231(provide 'which-func)
232
233;; which-func.el ends here