Don't require loadhist.
[bpt/emacs.git] / lisp / emacs-lisp / find-func.el
1 ;;; find-func.el --- find the definition of the Emacs Lisp function near point
2
3 ;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
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
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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.
27
28 ;;; Commentary:
29 ;;
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)
34 ;;
35 ;; or just:
36 ;;
37 ;; (load "find-func")
38 ;;
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.
42 ;;
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").
46
47 ;;; Code:
48
49 ;;; User variables:
50
51 (defgroup find-function nil
52 "Finds the definition of the Emacs Lisp symbol near point."
53 ;; :prefix "find-function"
54 :group 'lisp)
55
56 (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+")
57
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'.
73
74 Please send improvements and fixes to the maintainer."
75 :type 'regexp
76 :group 'find-function
77 :version "21.1")
78
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'.
89
90 Please send improvements and fixes to the maintainer."
91 :type 'regexp
92 :group 'find-function
93 :version "21.1")
94
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.
100
101 Please send improvements and fixes to the maintainer."
102 :type 'regexp
103 :group 'find-function
104 :version "22.1")
105
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)
114
115 (defcustom find-function-source-path nil
116 "The default list of directories where `find-function' searches.
117
118 If this variable is nil then `find-function' searches `load-path' by
119 default."
120 :type '(repeat directory)
121 :group 'find-function)
122
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")
131
132 (defcustom find-function-after-hook nil
133 "Hook run after finding symbol definition.
134
135 See the functions `find-function' and `find-variable'."
136 :group 'find-function
137 :version "20.3")
138
139 ;;; Functions:
140
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)))))
145
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)))
156
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.")
164
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))))
190
191 ;;;###autoload
192 (defun find-library (library)
193 "Find the elisp source of LIBRARY."
194 (interactive
195 (list
196 (completing-read "Library name: "
197 'locate-file-completion
198 (cons (or find-function-source-path load-path)
199 (find-library-suffixes)))))
200 (let ((buf (find-file-noselect (find-library-name library))))
201 (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
202
203 ;;;###autoload
204 (defun find-function-search-for-symbol (symbol type library)
205 "Search for SYMBOL's definition of type TYPE in LIBRARY.
206 Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
207 or just (BUFFER . nil) if the definition can't be found in the file.
208
209 If TYPE is nil, look for a function definition.
210 Otherwise, TYPE specifies the kind of definition,
211 and it is interpreted via `find-function-regexp-alist'.
212 The search is done in the source for library LIBRARY."
213 (if (null library)
214 (error "Don't know where `%s' is defined" symbol))
215 ;; Some functions are defined as part of the construct
216 ;; that defines something else.
217 (while (and (symbolp symbol) (get symbol 'definition-name))
218 (setq symbol (get symbol 'definition-name)))
219 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
220 (find-function-C-source symbol (match-string 1 library) type)
221 (if (string-match "\\.el\\(c\\)\\'" library)
222 (setq library (substring library 0 (match-beginning 1))))
223 (let* ((filename (find-library-name library))
224 (regexp-symbol (cdr (assq type find-function-regexp-alist))))
225 (with-current-buffer (find-file-noselect filename)
226 (let ((regexp (format (symbol-value regexp-symbol)
227 ;; Entry for ` (backquote) macro in loaddefs.el,
228 ;; (defalias (quote \`)..., has a \ but
229 ;; (symbol-name symbol) doesn't. Add an
230 ;; optional \ to catch this.
231 (concat "\\\\?"
232 (regexp-quote (symbol-name symbol)))))
233 (case-fold-search))
234 (with-syntax-table emacs-lisp-mode-syntax-table
235 (goto-char (point-min))
236 (if (or (re-search-forward regexp nil t)
237 ;; `regexp' matches definitions using known forms like
238 ;; `defun', or `defvar'. But some functions/variables
239 ;; are defined using special macros (or functions), so
240 ;; if `regexp' can't find the definition, we look for
241 ;; something of the form "(SOMETHING <symbol> ...)".
242 ;; This fails to distinguish function definitions from
243 ;; variable declarations (or even uses thereof), but is
244 ;; a good pragmatic fallback.
245 (re-search-forward
246 (concat "^([^ ]+" find-function-space-re "['(]?"
247 (regexp-quote (symbol-name symbol))
248 "\\_>")
249 nil t))
250 (progn
251 (beginning-of-line)
252 (cons (current-buffer) (point)))
253 (cons (current-buffer) nil))))))))
254
255 ;;;###autoload
256 (defun find-function-noselect (function)
257 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
258
259 Finds the source file containing the definition of FUNCTION
260 in a buffer and the point of the definition. The buffer is
261 not selected. If the function definition can't be found in
262 the buffer, returns (BUFFER).
263
264 If the file where FUNCTION is defined is not known, then it is
265 searched for in `find-function-source-path' if non-nil, otherwise
266 in `load-path'."
267 (if (not function)
268 (error "You didn't specify a function"))
269 (let ((def (symbol-function function))
270 aliases)
271 (while (symbolp def)
272 (or (eq def function)
273 (if aliases
274 (setq aliases (concat aliases
275 (format ", which is an alias for `%s'"
276 (symbol-name def))))
277 (setq aliases (format "`%s' an alias for `%s'"
278 function (symbol-name def)))))
279 (setq function (symbol-function function)
280 def (symbol-function function)))
281 (if aliases
282 (message "%s" aliases))
283 (let ((library
284 (cond ((eq (car-safe def) 'autoload)
285 (nth 1 def))
286 ((subrp def)
287 (help-C-file-name def 'subr))
288 ((symbol-file function 'defun)))))
289 (find-function-search-for-symbol function nil library))))
290
291 (defun find-function-read (&optional type)
292 "Read and return an interned symbol, defaulting to the one near point.
293
294 If TYPE is nil, insist on a symbol with a function definition.
295 Otherwise TYPE should be `defvar' or `defface'.
296 If TYPE is nil, defaults using `function-called-at-point',
297 otherwise uses `variable-at-point'."
298 (let ((symb (if (null type)
299 (function-called-at-point)
300 (if (eq type 'defvar)
301 (variable-at-point)
302 (variable-at-point t))))
303 (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp)
304 (defface . facep)))))
305 (prompt (cdr (assq type '((nil . "function") (defvar . "variable")
306 (defface . "face")))))
307 (enable-recursive-minibuffers t)
308 val)
309 (if (equal symb 0)
310 (setq symb nil))
311 (setq val (completing-read
312 (concat "Find "
313 prompt
314 (if symb
315 (format " (default %s)" symb))
316 ": ")
317 obarray predicate t nil))
318 (list (if (equal val "")
319 symb
320 (intern val)))))
321
322 (defun find-function-do-it (symbol type switch-fn)
323 "Find Emacs Lisp SYMBOL in a buffer and display it.
324 TYPE is nil to search for a function definition,
325 or else `defvar' or `defface'.
326
327 The variable `find-function-recenter-line' controls how
328 to recenter the display. SWITCH-FN is the function to call
329 to display and select the buffer.
330 See also `find-function-after-hook'.
331
332 Set mark before moving, if the buffer already existed."
333 (let* ((orig-point (point))
334 (orig-buf (window-buffer))
335 (orig-buffers (buffer-list))
336 (buffer-point (save-excursion
337 (find-definition-noselect symbol type)))
338 (new-buf (car buffer-point))
339 (new-point (cdr buffer-point)))
340 (when buffer-point
341 (when (memq new-buf orig-buffers)
342 (push-mark orig-point))
343 (funcall switch-fn new-buf)
344 (when new-point (goto-char new-point))
345 (recenter find-function-recenter-line)
346 (run-hooks 'find-function-after-hook))))
347
348 ;;;###autoload
349 (defun find-function (function)
350 "Find the definition of the FUNCTION near point.
351
352 Finds the source file containing the definition of the function
353 near point (selected by `function-called-at-point') in a buffer and
354 places point before the definition.
355 Set mark before moving, if the buffer already existed.
356
357 The library where FUNCTION is defined is searched for in
358 `find-function-source-path', if non-nil, otherwise in `load-path'.
359 See also `find-function-recenter-line' and `find-function-after-hook'."
360 (interactive (find-function-read))
361 (find-function-do-it function nil 'switch-to-buffer))
362
363 ;;;###autoload
364 (defun find-function-other-window (function)
365 "Find, in another window, the definition of FUNCTION near point.
366
367 See `find-function' for more details."
368 (interactive (find-function-read))
369 (find-function-do-it function nil 'switch-to-buffer-other-window))
370
371 ;;;###autoload
372 (defun find-function-other-frame (function)
373 "Find, in another frame, the definition of FUNCTION near point.
374
375 See `find-function' for more details."
376 (interactive (find-function-read))
377 (find-function-do-it function nil 'switch-to-buffer-other-frame))
378
379 ;;;###autoload
380 (defun find-variable-noselect (variable &optional file)
381 "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
382
383 Finds the library containing the definition of VARIABLE in a buffer and
384 the point of the definition. The buffer is not selected.
385 If the variable's definition can't be found in the buffer, return (BUFFER).
386
387 The library where VARIABLE is defined is searched for in FILE or
388 `find-function-source-path', if non-nil, otherwise in `load-path'."
389 (if (not variable)
390 (error "You didn't specify a variable")
391 (let ((library (or file
392 (symbol-file variable 'defvar)
393 (help-C-file-name variable 'var))))
394 (find-function-search-for-symbol variable 'defvar library))))
395
396 ;;;###autoload
397 (defun find-variable (variable)
398 "Find the definition of the VARIABLE at or before point.
399
400 Finds the library containing the definition of the variable
401 near point (selected by `variable-at-point') in a buffer and
402 places point before the definition.
403
404 Set mark before moving, if the buffer already existed.
405
406 The library where VARIABLE is defined is searched for in
407 `find-function-source-path', if non-nil, otherwise in `load-path'.
408 See also `find-function-recenter-line' and `find-function-after-hook'."
409 (interactive (find-function-read 'defvar))
410 (find-function-do-it variable 'defvar 'switch-to-buffer))
411
412 ;;;###autoload
413 (defun find-variable-other-window (variable)
414 "Find, in another window, the definition of VARIABLE near point.
415
416 See `find-variable' for more details."
417 (interactive (find-function-read 'defvar))
418 (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
419
420 ;;;###autoload
421 (defun find-variable-other-frame (variable)
422 "Find, in another frame, the definition of VARIABLE near point.
423
424 See `find-variable' for more details."
425 (interactive (find-function-read 'defvar))
426 (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
427
428 ;;;###autoload
429 (defun find-definition-noselect (symbol type &optional file)
430 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
431 If the definition can't be found in the buffer, return (BUFFER).
432 TYPE says what type of definition: nil for a function, `defvar' for a
433 variable, `defface' for a face. This function does not switch to the
434 buffer nor display it.
435
436 The library where SYMBOL is defined is searched for in FILE or
437 `find-function-source-path', if non-nil, otherwise in `load-path'."
438 (cond
439 ((not symbol)
440 (error "You didn't specify a symbol"))
441 ((null type)
442 (find-function-noselect symbol))
443 ((eq type 'defvar)
444 (find-variable-noselect symbol file))
445 (t
446 (let ((library (or file (symbol-file symbol type))))
447 (find-function-search-for-symbol symbol type library)))))
448
449 ;; For symmetry, this should be called find-face; but some programs
450 ;; assume that, if that name is defined, it means something else.
451 ;;;###autoload
452 (defun find-face-definition (face)
453 "Find the definition of FACE. FACE defaults to the name near point.
454
455 Finds the Emacs Lisp library containing the definition of the face
456 near point (selected by `variable-at-point') in a buffer and
457 places point before the definition.
458
459 Set mark before moving, if the buffer already existed.
460
461 The library where FACE is defined is searched for in
462 `find-function-source-path', if non-nil, otherwise in `load-path'.
463 See also `find-function-recenter-line' and `find-function-after-hook'."
464 (interactive (find-function-read 'defface))
465 (find-function-do-it face 'defface 'switch-to-buffer))
466
467 ;;;###autoload
468 (defun find-function-on-key (key)
469 "Find the function that KEY invokes. KEY is a string.
470 Set mark before moving, if the buffer already existed."
471 (interactive "kFind function on key: ")
472 (let (defn)
473 (save-excursion
474 (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
475 (start (event-start event))
476 (modifiers (event-modifiers event))
477 (window (and (or (memq 'click modifiers) (memq 'down modifiers)
478 (memq 'drag modifiers))
479 (posn-window start))))
480 ;; For a mouse button event, go to the button it applies to
481 ;; to get the right key bindings. And go to the right place
482 ;; in case the keymap depends on where you clicked.
483 (when (windowp window)
484 (set-buffer (window-buffer window))
485 (goto-char (posn-point start)))
486 (setq defn (key-binding key))))
487 (let ((key-desc (key-description key)))
488 (if (or (null defn) (integerp defn))
489 (message "%s is unbound" key-desc)
490 (if (consp defn)
491 (message "%s runs %s" key-desc (prin1-to-string defn))
492 (find-function-other-window defn))))))
493
494 ;;;###autoload
495 (defun find-function-at-point ()
496 "Find directly the function at point in the other window."
497 (interactive)
498 (let ((symb (function-called-at-point)))
499 (when symb
500 (find-function-other-window symb))))
501
502 ;;;###autoload
503 (defun find-variable-at-point ()
504 "Find directly the variable at point in the other window."
505 (interactive)
506 (let ((symb (variable-at-point)))
507 (when (and symb (not (equal symb 0)))
508 (find-variable-other-window symb))))
509
510 ;;;###autoload
511 (defun find-function-setup-keys ()
512 "Define some key bindings for the find-function family of functions."
513 (define-key ctl-x-map "F" 'find-function)
514 (define-key ctl-x-4-map "F" 'find-function-other-window)
515 (define-key ctl-x-5-map "F" 'find-function-other-frame)
516 (define-key ctl-x-map "K" 'find-function-on-key)
517 (define-key ctl-x-map "V" 'find-variable)
518 (define-key ctl-x-4-map "V" 'find-variable-other-window)
519 (define-key ctl-x-5-map "V" 'find-variable-other-frame))
520
521 (provide 'find-func)
522
523 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
524 ;;; find-func.el ends here