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