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