*** empty log message ***
[emacs.git] / lisp / emacs-lisp / find-func.el
blobb3c7c339030daff2647eae56d6803fc273bc0a38
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 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
9 ;; Created: 97/07/25
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)
16 ;; 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; 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.
28 ;;; Commentary:
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!!
32 ;; ;;; find-func
33 ;; (find-function-setup-keys)
35 ;; or just:
37 ;; (load "find-func")
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
45 ;; "fff.el").
47 ;;; Code:
49 ;;; User variables:
51 (defgroup find-function nil
52 "Finds the definition of the Emacs Lisp symbol near point."
53 ;; :prefix "find-function"
54 :group 'lisp)
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)
62 (concat
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."
75 :type 'regexp
76 :group 'find-function
77 :version "21.1")
79 (defcustom find-variable-regexp
80 (concat
81 "^\\s-*(\\(def[^fumag]\\(\\w\\|\\s_\\)+\\*?\\|\
82 easy-mmode-def\\(map\\|syntax\\)\\|easy-menu-define\\)"
83 find-function-space-re
84 "%s\\(\\s-\\|$\\)")
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."
91 :type 'regexp
92 :group 'find-function
93 :version "21.1")
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."
102 :type 'regexp
103 :group 'find-function
104 :version "22.1")
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
119 default."
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)
128 integer)
129 :group 'find-function
130 :version "20.3")
132 (defcustom find-function-after-hook nil
133 "Hook run after finding symbol definition.
135 See the functions `find-function' and `find-variable'."
136 :group 'find-function
137 :version "20.3")
139 ;;; Functions:
141 (defun find-library-suffixes ()
142 (let ((suffixes nil))
143 (dolist (suffix (get-load-suffixes) (nreverse suffixes))
144 (unless (string-match "elc" suffix) (push suffix suffixes)))))
146 (defun find-library-name (library)
147 "Return the absolute file name of the Lisp source of LIBRARY."
148 ;; If the library is byte-compiled, try to find a source library by
149 ;; the same name.
150 (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library)
151 (setq library (replace-match "" t t library)))
152 (or (locate-file library
153 (or find-function-source-path load-path)
154 (append (find-library-suffixes) load-file-rep-suffixes))
155 (error "Can't find library %s" library)))
157 (defvar find-function-C-source-directory
158 (let ((dir (expand-file-name "src" source-directory)))
159 (when (and (file-directory-p dir) (file-readable-p dir))
160 dir))
161 "Directory where the C source files of Emacs can be found.
162 If nil, do not try to find the source code of functions and variables
163 defined in C.")
165 (defun find-function-C-source (fun-or-var file type)
166 "Find the source location where FUN-OR-VAR is defined in FILE.
167 TYPE should be nil to find a function, or `defvar' to find a variable."
168 (unless find-function-C-source-directory
169 (setq find-function-C-source-directory
170 (read-directory-name "Emacs C source dir: " nil nil t)))
171 (setq file (expand-file-name file find-function-C-source-directory))
172 (unless (file-readable-p file)
173 (error "The C source file %s is not available"
174 (file-name-nondirectory file)))
175 (unless type
176 (setq fun-or-var (indirect-function fun-or-var)))
177 (with-current-buffer (find-file-noselect file)
178 (goto-char (point-min))
179 (unless (re-search-forward
180 (if type
181 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
182 (regexp-quote (symbol-name fun-or-var))
183 "\"")
184 (concat "DEFUN[ \t\n]*([ \t\n]*\""
185 (regexp-quote (subr-name fun-or-var))
186 "\""))
187 nil t)
188 (error "Can't find source for %s" fun-or-var))
189 (cons (current-buffer) (match-beginning 0))))
191 ;;;###autoload
192 (defun find-library (library)
193 "Find the elisp source of LIBRARY."
194 (interactive
195 (let* ((path (cons (or find-function-source-path load-path)
196 (find-library-suffixes)))
197 (def (if (eq (function-called-at-point) 'require)
198 (save-excursion
199 (backward-up-list)
200 (forward-char)
201 (backward-sexp -2)
202 (thing-at-point 'symbol))
203 (thing-at-point 'symbol))))
204 (when def
205 (setq def (and (locate-file-completion def path 'test) def)))
206 (list
207 (completing-read (if def (format "Library name (default %s): " def)
208 "Library name: ")
209 'locate-file-completion path nil nil nil def))))
210 (let ((buf (find-file-noselect (find-library-name library))))
211 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
213 ;;;###autoload
214 (defun find-function-search-for-symbol (symbol type library)
215 "Search for SYMBOL's definition of type TYPE in LIBRARY.
216 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
217 or just (BUFFER . nil) if the definition can't be found in the file.
219 If TYPE is nil, look for a function definition.
220 Otherwise, TYPE specifies the kind of definition,
221 and it is interpreted via `find-function-regexp-alist'.
222 The search is done in the source for library LIBRARY."
223 (if (null library)
224 (error "Don't know where `%s' is defined" symbol))
225 ;; Some functions are defined as part of the construct
226 ;; that defines something else.
227 (while (and (symbolp symbol) (get symbol 'definition-name))
228 (setq symbol (get symbol 'definition-name)))
229 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
230 (find-function-C-source symbol (match-string 1 library) type)
231 (if (string-match "\\.el\\(c\\)\\'" library)
232 (setq library (substring library 0 (match-beginning 1))))
233 (let* ((filename (find-library-name library))
234 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
235 (with-current-buffer (find-file-noselect filename)
236 (let ((regexp (format (symbol-value regexp-symbol)
237 ;; Entry for ` (backquote) macro in loaddefs.el,
238 ;; (defalias (quote \`)..., has a \ but
239 ;; (symbol-name symbol) doesn't. Add an
240 ;; optional \ to catch this.
241 (concat "\\\\?"
242 (regexp-quote (symbol-name symbol)))))
243 (case-fold-search))
244 (with-syntax-table emacs-lisp-mode-syntax-table
245 (goto-char (point-min))
246 (if (or (re-search-forward regexp nil t)
247 ;; `regexp' matches definitions using known forms like
248 ;; `defun', or `defvar'. But some functions/variables
249 ;; are defined using special macros (or functions), so
250 ;; if `regexp' can't find the definition, we look for
251 ;; something of the form "(SOMETHING <symbol> ...)".
252 ;; This fails to distinguish function definitions from
253 ;; variable declarations (or even uses thereof), but is
254 ;; a good pragmatic fallback.
255 (re-search-forward
256 (concat "^([^ ]+" find-function-space-re "['(]?"
257 (regexp-quote (symbol-name symbol))
258 "\\_>")
259 nil t))
260 (progn
261 (beginning-of-line)
262 (cons (current-buffer) (point)))
263 (cons (current-buffer) nil))))))))
265 ;;;###autoload
266 (defun find-function-noselect (function)
267 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
269 Finds the source file containing the definition of FUNCTION
270 in a buffer and the point of the definition. The buffer is
271 not selected. If the function definition can't be found in
272 the buffer, returns (BUFFER).
274 If the file where FUNCTION is defined is not known, then it is
275 searched for in `find-function-source-path' if non-nil, otherwise
276 in `load-path'."
277 (if (not function)
278 (error "You didn't specify a function"))
279 (let ((def (symbol-function function))
280 aliases)
281 (while (symbolp def)
282 (or (eq def function)
283 (if aliases
284 (setq aliases (concat aliases
285 (format ", which is an alias for `%s'"
286 (symbol-name def))))
287 (setq aliases (format "`%s' an alias for `%s'"
288 function (symbol-name def)))))
289 (setq function (symbol-function function)
290 def (symbol-function function)))
291 (if aliases
292 (message "%s" aliases))
293 (let ((library
294 (cond ((eq (car-safe def) 'autoload)
295 (nth 1 def))
296 ((subrp def)
297 (help-C-file-name def 'subr))
298 ((symbol-file function 'defun)))))
299 (find-function-search-for-symbol function nil library))))
301 (defun find-function-read (&optional type)
302 "Read and return an interned symbol, defaulting to the one near point.
304 If TYPE is nil, insist on a symbol with a function definition.
305 Otherwise TYPE should be `defvar' or `defface'.
306 If TYPE is nil, defaults using `function-called-at-point',
307 otherwise uses `variable-at-point'."
308 (let ((symb (if (null type)
309 (function-called-at-point)
310 (if (eq type 'defvar)
311 (variable-at-point)
312 (variable-at-point t))))
313 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
314 (defface . facep)))))
315 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
316 (defface . "face")))))
317 (enable-recursive-minibuffers t)
318 val)
319 (if (equal symb 0)
320 (setq symb nil))
321 (setq val (completing-read
322 (concat "Find "
323 prompt
324 (if symb
325 (format " (default %s)" symb))
326 ": ")
327 obarray predicate t nil))
328 (list (if (equal val "")
329 symb
330 (intern val)))))
332 (defun find-function-do-it (symbol type switch-fn)
333 "Find Emacs Lisp SYMBOL in a buffer and display it.
334 TYPE is nil to search for a function definition,
335 or else `defvar' or `defface'.
337 The variable `find-function-recenter-line' controls how
338 to recenter the display. SWITCH-FN is the function to call
339 to display and select the buffer.
340 See also `find-function-after-hook'.
342 Set mark before moving, if the buffer already existed."
343 (let* ((orig-point (point))
344 (orig-buf (window-buffer))
345 (orig-buffers (buffer-list))
346 (buffer-point (save-excursion
347 (find-definition-noselect symbol type)))
348 (new-buf (car buffer-point))
349 (new-point (cdr buffer-point)))
350 (when buffer-point
351 (when (memq new-buf orig-buffers)
352 (push-mark orig-point))
353 (funcall switch-fn new-buf)
354 (when new-point (goto-char new-point))
355 (recenter find-function-recenter-line)
356 (run-hooks 'find-function-after-hook))))
358 ;;;###autoload
359 (defun find-function (function)
360 "Find the definition of the FUNCTION near point.
362 Finds the source file containing the definition of the function
363 near point (selected by `function-called-at-point') in a buffer and
364 places point before the definition.
365 Set mark before moving, if the buffer already existed.
367 The library where FUNCTION is defined is searched for in
368 `find-function-source-path', if non-nil, otherwise in `load-path'.
369 See also `find-function-recenter-line' and `find-function-after-hook'."
370 (interactive (find-function-read))
371 (find-function-do-it function nil 'switch-to-buffer))
373 ;;;###autoload
374 (defun find-function-other-window (function)
375 "Find, in another window, the definition of FUNCTION near point.
377 See `find-function' for more details."
378 (interactive (find-function-read))
379 (find-function-do-it function nil 'switch-to-buffer-other-window))
381 ;;;###autoload
382 (defun find-function-other-frame (function)
383 "Find, in another frame, the definition of FUNCTION near point.
385 See `find-function' for more details."
386 (interactive (find-function-read))
387 (find-function-do-it function nil 'switch-to-buffer-other-frame))
389 ;;;###autoload
390 (defun find-variable-noselect (variable &optional file)
391 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
393 Finds the library containing the definition of VARIABLE in a buffer and
394 the point of the definition. The buffer is not selected.
395 If the variable's definition can't be found in the buffer, return (BUFFER).
397 The library where VARIABLE is defined is searched for in FILE or
398 `find-function-source-path', if non-nil, otherwise in `load-path'."
399 (if (not variable)
400 (error "You didn't specify a variable")
401 (let ((library (or file
402 (symbol-file variable 'defvar)
403 (help-C-file-name variable 'var))))
404 (find-function-search-for-symbol variable 'defvar library))))
406 ;;;###autoload
407 (defun find-variable (variable)
408 "Find the definition of the VARIABLE at or before point.
410 Finds the library containing the definition of the variable
411 near point (selected by `variable-at-point') in a buffer and
412 places point before the definition.
414 Set mark before moving, if the buffer already existed.
416 The library where VARIABLE is defined is searched for in
417 `find-function-source-path', if non-nil, otherwise in `load-path'.
418 See also `find-function-recenter-line' and `find-function-after-hook'."
419 (interactive (find-function-read 'defvar))
420 (find-function-do-it variable 'defvar 'switch-to-buffer))
422 ;;;###autoload
423 (defun find-variable-other-window (variable)
424 "Find, in another window, the definition of VARIABLE near point.
426 See `find-variable' for more details."
427 (interactive (find-function-read 'defvar))
428 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
430 ;;;###autoload
431 (defun find-variable-other-frame (variable)
432 "Find, in another frame, the definition of VARIABLE near point.
434 See `find-variable' for more details."
435 (interactive (find-function-read 'defvar))
436 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
438 ;;;###autoload
439 (defun find-definition-noselect (symbol type &optional file)
440 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
441 If the definition can't be found in the buffer, return (BUFFER).
442 TYPE says what type of definition: nil for a function, `defvar' for a
443 variable, `defface' for a face. This function does not switch to the
444 buffer nor display it.
446 The library where SYMBOL is defined is searched for in FILE or
447 `find-function-source-path', if non-nil, otherwise in `load-path'."
448 (cond
449 ((not symbol)
450 (error "You didn't specify a symbol"))
451 ((null type)
452 (find-function-noselect symbol))
453 ((eq type 'defvar)
454 (find-variable-noselect symbol file))
456 (let ((library (or file (symbol-file symbol type))))
457 (find-function-search-for-symbol symbol type library)))))
459 ;; For symmetry, this should be called find-face; but some programs
460 ;; assume that, if that name is defined, it means something else.
461 ;;;###autoload
462 (defun find-face-definition (face)
463 "Find the definition of FACE. FACE defaults to the name near point.
465 Finds the Emacs Lisp library containing the definition of the face
466 near point (selected by `variable-at-point') in a buffer and
467 places point before the definition.
469 Set mark before moving, if the buffer already existed.
471 The library where FACE is defined is searched for in
472 `find-function-source-path', if non-nil, otherwise in `load-path'.
473 See also `find-function-recenter-line' and `find-function-after-hook'."
474 (interactive (find-function-read 'defface))
475 (find-function-do-it face 'defface 'switch-to-buffer))
477 ;;;###autoload
478 (defun find-function-on-key (key)
479 "Find the function that KEY invokes. KEY is a string.
480 Set mark before moving, if the buffer already existed."
481 (interactive "kFind function on key: ")
482 (let (defn)
483 (save-excursion
484 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
485 (start (event-start event))
486 (modifiers (event-modifiers event))
487 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
488 (memq 'drag modifiers))
489 (posn-window start))))
490 ;; For a mouse button event, go to the button it applies to
491 ;; to get the right key bindings. And go to the right place
492 ;; in case the keymap depends on where you clicked.
493 (when (windowp window)
494 (set-buffer (window-buffer window))
495 (goto-char (posn-point start)))
496 (setq defn (key-binding key))))
497 (let ((key-desc (key-description key)))
498 (if (or (null defn) (integerp defn))
499 (message "%s is unbound" key-desc)
500 (if (consp defn)
501 (message "%s runs %s" key-desc (prin1-to-string defn))
502 (find-function-other-window defn))))))
504 ;;;###autoload
505 (defun find-function-at-point ()
506 "Find directly the function at point in the other window."
507 (interactive)
508 (let ((symb (function-called-at-point)))
509 (when symb
510 (find-function-other-window symb))))
512 ;;;###autoload
513 (defun find-variable-at-point ()
514 "Find directly the variable at point in the other window."
515 (interactive)
516 (let ((symb (variable-at-point)))
517 (when (and symb (not (equal symb 0)))
518 (find-variable-other-window symb))))
520 ;;;###autoload
521 (defun find-function-setup-keys ()
522 "Define some key bindings for the find-function family of functions."
523 (define-key ctl-x-map "F" 'find-function)
524 (define-key ctl-x-4-map "F" 'find-function-other-window)
525 (define-key ctl-x-5-map "F" 'find-function-other-frame)
526 (define-key ctl-x-map "K" 'find-function-on-key)
527 (define-key ctl-x-map "V" 'find-variable)
528 (define-key ctl-x-4-map "V" 'find-variable-other-window)
529 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
531 (provide 'find-func)
533 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
534 ;;; find-func.el ends here