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
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/>.
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!!
31 ;; (find-function-setup-keys)
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
49 (defgroup find-function nil
50 "Finds the definition of the Emacs Lisp symbol near point."
51 ;; :prefix "find-function"
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)
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."
77 (defcustom find-variable-regexp
79 "^\\s-*(\\(def[^fumag]\\(\\w\\|\\s_\\)+\\*?\\|\
80 easy-mmode-def\\(map\\|syntax\\)\\|easy-menu-define\\)"
81 find-function-space-re
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."
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."
101 :group
'find-function
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
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
)
127 :group
'find-function
130 (defcustom find-function-after-hook nil
131 "Hook run after finding symbol definition.
133 See the functions `find-function' and `find-variable'."
135 :group
'find-function
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
150 (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library
)
151 (setq library
(replace-match "" t t library
)))
154 (or find-function-source-path load-path
)
155 (find-library-suffixes))
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
))
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
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
)
176 (let ((ofunc (cdr (assq 'origname
(ad-get-advice-info func
)))))
177 (and (fboundp ofunc
) ofunc
)))
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
)))
191 ;; Either or both an alias and its target might be advised.
192 (setq fun-or-var
(find-function-advised-original
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
199 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
200 (regexp-quote (symbol-name fun-or-var
))
202 (concat "DEFUN[ \t\n]*([ \t\n]*\""
203 (regexp-quote (subr-name fun-or-var
))
206 (error "Can't find source for %s" fun-or-var
))
207 (cons (current-buffer) (match-beginning 0))))
210 (defun find-library (library)
211 "Find the Emacs Lisp source of LIBRARY.
212 LIBRARY should be a string (the name of the library)."
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.
226 (thing-at-point 'symbol
))
228 (thing-at-point 'symbol
))))
230 (setq def
(and (locate-file-completion-table
231 dirs suffixes def nil
'lambda
)
234 (completing-read (if def
(format "Library name (default %s): " def
)
236 (apply-partially 'locate-file-completion-table
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
)))))
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."
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
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.
275 (regexp-quote (symbol-name symbol
)))))
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.
289 (concat "^([^ ]+" find-function-space-re
"['(]?"
290 (regexp-quote (symbol-name symbol
))
295 (cons (current-buffer) (point)))
296 (cons (current-buffer) nil
))))))))
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
311 (error "You didn't specify a function"))
312 (let ((def (symbol-function (find-function-advised-original function
)))
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).
317 (or (eq def function
)
319 (setq aliases
(concat aliases
320 (format ", which is an alias for `%s'"
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
))))
327 (message "%s" aliases
))
329 (cond ((eq (car-safe def
) 'autoload
)
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
)
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
)
356 (setq val
(completing-read
360 (format " (default %s)" symb
))
362 obarray predicate t nil
))
363 (list (if (equal 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
)))
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
))))
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
))
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
))
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
))
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'."
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
))))
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
))
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
))
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
))
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'."
485 (error "You didn't specify a symbol"))
487 (find-function-noselect symbol
))
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.
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
))
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: ")
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
)
536 (message "%s runs %s" key-desc
(prin1-to-string defn
))
537 (find-function-other-window defn
))))))
540 (defun find-function-at-point ()
541 "Find directly the function at point in the other window."
543 (let ((symb (function-called-at-point)))
545 (find-function-other-window symb
))))
548 (defun find-variable-at-point ()
549 "Find directly the variable at point in the other window."
551 (let ((symb (variable-at-point)))
552 (when (and symb
(not (equal symb
0)))
553 (find-variable-other-window symb
))))
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
))
568 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
569 ;;; find-func.el ends here