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