Mercurial > emacs
annotate lisp/which-func.el @ 22772:3dc5c3a4f1f8
(sgml-mode): Call kill-local-variables
here directly, then set major-mode, then call sgml-mode-common.
Run the hooks here at the end.
(html-mode): Likewise.
(sgml-mode-common): Don't call kill-local-variables; don't run hooks.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 19 Jul 1998 00:04:01 +0000 |
parents | 87366acfb1e9 |
children | 114211878bf9 |
rev | line source |
---|---|
20322 | 1 ;;; which-func.el --- Print current function in mode line |
20321 | 2 |
21697 | 3 ;; Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc. |
20321 | 4 |
5 ;; Author: Alex Rezinsky <alexr@msil.sps.mot.com> | |
21697 | 6 ;; Keywords: mode-line, imenu, tools |
20321 | 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 | |
21697 | 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. | |
20321 | 30 |
21697 | 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. | |
20321 | 38 |
21697 | 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: | |
20321 | 48 |
21697 | 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. | |
20321 | 60 |
21697 | 61 ;;; Code: |
62 | |
63 ;; Variables for customization | |
64 ;; --------------------------- | |
65 ;; | |
20321 | 66 (defvar which-func-unknown "???" |
67 "String to display in the mode line when current function is unknown.") | |
68 | |
21653
e95a88dc6110
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
20573
diff
changeset
|
69 (defgroup which-func nil |
e95a88dc6110
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
20573
diff
changeset
|
70 "Mode to display the current function name in the modeline." |
e95a88dc6110
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
20573
diff
changeset
|
71 :group 'tools |
e95a88dc6110
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
20573
diff
changeset
|
72 :version "20.3") |
e95a88dc6110
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
20573
diff
changeset
|
73 |
20321 | 74 (defcustom which-func-modes |
75 '(emacs-lisp-mode c-mode c++-mode perl-mode makefile-mode sh-mode) | |
22134
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
76 "List of major modes for which Which Function mode should be used. |
20321 | 77 For other modes it is disabled. If this is equal to t, |
22134
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
78 then Which Function mode is enabled in any major mode that supports it." |
20321 | 79 :group 'which-func |
80 :type '(choice (const :tag "All modes" t) | |
22134
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
81 (repeat (symbol :tag "Major mode")))) |
20321 | 82 |
21964
390352ec56c6
(which-func-non-auto-modes): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
21960
diff
changeset
|
83 (defcustom which-func-non-auto-modes nil |
390352ec56c6
(which-func-non-auto-modes): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
21960
diff
changeset
|
84 "List of major modes where Which Function mode is inactive till Imenu is used. |
22134
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
85 This means that Which Function mode won't really do anything |
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
86 until you use Imenu, in these modes. Note that files |
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
87 larger than `which-func-maxout' behave in this way too; |
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
88 Which Function mode doesn't do anything until you use Imenu." |
20321 | 89 :group 'which-func |
22134
29c2db002dd5
(which-func-modes): Fix custom type.
Richard M. Stallman <rms@gnu.org>
parents:
21964
diff
changeset
|
90 :type '(repeat (symbol :tag "Major mode"))) |
20321 | 91 |
92 (defcustom which-func-maxout 100000 | |
93 "Don't automatically compute the Imenu menu if buffer is this big or bigger. | |
94 Zero 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 | |
21697 | 103 ;;;###autoload |
104 (defcustom which-func-mode-global nil | |
105 "*Toggle `which-func-mode'. | |
106 You 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 | |
20321 | 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 | |
124 (defvar which-func-mode nil | |
125 "Non-nil means display current function name in mode line. | |
21697 | 126 This makes a difference only if `which-func-mode-global' is non-nil") |
20321 | 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. | |
134 It 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 | |
22308
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
139 (condition-case nil |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
140 (if (and which-func-mode |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
141 (not (member major-mode which-func-non-auto-modes)) |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
142 (or (< buffer-saved-size which-func-maxout) |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
143 (= which-func-maxout 0))) |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
144 (setq imenu--index-alist |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
145 (save-excursion (funcall imenu-create-index-function)))) |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
146 (error |
87366acfb1e9
(which-func-ff-hook): If imenu gets error,
Karl Heuer <kwzh@gnu.org>
parents:
22134
diff
changeset
|
147 (setq which-func-mode nil)))) |
20321 | 148 |
149 (defun which-func-update () | |
150 ;; Update the string containing the current function. | |
151 (condition-case info | |
20573
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
152 (progn |
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
153 (if (not (setq which-func-current (which-function))) |
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
154 (setq which-func-current which-func-unknown)) |
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
155 (if (not (string= which-func-current which-func-previous)) |
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
156 (progn |
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
157 (force-mode-line-update) |
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
158 (setq which-func-previous which-func-current)))) |
20321 | 159 (error |
160 (ding) | |
161 (remove-hook 'post-command-hook 'which-func-update) | |
162 (which-func-mode -1) ; Function mode off | |
20573
b46523ef0e0a
(which-func-update): Fix paren error.
Richard M. Stallman <rms@gnu.org>
parents:
20322
diff
changeset
|
163 (message "Error in which-func-update: %s" info)))) |
20321 | 164 |
21960
79c7369b9ab4
(which-function-mode): New alias.
Richard M. Stallman <rms@gnu.org>
parents:
21697
diff
changeset
|
165 ;; This is the name people would normally expect. |
79c7369b9ab4
(which-function-mode): New alias.
Richard M. Stallman <rms@gnu.org>
parents:
21697
diff
changeset
|
166 ;;;###autoload |
79c7369b9ab4
(which-function-mode): New alias.
Richard M. Stallman <rms@gnu.org>
parents:
21697
diff
changeset
|
167 (defalias 'which-function-mode 'which-func-mode) |
79c7369b9ab4
(which-function-mode): New alias.
Richard M. Stallman <rms@gnu.org>
parents:
21697
diff
changeset
|
168 |
21697 | 169 ;;;###autoload |
20321 | 170 (defun which-func-mode (&optional arg) |
171 "Toggle Which Function mode, globally. | |
172 When Which Function mode is enabled, the current function name is | |
173 continuously displayed in the mode line, in certain major modes. | |
174 | |
175 With prefix arg, turn Which Function mode on iff arg is positive, | |
176 and off otherwise." | |
177 (interactive "P") | |
178 (if (or (and (null arg) which-func-mode-global) | |
179 (<= (prefix-numeric-value arg) 0)) | |
180 ;; Turn it off | |
181 (if which-func-mode-global | |
182 (progn | |
183 (remove-hook 'post-command-hook 'which-func-update) | |
184 (setq which-func-mode-global nil) | |
185 (setq which-func-mode nil) | |
186 (force-mode-line-update))) | |
187 ;;Turn it on | |
188 (if which-func-mode-global | |
189 () | |
190 (add-hook 'post-command-hook 'which-func-update) | |
191 (setq which-func-mode-global t) | |
192 (setq which-func-mode | |
193 (or (eq which-func-modes t) | |
194 (member major-mode which-func-modes)))))) | |
195 | |
196 (defun which-function () | |
21697 | 197 "Return current function name based on point. |
198 If `imenu--index-alist' does not exist, or is empty or if point | |
20321 | 199 is located before first function, returns nil." |
200 (and | |
201 (boundp 'imenu--index-alist) | |
202 imenu--index-alist | |
203 (let ((pair (car-safe imenu--index-alist)) | |
204 (rest (cdr-safe imenu--index-alist)) | |
205 (name nil)) | |
206 (while (and pair (or (not (number-or-marker-p (cdr pair))) | |
207 (> (point) (cdr pair)))) | |
208 (setq name (car pair)) | |
209 (setq pair (car-safe rest)) | |
210 (setq rest (cdr-safe rest))) | |
211 name))) | |
212 | |
213 (provide 'which-func) | |
214 | |
215 ;; which-func.el ends here |