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 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, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; The funniest thing about this is that I can't imagine why a package
31 ;; so obviously useful as this hasn't been written before!!
33 ;; (find-function-setup-keys)
39 ;; if you don't like the given keybindings and away you go! It does
40 ;; pretty much what you would expect, putting the cursor at the
41 ;; definition of the function or variable at point.
43 ;; The code started out from `describe-function', `describe-key'
44 ;; ("help.el") and `fff-find-loaded-emacs-lisp-function' (Noah Friedman's
51 (defgroup find-function nil
52 "Finds the definition of the Emacs Lisp symbol near point."
53 ;; :prefix "find-function"
56 (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+")
58 (defcustom find-function-regexp
59 ;; Match things like (defun foo ...), (defmacro foo ...),
60 ;; (define-skeleton foo ...), (define-generic-mode 'foo ...),
61 ;; (define-derived-mode foo ...), (define-minor-mode foo)
63 "^\\s-*(\\(def\\(ine-skeleton\\|ine-generic-mode\\|ine-derived-mode\\|\
64 ine\\(?:-global\\)?-minor-mode\\|ine-compilation-mode\\|un-cvs-mode\\|\
65 foo\\|[^icfgv]\\(\\w\\|\\s_\\)+\\*?\\)\\|easy-mmode-define-[a-z-]+\\|easy-menu-define\\|\
66 menu-bar-make-toggle\\)"
67 find-function-space-re
68 "\\('\\|\(quote \\)?%s\\(\\s-\\|$\\|\(\\|\)\\)")
69 "The regexp used by `find-function' to search for a function definition.
70 Note it must contain a `%s' at the place where `format'
71 should insert the function name. The default value avoids `defconst',
72 `defgroup', `defvar', `defface'.
74 Please send improvements and fixes to the maintainer."
79 (defcustom find-variable-regexp
81 "^\\s-*(\\(def[^fumag]\\(\\w\\|\\s_\\)+\\*?\\|\
82 easy-mmode-def\\(map\\|syntax\\)\\|easy-menu-define\\)"
83 find-function-space-re
85 "The regexp used by `find-variable' to search for a variable definition.
86 Note it must contain a `%s' at the place where `format'
87 should insert the variable name. The default value
88 avoids `defun', `defmacro', `defalias', `defadvice', `defgroup', `defface'.
90 Please send improvements and fixes to the maintainer."
95 (defcustom find-face-regexp
96 (concat"^\\s-*(defface" find-function-space-re "%s\\(\\s-\\|$\\)")
97 "The regexp used by `find-face' to search for a face definition.
98 Note it must contain a `%s' at the place where `format'
99 should insert the face name.
101 Please send improvements and fixes to the maintainer."
103 :group 'find-function
106 (defvar find-function-regexp-alist
107 '((nil . find-function-regexp)
108 (defvar . find-variable-regexp)
109 (defface . find-face-regexp))
110 "Alist mapping definition types into regexp variables.
111 Each regexp variable's value should actually be a format string
112 to be used to substitute the desired symbol name into the regexp.")
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-name (library)
148 "Return the absolute file name of the Lisp source of LIBRARY."
149 ;; If the library is byte-compiled, try to find a source library by
151 (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library)
152 (setq library (replace-match "" t t library)))
155 (or find-function-source-path load-path)
156 (find-library-suffixes))
158 (or find-function-source-path load-path)
159 load-file-rep-suffixes)
160 (error "Can't find library %s" library)))
162 (defvar find-function-C-source-directory
163 (let ((dir (expand-file-name "src" source-directory)))
164 (when (and (file-directory-p dir) (file-readable-p dir))
166 "Directory where the C source files of Emacs can be found.
167 If nil, do not try to find the source code of functions and variables
170 (defun find-function-C-source (fun-or-var file type)
171 "Find the source location where FUN-OR-VAR is defined in FILE.
172 TYPE should be nil to find a function, or `defvar' to find a variable."
173 (unless find-function-C-source-directory
174 (setq find-function-C-source-directory
175 (read-directory-name "Emacs C source dir: " nil nil t)))
176 (setq file (expand-file-name file find-function-C-source-directory))
177 (unless (file-readable-p file)
178 (error "The C source file %s is not available"
179 (file-name-nondirectory file)))
181 (setq fun-or-var (indirect-function fun-or-var)))
182 (with-current-buffer (find-file-noselect file)
183 (goto-char (point-min))
184 (unless (re-search-forward
186 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
187 (regexp-quote (symbol-name fun-or-var))
189 (concat "DEFUN[ \t\n]*([ \t\n]*\""
190 (regexp-quote (subr-name fun-or-var))
193 (error "Can't find source for %s" fun-or-var))
194 (cons (current-buffer) (match-beginning 0))))
197 (defun find-library (library)
198 "Find the elisp source of LIBRARY."
200 (let* ((dirs (or find-function-source-path load-path))
201 (suffixes (find-library-suffixes))
202 (def (if (eq (function-called-at-point) 'require)
203 ;; `function-called-at-point' may return 'require
204 ;; with `point' anywhere on this line. So wrap the
205 ;; `save-excursion' below in a `condition-case' to
206 ;; avoid reporting a scan-error here.
212 (thing-at-point 'symbol))
214 (thing-at-point 'symbol))))
216 (setq def (and (locate-file-completion-table
217 dirs suffixes def nil 'lambda)
220 (completing-read (if def (format "Library name (default %s): " def)
222 (apply-partially 'locate-file-completion-table
224 nil nil nil nil def))))
225 (let ((buf (find-file-noselect (find-library-name library))))
226 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
229 (defun find-function-search-for-symbol (symbol type library)
230 "Search for SYMBOL's definition of type TYPE in LIBRARY.
231 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
232 or just (BUFFER . nil) if the definition can't be found in the file.
234 If TYPE is nil, look for a function definition.
235 Otherwise, TYPE specifies the kind of definition,
236 and it is interpreted via `find-function-regexp-alist'.
237 The search is done in the source for library LIBRARY."
239 (error "Don't know where `%s' is defined" symbol))
240 ;; Some functions are defined as part of the construct
241 ;; that defines something else.
242 (while (and (symbolp symbol) (get symbol 'definition-name))
243 (setq symbol (get symbol 'definition-name)))
244 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
245 (find-function-C-source symbol (match-string 1 library) type)
246 (when (string-match "\\.el\\(c\\)\\'" library)
247 (setq library (substring library 0 (match-beginning 1))))
248 ;; Strip extension from .emacs.el to make sure symbol is searched in
250 (when (string-match "\\.emacs\\(.el\\)" library)
251 (setq library (substring library 0 (match-beginning 1))))
252 (let* ((filename (find-library-name library))
253 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
254 (with-current-buffer (find-file-noselect filename)
255 (let ((regexp (format (symbol-value regexp-symbol)
256 ;; Entry for ` (backquote) macro in loaddefs.el,
257 ;; (defalias (quote \`)..., has a \ but
258 ;; (symbol-name symbol) doesn't. Add an
259 ;; optional \ to catch this.
261 (regexp-quote (symbol-name symbol)))))
263 (with-syntax-table emacs-lisp-mode-syntax-table
264 (goto-char (point-min))
265 (if (or (re-search-forward regexp nil t)
266 ;; `regexp' matches definitions using known forms like
267 ;; `defun', or `defvar'. But some functions/variables
268 ;; are defined using special macros (or functions), so
269 ;; if `regexp' can't find the definition, we look for
270 ;; something of the form "(SOMETHING <symbol> ...)".
271 ;; This fails to distinguish function definitions from
272 ;; variable declarations (or even uses thereof), but is
273 ;; a good pragmatic fallback.
275 (concat "^([^ ]+" find-function-space-re "['(]?"
276 (regexp-quote (symbol-name symbol))
281 (cons (current-buffer) (point)))
282 (cons (current-buffer) nil))))))))
285 (defun find-function-noselect (function)
286 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
288 Finds the source file containing the definition of FUNCTION
289 in a buffer and the point of the definition. The buffer is
290 not selected. If the function definition can't be found in
291 the buffer, returns (BUFFER).
293 If the file where FUNCTION is defined is not known, then it is
294 searched for in `find-function-source-path' if non-nil, otherwise
297 (error "You didn't specify a function"))
298 (let ((def (symbol-function function))
301 (or (eq def function)
303 (setq aliases (concat aliases
304 (format ", which is an alias for `%s'"
306 (setq aliases (format "`%s' an alias for `%s'"
307 function (symbol-name def)))))
308 (setq function (symbol-function function)
309 def (symbol-function function)))
311 (message "%s" aliases))
313 (cond ((eq (car-safe def) 'autoload)
316 (help-C-file-name def 'subr))
317 ((symbol-file function 'defun)))))
318 (find-function-search-for-symbol function nil library))))
320 (defun find-function-read (&optional type)
321 "Read and return an interned symbol, defaulting to the one near point.
323 If TYPE is nil, insist on a symbol with a function definition.
324 Otherwise TYPE should be `defvar' or `defface'.
325 If TYPE is nil, defaults using `function-called-at-point',
326 otherwise uses `variable-at-point'."
327 (let ((symb (if (null type)
328 (function-called-at-point)
329 (if (eq type 'defvar)
331 (variable-at-point t))))
332 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
333 (defface . facep)))))
334 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
335 (defface . "face")))))
336 (enable-recursive-minibuffers t)
340 (setq val (completing-read
344 (format " (default %s)" symb))
346 obarray predicate t nil))
347 (list (if (equal val "")
351 (defun find-function-do-it (symbol type switch-fn)
352 "Find Emacs Lisp SYMBOL in a buffer and display it.
353 TYPE is nil to search for a function definition,
354 or else `defvar' or `defface'.
356 The variable `find-function-recenter-line' controls how
357 to recenter the display. SWITCH-FN is the function to call
358 to display and select the buffer.
359 See also `find-function-after-hook'.
361 Set mark before moving, if the buffer already existed."
362 (let* ((orig-point (point))
363 (orig-buf (window-buffer))
364 (orig-buffers (buffer-list))
365 (buffer-point (save-excursion
366 (find-definition-noselect symbol type)))
367 (new-buf (car buffer-point))
368 (new-point (cdr buffer-point)))
370 (when (memq new-buf orig-buffers)
371 (push-mark orig-point))
372 (funcall switch-fn new-buf)
373 (when new-point (goto-char new-point))
374 (recenter find-function-recenter-line)
375 (run-hooks 'find-function-after-hook))))
378 (defun find-function (function)
379 "Find the definition of the FUNCTION near point.
381 Finds the source file containing the definition of the function
382 near point (selected by `function-called-at-point') in a buffer and
383 places point before the definition.
384 Set mark before moving, if the buffer already existed.
386 The library where FUNCTION is defined is searched for in
387 `find-function-source-path', if non-nil, otherwise in `load-path'.
388 See also `find-function-recenter-line' and `find-function-after-hook'."
389 (interactive (find-function-read))
390 (find-function-do-it function nil 'switch-to-buffer))
393 (defun find-function-other-window (function)
394 "Find, in another window, the definition of FUNCTION near point.
396 See `find-function' for more details."
397 (interactive (find-function-read))
398 (find-function-do-it function nil 'switch-to-buffer-other-window))
401 (defun find-function-other-frame (function)
402 "Find, in another frame, the definition of FUNCTION near point.
404 See `find-function' for more details."
405 (interactive (find-function-read))
406 (find-function-do-it function nil 'switch-to-buffer-other-frame))
409 (defun find-variable-noselect (variable &optional file)
410 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
412 Finds the library containing the definition of VARIABLE in a buffer and
413 the point of the definition. The buffer is not selected.
414 If the variable's definition can't be found in the buffer, return (BUFFER).
416 The library where VARIABLE is defined is searched for in FILE or
417 `find-function-source-path', if non-nil, otherwise in `load-path'."
419 (error "You didn't specify a variable")
420 (let ((library (or file
421 (symbol-file variable 'defvar)
422 (help-C-file-name variable 'var))))
423 (find-function-search-for-symbol variable 'defvar library))))
426 (defun find-variable (variable)
427 "Find the definition of the VARIABLE at or before point.
429 Finds the library containing the definition of the variable
430 near point (selected by `variable-at-point') in a buffer and
431 places point before the definition.
433 Set mark before moving, if the buffer already existed.
435 The library where VARIABLE is defined is searched for in
436 `find-function-source-path', if non-nil, otherwise in `load-path'.
437 See also `find-function-recenter-line' and `find-function-after-hook'."
438 (interactive (find-function-read 'defvar))
439 (find-function-do-it variable 'defvar 'switch-to-buffer))
442 (defun find-variable-other-window (variable)
443 "Find, in another window, the definition of VARIABLE near point.
445 See `find-variable' for more details."
446 (interactive (find-function-read 'defvar))
447 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
450 (defun find-variable-other-frame (variable)
451 "Find, in another frame, the definition of VARIABLE near point.
453 See `find-variable' for more details."
454 (interactive (find-function-read 'defvar))
455 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
458 (defun find-definition-noselect (symbol type &optional file)
459 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
460 If the definition can't be found in the buffer, return (BUFFER).
461 TYPE says what type of definition: nil for a function, `defvar' for a
462 variable, `defface' for a face. This function does not switch to the
463 buffer nor display it.
465 The library where SYMBOL is defined is searched for in FILE or
466 `find-function-source-path', if non-nil, otherwise in `load-path'."
469 (error "You didn't specify a symbol"))
471 (find-function-noselect symbol))
473 (find-variable-noselect symbol file))
475 (let ((library (or file (symbol-file symbol type))))
476 (find-function-search-for-symbol symbol type library)))))
478 ;; For symmetry, this should be called find-face; but some programs
479 ;; assume that, if that name is defined, it means something else.
481 (defun find-face-definition (face)
482 "Find the definition of FACE. FACE defaults to the name near point.
484 Finds the Emacs Lisp library containing the definition of the face
485 near point (selected by `variable-at-point') in a buffer and
486 places point before the definition.
488 Set mark before moving, if the buffer already existed.
490 The library where FACE is defined is searched for in
491 `find-function-source-path', if non-nil, otherwise in `load-path'.
492 See also `find-function-recenter-line' and `find-function-after-hook'."
493 (interactive (find-function-read 'defface))
494 (find-function-do-it face 'defface 'switch-to-buffer))
497 (defun find-function-on-key (key)
498 "Find the function that KEY invokes. KEY is a string.
499 Set mark before moving, if the buffer already existed."
500 (interactive "kFind function on key: ")
503 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
504 (start (event-start event))
505 (modifiers (event-modifiers event))
506 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
507 (memq 'drag modifiers))
508 (posn-window start))))
509 ;; For a mouse button event, go to the button it applies to
510 ;; to get the right key bindings. And go to the right place
511 ;; in case the keymap depends on where you clicked.
512 (when (windowp window)
513 (set-buffer (window-buffer window))
514 (goto-char (posn-point start)))
515 (setq defn (key-binding key))))
516 (let ((key-desc (key-description key)))
517 (if (or (null defn) (integerp defn))
518 (message "%s is unbound" key-desc)
520 (message "%s runs %s" key-desc (prin1-to-string defn))
521 (find-function-other-window defn))))))
524 (defun find-function-at-point ()
525 "Find directly the function at point in the other window."
527 (let ((symb (function-called-at-point)))
529 (find-function-other-window symb))))
532 (defun find-variable-at-point ()
533 "Find directly the variable at point in the other window."
535 (let ((symb (variable-at-point)))
536 (when (and symb (not (equal symb 0)))
537 (find-variable-other-window symb))))
540 (defun find-function-setup-keys ()
541 "Define some key bindings for the find-function family of functions."
542 (define-key ctl-x-map "F" 'find-function)
543 (define-key ctl-x-4-map "F" 'find-function-other-window)
544 (define-key ctl-x-5-map "F" 'find-function-other-frame)
545 (define-key ctl-x-map "K" 'find-function-on-key)
546 (define-key ctl-x-map "V" 'find-variable)
547 (define-key ctl-x-4-map "V" 'find-variable-other-window)
548 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
552 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
553 ;;; find-func.el ends here