203
|
1 ;;; find-func.el --- find the definition of the Emacs Lisp function near point
|
|
2
|
|
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp>
|
|
6 ;; Maintainer: petersen@kurims.kyoto-u.ac.jp
|
|
7 ;; Keywords: emacs-lisp, functions
|
|
8 ;; Created: 97/07/25
|
|
9 ;; URL: <http://www.kurims.kyoto-u.ac.jp/~petersen/emacs-lisp/>
|
|
10
|
207
|
11 ;; $Id: find-func.el,v 1.2 1997/10/31 14:53:07 steve Exp $
|
203
|
12
|
|
13 ;; This file is part of XEmacs.
|
|
14
|
|
15 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
16 ;; under the terms of the GNU General Public License as published by
|
|
17 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
18 ;; any later version.
|
|
19
|
|
20 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
23 ;; General Public License for more details.
|
|
24
|
|
25 ;; You should have received a copy of the GNU General Public License
|
|
26 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
27 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
28 ;; Boston, MA 02111-1307, USA.
|
|
29
|
|
30 ;;; Commentary:
|
|
31 ;;
|
|
32 ;; The funniest thing about this is that I can't imagine why a package
|
|
33 ;; so obviously useful as this hasn't been written before!!
|
|
34 ;;
|
|
35 ;; Put this file in your `load-path', byte-compile it and add the
|
|
36 ;; following code in your init file:
|
|
37 ;;
|
|
38 ;; ;;; find-func
|
|
39 ;; (load "find-func")
|
|
40 ;; (global-set-key [(control ?c) ?f] 'find-function)
|
|
41 ;; (global-set-key [(control ?c) ?4 ?f] 'find-function-other-window)
|
|
42 ;; (global-set-key [(control ?c) ?5 ?f] 'find-function-other-frame)
|
|
43 ;; (global-set-key [(control ?c) ?k] 'find-function-on-key)
|
|
44 ;;
|
|
45 ;; and away you go! It does pretty much what you would expect,
|
|
46 ;; putting the cursor at the definition of the function at point.
|
|
47 ;;
|
|
48 ;; In XEmacs the source file of dumped functions is recorded (and can
|
|
49 ;; be accessed with the function `compiled-function-annotation', which
|
|
50 ;; doesn't exist in Emacs), so in XEmacs non-primitive dumped
|
|
51 ;; functions can also be found. Unfortunately this is not possible in
|
|
52 ;; Emacs. It would be nice if the location of primitive functions in
|
|
53 ;; the C code was also recorded!
|
|
54
|
|
55 ;; The code is adapted from `describe-function', `describe-key'
|
|
56 ;; ("help.el") and `fff-find-loaded-emacs-lisp-function' (Noah Friedman's
|
|
57 ;; "fff.el").
|
|
58
|
|
59 ;;; To do:
|
|
60 ;;
|
|
61 ;; o improve handling of advice'd functions? (at the moment it goes to
|
|
62 ;; the advice, not the actual definition)
|
|
63 ;;
|
|
64 ;; o `find-function-other-frame' is not quite right when the function
|
|
65 ;; is in the current buffer.
|
|
66 ;;
|
|
67 ;;;; Code:
|
|
68
|
|
69 (defgroup find-function nil
|
|
70 "Find the definition of the Emacs Lisp function near point."
|
|
71 :group 'lisp)
|
|
72
|
|
73 ;;; User variables:
|
|
74
|
|
75 (defcustom find-function-source-path nil
|
|
76 "The default list of directories where find-function searches.
|
|
77
|
|
78 If this variable is `nil' then find-function searches `load-path' by
|
|
79 default."
|
|
80 :type '(choice (const :tag "Use `load-path'" nil)
|
|
81 (repeat :tag "Directories"
|
|
82 :menu-tag "List"
|
|
83 :value ("")
|
|
84 directory))
|
|
85 :group 'find-function)
|
|
86
|
|
87
|
|
88 ;;; Functions:
|
|
89
|
|
90 (defun find-function-noselect (function)
|
|
91 "Returns a pair `(buffer . point)' pointing to the definition of FUNCTION.
|
|
92
|
|
93 Finds the Emacs Lisp library containing the definition of FUNCTION
|
|
94 in a buffer and the point of the definition. The buffer is
|
|
95 not selected.
|
|
96
|
|
97 The library where FUNCTION is defined is searched for in
|
|
98 `find-function-source-path', if non `nil', otherwise in `load-path'."
|
207
|
99 (if (not function)
|
|
100 (error "You didn't specify a function"))
|
203
|
101 (and (subrp (symbol-function function))
|
|
102 (error "%s is a primitive function" function))
|
|
103 (let ((def (symbol-function function))
|
|
104 library aliases)
|
|
105 (while (symbolp def)
|
|
106 (or (eq def function)
|
|
107 (if aliases
|
|
108 (setq aliases (concat aliases
|
|
109 (format ", which is an alias for %s"
|
|
110 (symbol-name def))))
|
|
111 (setq aliases (format "an alias for %s" (symbol-name def)))))
|
|
112 (setq function (symbol-function function)
|
|
113 def (symbol-function function)))
|
|
114 (if aliases
|
|
115 (message aliases))
|
|
116 (setq library
|
|
117 (cond ((eq (car-safe def) 'autoload)
|
|
118 (nth 1 def))
|
|
119 ((describe-function-find-file function))
|
|
120 ((compiled-function-p def)
|
207
|
121 (substring (compiled-function-annotation def) 0 -4))
|
|
122 ((eq 'macro (car-safe def))
|
|
123 (and (compiled-function-p (cdr def))
|
|
124 (substring (compiled-function-annotation (cdr def)) 0 -4)))))
|
203
|
125 (if (null library)
|
|
126 (error (format "Don't know where `%s' is defined" function)))
|
|
127 (if (string-match "\\.el\\(c\\)\\'" library)
|
|
128 (setq library (substring library 0 (match-beginning 1))))
|
|
129 (let* ((path find-function-source-path)
|
|
130 (filename (if (file-exists-p library)
|
|
131 library
|
|
132 (if (string-match "\\(\\.el\\)\\'" library)
|
|
133 (setq library (substring library 0
|
|
134 (match-beginning
|
|
135 1))))
|
|
136 (or (locate-library (concat library ".el") t path)
|
|
137 (locate-library library t path)))))
|
|
138 (if (not filename)
|
|
139 (error "The library \"%s\" is not in the path." library))
|
|
140 (with-current-buffer (find-file-noselect filename)
|
|
141 (save-match-data
|
|
142 (let (;; avoid defconst, defgroup, defvar (any others?)
|
|
143 (regexp
|
|
144 (format "^\\s-*(def[^cgv\W]\\w+\\*?\\s-+%s\\s-" function))
|
|
145 (syntable (syntax-table)))
|
|
146 (set-syntax-table emacs-lisp-mode-syntax-table)
|
|
147 (goto-char (point-min))
|
|
148 (if (prog1
|
|
149 (re-search-forward regexp nil t)
|
|
150 (set-syntax-table syntable))
|
|
151 (progn
|
|
152 (beginning-of-line)
|
|
153 (cons (current-buffer) (point)))
|
|
154 (error "Cannot find definition of `%s'" function))))))))
|
|
155
|
|
156 (defun find-function-read-function ()
|
|
157 "Read and return a function, defaulting to the one near point.
|
|
158
|
207
|
159 `function-at-point' is used to select the default function."
|
|
160 (let ((fn (function-at-point))
|
203
|
161 (enable-recursive-minibuffers t)
|
|
162 val)
|
|
163 (setq val (completing-read
|
|
164 (if fn
|
|
165 (format "Find function (default %s): " fn)
|
|
166 "Find function: ")
|
|
167 obarray 'fboundp t nil 'function-history))
|
|
168 (list (if (equal val "")
|
|
169 fn (intern val)))))
|
|
170
|
|
171 (defun find-function-do-it (function switch-fn)
|
|
172 "Find Emacs Lisp FUNCTION in a buffer and display it with SWITCH-FN.
|
|
173 Point is saved in the buffer if it is one of the current buffers."
|
|
174 (let ((orig-point (point))
|
|
175 (orig-buffers (buffer-list))
|
|
176 (buffer-point (find-function-noselect function)))
|
|
177 (when buffer-point
|
|
178 (funcall switch-fn (car buffer-point))
|
|
179 (when (memq (car buffer-point) orig-buffers)
|
|
180 (push-mark orig-point))
|
|
181 (goto-char (cdr buffer-point))
|
|
182 (recenter 0))))
|
|
183
|
|
184 ;;;###autoload
|
|
185 (defun find-function (function)
|
|
186 "Find the definition of the function near point in the current window.
|
|
187
|
|
188 Finds the Emacs Lisp library containing the definition of the function
|
207
|
189 near point (selected by `function-at-point') in a buffer and
|
203
|
190 places point before the definition. Point is saved in the buffer if
|
|
191 it is one of the current buffers.
|
|
192
|
|
193 The library where FUNCTION is defined is searched for in
|
|
194 `find-function-source-path', if non `nil', otherwise in `load-path'."
|
|
195 (interactive (find-function-read-function))
|
|
196 (find-function-do-it function 'switch-to-buffer))
|
|
197
|
|
198 ;;;###autoload
|
|
199 (defun find-function-other-window (function)
|
|
200 "Find the definition of the function near point in the other window.
|
|
201
|
|
202 Finds the Emacs Lisp library containing the definition of the function
|
207
|
203 near point (selected by `function-at-point') in a buffer and
|
203
|
204 places point before the definition. Point is saved in the buffer if
|
|
205 it is one of the current buffers.
|
|
206
|
|
207 The library where FUNCTION is defined is searched for in
|
|
208 `find-function-source-path', if non `nil', otherwise in `load-path'."
|
|
209 (interactive (find-function-read-function))
|
|
210 (find-function-do-it function 'switch-to-buffer-other-window))
|
|
211
|
|
212 ;;;###autoload
|
|
213 (defun find-function-other-frame (function)
|
|
214 "Find the definition of the function near point in the another frame.
|
|
215
|
|
216 Finds the Emacs Lisp library containing the definition of the function
|
207
|
217 near point (selected by `function-at-point') in a buffer and
|
203
|
218 places point before the definition. Point is saved in the buffer if
|
|
219 it is one of the current buffers.
|
|
220
|
|
221 The library where FUNCTION is defined is searched for in
|
|
222 `find-function-source-path', if non `nil', otherwise in `load-path'."
|
|
223 (interactive (find-function-read-function))
|
|
224 (find-function-do-it function 'switch-to-buffer-other-frame))
|
|
225
|
|
226 ;;;###autoload
|
|
227 (defun find-function-on-key (key)
|
|
228 "Find the function that KEY invokes. KEY is a string.
|
|
229 Point is saved if FUNCTION is in the current buffer."
|
|
230 (interactive "kFind function on key: ")
|
|
231 (let ((defn (key-or-menu-binding key)))
|
|
232 (if (or (null defn) (integerp defn))
|
|
233 (message "%s is undefined" (key-description key))
|
|
234 (if (and (consp defn) (not (eq 'lambda (car-safe defn))))
|
|
235 (message "runs %s" (prin1-to-string defn))
|
|
236 (find-function-other-window defn)))))
|
|
237
|
|
238 ;;;###autoload
|
|
239 (defun find-function-at-point ()
|
|
240 "Find directly the function at point in the other window."
|
|
241 (interactive)
|
|
242 (let ((symb (function-at-point)))
|
|
243 (when symb
|
|
244 (find-function-other-window symb))))
|
|
245
|
|
246 ;; (define-key ctl-x-map "F" 'find-function) ; conflicts with `facemenu-keymap'
|
|
247
|
|
248 ;;;###autoload
|
|
249 (define-key ctl-x-4-map "F" 'find-function-other-window)
|
|
250 ;;;###autoload
|
|
251 (define-key ctl-x-5-map "F" 'find-function-other-frame)
|
|
252 ;;;###autoload
|
|
253 (define-key ctl-x-map "K" 'find-function-on-key)
|
|
254
|
|
255 (provide 'find-func)
|
|
256 ;;; find-func.el ends here
|