(FORWARD_SIGNAL_TO_MAIN_THREAD): New define.
[emacs.git] / lisp / emacs-lisp / find-func.el
blob2022d3ab7d41941dc9cc12a3ab2a242f4dfd2207
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
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 (require 'loadhist)
51 ;;; User variables:
53 (defgroup find-function nil
54 "Finds the definition of the Emacs Lisp symbol near point."
55 ;; :prefix "find-function"
56 :group 'lisp)
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)
64 (concat
65 "^\\s-*(\\(def\\(ine-skeleton\\|ine-generic-mode\\|ine-derived-mode\\|\
66 ine\\(?:-global\\)?-minor-mode\\|ine-compilation-mode\\|un-cvs-mode\\|\
67 foo\\|[^icfgv]\\(\\w\\|\\s_\\)+\\*?\\)\\|easy-mmode-define-[a-z-]+\\|easy-menu-define\\|\
68 menu-bar-make-toggle\\)"
69 find-function-space-re
70 "\\('\\|\(quote \\)?%s\\(\\s-\\|$\\|\(\\|\)\\)")
71 "The regexp used by `find-function' to search for a function definition.
72 Note it must contain a `%s' at the place where `format'
73 should insert the function name. The default value avoids `defconst',
74 `defgroup', `defvar', `defface'.
76 Please send improvements and fixes to the maintainer."
77 :type 'regexp
78 :group 'find-function
79 :version "21.1")
81 (defcustom find-variable-regexp
82 (concat
83 "^\\s-*(\\(def[^fumag]\\(\\w\\|\\s_\\)+\\*?\\|\
84 easy-mmode-def\\(map\\|syntax\\)\\|easy-menu-define\\)"
85 find-function-space-re
86 "%s\\(\\s-\\|$\\)")
87 "The regexp used by `find-variable' to search for a variable definition.
88 Note it must contain a `%s' at the place where `format'
89 should insert the variable name. The default value
90 avoids `defun', `defmacro', `defalias', `defadvice', `defgroup', `defface'.
92 Please send improvements and fixes to the maintainer."
93 :type 'regexp
94 :group 'find-function
95 :version "21.1")
97 (defcustom find-face-regexp
98 (concat"^\\s-*(defface" find-function-space-re "%s\\(\\s-\\|$\\)")
99 "The regexp used by `find-face' to search for a face definition.
100 Note it must contain a `%s' at the place where `format'
101 should insert the face name.
103 Please send improvements and fixes to the maintainer."
104 :type 'regexp
105 :group 'find-function
106 :version "22.1")
108 (defvar find-function-regexp-alist
109 '((nil . find-function-regexp)
110 (defvar . find-variable-regexp)
111 (defface . find-face-regexp))
112 "Alist mapping definition types into regexp variables.
113 Each regexp variable's value should actually be a format string
114 to be used to substitute the desired symbol name into the regexp.")
115 (put 'find-function-regexp-alist 'risky-local-variable t)
117 (defcustom find-function-source-path nil
118 "The default list of directories where `find-function' searches.
120 If this variable is nil then `find-function' searches `load-path' by
121 default."
122 :type '(repeat directory)
123 :group 'find-function)
125 (defcustom find-function-recenter-line 1
126 "The window line-number from which to start displaying a symbol definition.
127 A value of nil implies center the beginning of the definition.
128 See `find-function' and `find-variable'."
129 :type '(choice (const :tag "Center" nil)
130 integer)
131 :group 'find-function
132 :version "20.3")
134 (defcustom find-function-after-hook nil
135 "Hook run after finding symbol definition.
137 See the functions `find-function' and `find-variable'."
138 :type 'hook
139 :group 'find-function
140 :version "20.3")
142 ;;; Functions:
144 (defun find-library-suffixes ()
145 (let ((suffixes nil))
146 (dolist (suffix (get-load-suffixes) (nreverse suffixes))
147 (unless (string-match "elc" suffix) (push suffix suffixes)))))
149 (defun find-library-name (library)
150 "Return the absolute file name of the Lisp source of LIBRARY."
151 ;; If the library is byte-compiled, try to find a source library by
152 ;; the same name.
153 (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library)
154 (setq library (replace-match "" t t library)))
155 (or (locate-file library
156 (or find-function-source-path load-path)
157 (append (find-library-suffixes) load-file-rep-suffixes))
158 (error "Can't find library %s" library)))
160 (defvar find-function-C-source-directory
161 (let ((dir (expand-file-name "src" source-directory)))
162 (when (and (file-directory-p dir) (file-readable-p dir))
163 dir))
164 "Directory where the C source files of Emacs can be found.
165 If nil, do not try to find the source code of functions and variables
166 defined in C.")
168 (defun find-function-C-source (fun-or-var file type)
169 "Find the source location where FUN-OR-VAR is defined in FILE.
170 TYPE should be nil to find a function, or `defvar' to find a variable."
171 (unless find-function-C-source-directory
172 (setq find-function-C-source-directory
173 (read-directory-name "Emacs C source dir: " nil nil t)))
174 (setq file (expand-file-name file find-function-C-source-directory))
175 (unless (file-readable-p file)
176 (error "The C source file %s is not available"
177 (file-name-nondirectory file)))
178 (unless type
179 (setq fun-or-var (indirect-function fun-or-var)))
180 (with-current-buffer (find-file-noselect file)
181 (goto-char (point-min))
182 (unless (re-search-forward
183 (if type
184 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
185 (regexp-quote (symbol-name fun-or-var))
186 "\"")
187 (concat "DEFUN[ \t\n]*([ \t\n]*\""
188 (regexp-quote (subr-name fun-or-var))
189 "\""))
190 nil t)
191 (error "Can't find source for %s" fun-or-var))
192 (cons (current-buffer) (match-beginning 0))))
194 ;;;###autoload
195 (defun find-library (library)
196 "Find the elisp source of LIBRARY."
197 (interactive
198 (let* ((path (cons (or find-function-source-path load-path)
199 (find-library-suffixes)))
200 (def (if (eq (function-called-at-point) 'require)
201 (save-excursion
202 (backward-up-list)
203 (forward-char)
204 (backward-sexp -2)
205 (thing-at-point 'symbol))
206 (thing-at-point 'symbol))))
207 (when def
208 (setq def (and (locate-file-completion def path 'test) def)))
209 (list
210 (completing-read (if def (format "Library name (default %s): " def)
211 "Library name: ")
212 'locate-file-completion path nil nil nil def))))
213 (let ((buf (find-file-noselect (find-library-name library))))
214 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
216 ;;;###autoload
217 (defun find-function-search-for-symbol (symbol type library)
218 "Search for SYMBOL's definition of type TYPE in LIBRARY.
219 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
220 or just (BUFFER . nil) if the definition can't be found in the file.
222 If TYPE is nil, look for a function definition.
223 Otherwise, TYPE specifies the kind of definition,
224 and it is interpreted via `find-function-regexp-alist'.
225 The search is done in the source for library LIBRARY."
226 (if (null library)
227 (error "Don't know where `%s' is defined" symbol))
228 ;; Some functions are defined as part of the construct
229 ;; that defines something else.
230 (while (and (symbolp symbol) (get symbol 'definition-name))
231 (setq symbol (get symbol 'definition-name)))
232 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
233 (find-function-C-source symbol (match-string 1 library) type)
234 (when (string-match "\\.el\\(c\\)\\'" library)
235 (setq library (substring library 0 (match-beginning 1))))
236 ;; Strip extension from .emacs.el to make sure symbol is searched in
237 ;; .emacs too.
238 (when (string-match "\\.emacs\\(.el\\)" library)
239 (setq library (substring library 0 (match-beginning 1))))
240 (let* ((filename (find-library-name library))
241 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
242 (with-current-buffer (find-file-noselect filename)
243 (let ((regexp (format (symbol-value regexp-symbol)
244 ;; Entry for ` (backquote) macro in loaddefs.el,
245 ;; (defalias (quote \`)..., has a \ but
246 ;; (symbol-name symbol) doesn't. Add an
247 ;; optional \ to catch this.
248 (concat "\\\\?"
249 (regexp-quote (symbol-name symbol)))))
250 (case-fold-search))
251 (with-syntax-table emacs-lisp-mode-syntax-table
252 (goto-char (point-min))
253 (if (or (re-search-forward regexp nil t)
254 ;; `regexp' matches definitions using known forms like
255 ;; `defun', or `defvar'. But some functions/variables
256 ;; are defined using special macros (or functions), so
257 ;; if `regexp' can't find the definition, we look for
258 ;; something of the form "(SOMETHING <symbol> ...)".
259 ;; This fails to distinguish function definitions from
260 ;; variable declarations (or even uses thereof), but is
261 ;; a good pragmatic fallback.
262 (re-search-forward
263 (concat "^([^ ]+" find-function-space-re "['(]?"
264 (regexp-quote (symbol-name symbol))
265 "\\_>")
266 nil t))
267 (progn
268 (beginning-of-line)
269 (cons (current-buffer) (point)))
270 (cons (current-buffer) nil))))))))
272 ;;;###autoload
273 (defun find-function-noselect (function)
274 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
276 Finds the source file containing the definition of FUNCTION
277 in a buffer and the point of the definition. The buffer is
278 not selected. If the function definition can't be found in
279 the buffer, returns (BUFFER).
281 If the file where FUNCTION is defined is not known, then it is
282 searched for in `find-function-source-path' if non-nil, otherwise
283 in `load-path'."
284 (if (not function)
285 (error "You didn't specify a function"))
286 (let ((def (symbol-function function))
287 aliases)
288 (while (symbolp def)
289 (or (eq def function)
290 (if aliases
291 (setq aliases (concat aliases
292 (format ", which is an alias for `%s'"
293 (symbol-name def))))
294 (setq aliases (format "`%s' an alias for `%s'"
295 function (symbol-name def)))))
296 (setq function (symbol-function function)
297 def (symbol-function function)))
298 (if aliases
299 (message "%s" aliases))
300 (let ((library
301 (cond ((eq (car-safe def) 'autoload)
302 (nth 1 def))
303 ((subrp def)
304 (help-C-file-name def 'subr))
305 ((symbol-file function 'defun)))))
306 (find-function-search-for-symbol function nil library))))
308 (defun find-function-read (&optional type)
309 "Read and return an interned symbol, defaulting to the one near point.
311 If TYPE is nil, insist on a symbol with a function definition.
312 Otherwise TYPE should be `defvar' or `defface'.
313 If TYPE is nil, defaults using `function-called-at-point',
314 otherwise uses `variable-at-point'."
315 (let ((symb (if (null type)
316 (function-called-at-point)
317 (if (eq type 'defvar)
318 (variable-at-point)
319 (variable-at-point t))))
320 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
321 (defface . facep)))))
322 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
323 (defface . "face")))))
324 (enable-recursive-minibuffers t)
325 val)
326 (if (equal symb 0)
327 (setq symb nil))
328 (setq val (completing-read
329 (concat "Find "
330 prompt
331 (if symb
332 (format " (default %s)" symb))
333 ": ")
334 obarray predicate t nil))
335 (list (if (equal val "")
336 symb
337 (intern val)))))
339 (defun find-function-do-it (symbol type switch-fn)
340 "Find Emacs Lisp SYMBOL in a buffer and display it.
341 TYPE is nil to search for a function definition,
342 or else `defvar' or `defface'.
344 The variable `find-function-recenter-line' controls how
345 to recenter the display. SWITCH-FN is the function to call
346 to display and select the buffer.
347 See also `find-function-after-hook'.
349 Set mark before moving, if the buffer already existed."
350 (let* ((orig-point (point))
351 (orig-buf (window-buffer))
352 (orig-buffers (buffer-list))
353 (buffer-point (save-excursion
354 (find-definition-noselect symbol type)))
355 (new-buf (car buffer-point))
356 (new-point (cdr buffer-point)))
357 (when buffer-point
358 (when (memq new-buf orig-buffers)
359 (push-mark orig-point))
360 (funcall switch-fn new-buf)
361 (when new-point (goto-char new-point))
362 (recenter find-function-recenter-line)
363 (run-hooks 'find-function-after-hook))))
365 ;;;###autoload
366 (defun find-function (function)
367 "Find the definition of the FUNCTION near point.
369 Finds the source file containing the definition of the function
370 near point (selected by `function-called-at-point') in a buffer and
371 places point before the definition.
372 Set mark before moving, if the buffer already existed.
374 The library where FUNCTION is defined is searched for in
375 `find-function-source-path', if non-nil, otherwise in `load-path'.
376 See also `find-function-recenter-line' and `find-function-after-hook'."
377 (interactive (find-function-read))
378 (find-function-do-it function nil 'switch-to-buffer))
380 ;;;###autoload
381 (defun find-function-other-window (function)
382 "Find, in another window, the definition of FUNCTION near point.
384 See `find-function' for more details."
385 (interactive (find-function-read))
386 (find-function-do-it function nil 'switch-to-buffer-other-window))
388 ;;;###autoload
389 (defun find-function-other-frame (function)
390 "Find, in another frame, 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-frame))
396 ;;;###autoload
397 (defun find-variable-noselect (variable &optional file)
398 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
400 Finds the library containing the definition of VARIABLE in a buffer and
401 the point of the definition. The buffer is not selected.
402 If the variable's definition can't be found in the buffer, return (BUFFER).
404 The library where VARIABLE is defined is searched for in FILE or
405 `find-function-source-path', if non-nil, otherwise in `load-path'."
406 (if (not variable)
407 (error "You didn't specify a variable")
408 (let ((library (or file
409 (symbol-file variable 'defvar)
410 (help-C-file-name variable 'var))))
411 (find-function-search-for-symbol variable 'defvar library))))
413 ;;;###autoload
414 (defun find-variable (variable)
415 "Find the definition of the VARIABLE at or before point.
417 Finds the library containing the definition of the variable
418 near point (selected by `variable-at-point') in a buffer and
419 places point before the definition.
421 Set mark before moving, if the buffer already existed.
423 The library where VARIABLE is defined is searched for in
424 `find-function-source-path', if non-nil, otherwise in `load-path'.
425 See also `find-function-recenter-line' and `find-function-after-hook'."
426 (interactive (find-function-read 'defvar))
427 (find-function-do-it variable 'defvar 'switch-to-buffer))
429 ;;;###autoload
430 (defun find-variable-other-window (variable)
431 "Find, in another window, the definition of VARIABLE near point.
433 See `find-variable' for more details."
434 (interactive (find-function-read 'defvar))
435 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
437 ;;;###autoload
438 (defun find-variable-other-frame (variable)
439 "Find, in another frame, 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-frame))
445 ;;;###autoload
446 (defun find-definition-noselect (symbol type &optional file)
447 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
448 If the definition can't be found in the buffer, return (BUFFER).
449 TYPE says what type of definition: nil for a function, `defvar' for a
450 variable, `defface' for a face. This function does not switch to the
451 buffer nor display it.
453 The library where SYMBOL is defined is searched for in FILE or
454 `find-function-source-path', if non-nil, otherwise in `load-path'."
455 (cond
456 ((not symbol)
457 (error "You didn't specify a symbol"))
458 ((null type)
459 (find-function-noselect symbol))
460 ((eq type 'defvar)
461 (find-variable-noselect symbol file))
463 (let ((library (or file (symbol-file symbol type))))
464 (find-function-search-for-symbol symbol type library)))))
466 ;; For symmetry, this should be called find-face; but some programs
467 ;; assume that, if that name is defined, it means something else.
468 ;;;###autoload
469 (defun find-face-definition (face)
470 "Find the definition of FACE. FACE defaults to the name near point.
472 Finds the Emacs Lisp library containing the definition of the face
473 near point (selected by `variable-at-point') in a buffer and
474 places point before the definition.
476 Set mark before moving, if the buffer already existed.
478 The library where FACE is defined is searched for in
479 `find-function-source-path', if non-nil, otherwise in `load-path'.
480 See also `find-function-recenter-line' and `find-function-after-hook'."
481 (interactive (find-function-read 'defface))
482 (find-function-do-it face 'defface 'switch-to-buffer))
484 ;;;###autoload
485 (defun find-function-on-key (key)
486 "Find the function that KEY invokes. KEY is a string.
487 Set mark before moving, if the buffer already existed."
488 (interactive "kFind function on key: ")
489 (let (defn)
490 (save-excursion
491 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
492 (start (event-start event))
493 (modifiers (event-modifiers event))
494 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
495 (memq 'drag modifiers))
496 (posn-window start))))
497 ;; For a mouse button event, go to the button it applies to
498 ;; to get the right key bindings. And go to the right place
499 ;; in case the keymap depends on where you clicked.
500 (when (windowp window)
501 (set-buffer (window-buffer window))
502 (goto-char (posn-point start)))
503 (setq defn (key-binding key))))
504 (let ((key-desc (key-description key)))
505 (if (or (null defn) (integerp defn))
506 (message "%s is unbound" key-desc)
507 (if (consp defn)
508 (message "%s runs %s" key-desc (prin1-to-string defn))
509 (find-function-other-window defn))))))
511 ;;;###autoload
512 (defun find-function-at-point ()
513 "Find directly the function at point in the other window."
514 (interactive)
515 (let ((symb (function-called-at-point)))
516 (when symb
517 (find-function-other-window symb))))
519 ;;;###autoload
520 (defun find-variable-at-point ()
521 "Find directly the variable at point in the other window."
522 (interactive)
523 (let ((symb (variable-at-point)))
524 (when (and symb (not (equal symb 0)))
525 (find-variable-other-window symb))))
527 ;;;###autoload
528 (defun find-function-setup-keys ()
529 "Define some key bindings for the find-function family of functions."
530 (define-key ctl-x-map "F" 'find-function)
531 (define-key ctl-x-4-map "F" 'find-function-other-window)
532 (define-key ctl-x-5-map "F" 'find-function-other-frame)
533 (define-key ctl-x-map "K" 'find-function-on-key)
534 (define-key ctl-x-map "V" 'find-variable)
535 (define-key ctl-x-4-map "V" 'find-variable-other-window)
536 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
538 (provide 'find-func)
540 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
541 ;;; find-func.el ends here