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
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/>.
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!!
30 ;; (find-function-setup-keys)
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
48 (defgroup find-function nil
49 "Finds the definition of the Emacs Lisp symbol near point."
50 ;; :prefix "find-function"
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)
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."
76 (defcustom find-variable-regexp
78 "^\\s-*(\\(def[^fumag]\\(\\w\\|\\s_\\)+\\*?\\|\
79 easy-mmode-def\\(map\\|syntax\\)\\|easy-menu-define\\)"
80 find-function-space-re
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."
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."
100 :group
'find-function
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
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
)
129 :group
'find-function
132 (defcustom find-function-after-hook nil
133 "Hook run after finding symbol definition.
135 See the functions `find-function' and `find-variable'."
137 :group
'find-function
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
)))
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
161 (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library
)
162 (setq library
(replace-match "" t t library
)))
165 (or find-function-source-path load-path
)
166 (find-library-suffixes))
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
)))
175 (or find-function-source-path load-path
)
176 (find-library-suffixes))
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
189 (declare-function ad-get-advice-info
"advice" (function))
191 (defun find-function-advised-original (func)
192 "Return the original function symbol of an advised function FUNC.
193 If FUNC is not the symbol of an advised function, just returns FUNC."
194 (or (and (symbolp func
)
196 (let ((ofunc (cdr (assq 'origname
(ad-get-advice-info func
)))))
197 (and (fboundp ofunc
) ofunc
)))
200 (defun find-function-C-source (fun-or-var file type
)
201 "Find the source location where FUN-OR-VAR is defined in FILE.
202 TYPE should be nil to find a function, or `defvar' to find a variable."
203 (let ((dir (or find-function-C-source-directory
204 (read-directory-name "Emacs C source dir: " nil nil t
))))
205 (setq file
(expand-file-name file dir
))
206 (if (file-readable-p file
)
207 (if (null find-function-C-source-directory
)
208 (setq find-function-C-source-directory dir
))
209 (error "The C source file %s is not available"
210 (file-name-nondirectory file
))))
212 ;; Either or both an alias and its target might be advised.
213 (setq fun-or-var
(find-function-advised-original
215 (find-function-advised-original fun-or-var
)))))
216 (with-current-buffer (find-file-noselect file
)
217 (goto-char (point-min))
218 (unless (re-search-forward
220 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
221 (regexp-quote (symbol-name fun-or-var
))
223 (concat "DEFUN[ \t\n]*([ \t\n]*\""
224 (regexp-quote (subr-name (advice--cd*r fun-or-var
)))
227 (error "Can't find source for %s" fun-or-var
))
228 (cons (current-buffer) (match-beginning 0))))
231 (defun find-library (library)
232 "Find the Emacs Lisp source of LIBRARY.
233 LIBRARY should be a string (the name of the library)."
235 (let* ((dirs (or find-function-source-path load-path
))
236 (suffixes (find-library-suffixes))
237 (table (apply-partially 'locate-file-completion-table
239 (def (if (eq (function-called-at-point) 'require
)
240 ;; `function-called-at-point' may return 'require
241 ;; with `point' anywhere on this line. So wrap the
242 ;; `save-excursion' below in a `condition-case' to
243 ;; avoid reporting a scan-error here.
249 (thing-at-point 'symbol
))
251 (thing-at-point 'symbol
))))
252 (when (and def
(not (test-completion def table
)))
255 (completing-read (if def
(format "Library name (default %s): " def
)
257 table nil nil nil nil def
))))
258 (let ((buf (find-file-noselect (find-library-name library
))))
259 (condition-case nil
(switch-to-buffer buf
) (error (pop-to-buffer buf
)))))
262 (defun find-function-search-for-symbol (symbol type library
)
263 "Search for SYMBOL's definition of type TYPE in LIBRARY.
264 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
265 or just (BUFFER . nil) if the definition can't be found in the file.
267 If TYPE is nil, look for a function definition.
268 Otherwise, TYPE specifies the kind of definition,
269 and it is interpreted via `find-function-regexp-alist'.
270 The search is done in the source for library LIBRARY."
272 (error "Don't know where `%s' is defined" symbol
))
273 ;; Some functions are defined as part of the construct
274 ;; that defines something else.
275 (while (and (symbolp symbol
) (get symbol
'definition-name
))
276 (setq symbol
(get symbol
'definition-name
)))
277 (if (string-match "\\`src/\\(.*\\.\\(c\\|m\\)\\)\\'" library
)
278 (find-function-C-source symbol
(match-string 1 library
) type
)
279 (when (string-match "\\.el\\(c\\)\\'" library
)
280 (setq library
(substring library
0 (match-beginning 1))))
281 ;; Strip extension from .emacs.el to make sure symbol is searched in
283 (when (string-match "\\.emacs\\(.el\\)" library
)
284 (setq library
(substring library
0 (match-beginning 1))))
285 (let* ((filename (find-library-name library
))
286 (regexp-symbol (cdr (assq type find-function-regexp-alist
))))
287 (with-current-buffer (find-file-noselect filename
)
288 (let ((regexp (if (functionp regexp-symbol
) regexp-symbol
289 (format (symbol-value regexp-symbol
)
290 ;; Entry for ` (backquote) macro in loaddefs.el,
291 ;; (defalias (quote \`)..., has a \ but
292 ;; (symbol-name symbol) doesn't. Add an
293 ;; optional \ to catch this.
295 (regexp-quote (symbol-name symbol
))))))
297 (with-syntax-table emacs-lisp-mode-syntax-table
298 (goto-char (point-min))
299 (if (if (functionp regexp
)
300 (funcall regexp symbol
)
301 (or (re-search-forward regexp nil t
)
302 ;; `regexp' matches definitions using known forms like
303 ;; `defun', or `defvar'. But some functions/variables
304 ;; are defined using special macros (or functions), so
305 ;; if `regexp' can't find the definition, we look for
306 ;; something of the form "(SOMETHING <symbol> ...)".
307 ;; This fails to distinguish function definitions from
308 ;; variable declarations (or even uses thereof), but is
309 ;; a good pragmatic fallback.
311 (concat "^([^ ]+" find-function-space-re
"['(]?"
312 (regexp-quote (symbol-name symbol
))
317 (cons (current-buffer) (point)))
318 (cons (current-buffer) nil
))))))))
320 (defun find-function-library (function &optional lisp-only verbose
)
321 "Return the pair (ORIG-FUNCTION . LIBRARY) for FUNCTION.
323 ORIG-FUNCTION is the original name, after removing all advice and
324 resolving aliases. LIBRARY is an absolute file name, a relative
325 file name inside the C sources directory, or a name of an
328 If ORIG-FUNCTION is a built-in function and LISP-ONLY is non-nil,
331 If VERBOSE is non-nil, and FUNCTION is an alias, display a
332 message about the whole chain of aliases."
333 (let ((def (if (symbolp function
)
334 (symbol-function (find-function-advised-original function
))))
336 ;; FIXME for completeness, it might be nice to print something like:
337 ;; foo (which is advised), which is an alias for bar (which is advised).
338 (while (and def
(symbolp def
))
339 (or (eq def function
)
341 (setq aliases
(if aliases
343 (format ", which is an alias for `%s'"
345 (format "`%s' is an alias for `%s'"
346 function
(symbol-name def
)))))
347 (setq function
(symbol-function (find-function-advised-original function
))
348 def
(symbol-function (find-function-advised-original function
))))
350 (message "%s" aliases
))
353 ((autoloadp def
) (nth 1 def
))
356 (error "%s is a built-in function" function
))
357 (help-C-file-name def
'subr
))
358 ((symbol-file function
'defun
))))))
361 (defun find-function-noselect (function &optional lisp-only
)
362 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
364 Finds the source file containing the definition of FUNCTION
365 in a buffer and the point of the definition. The buffer is
366 not selected. If the function definition can't be found in
367 the buffer, returns (BUFFER).
369 If FUNCTION is a built-in function, this function normally
370 attempts to find it in the Emacs C sources; however, if LISP-ONLY
371 is non-nil, signal an error instead.
373 If the file where FUNCTION is defined is not known, then it is
374 searched for in `find-function-source-path' if non-nil, otherwise
377 (error "You didn't specify a function"))
378 (let ((func-lib (find-function-library function lisp-only t
)))
379 (find-function-search-for-symbol (car func-lib
) nil
(cdr func-lib
))))
381 (defun find-function-read (&optional type
)
382 "Read and return an interned symbol, defaulting to the one near point.
384 If TYPE is nil, insist on a symbol with a function definition.
385 Otherwise TYPE should be `defvar' or `defface'.
386 If TYPE is nil, defaults using `function-called-at-point',
387 otherwise uses `variable-at-point'."
388 (let* ((symb1 (cond ((null type
) (function-called-at-point))
389 ((eq type
'defvar
) (variable-at-point))
390 (t (variable-at-point t
))))
391 (symb (unless (eq symb1
0) symb1
))
392 (predicate (cdr (assq type
'((nil . fboundp
)
394 (defface . facep
)))))
395 (prompt-type (cdr (assq type
'((nil .
"function")
396 (defvar .
"variable")
397 (defface .
"face")))))
398 (prompt (concat "Find " prompt-type
399 (and symb
(format " (default %s)" symb
))
401 (enable-recursive-minibuffers t
))
402 (list (intern (completing-read
403 prompt obarray predicate
404 t nil nil
(and symb
(symbol-name symb
)))))))
406 (defun find-function-do-it (symbol type switch-fn
)
407 "Find Emacs Lisp SYMBOL in a buffer and display it.
408 TYPE is nil to search for a function definition,
409 or else `defvar' or `defface'.
411 The variable `find-function-recenter-line' controls how
412 to recenter the display. SWITCH-FN is the function to call
413 to display and select the buffer.
414 See also `find-function-after-hook'.
416 Set mark before moving, if the buffer already existed."
417 (let* ((orig-point (point))
418 (orig-buffers (buffer-list))
419 (buffer-point (save-excursion
420 (find-definition-noselect symbol type
)))
421 (new-buf (car buffer-point
))
422 (new-point (cdr buffer-point
)))
424 (when (memq new-buf orig-buffers
)
425 (push-mark orig-point
))
426 (funcall switch-fn new-buf
)
427 (when new-point
(goto-char new-point
))
428 (recenter find-function-recenter-line
)
429 (run-hooks 'find-function-after-hook
))))
432 (defun find-function (function)
433 "Find the definition of the FUNCTION near point.
435 Finds the source file containing the definition of the function
436 near point (selected by `function-called-at-point') in a buffer and
437 places point before the definition.
438 Set mark before moving, if the buffer already existed.
440 The library where FUNCTION is defined is searched for in
441 `find-function-source-path', if non-nil, otherwise in `load-path'.
442 See also `find-function-recenter-line' and `find-function-after-hook'."
443 (interactive (find-function-read))
444 (find-function-do-it function nil
'switch-to-buffer
))
447 (defun find-function-other-window (function)
448 "Find, in another window, the definition of FUNCTION near point.
450 See `find-function' for more details."
451 (interactive (find-function-read))
452 (find-function-do-it function nil
'switch-to-buffer-other-window
))
455 (defun find-function-other-frame (function)
456 "Find, in another frame, the definition of FUNCTION near point.
458 See `find-function' for more details."
459 (interactive (find-function-read))
460 (find-function-do-it function nil
'switch-to-buffer-other-frame
))
463 (defun find-variable-noselect (variable &optional file
)
464 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
466 Finds the library containing the definition of VARIABLE in a buffer and
467 the point of the definition. The buffer is not selected.
468 If the variable's definition can't be found in the buffer, return (BUFFER).
470 The library where VARIABLE is defined is searched for in FILE or
471 `find-function-source-path', if non-nil, otherwise in `load-path'."
473 (error "You didn't specify a variable")
474 (let ((library (or file
475 (symbol-file variable
'defvar
)
476 (help-C-file-name variable
'var
))))
477 (find-function-search-for-symbol variable
'defvar library
))))
480 (defun find-variable (variable)
481 "Find the definition of the VARIABLE at or before point.
483 Finds the library containing the definition of the variable
484 near point (selected by `variable-at-point') in a buffer and
485 places point before the definition.
487 Set mark before moving, if the buffer already existed.
489 The library where VARIABLE is defined is searched for in
490 `find-function-source-path', if non-nil, otherwise in `load-path'.
491 See also `find-function-recenter-line' and `find-function-after-hook'."
492 (interactive (find-function-read 'defvar
))
493 (find-function-do-it variable
'defvar
'switch-to-buffer
))
496 (defun find-variable-other-window (variable)
497 "Find, in another window, the definition of VARIABLE near point.
499 See `find-variable' for more details."
500 (interactive (find-function-read 'defvar
))
501 (find-function-do-it variable
'defvar
'switch-to-buffer-other-window
))
504 (defun find-variable-other-frame (variable)
505 "Find, in another frame, the definition of VARIABLE near point.
507 See `find-variable' for more details."
508 (interactive (find-function-read 'defvar
))
509 (find-function-do-it variable
'defvar
'switch-to-buffer-other-frame
))
512 (defun find-definition-noselect (symbol type
&optional file
)
513 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
514 If the definition can't be found in the buffer, return (BUFFER).
515 TYPE says what type of definition: nil for a function, `defvar' for a
516 variable, `defface' for a face. This function does not switch to the
517 buffer nor display it.
519 The library where SYMBOL is defined is searched for in FILE or
520 `find-function-source-path', if non-nil, otherwise in `load-path'."
523 (error "You didn't specify a symbol"))
525 (find-function-noselect symbol
))
527 (find-variable-noselect symbol file
))
529 (let ((library (or file
(symbol-file symbol type
))))
530 (find-function-search-for-symbol symbol type library
)))))
532 ;; For symmetry, this should be called find-face; but some programs
533 ;; assume that, if that name is defined, it means something else.
535 (defun find-face-definition (face)
536 "Find the definition of FACE. FACE defaults to the name near point.
538 Finds the Emacs Lisp library containing the definition of the face
539 near point (selected by `variable-at-point') in a buffer and
540 places point before the definition.
542 Set mark before moving, if the buffer already existed.
544 The library where FACE is defined is searched for in
545 `find-function-source-path', if non-nil, otherwise in `load-path'.
546 See also `find-function-recenter-line' and `find-function-after-hook'."
547 (interactive (find-function-read 'defface
))
548 (find-function-do-it face
'defface
'switch-to-buffer
))
551 (defun find-function-on-key (key)
552 "Find the function that KEY invokes. KEY is a string.
553 Set mark before moving, if the buffer already existed."
554 (interactive "kFind function on key: ")
557 (let* ((event (and (eventp key
) (aref key
0))) ; Null event OK below.
558 (start (event-start event
))
559 (modifiers (event-modifiers event
))
560 (window (and (or (memq 'click modifiers
) (memq 'down modifiers
)
561 (memq 'drag modifiers
))
562 (posn-window start
))))
563 ;; For a mouse button event, go to the button it applies to
564 ;; to get the right key bindings. And go to the right place
565 ;; in case the keymap depends on where you clicked.
566 (when (windowp window
)
567 (set-buffer (window-buffer window
))
568 (goto-char (posn-point start
)))
569 (setq defn
(key-binding key
))))
570 (let ((key-desc (key-description key
)))
571 (if (or (null defn
) (integerp defn
))
572 (message "%s is unbound" key-desc
)
574 (message "%s runs %s" key-desc
(prin1-to-string defn
))
575 (find-function-other-window defn
))))))
578 (defun find-function-at-point ()
579 "Find directly the function at point in the other window."
581 (let ((symb (function-called-at-point)))
583 (find-function-other-window symb
))))
586 (defun find-variable-at-point ()
587 "Find directly the variable at point in the other window."
589 (let ((symb (variable-at-point)))
590 (when (and symb
(not (equal symb
0)))
591 (find-variable-other-window symb
))))
594 (defun find-function-setup-keys ()
595 "Define some key bindings for the find-function family of functions."
596 (define-key ctl-x-map
"F" 'find-function
)
597 (define-key ctl-x-4-map
"F" 'find-function-other-window
)
598 (define-key ctl-x-5-map
"F" 'find-function-other-frame
)
599 (define-key ctl-x-map
"K" 'find-function-on-key
)
600 (define-key ctl-x-map
"V" 'find-variable
)
601 (define-key ctl-x-4-map
"V" 'find-variable-other-window
)
602 (define-key ctl-x-5-map
"V" 'find-variable-other-frame
))
606 ;;; find-func.el ends here