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