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