Merge from mainline.
[emacs.git] / lisp / emacs-lisp / find-func.el
blob216d91baa7b8b0b46a3f464f32b20bae122aa605
1 ;;; find-func.el --- find the definition of the Emacs Lisp function near point
3 ;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;;; Commentary:
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)
33 ;; or just:
35 ;; (load "find-func")
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.
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").
45 ;;; Code:
47 ;;; User variables:
49 (defgroup find-function nil
50 "Finds the definition of the Emacs Lisp symbol near point."
51 ;; :prefix "find-function"
52 :group 'lisp)
54 (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+")
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'.
72 Please send improvements and fixes to the maintainer."
73 :type 'regexp
74 :group 'find-function
75 :version "21.1")
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'.
88 Please send improvements and fixes to the maintainer."
89 :type 'regexp
90 :group 'find-function
91 :version "21.1")
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.
99 Please send improvements and fixes to the maintainer."
100 :type 'regexp
101 :group 'find-function
102 :version "22.1")
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)
113 (defcustom find-function-source-path nil
114 "The default list of directories where `find-function' searches.
116 If this variable is nil then `find-function' searches `load-path' by
117 default."
118 :type '(repeat directory)
119 :group 'find-function)
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")
130 (defcustom find-function-after-hook nil
131 "Hook run after finding symbol definition.
133 See the functions `find-function' and `find-variable'."
134 :type 'hook
135 :group 'find-function
136 :version "20.3")
138 ;;; Functions:
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)))))
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)))
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.")
169 (declare-function ad-get-advice-info "advice" (function))
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))
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))))
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 (def (if (eq (function-called-at-point) 'require)
217 ;; `function-called-at-point' may return 'require
218 ;; with `point' anywhere on this line. So wrap the
219 ;; `save-excursion' below in a `condition-case' to
220 ;; avoid reporting a scan-error here.
221 (condition-case nil
222 (save-excursion
223 (backward-up-list)
224 (forward-char)
225 (forward-sexp 2)
226 (thing-at-point 'symbol))
227 (error nil))
228 (thing-at-point 'symbol))))
229 (when def
230 (setq def (and (locate-file-completion-table
231 dirs suffixes def nil 'lambda)
232 def)))
233 (list
234 (completing-read (if def (format "Library name (default %s): " def)
235 "Library name: ")
236 (apply-partially 'locate-file-completion-table
237 dirs suffixes)
238 nil nil nil nil def))))
239 (let ((buf (find-file-noselect (find-library-name library))))
240 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
242 ;;;###autoload
243 (defun find-function-search-for-symbol (symbol type library)
244 "Search for SYMBOL's definition of type TYPE in LIBRARY.
245 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
246 or just (BUFFER . nil) if the definition can't be found in the file.
248 If TYPE is nil, look for a function definition.
249 Otherwise, TYPE specifies the kind of definition,
250 and it is interpreted via `find-function-regexp-alist'.
251 The search is done in the source for library LIBRARY."
252 (if (null library)
253 (error "Don't know where `%s' is defined" symbol))
254 ;; Some functions are defined as part of the construct
255 ;; that defines something else.
256 (while (and (symbolp symbol) (get symbol 'definition-name))
257 (setq symbol (get symbol 'definition-name)))
258 (if (string-match "\\`src/\\(.*\\.\\(c\\|m\\)\\)\\'" library)
259 (find-function-C-source symbol (match-string 1 library) type)
260 (when (string-match "\\.el\\(c\\)\\'" library)
261 (setq library (substring library 0 (match-beginning 1))))
262 ;; Strip extension from .emacs.el to make sure symbol is searched in
263 ;; .emacs too.
264 (when (string-match "\\.emacs\\(.el\\)" library)
265 (setq library (substring library 0 (match-beginning 1))))
266 (let* ((filename (find-library-name library))
267 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
268 (with-current-buffer (find-file-noselect filename)
269 (let ((regexp (format (symbol-value regexp-symbol)
270 ;; Entry for ` (backquote) macro in loaddefs.el,
271 ;; (defalias (quote \`)..., has a \ but
272 ;; (symbol-name symbol) doesn't. Add an
273 ;; optional \ to catch this.
274 (concat "\\\\?"
275 (regexp-quote (symbol-name symbol)))))
276 (case-fold-search))
277 (with-syntax-table emacs-lisp-mode-syntax-table
278 (goto-char (point-min))
279 (if (or (re-search-forward regexp nil t)
280 ;; `regexp' matches definitions using known forms like
281 ;; `defun', or `defvar'. But some functions/variables
282 ;; are defined using special macros (or functions), so
283 ;; if `regexp' can't find the definition, we look for
284 ;; something of the form "(SOMETHING <symbol> ...)".
285 ;; This fails to distinguish function definitions from
286 ;; variable declarations (or even uses thereof), but is
287 ;; a good pragmatic fallback.
288 (re-search-forward
289 (concat "^([^ ]+" find-function-space-re "['(]?"
290 (regexp-quote (symbol-name symbol))
291 "\\_>")
292 nil t))
293 (progn
294 (beginning-of-line)
295 (cons (current-buffer) (point)))
296 (cons (current-buffer) nil))))))))
298 ;;;###autoload
299 (defun find-function-noselect (function)
300 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
302 Finds the source file containing the definition of FUNCTION
303 in a buffer and the point of the definition. The buffer is
304 not selected. If the function definition can't be found in
305 the buffer, returns (BUFFER).
307 If the file where FUNCTION is defined is not known, then it is
308 searched for in `find-function-source-path' if non-nil, otherwise
309 in `load-path'."
310 (if (not function)
311 (error "You didn't specify a function"))
312 (let ((def (symbol-function (find-function-advised-original function)))
313 aliases)
314 ;; FIXME for completeness, it might be nice to print something like:
315 ;; foo (which is advised), which is an alias for bar (which is advised).
316 (while (symbolp def)
317 (or (eq def function)
318 (if aliases
319 (setq aliases (concat aliases
320 (format ", which is an alias for `%s'"
321 (symbol-name def))))
322 (setq aliases (format "`%s' is an alias for `%s'"
323 function (symbol-name def)))))
324 (setq function (symbol-function (find-function-advised-original function))
325 def (symbol-function (find-function-advised-original function))))
326 (if aliases
327 (message "%s" aliases))
328 (let ((library
329 (cond ((eq (car-safe def) 'autoload)
330 (nth 1 def))
331 ((subrp def)
332 (help-C-file-name def 'subr))
333 ((symbol-file function 'defun)))))
334 (find-function-search-for-symbol function nil library))))
336 (defun find-function-read (&optional type)
337 "Read and return an interned symbol, defaulting to the one near point.
339 If TYPE is nil, insist on a symbol with a function definition.
340 Otherwise TYPE should be `defvar' or `defface'.
341 If TYPE is nil, defaults using `function-called-at-point',
342 otherwise uses `variable-at-point'."
343 (let ((symb (if (null type)
344 (function-called-at-point)
345 (if (eq type 'defvar)
346 (variable-at-point)
347 (variable-at-point t))))
348 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
349 (defface . facep)))))
350 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
351 (defface . "face")))))
352 (enable-recursive-minibuffers t)
353 val)
354 (if (equal symb 0)
355 (setq symb nil))
356 (setq val (completing-read
357 (concat "Find "
358 prompt
359 (if symb
360 (format " (default %s)" symb))
361 ": ")
362 obarray predicate t nil))
363 (list (if (equal val "")
364 symb
365 (intern val)))))
367 (defun find-function-do-it (symbol type switch-fn)
368 "Find Emacs Lisp SYMBOL in a buffer and display it.
369 TYPE is nil to search for a function definition,
370 or else `defvar' or `defface'.
372 The variable `find-function-recenter-line' controls how
373 to recenter the display. SWITCH-FN is the function to call
374 to display and select the buffer.
375 See also `find-function-after-hook'.
377 Set mark before moving, if the buffer already existed."
378 (let* ((orig-point (point))
379 (orig-buf (window-buffer))
380 (orig-buffers (buffer-list))
381 (buffer-point (save-excursion
382 (find-definition-noselect symbol type)))
383 (new-buf (car buffer-point))
384 (new-point (cdr buffer-point)))
385 (when buffer-point
386 (when (memq new-buf orig-buffers)
387 (push-mark orig-point))
388 (funcall switch-fn new-buf)
389 (when new-point (goto-char new-point))
390 (recenter find-function-recenter-line)
391 (run-hooks 'find-function-after-hook))))
393 ;;;###autoload
394 (defun find-function (function)
395 "Find the definition of the FUNCTION near point.
397 Finds the source file containing the definition of the function
398 near point (selected by `function-called-at-point') in a buffer and
399 places point before the definition.
400 Set mark before moving, if the buffer already existed.
402 The library where FUNCTION is defined is searched for in
403 `find-function-source-path', if non-nil, otherwise in `load-path'.
404 See also `find-function-recenter-line' and `find-function-after-hook'."
405 (interactive (find-function-read))
406 (find-function-do-it function nil 'switch-to-buffer))
408 ;;;###autoload
409 (defun find-function-other-window (function)
410 "Find, in another window, the definition of FUNCTION near point.
412 See `find-function' for more details."
413 (interactive (find-function-read))
414 (find-function-do-it function nil 'switch-to-buffer-other-window))
416 ;;;###autoload
417 (defun find-function-other-frame (function)
418 "Find, in another frame, the definition of FUNCTION near point.
420 See `find-function' for more details."
421 (interactive (find-function-read))
422 (find-function-do-it function nil 'switch-to-buffer-other-frame))
424 ;;;###autoload
425 (defun find-variable-noselect (variable &optional file)
426 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
428 Finds the library containing the definition of VARIABLE in a buffer and
429 the point of the definition. The buffer is not selected.
430 If the variable's definition can't be found in the buffer, return (BUFFER).
432 The library where VARIABLE is defined is searched for in FILE or
433 `find-function-source-path', if non-nil, otherwise in `load-path'."
434 (if (not variable)
435 (error "You didn't specify a variable")
436 (let ((library (or file
437 (symbol-file variable 'defvar)
438 (help-C-file-name variable 'var))))
439 (find-function-search-for-symbol variable 'defvar library))))
441 ;;;###autoload
442 (defun find-variable (variable)
443 "Find the definition of the VARIABLE at or before point.
445 Finds the library containing the definition of the variable
446 near point (selected by `variable-at-point') in a buffer and
447 places point before the definition.
449 Set mark before moving, if the buffer already existed.
451 The library where VARIABLE is defined is searched for in
452 `find-function-source-path', if non-nil, otherwise in `load-path'.
453 See also `find-function-recenter-line' and `find-function-after-hook'."
454 (interactive (find-function-read 'defvar))
455 (find-function-do-it variable 'defvar 'switch-to-buffer))
457 ;;;###autoload
458 (defun find-variable-other-window (variable)
459 "Find, in another window, the definition of VARIABLE near point.
461 See `find-variable' for more details."
462 (interactive (find-function-read 'defvar))
463 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
465 ;;;###autoload
466 (defun find-variable-other-frame (variable)
467 "Find, in another frame, the definition of VARIABLE near point.
469 See `find-variable' for more details."
470 (interactive (find-function-read 'defvar))
471 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
473 ;;;###autoload
474 (defun find-definition-noselect (symbol type &optional file)
475 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
476 If the definition can't be found in the buffer, return (BUFFER).
477 TYPE says what type of definition: nil for a function, `defvar' for a
478 variable, `defface' for a face. This function does not switch to the
479 buffer nor display it.
481 The library where SYMBOL is defined is searched for in FILE or
482 `find-function-source-path', if non-nil, otherwise in `load-path'."
483 (cond
484 ((not symbol)
485 (error "You didn't specify a symbol"))
486 ((null type)
487 (find-function-noselect symbol))
488 ((eq type 'defvar)
489 (find-variable-noselect symbol file))
491 (let ((library (or file (symbol-file symbol type))))
492 (find-function-search-for-symbol symbol type library)))))
494 ;; For symmetry, this should be called find-face; but some programs
495 ;; assume that, if that name is defined, it means something else.
496 ;;;###autoload
497 (defun find-face-definition (face)
498 "Find the definition of FACE. FACE defaults to the name near point.
500 Finds the Emacs Lisp library containing the definition of the face
501 near point (selected by `variable-at-point') in a buffer and
502 places point before the definition.
504 Set mark before moving, if the buffer already existed.
506 The library where FACE is defined is searched for in
507 `find-function-source-path', if non-nil, otherwise in `load-path'.
508 See also `find-function-recenter-line' and `find-function-after-hook'."
509 (interactive (find-function-read 'defface))
510 (find-function-do-it face 'defface 'switch-to-buffer))
512 ;;;###autoload
513 (defun find-function-on-key (key)
514 "Find the function that KEY invokes. KEY is a string.
515 Set mark before moving, if the buffer already existed."
516 (interactive "kFind function on key: ")
517 (let (defn)
518 (save-excursion
519 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
520 (start (event-start event))
521 (modifiers (event-modifiers event))
522 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
523 (memq 'drag modifiers))
524 (posn-window start))))
525 ;; For a mouse button event, go to the button it applies to
526 ;; to get the right key bindings. And go to the right place
527 ;; in case the keymap depends on where you clicked.
528 (when (windowp window)
529 (set-buffer (window-buffer window))
530 (goto-char (posn-point start)))
531 (setq defn (key-binding key))))
532 (let ((key-desc (key-description key)))
533 (if (or (null defn) (integerp defn))
534 (message "%s is unbound" key-desc)
535 (if (consp defn)
536 (message "%s runs %s" key-desc (prin1-to-string defn))
537 (find-function-other-window defn))))))
539 ;;;###autoload
540 (defun find-function-at-point ()
541 "Find directly the function at point in the other window."
542 (interactive)
543 (let ((symb (function-called-at-point)))
544 (when symb
545 (find-function-other-window symb))))
547 ;;;###autoload
548 (defun find-variable-at-point ()
549 "Find directly the variable at point in the other window."
550 (interactive)
551 (let ((symb (variable-at-point)))
552 (when (and symb (not (equal symb 0)))
553 (find-variable-other-window symb))))
555 ;;;###autoload
556 (defun find-function-setup-keys ()
557 "Define some key bindings for the find-function family of functions."
558 (define-key ctl-x-map "F" 'find-function)
559 (define-key ctl-x-4-map "F" 'find-function-other-window)
560 (define-key ctl-x-5-map "F" 'find-function-other-frame)
561 (define-key ctl-x-map "K" 'find-function-on-key)
562 (define-key ctl-x-map "V" 'find-variable)
563 (define-key ctl-x-4-map "V" 'find-variable-other-window)
564 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
566 (provide 'find-func)
568 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
569 ;;; find-func.el ends here