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* ((path (cons (or find-function-source-path load-path)
201 (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 def path 'test) def)))
218 (completing-read (if def (format "Library name (default %s): " def)
220 'locate-file-completion path nil nil nil def))))
221 (let ((buf (find-file-noselect (find-library-name library))))
222 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
225 (defun find-function-search-for-symbol (symbol type library)
226 "Search for SYMBOL's definition of type TYPE in LIBRARY.
227 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
228 or just (BUFFER . nil) if the definition can't be found in the file.
230 If TYPE is nil, look for a function definition.
231 Otherwise, TYPE specifies the kind of definition,
232 and it is interpreted via `find-function-regexp-alist'.
233 The search is done in the source for library LIBRARY."
235 (error "Don't know where `%s' is defined" symbol))
236 ;; Some functions are defined as part of the construct
237 ;; that defines something else.
238 (while (and (symbolp symbol) (get symbol 'definition-name))
239 (setq symbol (get symbol 'definition-name)))
240 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
241 (find-function-C-source symbol (match-string 1 library) type)
242 (when (string-match "\\.el\\(c\\)\\'" library)
243 (setq library (substring library 0 (match-beginning 1))))
244 ;; Strip extension from .emacs.el to make sure symbol is searched in
246 (when (string-match "\\.emacs\\(.el\\)" library)
247 (setq library (substring library 0 (match-beginning 1))))
248 (let* ((filename (find-library-name library))
249 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
250 (with-current-buffer (find-file-noselect filename)
251 (let ((regexp (format (symbol-value regexp-symbol)
252 ;; Entry for ` (backquote) macro in loaddefs.el,
253 ;; (defalias (quote \`)..., has a \ but
254 ;; (symbol-name symbol) doesn't. Add an
255 ;; optional \ to catch this.
257 (regexp-quote (symbol-name symbol)))))
259 (with-syntax-table emacs-lisp-mode-syntax-table
260 (goto-char (point-min))
261 (if (or (re-search-forward regexp nil t)
262 ;; `regexp' matches definitions using known forms like
263 ;; `defun', or `defvar'. But some functions/variables
264 ;; are defined using special macros (or functions), so
265 ;; if `regexp' can't find the definition, we look for
266 ;; something of the form "(SOMETHING <symbol> ...)".
267 ;; This fails to distinguish function definitions from
268 ;; variable declarations (or even uses thereof), but is
269 ;; a good pragmatic fallback.
271 (concat "^([^ ]+" find-function-space-re "['(]?"
272 (regexp-quote (symbol-name symbol))
277 (cons (current-buffer) (point)))
278 (cons (current-buffer) nil))))))))
281 (defun find-function-noselect (function)
282 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
284 Finds the source file containing the definition of FUNCTION
285 in a buffer and the point of the definition. The buffer is
286 not selected. If the function definition can't be found in
287 the buffer, returns (BUFFER).
289 If the file where FUNCTION is defined is not known, then it is
290 searched for in `find-function-source-path' if non-nil, otherwise
293 (error "You didn't specify a function"))
294 (let ((def (symbol-function function))
297 (or (eq def function)
299 (setq aliases (concat aliases
300 (format ", which is an alias for `%s'"
302 (setq aliases (format "`%s' an alias for `%s'"
303 function (symbol-name def)))))
304 (setq function (symbol-function function)
305 def (symbol-function function)))
307 (message "%s" aliases))
309 (cond ((eq (car-safe def) 'autoload)
312 (help-C-file-name def 'subr))
313 ((symbol-file function 'defun)))))
314 (find-function-search-for-symbol function nil library))))
316 (defun find-function-read (&optional type)
317 "Read and return an interned symbol, defaulting to the one near point.
319 If TYPE is nil, insist on a symbol with a function definition.
320 Otherwise TYPE should be `defvar' or `defface'.
321 If TYPE is nil, defaults using `function-called-at-point',
322 otherwise uses `variable-at-point'."
323 (let ((symb (if (null type)
324 (function-called-at-point)
325 (if (eq type 'defvar)
327 (variable-at-point t))))
328 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
329 (defface . facep)))))
330 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
331 (defface . "face")))))
332 (enable-recursive-minibuffers t)
336 (setq val (completing-read
340 (format " (default %s)" symb))
342 obarray predicate t nil))
343 (list (if (equal val "")
347 (defun find-function-do-it (symbol type switch-fn)
348 "Find Emacs Lisp SYMBOL in a buffer and display it.
349 TYPE is nil to search for a function definition,
350 or else `defvar' or `defface'.
352 The variable `find-function-recenter-line' controls how
353 to recenter the display. SWITCH-FN is the function to call
354 to display and select the buffer.
355 See also `find-function-after-hook'.
357 Set mark before moving, if the buffer already existed."
358 (let* ((orig-point (point))
359 (orig-buf (window-buffer))
360 (orig-buffers (buffer-list))
361 (buffer-point (save-excursion
362 (find-definition-noselect symbol type)))
363 (new-buf (car buffer-point))
364 (new-point (cdr buffer-point)))
366 (when (memq new-buf orig-buffers)
367 (push-mark orig-point))
368 (funcall switch-fn new-buf)
369 (when new-point (goto-char new-point))
370 (recenter find-function-recenter-line)
371 (run-hooks 'find-function-after-hook))))
374 (defun find-function (function)
375 "Find the definition of the FUNCTION near point.
377 Finds the source file containing the definition of the function
378 near point (selected by `function-called-at-point') in a buffer and
379 places point before the definition.
380 Set mark before moving, if the buffer already existed.
382 The library where FUNCTION is defined is searched for in
383 `find-function-source-path', if non-nil, otherwise in `load-path'.
384 See also `find-function-recenter-line' and `find-function-after-hook'."
385 (interactive (find-function-read))
386 (find-function-do-it function nil 'switch-to-buffer))
389 (defun find-function-other-window (function)
390 "Find, in another window, the definition of FUNCTION near point.
392 See `find-function' for more details."
393 (interactive (find-function-read))
394 (find-function-do-it function nil 'switch-to-buffer-other-window))
397 (defun find-function-other-frame (function)
398 "Find, in another frame, the definition of FUNCTION near point.
400 See `find-function' for more details."
401 (interactive (find-function-read))
402 (find-function-do-it function nil 'switch-to-buffer-other-frame))
405 (defun find-variable-noselect (variable &optional file)
406 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
408 Finds the library containing the definition of VARIABLE in a buffer and
409 the point of the definition. The buffer is not selected.
410 If the variable's definition can't be found in the buffer, return (BUFFER).
412 The library where VARIABLE is defined is searched for in FILE or
413 `find-function-source-path', if non-nil, otherwise in `load-path'."
415 (error "You didn't specify a variable")
416 (let ((library (or file
417 (symbol-file variable 'defvar)
418 (help-C-file-name variable 'var))))
419 (find-function-search-for-symbol variable 'defvar library))))
422 (defun find-variable (variable)
423 "Find the definition of the VARIABLE at or before point.
425 Finds the library containing the definition of the variable
426 near point (selected by `variable-at-point') in a buffer and
427 places point before the definition.
429 Set mark before moving, if the buffer already existed.
431 The library where VARIABLE is defined is searched for in
432 `find-function-source-path', if non-nil, otherwise in `load-path'.
433 See also `find-function-recenter-line' and `find-function-after-hook'."
434 (interactive (find-function-read 'defvar))
435 (find-function-do-it variable 'defvar 'switch-to-buffer))
438 (defun find-variable-other-window (variable)
439 "Find, in another window, the definition of VARIABLE near point.
441 See `find-variable' for more details."
442 (interactive (find-function-read 'defvar))
443 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
446 (defun find-variable-other-frame (variable)
447 "Find, in another frame, the definition of VARIABLE near point.
449 See `find-variable' for more details."
450 (interactive (find-function-read 'defvar))
451 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
454 (defun find-definition-noselect (symbol type &optional file)
455 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
456 If the definition can't be found in the buffer, return (BUFFER).
457 TYPE says what type of definition: nil for a function, `defvar' for a
458 variable, `defface' for a face. This function does not switch to the
459 buffer nor display it.
461 The library where SYMBOL is defined is searched for in FILE or
462 `find-function-source-path', if non-nil, otherwise in `load-path'."
465 (error "You didn't specify a symbol"))
467 (find-function-noselect symbol))
469 (find-variable-noselect symbol file))
471 (let ((library (or file (symbol-file symbol type))))
472 (find-function-search-for-symbol symbol type library)))))
474 ;; For symmetry, this should be called find-face; but some programs
475 ;; assume that, if that name is defined, it means something else.
477 (defun find-face-definition (face)
478 "Find the definition of FACE. FACE defaults to the name near point.
480 Finds the Emacs Lisp library containing the definition of the face
481 near point (selected by `variable-at-point') in a buffer and
482 places point before the definition.
484 Set mark before moving, if the buffer already existed.
486 The library where FACE is defined is searched for in
487 `find-function-source-path', if non-nil, otherwise in `load-path'.
488 See also `find-function-recenter-line' and `find-function-after-hook'."
489 (interactive (find-function-read 'defface))
490 (find-function-do-it face 'defface 'switch-to-buffer))
493 (defun find-function-on-key (key)
494 "Find the function that KEY invokes. KEY is a string.
495 Set mark before moving, if the buffer already existed."
496 (interactive "kFind function on key: ")
499 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
500 (start (event-start event))
501 (modifiers (event-modifiers event))
502 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
503 (memq 'drag modifiers))
504 (posn-window start))))
505 ;; For a mouse button event, go to the button it applies to
506 ;; to get the right key bindings. And go to the right place
507 ;; in case the keymap depends on where you clicked.
508 (when (windowp window)
509 (set-buffer (window-buffer window))
510 (goto-char (posn-point start)))
511 (setq defn (key-binding key))))
512 (let ((key-desc (key-description key)))
513 (if (or (null defn) (integerp defn))
514 (message "%s is unbound" key-desc)
516 (message "%s runs %s" key-desc (prin1-to-string defn))
517 (find-function-other-window defn))))))
520 (defun find-function-at-point ()
521 "Find directly the function at point in the other window."
523 (let ((symb (function-called-at-point)))
525 (find-function-other-window symb))))
528 (defun find-variable-at-point ()
529 "Find directly the variable at point in the other window."
531 (let ((symb (variable-at-point)))
532 (when (and symb (not (equal symb 0)))
533 (find-variable-other-window symb))))
536 (defun find-function-setup-keys ()
537 "Define some key bindings for the find-function family of functions."
538 (define-key ctl-x-map "F" 'find-function)
539 (define-key ctl-x-4-map "F" 'find-function-other-window)
540 (define-key ctl-x-5-map "F" 'find-function-other-frame)
541 (define-key ctl-x-map "K" 'find-function-on-key)
542 (define-key ctl-x-map "V" 'find-variable)
543 (define-key ctl-x-4-map "V" 'find-variable-other-window)
544 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
548 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
549 ;;; find-func.el ends here