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