(rmail-mime-feature): Doc fix.
[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
d6d2fa1f
RS
74(defcustom which-func-modes
75 '(emacs-lisp-mode c-mode c++-mode perl-mode makefile-mode sh-mode)
76 "List of major modes for which `which-func-mode' should be used.
77For other modes it is disabled. If this is equal to t,
78then current-function recognition is enabled in any mode."
79 :group 'which-func
80 :type '(choice (const :tag "All modes" t)
81 (list (symbol :tag "Major mode"))))
82
4962cb1a
RS
83(defcustom which-func-non-auto-modes nil
84 "List of major modes where Which Function mode is inactive till Imenu is used.
85
86Note that the menu is never computed automatically if the buffer size
87exceeds `which-func-maxout'."
88
d6d2fa1f
RS
89 :group 'which-func
90 :type '(list (symbol :tag "Major mode")))
91
92(defcustom which-func-maxout 100000
93 "Don't automatically compute the Imenu menu if buffer is this big or bigger.
94Zero means compute the Imenu menu regardless of size."
95 :group 'which-func
96 :type 'integer)
97
98(defcustom which-func-format '(" [" which-func-current "]")
99 "Format for displaying the function in the mode line."
100 :group 'which-func
101 :type 'sexp)
102
0211dda3
DL
103;;;###autoload
104(defcustom which-func-mode-global nil
105 "*Toggle `which-func-mode'.
106You must modify via \\[customize] for this variable to have an effect."
107 :set #'(lambda (symbol value)
108 (which-func-mode (or value 0)))
109 :initialize 'custom-initialize-default
110 :type 'boolean
111 :group 'which-func
112 :require 'which-func)
113
d6d2fa1f
RS
114;;; Code, nothing to customize below here
115;;; -------------------------------------
116;;;
117(require 'imenu)
118
119(defvar which-func-current which-func-unknown)
120(defvar which-func-previous which-func-unknown)
121(make-variable-buffer-local 'which-func-current)
122(make-variable-buffer-local 'which-func-previous)
123
d6d2fa1f
RS
124(defvar which-func-mode nil
125 "Non-nil means display current function name in mode line.
0211dda3 126This makes a difference only if `which-func-mode-global' is non-nil")
d6d2fa1f
RS
127(make-variable-buffer-local 'which-func-mode)
128(put 'which-func-mode 'permanent-local t)
129
130(add-hook 'find-file-hooks 'which-func-ff-hook t)
131
132(defun which-func-ff-hook ()
133 "File find hook for Which Function mode.
134It creates the Imenu index for the buffer, if necessary."
135 (if (or (eq which-func-modes t) (member major-mode which-func-modes))
136 (setq which-func-mode which-func-mode-global)
137 (setq which-func-mode nil))
138
139 (if (and which-func-mode
4962cb1a 140 (not (member major-mode which-func-non-auto-modes))
d6d2fa1f
RS
141 (or (< buffer-saved-size which-func-maxout)
142 (= which-func-maxout 0)))
143 (setq imenu--index-alist
144 (save-excursion (funcall imenu-create-index-function)))))
145
146(defun which-func-update ()
147 ;; Update the string containing the current function.
148 (condition-case info
c5059c2c
RS
149 (progn
150 (if (not (setq which-func-current (which-function)))
151 (setq which-func-current which-func-unknown))
152 (if (not (string= which-func-current which-func-previous))
153 (progn
154 (force-mode-line-update)
155 (setq which-func-previous which-func-current))))
d6d2fa1f
RS
156 (error
157 (ding)
158 (remove-hook 'post-command-hook 'which-func-update)
159 (which-func-mode -1) ; Function mode off
c5059c2c 160 (message "Error in which-func-update: %s" info))))
d6d2fa1f 161
4ed0c28d
RS
162;; This is the name people would normally expect.
163;;;###autoload
164(defalias 'which-function-mode 'which-func-mode)
165
0211dda3 166;;;###autoload
d6d2fa1f
RS
167(defun which-func-mode (&optional arg)
168 "Toggle Which Function mode, globally.
169When Which Function mode is enabled, the current function name is
170continuously displayed in the mode line, in certain major modes.
171
172With prefix arg, turn Which Function mode on iff arg is positive,
173and off otherwise."
174 (interactive "P")
175 (if (or (and (null arg) which-func-mode-global)
176 (<= (prefix-numeric-value arg) 0))
177 ;; Turn it off
178 (if which-func-mode-global
179 (progn
180 (remove-hook 'post-command-hook 'which-func-update)
181 (setq which-func-mode-global nil)
182 (setq which-func-mode nil)
183 (force-mode-line-update)))
184 ;;Turn it on
185 (if which-func-mode-global
186 ()
187 (add-hook 'post-command-hook 'which-func-update)
188 (setq which-func-mode-global t)
189 (setq which-func-mode
190 (or (eq which-func-modes t)
191 (member major-mode which-func-modes))))))
192
193(defun which-function ()
0211dda3
DL
194 "Return current function name based on point.
195If `imenu--index-alist' does not exist, or is empty or if point
d6d2fa1f
RS
196is located before first function, returns nil."
197 (and
198 (boundp 'imenu--index-alist)
199 imenu--index-alist
200 (let ((pair (car-safe imenu--index-alist))
201 (rest (cdr-safe imenu--index-alist))
202 (name nil))
203 (while (and pair (or (not (number-or-marker-p (cdr pair)))
204 (> (point) (cdr pair))))
205 (setq name (car pair))
206 (setq pair (car-safe rest))
207 (setq rest (cdr-safe rest)))
208 name)))
209
210(provide 'which-func)
211
212;; which-func.el ends here