Commit | Line | Data |
---|---|---|
95044490 | 1 | ;;; which-func.el --- Print current function in mode line |
d6d2fa1f RS |
2 | |
3 | ;; Copyright (C) 1994, 1997 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Alex Rezinsky <alexr@msil.sps.mot.com> | |
6 | ;; Keywords: mode-line imenu | |
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 | |
34 | ;;; is located now", but "nearest function which defined above the | |
35 | ;;; current point". So if your current point is located after end of | |
36 | ;;; function FOO but before begin of function BAR, FOO will be | |
37 | ;;; displayed in mode 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 | ;;; THANKS TO | |
48 | ;;; --------- | |
49 | ;;; Per Abrahamsen <abraham@iesd.auc.dk> | |
50 | ;;; Some ideas (inserting in mode-line, using of post-command hook | |
51 | ;;; and toggling this mode) have been borrowed from his package | |
52 | ;;; column.el | |
53 | ;;; Peter Eisenhauer <pipe@fzi.de> | |
54 | ;;; Bug fixing in case nested indexes. | |
55 | ;;; Terry Tateyama <ttt@ursa0.cs.utah.edu> | |
56 | ;;; Suggestion to use find-file-hooks for first imenu | |
57 | ;;; index building. | |
58 | ||
59 | ;;; Variables for customization | |
60 | ;;; --------------------------- | |
61 | ;;; | |
62 | (defvar which-func-unknown "???" | |
63 | "String to display in the mode line when current function is unknown.") | |
64 | ||
65 | (defcustom which-func-modes | |
66 | '(emacs-lisp-mode c-mode c++-mode perl-mode makefile-mode sh-mode) | |
67 | "List of major modes for which `which-func-mode' should be used. | |
68 | For other modes it is disabled. If this is equal to t, | |
69 | then current-function recognition is enabled in any mode." | |
70 | :group 'which-func | |
71 | :type '(choice (const :tag "All modes" t) | |
72 | (list (symbol :tag "Major mode")))) | |
73 | ||
74 | (defcustom which-func-amodes '(emacs-lisp-mode c-mode c++-mode) | |
75 | "List of major modes for which the Imenu menu should be computed automatically. | |
76 | It is computed when you visit a file in one of these major modes. | |
77 | If the variable's value is nil, then the Imenu menu is not computed | |
78 | automatically in any mode. Note that the menu is never computed | |
79 | automatically if the buffer size exceeds `which-func-maxout'." | |
80 | :group 'which-func | |
81 | :type '(list (symbol :tag "Major mode"))) | |
82 | ||
83 | (defcustom which-func-maxout 100000 | |
84 | "Don't automatically compute the Imenu menu if buffer is this big or bigger. | |
85 | Zero means compute the Imenu menu regardless of size." | |
86 | :group 'which-func | |
87 | :type 'integer) | |
88 | ||
89 | (defcustom which-func-format '(" [" which-func-current "]") | |
90 | "Format for displaying the function in the mode line." | |
91 | :group 'which-func | |
92 | :type 'sexp) | |
93 | ||
94 | ;;; Code, nothing to customize below here | |
95 | ;;; ------------------------------------- | |
96 | ;;; | |
97 | (require 'imenu) | |
98 | ||
99 | (defvar which-func-current which-func-unknown) | |
100 | (defvar which-func-previous which-func-unknown) | |
101 | (make-variable-buffer-local 'which-func-current) | |
102 | (make-variable-buffer-local 'which-func-previous) | |
103 | ||
104 | (defvar which-func-mode-global nil | |
105 | "Non-nil means display current function name in mode line.") | |
106 | ||
107 | (defvar which-func-mode nil | |
108 | "Non-nil means display current function name in mode line. | |
109 | This makes a difference only if which-func-mode-global is non-nil") | |
110 | (make-variable-buffer-local 'which-func-mode) | |
111 | (put 'which-func-mode 'permanent-local t) | |
112 | ||
113 | (add-hook 'find-file-hooks 'which-func-ff-hook t) | |
114 | ||
115 | (defun which-func-ff-hook () | |
116 | "File find hook for Which Function mode. | |
117 | It creates the Imenu index for the buffer, if necessary." | |
118 | (if (or (eq which-func-modes t) (member major-mode which-func-modes)) | |
119 | (setq which-func-mode which-func-mode-global) | |
120 | (setq which-func-mode nil)) | |
121 | ||
122 | (if (and which-func-mode | |
123 | (member major-mode which-func-amodes) | |
124 | (or (< buffer-saved-size which-func-maxout) | |
125 | (= which-func-maxout 0))) | |
126 | (setq imenu--index-alist | |
127 | (save-excursion (funcall imenu-create-index-function))))) | |
128 | ||
129 | (defun which-func-update () | |
130 | ;; Update the string containing the current function. | |
131 | (condition-case info | |
c5059c2c RS |
132 | (progn |
133 | (if (not (setq which-func-current (which-function))) | |
134 | (setq which-func-current which-func-unknown)) | |
135 | (if (not (string= which-func-current which-func-previous)) | |
136 | (progn | |
137 | (force-mode-line-update) | |
138 | (setq which-func-previous which-func-current)))) | |
d6d2fa1f RS |
139 | (error |
140 | (ding) | |
141 | (remove-hook 'post-command-hook 'which-func-update) | |
142 | (which-func-mode -1) ; Function mode off | |
c5059c2c | 143 | (message "Error in which-func-update: %s" info)))) |
d6d2fa1f RS |
144 | |
145 | (defun which-func-mode (&optional arg) | |
146 | "Toggle Which Function mode, globally. | |
147 | When Which Function mode is enabled, the current function name is | |
148 | continuously displayed in the mode line, in certain major modes. | |
149 | ||
150 | With prefix arg, turn Which Function mode on iff arg is positive, | |
151 | and off otherwise." | |
152 | (interactive "P") | |
153 | (if (or (and (null arg) which-func-mode-global) | |
154 | (<= (prefix-numeric-value arg) 0)) | |
155 | ;; Turn it off | |
156 | (if which-func-mode-global | |
157 | (progn | |
158 | (remove-hook 'post-command-hook 'which-func-update) | |
159 | (setq which-func-mode-global nil) | |
160 | (setq which-func-mode nil) | |
161 | (force-mode-line-update))) | |
162 | ;;Turn it on | |
163 | (if which-func-mode-global | |
164 | () | |
165 | (add-hook 'post-command-hook 'which-func-update) | |
166 | (setq which-func-mode-global t) | |
167 | (setq which-func-mode | |
168 | (or (eq which-func-modes t) | |
169 | (member major-mode which-func-modes)))))) | |
170 | ||
171 | (defun which-function () | |
172 | "Returns current function name based on point. | |
173 | If imenu--index-alist does not exist, or is empty or if point | |
174 | is located before first function, returns nil." | |
175 | (and | |
176 | (boundp 'imenu--index-alist) | |
177 | imenu--index-alist | |
178 | (let ((pair (car-safe imenu--index-alist)) | |
179 | (rest (cdr-safe imenu--index-alist)) | |
180 | (name nil)) | |
181 | (while (and pair (or (not (number-or-marker-p (cdr pair))) | |
182 | (> (point) (cdr pair)))) | |
183 | (setq name (car pair)) | |
184 | (setq pair (car-safe rest)) | |
185 | (setq rest (cdr-safe rest))) | |
186 | name))) | |
187 | ||
188 | (provide 'which-func) | |
189 | ||
190 | ;; which-func.el ends here |