]> 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* ((path (cons (or find-function-source-path load-path)
201 (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 def path 'test) def)))
217 (list
218 (completing-read (if def (format "Library name (default %s): " def)
219 "Library name: ")
220 'locate-file-completion path nil nil nil def))))
221 (let ((buf (find-file-noselect (find-library-name library))))
222 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
223
224 ;;;###autoload
225 (defun find-function-search-for-symbol (symbol type library)
226 "Search for SYMBOL's definition of type TYPE in LIBRARY.
227 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
228 or just (BUFFER . nil) if the definition can't be found in the file.
229
230 If TYPE is nil, look for a function definition.
231 Otherwise, TYPE specifies the kind of definition,
232 and it is interpreted via `find-function-regexp-alist'.
233 The search is done in the source for library LIBRARY."
234 (if (null library)
235 (error "Don't know where `%s' is defined" symbol))
236 ;; Some functions are defined as part of the construct
237 ;; that defines something else.
238 (while (and (symbolp symbol) (get symbol 'definition-name))
239 (setq symbol (get symbol 'definition-name)))
240 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
241 (find-function-C-source symbol (match-string 1 library) type)
242 (when (string-match "\\.el\\(c\\)\\'" library)
243 (setq library (substring library 0 (match-beginning 1))))
244 ;; Strip extension from .emacs.el to make sure symbol is searched in
245 ;; .emacs too.
246 (when (string-match "\\.emacs\\(.el\\)" library)
247 (setq library (substring library 0 (match-beginning 1))))
248 (let* ((filename (find-library-name library))
249 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
250 (with-current-buffer (find-file-noselect filename)
251 (let ((regexp (format (symbol-value regexp-symbol)
252 ;; Entry for ` (backquote) macro in loaddefs.el,
253 ;; (defalias (quote \`)..., has a \ but
254 ;; (symbol-name symbol) doesn't. Add an
255 ;; optional \ to catch this.
256 (concat "\\\\?"
257 (regexp-quote (symbol-name symbol)))))
258 (case-fold-search))
259 (with-syntax-table emacs-lisp-mode-syntax-table
260 (goto-char (point-min))
261 (if (or (re-search-forward regexp nil t)
262 ;; `regexp' matches definitions using known forms like
263 ;; `defun', or `defvar'. But some functions/variables
264 ;; are defined using special macros (or functions), so
265 ;; if `regexp' can't find the definition, we look for
266 ;; something of the form "(SOMETHING <symbol> ...)".
267 ;; This fails to distinguish function definitions from
268 ;; variable declarations (or even uses thereof), but is
269 ;; a good pragmatic fallback.
270 (re-search-forward
271 (concat "^([^ ]+" find-function-space-re "['(]?"
272 (regexp-quote (symbol-name symbol))
273 "\\_>")
274 nil t))
275 (progn
276 (beginning-of-line)
277 (cons (current-buffer) (point)))
278 (cons (current-buffer) nil))))))))
279
280 ;;;###autoload
281 (defun find-function-noselect (function)
282 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
283
284 Finds the source file containing the definition of FUNCTION
285 in a buffer and the point of the definition. The buffer is
286 not selected. If the function definition can't be found in
287 the buffer, returns (BUFFER).
288
289 If the file where FUNCTION is defined is not known, then it is
290 searched for in `find-function-source-path' if non-nil, otherwise
291 in `load-path'."
292 (if (not function)
293 (error "You didn't specify a function"))
294 (let ((def (symbol-function function))
295 aliases)
296 (while (symbolp def)
297 (or (eq def function)
298 (if aliases
299 (setq aliases (concat aliases
300 (format ", which is an alias for `%s'"
301 (symbol-name def))))
302 (setq aliases (format "`%s' an alias for `%s'"
303 function (symbol-name def)))))
304 (setq function (symbol-function function)
305 def (symbol-function function)))
306 (if aliases
307 (message "%s" aliases))
308 (let ((library
309 (cond ((eq (car-safe def) 'autoload)
310 (nth 1 def))
311 ((subrp def)
312 (help-C-file-name def 'subr))
313 ((symbol-file function 'defun)))))
314 (find-function-search-for-symbol function nil library))))
315
316 (defun find-function-read (&optional type)
317 "Read and return an interned symbol, defaulting to the one near point.
318
319 If TYPE is nil, insist on a symbol with a function definition.
320 Otherwise TYPE should be `defvar' or `defface'.
321 If TYPE is nil, defaults using `function-called-at-point',
322 otherwise uses `variable-at-point'."
323 (let ((symb (if (null type)
324 (function-called-at-point)
325 (if (eq type 'defvar)
326 (variable-at-point)
327 (variable-at-point t))))
328 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
329 (defface . facep)))))
330 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
331 (defface . "face")))))
332 (enable-recursive-minibuffers t)
333 val)
334 (if (equal symb 0)
335 (setq symb nil))
336 (setq val (completing-read
337 (concat "Find "
338 prompt
339 (if symb
340 (format " (default %s)" symb))
341 ": ")
342 obarray predicate t nil))
343 (list (if (equal val "")
344 symb
345 (intern val)))))
346
347 (defun find-function-do-it (symbol type switch-fn)
348 "Find Emacs Lisp SYMBOL in a buffer and display it.
349 TYPE is nil to search for a function definition,
350 or else `defvar' or `defface'.
351
352 The variable `find-function-recenter-line' controls how
353 to recenter the display. SWITCH-FN is the function to call
354 to display and select the buffer.
355 See also `find-function-after-hook'.
356
357 Set mark before moving, if the buffer already existed."
358 (let* ((orig-point (point))
359 (orig-buf (window-buffer))
360 (orig-buffers (buffer-list))
361 (buffer-point (save-excursion
362 (find-definition-noselect symbol type)))
363 (new-buf (car buffer-point))
364 (new-point (cdr buffer-point)))
365 (when buffer-point
366 (when (memq new-buf orig-buffers)
367 (push-mark orig-point))
368 (funcall switch-fn new-buf)
369 (when new-point (goto-char new-point))
370 (recenter find-function-recenter-line)
371 (run-hooks 'find-function-after-hook))))
372
373 ;;;###autoload
374 (defun find-function (function)
375 "Find the definition of the FUNCTION near point.
376
377 Finds the source file containing the definition of the function
378 near point (selected by `function-called-at-point') in a buffer and
379 places point before the definition.
380 Set mark before moving, if the buffer already existed.
381
382 The library where FUNCTION is defined is searched for in
383 `find-function-source-path', if non-nil, otherwise in `load-path'.
384 See also `find-function-recenter-line' and `find-function-after-hook'."
385 (interactive (find-function-read))
386 (find-function-do-it function nil 'switch-to-buffer))
387
388 ;;;###autoload
389 (defun find-function-other-window (function)
390 "Find, in another window, the definition of FUNCTION near point.
391
392 See `find-function' for more details."
393 (interactive (find-function-read))
394 (find-function-do-it function nil 'switch-to-buffer-other-window))
395
396 ;;;###autoload
397 (defun find-function-other-frame (function)
398 "Find, in another frame, the definition of FUNCTION near point.
399
400 See `find-function' for more details."
401 (interactive (find-function-read))
402 (find-function-do-it function nil 'switch-to-buffer-other-frame))
403
404 ;;;###autoload
405 (defun find-variable-noselect (variable &optional file)
406 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
407
408 Finds the library containing the definition of VARIABLE in a buffer and
409 the point of the definition. The buffer is not selected.
410 If the variable's definition can't be found in the buffer, return (BUFFER).
411
412 The library where VARIABLE is defined is searched for in FILE or
413 `find-function-source-path', if non-nil, otherwise in `load-path'."
414 (if (not variable)
415 (error "You didn't specify a variable")
416 (let ((library (or file
417 (symbol-file variable 'defvar)
418 (help-C-file-name variable 'var))))
419 (find-function-search-for-symbol variable 'defvar library))))
420
421 ;;;###autoload
422 (defun find-variable (variable)
423 "Find the definition of the VARIABLE at or before point.
424
425 Finds the library containing the definition of the variable
426 near point (selected by `variable-at-point') in a buffer and
427 places point before the definition.
428
429 Set mark before moving, if the buffer already existed.
430
431 The library where VARIABLE is defined is searched for in
432 `find-function-source-path', if non-nil, otherwise in `load-path'.
433 See also `find-function-recenter-line' and `find-function-after-hook'."
434 (interactive (find-function-read 'defvar))
435 (find-function-do-it variable 'defvar 'switch-to-buffer))
436
437 ;;;###autoload
438 (defun find-variable-other-window (variable)
439 "Find, in another window, the definition of VARIABLE near point.
440
441 See `find-variable' for more details."
442 (interactive (find-function-read 'defvar))
443 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
444
445 ;;;###autoload
446 (defun find-variable-other-frame (variable)
447 "Find, in another frame, the definition of VARIABLE near point.
448
449 See `find-variable' for more details."
450 (interactive (find-function-read 'defvar))
451 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
452
453 ;;;###autoload
454 (defun find-definition-noselect (symbol type &optional file)
455 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
456 If the definition can't be found in the buffer, return (BUFFER).
457 TYPE says what type of definition: nil for a function, `defvar' for a
458 variable, `defface' for a face. This function does not switch to the
459 buffer nor display it.
460
461 The library where SYMBOL is defined is searched for in FILE or
462 `find-function-source-path', if non-nil, otherwise in `load-path'."
463 (cond
464 ((not symbol)
465 (error "You didn't specify a symbol"))
466 ((null type)
467 (find-function-noselect symbol))
468 ((eq type 'defvar)
469 (find-variable-noselect symbol file))
470 (t
471 (let ((library (or file (symbol-file symbol type))))
472 (find-function-search-for-symbol symbol type library)))))
473
474 ;; For symmetry, this should be called find-face; but some programs
475 ;; assume that, if that name is defined, it means something else.
476 ;;;###autoload
477 (defun find-face-definition (face)
478 "Find the definition of FACE. FACE defaults to the name near point.
479
480 Finds the Emacs Lisp library containing the definition of the face
481 near point (selected by `variable-at-point') in a buffer and
482 places point before the definition.
483
484 Set mark before moving, if the buffer already existed.
485
486 The library where FACE is defined is searched for in
487 `find-function-source-path', if non-nil, otherwise in `load-path'.
488 See also `find-function-recenter-line' and `find-function-after-hook'."
489 (interactive (find-function-read 'defface))
490 (find-function-do-it face 'defface 'switch-to-buffer))
491
492 ;;;###autoload
493 (defun find-function-on-key (key)
494 "Find the function that KEY invokes. KEY is a string.
495 Set mark before moving, if the buffer already existed."
496 (interactive "kFind function on key: ")
497 (let (defn)
498 (save-excursion
499 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
500 (start (event-start event))
501 (modifiers (event-modifiers event))
502 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
503 (memq 'drag modifiers))
504 (posn-window start))))
505 ;; For a mouse button event, go to the button it applies to
506 ;; to get the right key bindings. And go to the right place
507 ;; in case the keymap depends on where you clicked.
508 (when (windowp window)
509 (set-buffer (window-buffer window))
510 (goto-char (posn-point start)))
511 (setq defn (key-binding key))))
512 (let ((key-desc (key-description key)))
513 (if (or (null defn) (integerp defn))
514 (message "%s is unbound" key-desc)
515 (if (consp defn)
516 (message "%s runs %s" key-desc (prin1-to-string defn))
517 (find-function-other-window defn))))))
518
519 ;;;###autoload
520 (defun find-function-at-point ()
521 "Find directly the function at point in the other window."
522 (interactive)
523 (let ((symb (function-called-at-point)))
524 (when symb
525 (find-function-other-window symb))))
526
527 ;;;###autoload
528 (defun find-variable-at-point ()
529 "Find directly the variable at point in the other window."
530 (interactive)
531 (let ((symb (variable-at-point)))
532 (when (and symb (not (equal symb 0)))
533 (find-variable-other-window symb))))
534
535 ;;;###autoload
536 (defun find-function-setup-keys ()
537 "Define some key bindings for the find-function family of functions."
538 (define-key ctl-x-map "F" 'find-function)
539 (define-key ctl-x-4-map "F" 'find-function-other-window)
540 (define-key ctl-x-5-map "F" 'find-function-other-frame)
541 (define-key ctl-x-map "K" 'find-function-on-key)
542 (define-key ctl-x-map "V" 'find-variable)
543 (define-key ctl-x-4-map "V" 'find-variable-other-window)
544 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
545
546 (provide 'find-func)
547
548 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
549 ;;; find-func.el ends here