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