Change release version from 21.4 to 22.1 throughout.
[emacs.git] / lisp / emacs-lisp / find-func.el
blob18e5706dc3d1f204fe9e741ce11e9d9c931cc697
1 ;;; find-func.el --- find the definition of the Emacs Lisp function near point
3 ;; Copyright (C) 1997, 1999, 2001, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp>
6 ;; Maintainer: petersen@kurims.kyoto-u.ac.jp
7 ;; Keywords: emacs-lisp, functions, variables
8 ;; Created: 97/07/25
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; The funniest thing about this is that I can't imagine why a package
30 ;; so obviously useful as this hasn't been written before!!
31 ;; ;;; find-func
32 ;; (find-function-setup-keys)
34 ;; or just:
36 ;; (load "find-func")
38 ;; if you don't like the given keybindings and away you go! It does
39 ;; pretty much what you would expect, putting the cursor at the
40 ;; definition of the function or variable at point.
42 ;; The code started out from `describe-function', `describe-key'
43 ;; ("help.el") and `fff-find-loaded-emacs-lisp-function' (Noah Friedman's
44 ;; "fff.el").
46 ;;; Code:
48 (require 'loadhist)
50 ;;; User variables:
52 (defgroup find-function nil
53 "Finds the definition of the Emacs Lisp symbol near point."
54 ;; :prefix "find-function"
55 :group 'lisp)
57 (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+")
59 (defcustom find-function-regexp
60 ;; Match things like (defun foo ...), (defmacro foo ...),
61 ;; (define-skeleton foo ...), (define-generic-mode 'foo ...),
62 ;; (define-derived-mode foo ...), (define-minor-mode foo)
63 (concat
64 "^\\s-*(\\(def\\(ine-skeleton\\|ine-generic-mode\\|ine-derived-mode\\|\
65 ine-minor-mode\\|un-cvs-mode\\|foo\\|[^cfgv]\\w+\\*?\\)\
66 \\|easy-mmode-define-global-mode\\)" find-function-space-re
67 "\\('\\|\(quote \\)?%s\\(\\s-\\|$\\|\(\\|\)\\)")
68 "The regexp used by `find-function' to search for a function definition.
69 Note it must contain a `%s' at the place where `format'
70 should insert the function name. The default value avoids `defconst',
71 `defgroup', `defvar', `defface'.
73 Please send improvements and fixes to the maintainer."
74 :type 'regexp
75 :group 'find-function
76 :version "21.1")
78 (defcustom find-variable-regexp
79 (concat"^\\s-*(def[^fumag]\\(\\w\\|\\s_\\)+\\*?" find-function-space-re "%s\\(\\s-\\|$\\)")
80 "The regexp used by `find-variable' to search for a variable definition.
81 Note it must contain a `%s' at the place where `format'
82 should insert the variable name. The default value
83 avoids `defun', `defmacro', `defalias', `defadvice', `defgroup', `defface'.
85 Please send improvements and fixes to the maintainer."
86 :type 'regexp
87 :group 'find-function
88 :version "21.1")
90 (defcustom find-face-regexp
91 (concat"^\\s-*(defface" find-function-space-re "%s\\(\\s-\\|$\\)")
92 "The regexp used by `find-face' to search for a face definition.
93 Note it must contain a `%s' at the place where `format'
94 should insert the face name.
96 Please send improvements and fixes to the maintainer."
97 :type 'regexp
98 :group 'find-function
99 :version "22.1")
101 (defvar find-function-regexp-alist
102 '((nil . find-function-regexp)
103 (defvar . find-variable-regexp)
104 (defface . find-face-regexp))
105 "Alist mapping definition types into regexp variables.
106 Each regexp variable's value should actually be a format string
107 to be used to substitute the desired symbol name into the regexp.")
108 (put 'find-function-regexp-alist 'risky-local-variable t)
110 (defcustom find-function-source-path nil
111 "The default list of directories where `find-function' searches.
113 If this variable is nil then `find-function' searches `load-path' by
114 default."
115 :type '(repeat directory)
116 :group 'find-function)
118 (defcustom find-function-recenter-line 1
119 "The window line-number from which to start displaying a symbol definition.
120 A value of nil implies center the beginning of the definition.
121 See `find-function' and `find-variable'."
122 :type '(choice (const :tag "Center" nil)
123 integer)
124 :group 'find-function
125 :version "20.3")
127 (defcustom find-function-after-hook nil
128 "Hook run after finding symbol definition.
130 See the functions `find-function' and `find-variable'."
131 :group 'find-function
132 :version "20.3")
134 ;;; Functions:
136 (defun find-library-suffixes ()
137 (let ((suffixes nil))
138 (dolist (suffix load-suffixes (nreverse suffixes))
139 (unless (string-match "elc" suffix) (push suffix suffixes)))))
141 (defun find-library-name (library)
142 "Return the full name of the elisp source of LIBRARY."
143 ;; If the library is byte-compiled, try to find a source library by
144 ;; the same name.
145 (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library)
146 (setq library (replace-match "" t t library)))
147 (or (locate-file library
148 (or find-function-source-path load-path)
149 (append (find-library-suffixes) '("")))
150 (error "Can't find library %s" library)))
152 (defvar find-function-C-source-directory
153 (let ((dir (expand-file-name "src" source-directory)))
154 (when (and (file-directory-p dir) (file-readable-p dir))
155 dir))
156 "Directory where the C source files of Emacs can be found.
157 If nil, do not try to find the source code of functions and variables
158 defined in C.")
160 (defun find-function-C-source (fun-or-var file type)
161 "Find the source location where SUBR-OR-VAR is defined in FILE.
162 TYPE should be nil to find a function, or `defvar' to find a variable."
163 (unless find-function-C-source-directory
164 (setq find-function-C-source-directory
165 (read-directory-name "Emacs C source dir: " nil nil t)))
166 (setq file (expand-file-name file find-function-C-source-directory))
167 (unless (file-readable-p file)
168 (error "The C source file %s is not available"
169 (file-name-nondirectory file)))
170 (unless type
171 (setq fun-or-var (indirect-function fun-or-var)))
172 (with-current-buffer (find-file-noselect file)
173 (goto-char (point-min))
174 (unless (re-search-forward
175 (if type
176 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
177 (regexp-quote (symbol-name fun-or-var))
178 "\"")
179 (concat "DEFUN[ \t\n]*([ \t\n]*\""
180 (regexp-quote (subr-name fun-or-var))
181 "\""))
182 nil t)
183 (error "Can't find source for %s" fun-or-var))
184 (cons (current-buffer) (match-beginning 0))))
186 ;;;###autoload
187 (defun find-library (library)
188 "Find the elisp source of LIBRARY."
189 (interactive
190 (list
191 (completing-read "Library name: "
192 'locate-file-completion
193 (cons (or find-function-source-path load-path)
194 (find-library-suffixes)))))
195 (let ((buf (find-file-noselect (find-library-name library))))
196 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
198 ;;;###autoload
199 (defun find-function-search-for-symbol (symbol type library)
200 "Search for SYMBOL's definition of type TYPE in LIBRARY.
201 If TYPE is nil, look for a function definition.
202 Otherwise, TYPE specifies the kind of definition,
203 and it is interpreted via `find-function-regexp-alist'.
204 The search is done in the source for library LIBRARY."
205 (if (null library)
206 (error "Don't know where `%s' is defined" symbol))
207 ;; Some functions are defined as part of the construct
208 ;; that defines something else.
209 (while (and (symbolp symbol) (get symbol 'definition-name))
210 (setq symbol (get symbol 'definition-name)))
211 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
212 (find-function-C-source symbol (match-string 1 library) type)
213 (if (string-match "\\.el\\(c\\)\\'" library)
214 (setq library (substring library 0 (match-beginning 1))))
215 (let* ((filename (find-library-name library))
216 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
217 (with-current-buffer (find-file-noselect filename)
218 (let ((regexp (format (symbol-value regexp-symbol)
219 (regexp-quote (symbol-name symbol))))
220 (case-fold-search))
221 (with-syntax-table emacs-lisp-mode-syntax-table
222 (goto-char (point-min))
223 (if (or (re-search-forward regexp nil t)
224 (re-search-forward
225 (concat "^([^ ]+" find-function-space-re "['(]"
226 (regexp-quote (symbol-name symbol))
227 "\\_>")
228 nil t))
229 (progn
230 (beginning-of-line)
231 (cons (current-buffer) (point)))
232 (error "Cannot find definition of `%s' in library `%s'"
233 symbol library))))))))
235 ;;;###autoload
236 (defun find-function-noselect (function)
237 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
239 Finds the Emacs Lisp library containing the definition of FUNCTION
240 in a buffer and the point of the definition. The buffer is
241 not selected.
243 If the file where FUNCTION is defined is not known, then it is
244 searched for in `find-function-source-path' if non nil, otherwise
245 in `load-path'."
246 (if (not function)
247 (error "You didn't specify a function"))
248 (and (subrp (symbol-function function))
249 (error "%s is a primitive function" function))
250 (let ((def (symbol-function function))
251 aliases)
252 (while (symbolp def)
253 (or (eq def function)
254 (if aliases
255 (setq aliases (concat aliases
256 (format ", which is an alias for `%s'"
257 (symbol-name def))))
258 (setq aliases (format "`%s' an alias for `%s'"
259 function (symbol-name def)))))
260 (setq function (symbol-function function)
261 def (symbol-function function)))
262 (if aliases
263 (message aliases))
264 (let ((library
265 (cond ((eq (car-safe def) 'autoload)
266 (nth 1 def))
267 ((symbol-file function 'defun)))))
268 (find-function-search-for-symbol function nil library))))
270 (defun find-function-read (&optional type)
271 "Read and return an interned symbol, defaulting to the one near point.
273 If TYPE is nil, insist on a symbol with a function definition.
274 Otherwise TYPE should be `defvar' or `defface'.
275 If TYPE is nil, defaults using `function-called-at-point',
276 otherwise uses `variable-at-point'."
277 (let ((symb (if (null type)
278 (function-called-at-point)
279 (if (eq type 'defvar)
280 (variable-at-point)
281 (variable-at-point t))))
282 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
283 (defface . facep)))))
284 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
285 (defface . "face")))))
286 (enable-recursive-minibuffers t)
287 val)
288 (if (equal symb 0)
289 (setq symb nil))
290 (setq val (completing-read
291 (concat "Find "
292 prompt
293 (if symb
294 (format " (default %s)" symb))
295 ": ")
296 obarray predicate t nil))
297 (list (if (equal val "")
298 symb
299 (intern val)))))
301 (defun find-function-do-it (symbol type switch-fn)
302 "Find Emacs Lisp SYMBOL in a buffer and display it.
303 TYPE is nil to search for a function definition,
304 or else `defvar' or `defface'.
306 The variable `find-function-recenter-line' controls how
307 to recenter the display. SWITCH-FN is the function to call
308 to display and select the buffer.
309 See also `find-function-after-hook'.
311 Set mark before moving, if the buffer already existed."
312 (let* ((orig-point (point))
313 (orig-buf (window-buffer))
314 (orig-buffers (buffer-list))
315 (buffer-point (save-excursion
316 (find-definition-noselect symbol type)))
317 (new-buf (car buffer-point))
318 (new-point (cdr buffer-point)))
319 (when buffer-point
320 (when (memq new-buf orig-buffers)
321 (push-mark orig-point))
322 (funcall switch-fn new-buf)
323 (goto-char new-point)
324 (recenter find-function-recenter-line)
325 (run-hooks 'find-function-after-hook))))
327 ;;;###autoload
328 (defun find-function (function)
329 "Find the definition of the FUNCTION near point.
331 Finds the Emacs Lisp library containing the definition of the function
332 near point (selected by `function-called-at-point') in a buffer and
333 places point before the definition.
334 Set mark before moving, if the buffer already existed.
336 The library where FUNCTION is defined is searched for in
337 `find-function-source-path', if non nil, otherwise in `load-path'.
338 See also `find-function-recenter-line' and `find-function-after-hook'."
339 (interactive (find-function-read))
340 (find-function-do-it function nil 'switch-to-buffer))
342 ;;;###autoload
343 (defun find-function-other-window (function)
344 "Find, in another window, the definition of FUNCTION near point.
346 See `find-function' for more details."
347 (interactive (find-function-read))
348 (find-function-do-it function nil 'switch-to-buffer-other-window))
350 ;;;###autoload
351 (defun find-function-other-frame (function)
352 "Find, in ananother frame, the definition of FUNCTION near point.
354 See `find-function' for more details."
355 (interactive (find-function-read))
356 (find-function-do-it function nil 'switch-to-buffer-other-frame))
358 ;;;###autoload
359 (defun find-variable-noselect (variable &optional file)
360 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
362 Finds the Emacs Lisp library containing the definition of SYMBOL
363 in a buffer, and the point of the definition. It does not switch
364 to the buffer or display it.
366 The library where VARIABLE is defined is searched for in FILE or
367 `find-function-source-path', if non nil, otherwise in `load-path'."
368 (if (not variable)
369 (error "You didn't specify a variable"))
370 (let ((library (or file (symbol-file variable 'defvar))))
371 (find-function-search-for-symbol variable 'defvar library)))
373 ;;;###autoload
374 (defun find-variable (variable)
375 "Find the definition of the VARIABLE near point.
377 Finds the Emacs Lisp library containing the definition of the variable
378 near point (selected by `variable-at-point') in a buffer and
379 places point before the definition.
381 Set mark before moving, if the buffer already existed.
383 The library where VARIABLE is defined is searched for in
384 `find-function-source-path', if non nil, otherwise in `load-path'.
385 See also `find-function-recenter-line' and `find-function-after-hook'."
386 (interactive (find-function-read 'defvar))
387 (find-function-do-it variable 'defvar 'switch-to-buffer))
389 ;;;###autoload
390 (defun find-variable-other-window (variable)
391 "Find, in another window, the definition of VARIABLE near point.
393 See `find-variable' for more details."
394 (interactive (find-function-read 'defvar))
395 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
397 ;;;###autoload
398 (defun find-variable-other-frame (variable)
399 "Find, in annother frame, the definition of VARIABLE near point.
401 See `find-variable' for more details."
402 (interactive (find-function-read 'defvar))
403 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
405 ;;;###autoload
406 (defun find-definition-noselect (symbol type &optional file)
407 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
408 TYPE says what type of definition: nil for a function,
409 `defvar' or `defface' for a variable or face. This functoin
410 does not switch to the buffer or display it.
412 The library where SYMBOL is defined is searched for in FILE or
413 `find-function-source-path', if non nil, otherwise in `load-path'."
414 (if (not symbol)
415 (error "You didn't specify a symbol"))
416 (if (null type)
417 (find-function-noselect symbol)
418 (let ((library (or file (symbol-file symbol type))))
419 (find-function-search-for-symbol symbol type library))))
421 ;; For symmetry, this should be called find-face; but some programs
422 ;; assume that, if that name is defined, it means something else.
423 ;;;###autoload
424 (defun find-face-definition (face)
425 "Find the definition of FACE. FACE defaults to the name near point.
427 Finds the Emacs Lisp library containing the definition of the face
428 near point (selected by `variable-at-point') in a buffer and
429 places point before the definition.
431 Set mark before moving, if the buffer already existed.
433 The library where FACE is defined is searched for in
434 `find-function-source-path', if non nil, otherwise in `load-path'.
435 See also `find-function-recenter-line' and `find-function-after-hook'."
436 (interactive (find-function-read 'defface))
437 (find-function-do-it face 'defface 'switch-to-buffer))
439 ;;;###autoload
440 (defun find-function-on-key (key)
441 "Find the function that KEY invokes. KEY is a string.
442 Set mark before moving, if the buffer already existed."
443 (interactive "kFind function on key: ")
444 (let (defn)
445 (save-excursion
446 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
447 (start (event-start event))
448 (modifiers (event-modifiers event))
449 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
450 (memq 'drag modifiers))
451 (posn-window start))))
452 ;; For a mouse button event, go to the button it applies to
453 ;; to get the right key bindings. And go to the right place
454 ;; in case the keymap depends on where you clicked.
455 (when (windowp window)
456 (set-buffer (window-buffer window))
457 (goto-char (posn-point start)))
458 (setq defn (key-binding key))))
459 (let ((key-desc (key-description key)))
460 (if (or (null defn) (integerp defn))
461 (message "%s is unbound" key-desc)
462 (if (consp defn)
463 (message "%s runs %s" key-desc (prin1-to-string defn))
464 (find-function-other-window defn))))))
466 ;;;###autoload
467 (defun find-function-at-point ()
468 "Find directly the function at point in the other window."
469 (interactive)
470 (let ((symb (function-called-at-point)))
471 (when symb
472 (find-function-other-window symb))))
474 ;;;###autoload
475 (defun find-variable-at-point ()
476 "Find directly the function at point in the other window."
477 (interactive)
478 (let ((symb (variable-at-point)))
479 (when (and symb (not (equal symb 0)))
480 (find-variable-other-window symb))))
482 ;;;###autoload
483 (defun find-function-setup-keys ()
484 "Define some key bindings for the find-function family of functions."
485 (define-key ctl-x-map "F" 'find-function)
486 (define-key ctl-x-4-map "F" 'find-function-other-window)
487 (define-key ctl-x-5-map "F" 'find-function-other-frame)
488 (define-key ctl-x-map "K" 'find-function-on-key)
489 (define-key ctl-x-map "V" 'find-variable)
490 (define-key ctl-x-4-map "V" 'find-variable-other-window)
491 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
493 (provide 'find-func)
495 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
496 ;;; find-func.el ends here