]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/find-func.el
(find-library-name): Doc fix.
[gnu-emacs] / 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 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 2, 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 (require 'loadhist)
50
51 ;;; User variables:
52
53 (defgroup find-function nil
54 "Finds the definition of the Emacs Lisp symbol near point."
55 ;; :prefix "find-function"
56 :group 'lisp)
57
58 (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+")
59
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-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'.
74
75 Please send improvements and fixes to the maintainer."
76 :type 'regexp
77 :group 'find-function
78 :version "21.1")
79
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'.
86
87 Please send improvements and fixes to the maintainer."
88 :type 'regexp
89 :group 'find-function
90 :version "21.1")
91
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.
97
98 Please send improvements and fixes to the maintainer."
99 :type 'regexp
100 :group 'find-function
101 :version "22.1")
102
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)
111
112 (defcustom find-function-source-path nil
113 "The default list of directories where `find-function' searches.
114
115 If this variable is nil then `find-function' searches `load-path' by
116 default."
117 :type '(repeat directory)
118 :group 'find-function)
119
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)
125 integer)
126 :group 'find-function
127 :version "20.3")
128
129 (defcustom find-function-after-hook nil
130 "Hook run after finding symbol definition.
131
132 See the functions `find-function' and `find-variable'."
133 :group 'find-function
134 :version "20.3")
135
136 ;;; Functions:
137
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)))))
142
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
146 ;; the same name.
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)))
153
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))
157 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
160 defined in C.")
161
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)))
172 (unless type
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
177 (if type
178 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
179 (regexp-quote (symbol-name fun-or-var))
180 "\"")
181 (concat "DEFUN[ \t\n]*([ \t\n]*\""
182 (regexp-quote (subr-name fun-or-var))
183 "\""))
184 nil t)
185 (error "Can't find source for %s" fun-or-var))
186 (cons (current-buffer) (match-beginning 0))))
187
188 ;;;###autoload
189 (defun find-library (library)
190 "Find the elisp source of LIBRARY."
191 (interactive
192 (list
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)))))
199
200 ;;;###autoload
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."
207 (if (null 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))))
222 (case-fold-search))
223 (with-syntax-table emacs-lisp-mode-syntax-table
224 (goto-char (point-min))
225 (if (or (re-search-forward regexp nil t)
226 (re-search-forward
227 (concat "^([^ ]+" find-function-space-re "['(]"
228 (regexp-quote (symbol-name symbol))
229 "\\_>")
230 nil t))
231 (progn
232 (beginning-of-line)
233 (cons (current-buffer) (point)))
234 (error "Cannot find definition of `%s' in library `%s'"
235 symbol library))))))))
236
237 ;;;###autoload
238 (defun find-function-noselect (function)
239 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
240
241 Finds the Emacs Lisp library containing the definition of FUNCTION
242 in a buffer and the point of the definition. The buffer is
243 not selected.
244
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
247 in `load-path'."
248 (if (not function)
249 (error "You didn't specify a function"))
250 (let ((def (symbol-function function))
251 aliases)
252 (while (symbolp def)
253 (or (eq def function)
254 (if aliases
255 (setq aliases (concat aliases
256 (format ", which is an alias for `%s'"
257 (symbol-name def))))
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)))
262 (if aliases
263 (message "%s" aliases))
264 (let ((library
265 (cond ((eq (car-safe def) 'autoload)
266 (nth 1 def))
267 ((subrp def)
268 (help-C-file-name def 'subr))
269 ((symbol-file function 'defun)))))
270 (find-function-search-for-symbol function nil library))))
271
272 (defun find-function-read (&optional type)
273 "Read and return an interned symbol, defaulting to the one near point.
274
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)
282 (variable-at-point)
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)
289 val)
290 (if (equal symb 0)
291 (setq symb nil))
292 (setq val (completing-read
293 (concat "Find "
294 prompt
295 (if symb
296 (format " (default %s)" symb))
297 ": ")
298 obarray predicate t nil))
299 (list (if (equal val "")
300 symb
301 (intern val)))))
302
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'.
307
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'.
312
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)))
321 (when 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))))
328
329 ;;;###autoload
330 (defun find-function (function)
331 "Find the definition of the FUNCTION near point.
332
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.
337
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))
343
344 ;;;###autoload
345 (defun find-function-other-window (function)
346 "Find, in another window, the definition of FUNCTION near point.
347
348 See `find-function' for more details."
349 (interactive (find-function-read))
350 (find-function-do-it function nil 'switch-to-buffer-other-window))
351
352 ;;;###autoload
353 (defun find-function-other-frame (function)
354 "Find, in ananother frame, the definition of FUNCTION near point.
355
356 See `find-function' for more details."
357 (interactive (find-function-read))
358 (find-function-do-it function nil 'switch-to-buffer-other-frame))
359
360 ;;;###autoload
361 (defun find-variable-noselect (variable &optional file)
362 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
363
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.
367
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'."
370 (if (not variable)
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)))
374
375 ;;;###autoload
376 (defun find-variable (variable)
377 "Find the definition of the VARIABLE near point.
378
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.
382
383 Set mark before moving, if the buffer already existed.
384
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))
390
391 ;;;###autoload
392 (defun find-variable-other-window (variable)
393 "Find, in another window, the definition of VARIABLE near point.
394
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))
398
399 ;;;###autoload
400 (defun find-variable-other-frame (variable)
401 "Find, in annother frame, the definition of VARIABLE near point.
402
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))
406
407 ;;;###autoload
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.
413
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'."
416 (if (not symbol)
417 (error "You didn't specify a symbol"))
418 (if (null type)
419 (find-function-noselect symbol)
420 (let ((library (or file (symbol-file symbol type))))
421 (find-function-search-for-symbol symbol type library))))
422
423 ;; For symmetry, this should be called find-face; but some programs
424 ;; assume that, if that name is defined, it means something else.
425 ;;;###autoload
426 (defun find-face-definition (face)
427 "Find the definition of FACE. FACE defaults to the name near point.
428
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.
432
433 Set mark before moving, if the buffer already existed.
434
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))
440
441 ;;;###autoload
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: ")
446 (let (defn)
447 (save-excursion
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)
464 (if (consp defn)
465 (message "%s runs %s" key-desc (prin1-to-string defn))
466 (find-function-other-window defn))))))
467
468 ;;;###autoload
469 (defun find-function-at-point ()
470 "Find directly the function at point in the other window."
471 (interactive)
472 (let ((symb (function-called-at-point)))
473 (when symb
474 (find-function-other-window symb))))
475
476 ;;;###autoload
477 (defun find-variable-at-point ()
478 "Find directly the function at point in the other window."
479 (interactive)
480 (let ((symb (variable-at-point)))
481 (when (and symb (not (equal symb 0)))
482 (find-variable-other-window symb))))
483
484 ;;;###autoload
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))
494
495 (provide 'find-func)
496
497 ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64
498 ;;; find-func.el ends here