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 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 2, 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
53 (defgroup find-function nil
54 "Finds the definition of the Emacs Lisp symbol near point."
55 ;; :prefix "find-function"
58 (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+")
60 (defcustom find-function-regexp
61 ;; Match things like (defun foo ...), (defmacro foo ...),
62 ;; (define-skeleton foo ...), (define-generic-mode 'foo ...),
63 ;; (define-derived-mode foo ...), (define-minor-mode foo)
65 "^\\s-*(\\(def\\(ine-skeleton\\|ine-generic-mode\\|ine-derived-mode\\|\
66 ine-minor-mode\\|ine-compilation-mode\\|un-cvs-mode\\|foo\\|[^cfgv]\\w+\\*?\\)\
67 \\|easy-mmode-define-global-mode\\|menu-bar-make-toggle\\)"
68 find-function-space-re
69 "\\('\\|\(quote \\)?%s\\(\\s-\\|$\\|\(\\|\)\\)")
70 "The regexp used by `find-function' to search for a function definition.
71 Note it must contain a `%s' at the place where `format'
72 should insert the function name. The default value avoids `defconst',
73 `defgroup', `defvar', `defface'.
75 Please send improvements and fixes to the maintainer."
80 (defcustom find-variable-regexp
81 (concat"^\\s-*(def[^fumag]\\(\\w\\|\\s_\\)+\\*?" find-function-space-re "%s\\(\\s-\\|$\\)")
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 (put 'find-function-regexp-alist 'risky-local-variable t)
112 (defcustom find-function-source-path nil
113 "The default list of directories where `find-function' searches.
115 If this variable is nil then `find-function' searches `load-path' by
117 :type '(repeat directory)
118 :group 'find-function)
120 (defcustom find-function-recenter-line 1
121 "The window line-number from which to start displaying a symbol definition.
122 A value of nil implies center the beginning of the definition.
123 See `find-function' and `find-variable'."
124 :type '(choice (const :tag "Center" nil)
126 :group 'find-function
129 (defcustom find-function-after-hook nil
130 "Hook run after finding symbol definition.
132 See the functions `find-function' and `find-variable'."
133 :group 'find-function
138 (defun find-library-suffixes ()
139 (let ((suffixes nil))
140 (dolist (suffix load-suffixes (nreverse suffixes))
141 (unless (string-match "elc" suffix) (push suffix suffixes)))))
143 (defun find-library-name (library)
144 "Return the absolute file name of the Lisp source of LIBRARY."
145 ;; If the library is byte-compiled, try to find a source library by
147 (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library)
148 (setq library (replace-match "" t t library)))
149 (or (locate-file library
150 (or find-function-source-path load-path)
151 (append (find-library-suffixes) '("")))
152 (error "Can't find library %s" library)))
154 (defvar find-function-C-source-directory
155 (let ((dir (expand-file-name "src" source-directory)))
156 (when (and (file-directory-p dir) (file-readable-p dir))
158 "Directory where the C source files of Emacs can be found.
159 If nil, do not try to find the source code of functions and variables
162 (defun find-function-C-source (fun-or-var file type)
163 "Find the source location where SUBR-OR-VAR is defined in FILE.
164 TYPE should be nil to find a function, or `defvar' to find a variable."
165 (unless find-function-C-source-directory
166 (setq find-function-C-source-directory
167 (read-directory-name "Emacs C source dir: " nil nil t)))
168 (setq file (expand-file-name file find-function-C-source-directory))
169 (unless (file-readable-p file)
170 (error "The C source file %s is not available"
171 (file-name-nondirectory file)))
173 (setq fun-or-var (indirect-function fun-or-var)))
174 (with-current-buffer (find-file-noselect file)
175 (goto-char (point-min))
176 (unless (re-search-forward
178 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
179 (regexp-quote (symbol-name fun-or-var))
181 (concat "DEFUN[ \t\n]*([ \t\n]*\""
182 (regexp-quote (subr-name fun-or-var))
185 (error "Can't find source for %s" fun-or-var))
186 (cons (current-buffer) (match-beginning 0))))
189 (defun find-library (library)
190 "Find the elisp source of LIBRARY."
193 (completing-read "Library name: "
194 'locate-file-completion
195 (cons (or find-function-source-path load-path)
196 (find-library-suffixes)))))
197 (let ((buf (find-file-noselect (find-library-name library))))
198 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
201 (defun find-function-search-for-symbol (symbol type library)
202 "Search for SYMBOL's definition of type TYPE in LIBRARY.
203 If TYPE is nil, look for a function definition.
204 Otherwise, TYPE specifies the kind of definition,
205 and it is interpreted via `find-function-regexp-alist'.
206 The search is done in the source for library LIBRARY."
208 (error "Don't know where `%s' is defined" symbol))
209 ;; Some functions are defined as part of the construct
210 ;; that defines something else.
211 (while (and (symbolp symbol) (get symbol 'definition-name))
212 (setq symbol (get symbol 'definition-name)))
213 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
214 (find-function-C-source symbol (match-string 1 library) type)
215 (if (string-match "\\.el\\(c\\)\\'" library)
216 (setq library (substring library 0 (match-beginning 1))))
217 (let* ((filename (find-library-name library))
218 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
219 (with-current-buffer (find-file-noselect filename)
220 (let ((regexp (format (symbol-value regexp-symbol)
221 (regexp-quote (symbol-name symbol))))
223 (with-syntax-table emacs-lisp-mode-syntax-table
224 (goto-char (point-min))
225 (if (or (re-search-forward regexp nil t)
227 (concat "^([^ ]+" find-function-space-re "['(]"
228 (regexp-quote (symbol-name symbol))
233 (cons (current-buffer) (point)))
234 (error "Cannot find definition of `%s' in library `%s'"
235 symbol library))))))))
238 (defun find-function-noselect (function)
239 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
241 Finds the Emacs Lisp library containing the definition of FUNCTION
242 in a buffer and the point of the definition. The buffer is
245 If the file where FUNCTION is defined is not known, then it is
246 searched for in `find-function-source-path' if non nil, otherwise
249 (error "You didn't specify a function"))
250 (let ((def (symbol-function function))
253 (or (eq def function)
255 (setq aliases (concat aliases
256 (format ", which is an alias for `%s'"
258 (setq aliases (format "`%s' an alias for `%s'"
259 function (symbol-name def)))))
260 (setq function (symbol-function function)
261 def (symbol-function function)))
263 (message "%s" aliases))
265 (cond ((eq (car-safe def) 'autoload)
268 (help-C-file-name def 'subr))
269 ((symbol-file function 'defun)))))
270 (find-function-search-for-symbol function nil library))))
272 (defun find-function-read (&optional type)
273 "Read and return an interned symbol, defaulting to the one near point.
275 If TYPE is nil, insist on a symbol with a function definition.
276 Otherwise TYPE should be `defvar' or `defface'.
277 If TYPE is nil, defaults using `function-called-at-point',
278 otherwise uses `variable-at-point'."
279 (let ((symb (if (null type)
280 (function-called-at-point)
281 (if (eq type 'defvar)
283 (variable-at-point t))))
284 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
285 (defface . facep)))))
286 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
287 (defface . "face")))))
288 (enable-recursive-minibuffers t)
292 (setq val (completing-read
296 (format " (default %s)" symb))
298 obarray predicate t nil))
299 (list (if (equal val "")
303 (defun find-function-do-it (symbol type switch-fn)
304 "Find Emacs Lisp SYMBOL in a buffer and display it.
305 TYPE is nil to search for a function definition,
306 or else `defvar' or `defface'.
308 The variable `find-function-recenter-line' controls how
309 to recenter the display. SWITCH-FN is the function to call
310 to display and select the buffer.
311 See also `find-function-after-hook'.
313 Set mark before moving, if the buffer already existed."
314 (let* ((orig-point (point))
315 (orig-buf (window-buffer))
316 (orig-buffers (buffer-list))
317 (buffer-point (save-excursion
318 (find-definition-noselect symbol type)))
319 (new-buf (car buffer-point))
320 (new-point (cdr buffer-point)))
322 (when (memq new-buf orig-buffers)
323 (push-mark orig-point))
324 (funcall switch-fn new-buf)
325 (goto-char new-point)
326 (recenter find-function-recenter-line)
327 (run-hooks 'find-function-after-hook))))
330 (defun find-function (function)
331 "Find the definition of the FUNCTION near point.
333 Finds the Emacs Lisp library containing the definition of the function
334 near point (selected by `function-called-at-point') in a buffer and
335 places point before the definition.
336 Set mark before moving, if the buffer already existed.
338 The library where FUNCTION is defined is searched for in
339 `find-function-source-path', if non nil, otherwise in `load-path'.
340 See also `find-function-recenter-line' and `find-function-after-hook'."
341 (interactive (find-function-read))
342 (find-function-do-it function nil 'switch-to-buffer))
345 (defun find-function-other-window (function)
346 "Find, in another window, the definition of FUNCTION near point.
348 See `find-function' for more details."
349 (interactive (find-function-read))
350 (find-function-do-it function nil 'switch-to-buffer-other-window))
353 (defun find-function-other-frame (function)
354 "Find, in ananother frame, the definition of FUNCTION near point.
356 See `find-function' for more details."
357 (interactive (find-function-read))
358 (find-function-do-it function nil 'switch-to-buffer-other-frame))
361 (defun find-variable-noselect (variable &optional file)
362 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
364 Finds the Emacs Lisp library containing the definition of SYMBOL
365 in a buffer, and the point of the definition. It does not switch
366 to the buffer or display it.
368 The library where VARIABLE is defined is searched for in FILE or
369 `find-function-source-path', if non nil, otherwise in `load-path'."
371 (error "You didn't specify a variable"))
372 (let ((library (or file (symbol-file variable 'defvar))))
373 (find-function-search-for-symbol variable 'defvar library)))
376 (defun find-variable (variable)
377 "Find the definition of the VARIABLE near point.
379 Finds the Emacs Lisp library containing the definition of the variable
380 near point (selected by `variable-at-point') in a buffer and
381 places point before the definition.
383 Set mark before moving, if the buffer already existed.
385 The library where VARIABLE is defined is searched for in
386 `find-function-source-path', if non nil, otherwise in `load-path'.
387 See also `find-function-recenter-line' and `find-function-after-hook'."
388 (interactive (find-function-read 'defvar))
389 (find-function-do-it variable 'defvar 'switch-to-buffer))
392 (defun find-variable-other-window (variable)
393 "Find, in another window, the definition of VARIABLE near point.
395 See `find-variable' for more details."
396 (interactive (find-function-read 'defvar))
397 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
400 (defun find-variable-other-frame (variable)
401 "Find, in annother frame, the definition of VARIABLE near point.
403 See `find-variable' for more details."
404 (interactive (find-function-read 'defvar))
405 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
408 (defun find-definition-noselect (symbol type &optional file)
409 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
410 TYPE says what type of definition: nil for a function,
411 `defvar' or `defface' for a variable or face. This functoin
412 does not switch to the buffer or display it.
414 The library where SYMBOL is defined is searched for in FILE or
415 `find-function-source-path', if non nil, otherwise in `load-path'."
417 (error "You didn't specify a symbol"))
419 (find-function-noselect symbol)
420 (let ((library (or file (symbol-file symbol type))))
421 (find-function-search-for-symbol symbol type library))))
423 ;; For symmetry, this should be called find-face; but some programs
424 ;; assume that, if that name is defined, it means something else.
426 (defun find-face-definition (face)
427 "Find the definition of FACE. FACE defaults to the name near point.
429 Finds the Emacs Lisp library containing the definition of the face
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 FACE 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 'defface))
439 (find-function-do-it face 'defface 'switch-to-buffer))
442 (defun find-function-on-key (key)
443 "Find the function that KEY invokes. KEY is a string.
444 Set mark before moving, if the buffer already existed."
445 (interactive "kFind function on key: ")
448 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
449 (start (event-start event))
450 (modifiers (event-modifiers event))
451 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
452 (memq 'drag modifiers))
453 (posn-window start))))
454 ;; For a mouse button event, go to the button it applies to
455 ;; to get the right key bindings. And go to the right place
456 ;; in case the keymap depends on where you clicked.
457 (when (windowp window)
458 (set-buffer (window-buffer window))
459 (goto-char (posn-point start)))
460 (setq defn (key-binding key))))
461 (let ((key-desc (key-description key)))
462 (if (or (null defn) (integerp defn))
463 (message "%s is unbound" key-desc)
465 (message "%s runs %s" key-desc (prin1-to-string defn))
466 (find-function-other-window defn))))))
469 (defun find-function-at-point ()
470 "Find directly the function at point in the other window."
472 (let ((symb (function-called-at-point)))
474 (find-function-other-window symb))))
477 (defun find-variable-at-point ()
478 "Find directly the function at point in the other window."
480 (let ((symb (variable-at-point)))
481 (when (and symb (not (equal symb 0)))
482 (find-variable-other-window symb))))
485 (defun find-function-setup-keys ()
486 "Define some key bindings for the find-function family of functions."
487 (define-key ctl-x-map "F" 'find-function)
488 (define-key ctl-x-4-map "F" 'find-function-other-window)
489 (define-key ctl-x-5-map "F" 'find-function-other-frame)
490 (define-key ctl-x-map "K" 'find-function-on-key)
491 (define-key ctl-x-map "V" 'find-variable)
492 (define-key ctl-x-4-map "V" 'find-variable-other-window)
493 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
497 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
498 ;;; find-func.el ends here