1 ;;; find-func.el --- find the definition of the Emacs Lisp function near point
3 ;; Copyright (C) 1997, 1999, 2001 Free Software Foundation, Inc.
5 ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp>
6 ;; Maintainer: petersen@kurims.kyoto-u.ac.jp
7 ;; Keywords: emacs-lisp, functions, variables
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; The funniest thing about this is that I can't imagine why a package
30 ;; so obviously useful as this hasn't been written before!!
32 ;; (find-function-setup-keys)
38 ;; if you don't like the given keybindings and away you go! It does
39 ;; pretty much what you would expect, putting the cursor at the
40 ;; definition of the function or variable at point.
42 ;; The code started out from `describe-function', `describe-key'
43 ;; ("help.el") and `fff-find-loaded-emacs-lisp-function' (Noah Friedman's
52 (defgroup find-function nil
53 "Finds the definition of the Emacs Lisp symbol near point."
54 ;; :prefix "find-function"
57 (defcustom find-function-regexp
58 ;; Match things like (defun foo ...), (defmacro foo ...),
59 ;; (define-skeleton foo ...), (define-generic-mode 'foo ...),
60 ;; (define-derived-mode foo ...), (define-minor-mode foo)
61 "^\\s-*(\\(def\\(ine-skeleton\\|ine-generic-mode\\|ine-derived-mode\\|\
62 \[^cgv\W]\\w+\\*?\\)\\|define-minor-mode\
63 \\|easy-mmode-define-global-mode\\)\\(\\s-\\|\n\\)+\\('\\|\(quote \\)?\
64 %s\\(\\s-\\|$\\|\(\\|\)\\)"
65 "The regexp used by `find-function' to search for a function definition.
66 Note it must contain a `%s' at the place where `format'
67 should insert the function name. The default value avoids `defconst',
70 Please send improvements and fixes to the maintainer."
75 (defcustom find-variable-regexp
76 "^\\s-*(def[^umag]\\(\\w\\|\\s_\\)+\\*?\\s-+%s\\(\\s-\\|$\\)"
77 "The regexp used by `find-variable' to search for a variable definition.
78 It should match right up to the variable name. The default value
79 avoids `defun', `defmacro', `defalias', `defadvice', `defgroup'.
81 Please send improvements and fixes to the maintainer."
86 (defcustom find-function-source-path nil
87 "The default list of directories where `find-function' searches.
89 If this variable is nil then `find-function' searches `load-path' by
91 :type
'(repeat directory
)
92 :group
'find-function
)
94 (defcustom find-function-recenter-line
1
95 "The window line-number from which to start displaying a symbol definition.
96 A value of nil implies center the beginning of the definition.
97 See the function `center-to-window-line' for more information, and
98 `find-function' and `find-variable'."
102 (defcustom find-function-after-hook nil
103 "Hook run after finding symbol definition.
105 See the functions `find-function' and `find-variable'."
106 :group
'find-function
112 (defun find-function-search-for-symbol (symbol variable-p library
)
114 If VARIABLE-P is nil, `find-function-regexp' is used, otherwise
115 `find-variable-regexp' is used. The search is done in library LIBRARY."
117 (error "Don't know where `%s' is defined" symbol
))
119 (if (string-match "\\.el\\(c\\)\\'" library
)
120 (setq library
(substring library
0 (match-beginning 1))))
121 (let* ((path find-function-source-path
)
122 (compression (or (rassq 'jka-compr-handler file-name-handler-alist
)
123 (member 'crypt-find-file-hook find-file-hooks
)))
125 ;; use `file-name-sans-extension' here? (if it gets fixed)
126 (if (string-match "\\(\\.el\\)\\'" library
)
127 (setq library
(substring library
0
128 (match-beginning 1))))
129 (or (locate-library (concat library
".el") t path
)
130 (locate-library library t path
)
132 (or (locate-library (concat library
".el.gz")
134 (locate-library (concat library
".gz")
137 (error "The library `%s' is not in the path" library
))
138 (with-current-buffer (find-file-noselect filename
)
139 (let ((regexp (format (if variable-p
141 find-function-regexp
)
142 (regexp-quote (symbol-name symbol
))))
144 (with-syntax-table emacs-lisp-mode-syntax-table
145 (goto-char (point-min))
146 (if (or (re-search-forward regexp nil t
)
149 (regexp-quote (symbol-name symbol
))
154 (cons (current-buffer) (point)))
155 (error "Cannot find definition of `%s' in library `%s'"
156 symbol library
))))))))
159 (defun find-function-noselect (function)
160 "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
162 Finds the Emacs Lisp library containing the definition of FUNCTION
163 in a buffer and the point of the definition. The buffer is
166 If the file where FUNCTION is defined is not known, then it is
167 searched for in `find-function-source-path' if non nil, otherwise
170 (error "You didn't specify a function"))
171 (and (subrp (symbol-function function
))
172 (error "%s is a primitive function" function
))
173 (let ((def (symbol-function function
))
176 (or (eq def function
)
178 (setq aliases
(concat aliases
179 (format ", which is an alias for `%s'"
181 (setq aliases
(format "`%s' an alias for `%s'"
182 function
(symbol-name def
)))))
183 (setq function
(symbol-function function
)
184 def
(symbol-function function
)))
188 (cond ((eq (car-safe def
) 'autoload
)
190 ((symbol-file function
)))))
191 (find-function-search-for-symbol function nil library
))))
193 (defalias 'function-at-point
'function-called-at-point
)
195 (defun find-function-read (&optional variable-p
)
196 "Read and return an interned symbol, defaulting to the one near point.
198 If the optional VARIABLE-P is nil, then a function is gotten
199 defaulting to the value of the function `function-at-point', otherwise
200 a variable is asked for, with the default coming from
201 `variable-at-point'."
202 (let ((symb (funcall (if variable-p
204 'function-at-point
)))
205 (enable-recursive-minibuffers t
)
209 (setq val
(if variable-p
211 (concat "Find variable"
213 (format " (default %s)" symb
))
215 obarray
'boundp t nil
)
217 (concat "Find function"
219 (format " (default %s)" symb
))
221 obarray
'fboundp t nil
)))
222 (list (if (equal val
"")
226 (defun find-function-do-it (symbol variable-p switch-fn
)
227 "Find Emacs Lisp SYMBOL in a buffer and display it.
228 If VARIABLE-P is nil, a function definition is searched for, otherwise
229 a variable definition is searched for. The start of a definition is
230 centered according to the variable `find-function-recenter-line'.
231 See also `find-function-after-hook' It is displayed with function SWITCH-FN.
233 Point is saved in the buffer if it is one of the current buffers."
234 (let* ((orig-point (point))
235 (orig-buf (window-buffer))
236 (orig-buffers (buffer-list))
237 (buffer-point (save-excursion
238 (funcall (if variable-p
239 'find-variable-noselect
240 'find-function-noselect
)
242 (new-buf (car buffer-point
))
243 (new-point (cdr buffer-point
)))
245 (when (memq new-buf orig-buffers
)
246 (push-mark orig-point
))
247 (funcall switch-fn new-buf
)
248 (goto-char new-point
)
249 (recenter find-function-recenter-line
)
250 (run-hooks 'find-function-after-hook
))))
253 (defun find-function (function)
254 "Find the definition of the FUNCTION near point.
256 Finds the Emacs Lisp library containing the definition of the function
257 near point (selected by `function-at-point') in a buffer and
258 places point before the definition. Point is saved in the buffer if
259 it is one of the current buffers.
261 The library where FUNCTION is defined is searched for in
262 `find-function-source-path', if non nil, otherwise in `load-path'.
263 See also `find-function-recenter-line' and `find-function-after-hook'."
264 (interactive (find-function-read))
265 (find-function-do-it function nil
'switch-to-buffer
))
268 (defun find-function-other-window (function)
269 "Find, in another window, the definition of FUNCTION near point.
271 See `find-function' for more details."
272 (interactive (find-function-read))
273 (find-function-do-it function nil
'switch-to-buffer-other-window
))
276 (defun find-function-other-frame (function)
277 "Find, in ananother frame, the definition of FUNCTION near point.
279 See `find-function' for more details."
280 (interactive (find-function-read))
281 (find-function-do-it function nil
'switch-to-buffer-other-frame
))
284 (defun find-variable-noselect (variable &optional file
)
285 "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
287 Finds the Emacs Lisp library containing the definition of SYMBOL
288 in a buffer and the point of the definition. The buffer is
291 The library where VARIABLE is defined is searched for in FILE or
292 `find-function-source-path', if non nil, otherwise in `load-path'."
294 (error "You didn't specify a variable"))
295 (let ((library (or file
(symbol-file variable
))))
296 (find-function-search-for-symbol variable
'variable library
)))
299 (defun find-variable (variable)
300 "Find the definition of the VARIABLE near point.
302 Finds the Emacs Lisp library containing the definition of the variable
303 near point (selected by `variable-at-point') in a buffer and
304 places point before the definition. Point is saved in the buffer if
305 it is one of the current buffers.
307 The library where VARIABLE is defined is searched for in
308 `find-function-source-path', if non nil, otherwise in `load-path'.
309 See also `find-function-recenter-line' and `find-function-after-hook'."
310 (interactive (find-function-read 'variable
))
311 (find-function-do-it variable t
'switch-to-buffer
))
314 (defun find-variable-other-window (variable)
315 "Find, in another window, the definition of VARIABLE near point.
317 See `find-variable' for more details."
318 (interactive (find-function-read 'variable
))
319 (find-function-do-it variable t
'switch-to-buffer-other-window
))
322 (defun find-variable-other-frame (variable)
323 "Find, in annother frame, the definition of VARIABLE near point.
325 See `find-variable' for more details."
326 (interactive (find-function-read 'variable
))
327 (find-function-do-it variable t
'switch-to-buffer-other-frame
))
330 (defun find-function-on-key (key)
331 "Find the function that KEY invokes. KEY is a string.
332 Point is saved if FUNCTION is in the current buffer."
333 (interactive "kFind function on key: ")
335 (let* ((event (and (eventp key
) (aref key
0))) ; Null event OK below.
336 (start (event-start event
))
337 (modifiers (event-modifiers event
))
338 (window (and (or (memq 'click modifiers
) (memq 'down modifiers
)
339 (memq 'drag modifiers
))
340 (posn-window start
))))
341 ;; For a mouse button event, go to the button it applies to
342 ;; to get the right key bindings. And go to the right place
343 ;; in case the keymap depends on where you clicked.
344 (when (windowp window
)
345 (set-buffer (window-buffer window
))
346 (goto-char (posn-point start
)))
347 (let ((defn (key-binding key
))
348 (key-desc (key-description key
)))
349 (if (or (null defn
) (integerp defn
))
350 (message "%s is unbound" key-desc
)
352 (message "%s runs %s" key-desc
(prin1-to-string defn
))
353 (find-function-other-window defn
)))))))
356 (defun find-function-at-point ()
357 "Find directly the function at point in the other window."
359 (let ((symb (function-at-point)))
361 (find-function-other-window symb
))))
364 (defun find-variable-at-point ()
365 "Find directly the function at point in the other window."
367 (let ((symb (variable-at-point)))
368 (when (and symb
(not (equal symb
0)))
369 (find-variable-other-window symb
))))
372 (defun find-function-setup-keys ()
373 "Define some key bindings for the find-function family of functions."
374 (define-key ctl-x-map
"F" 'find-function
)
375 (define-key ctl-x-4-map
"F" 'find-function-other-window
)
376 (define-key ctl-x-5-map
"F" 'find-function-other-frame
)
377 (define-key ctl-x-map
"K" 'find-function-on-key
)
378 (define-key ctl-x-map
"V" 'find-variable
)
379 (define-key ctl-x-4-map
"V" 'find-variable-other-window
)
380 (define-key ctl-x-5-map
"V" 'find-variable-other-frame
))
384 ;;; find-func.el ends here