Commit | Line | Data |
---|---|---|
2cd6a032 | 1 | ;;; find-func.el --- find the definition of the Emacs Lisp function near point |
fffee8be | 2 | |
3731a850 | 3 | ;; Copyright (C) 1997, 1999, 2001, 2002, 2003, 2004, |
ceb4c4d3 | 4 | ;; 2005, 2006 Free Software Foundation, Inc. |
fffee8be DL |
5 | |
6 | ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp> | |
7 | ;; Maintainer: petersen@kurims.kyoto-u.ac.jp | |
2cd6a032 | 8 | ;; Keywords: emacs-lisp, functions, variables |
fffee8be DL |
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 | |
3a35cf56 LK |
25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 | ;; Boston, MA 02110-1301, USA. | |
fffee8be DL |
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!! | |
fffee8be | 32 | ;; ;;; find-func |
86f28125 RS |
33 | ;; (find-function-setup-keys) |
34 | ;; | |
35 | ;; or just: | |
36 | ;; | |
2cd6a032 | 37 | ;; (load "find-func") |
fffee8be | 38 | ;; |
86f28125 | 39 | ;; if you don't like the given keybindings and away you go! It does |
2cd6a032 RS |
40 | ;; pretty much what you would expect, putting the cursor at the |
41 | ;; definition of the function or variable at point. | |
fffee8be | 42 | ;; |
2cd6a032 | 43 | ;; The code started out from `describe-function', `describe-key' |
fffee8be DL |
44 | ;; ("help.el") and `fff-find-loaded-emacs-lisp-function' (Noah Friedman's |
45 | ;; "fff.el"). | |
46 | ||
e8af40ee | 47 | ;;; Code: |
fffee8be | 48 | |
2cd6a032 RS |
49 | (require 'loadhist) |
50 | ||
fffee8be | 51 | ;;; User variables: |
2cd6a032 | 52 | |
0b5bb3ec | 53 | (defgroup find-function nil |
2cd6a032 RS |
54 | "Finds the definition of the Emacs Lisp symbol near point." |
55 | ;; :prefix "find-function" | |
0b5bb3ec | 56 | :group 'lisp) |
fffee8be | 57 | |
54cabb1b SM |
58 | (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+") |
59 | ||
2cd6a032 | 60 | (defcustom find-function-regexp |
d3d4fb62 RS |
61 | ;; Match things like (defun foo ...), (defmacro foo ...), |
62 | ;; (define-skeleton foo ...), (define-generic-mode 'foo ...), | |
d8f479c6 | 63 | ;; (define-derived-mode foo ...), (define-minor-mode foo) |
54cabb1b SM |
64 | (concat |
65 | "^\\s-*(\\(def\\(ine-skeleton\\|ine-generic-mode\\|ine-derived-mode\\|\ | |
aa4a1bf8 | 66 | ine\\(?:-global\\)?-minor-mode\\|ine-compilation-mode\\|un-cvs-mode\\|\ |
8f1a5a9b | 67 | foo\\|[^icfgv]\\(\\w\\|\\s_\\)+\\*?\\)\\|easy-mmode-define-[a-z-]+\\|easy-menu-define\\|\ |
aa4a1bf8 | 68 | menu-bar-make-toggle\\)" |
fe3947c3 | 69 | find-function-space-re |
54cabb1b | 70 | "\\('\\|\(quote \\)?%s\\(\\s-\\|$\\|\(\\|\)\\)") |
d3d4fb62 RS |
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' | |
2cd6a032 | 73 | should insert the function name. The default value avoids `defconst', |
b0339ffd | 74 | `defgroup', `defvar', `defface'. |
fffee8be | 75 | |
2cd6a032 RS |
76 | Please send improvements and fixes to the maintainer." |
77 | :type 'regexp | |
86f28125 | 78 | :group 'find-function |
d8f479c6 | 79 | :version "21.1") |
2cd6a032 RS |
80 | |
81 | (defcustom find-variable-regexp | |
aa4a1bf8 JL |
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-\\|$\\)") | |
2cd6a032 | 87 | "The regexp used by `find-variable' to search for a variable definition. |
0acffda1 RS |
88 | Note it must contain a `%s' at the place where `format' |
89 | should insert the variable name. The default value | |
1b60b25a | 90 | avoids `defun', `defmacro', `defalias', `defadvice', `defgroup', `defface'. |
2cd6a032 RS |
91 | |
92 | Please send improvements and fixes to the maintainer." | |
93 | :type 'regexp | |
86f28125 | 94 | :group 'find-function |
a469e8ba | 95 | :version "21.1") |
fffee8be | 96 | |
0acffda1 RS |
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. | |
102 | ||
103 | Please send improvements and fixes to the maintainer." | |
104 | :type 'regexp | |
105 | :group 'find-function | |
bf247b6e | 106 | :version "22.1") |
0acffda1 RS |
107 | |
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) | |
116 | ||
0b5bb3ec | 117 | (defcustom find-function-source-path nil |
d3d4fb62 | 118 | "The default list of directories where `find-function' searches. |
fffee8be | 119 | |
d3d4fb62 | 120 | If this variable is nil then `find-function' searches `load-path' by |
0b5bb3ec SE |
121 | default." |
122 | :type '(repeat directory) | |
123 | :group 'find-function) | |
fffee8be | 124 | |
2cd6a032 RS |
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. | |
cb0fd101 JPW |
128 | See `find-function' and `find-variable'." |
129 | :type '(choice (const :tag "Center" nil) | |
130 | integer) | |
86f28125 | 131 | :group 'find-function |
a6223467 | 132 | :version "20.3") |
2cd6a032 RS |
133 | |
134 | (defcustom find-function-after-hook nil | |
135 | "Hook run after finding symbol definition. | |
136 | ||
137 | See the functions `find-function' and `find-variable'." | |
86f28125 | 138 | :group 'find-function |
a6223467 | 139 | :version "20.3") |
2cd6a032 RS |
140 | |
141 | ;;; Functions: | |
142 | ||
f77a6050 SM |
143 | (defun find-library-suffixes () |
144 | (let ((suffixes nil)) | |
f352b9d7 | 145 | (dolist (suffix (get-load-suffixes) (nreverse suffixes)) |
f77a6050 SM |
146 | (unless (string-match "elc" suffix) (push suffix suffixes))))) |
147 | ||
148 | (defun find-library-name (library) | |
b44da9f1 | 149 | "Return the absolute file name of the Lisp source of LIBRARY." |
53aaf457 RF |
150 | ;; Strip off the extension to take advantage of library suffixes in |
151 | ;; the call to `locate-file'. | |
152 | (if (string-match "\\.el\\(c\\(\\..*\\)?\\)?\\'" library) | |
1a40d81d | 153 | (setq library (replace-match "" t t library))) |
f77a6050 SM |
154 | (or (locate-file library |
155 | (or find-function-source-path load-path) | |
f352b9d7 | 156 | (append (find-library-suffixes) load-file-rep-suffixes)) |
4dbf594b | 157 | (error "Can't find library %s" library))) |
f77a6050 | 158 | |
4479a2f8 SM |
159 | (defvar find-function-C-source-directory |
160 | (let ((dir (expand-file-name "src" source-directory))) | |
161 | (when (and (file-directory-p dir) (file-readable-p dir)) | |
162 | dir)) | |
163 | "Directory where the C source files of Emacs can be found. | |
164 | If nil, do not try to find the source code of functions and variables | |
165 | defined in C.") | |
166 | ||
0acffda1 | 167 | (defun find-function-C-source (fun-or-var file type) |
3bdedbcb | 168 | "Find the source location where FUN-OR-VAR is defined in FILE. |
0acffda1 | 169 | TYPE should be nil to find a function, or `defvar' to find a variable." |
4479a2f8 SM |
170 | (unless find-function-C-source-directory |
171 | (setq find-function-C-source-directory | |
172 | (read-directory-name "Emacs C source dir: " nil nil t))) | |
173 | (setq file (expand-file-name file find-function-C-source-directory)) | |
174 | (unless (file-readable-p file) | |
175 | (error "The C source file %s is not available" | |
176 | (file-name-nondirectory file))) | |
0acffda1 | 177 | (unless type |
4479a2f8 SM |
178 | (setq fun-or-var (indirect-function fun-or-var))) |
179 | (with-current-buffer (find-file-noselect file) | |
180 | (goto-char (point-min)) | |
181 | (unless (re-search-forward | |
0acffda1 | 182 | (if type |
4479a2f8 SM |
183 | (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\"" |
184 | (regexp-quote (symbol-name fun-or-var)) | |
185 | "\"") | |
186 | (concat "DEFUN[ \t\n]*([ \t\n]*\"" | |
187 | (regexp-quote (subr-name fun-or-var)) | |
188 | "\"")) | |
189 | nil t) | |
190 | (error "Can't find source for %s" fun-or-var)) | |
191 | (cons (current-buffer) (match-beginning 0)))) | |
192 | ||
f77a6050 SM |
193 | ;;;###autoload |
194 | (defun find-library (library) | |
195 | "Find the elisp source of LIBRARY." | |
196 | (interactive | |
197 | (list | |
198 | (completing-read "Library name: " | |
199 | 'locate-file-completion | |
200 | (cons (or find-function-source-path load-path) | |
201 | (find-library-suffixes))))) | |
202 | (let ((buf (find-file-noselect (find-library-name library)))) | |
203 | (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf))))) | |
204 | ||
a4f43048 | 205 | ;;;###autoload |
0acffda1 RS |
206 | (defun find-function-search-for-symbol (symbol type library) |
207 | "Search for SYMBOL's definition of type TYPE in LIBRARY. | |
39ff5f64 RS |
208 | Visit the library in a buffer, and return a cons cell (BUFFER . POSITION), |
209 | or just (BUFFER . nil) if the definition can't be found in the file. | |
210 | ||
0acffda1 RS |
211 | If TYPE is nil, look for a function definition. |
212 | Otherwise, TYPE specifies the kind of definition, | |
213 | and it is interpreted via `find-function-regexp-alist'. | |
214 | The search is done in the source for library LIBRARY." | |
2cd6a032 RS |
215 | (if (null library) |
216 | (error "Don't know where `%s' is defined" symbol)) | |
2bef95e5 RS |
217 | ;; Some functions are defined as part of the construct |
218 | ;; that defines something else. | |
4479a2f8 | 219 | (while (and (symbolp symbol) (get symbol 'definition-name)) |
2bef95e5 | 220 | (setq symbol (get symbol 'definition-name))) |
4479a2f8 | 221 | (if (string-match "\\`src/\\(.*\\.c\\)\\'" library) |
0acffda1 | 222 | (find-function-C-source symbol (match-string 1 library) type) |
78b6524e DL |
223 | (if (string-match "\\.el\\(c\\)\\'" library) |
224 | (setq library (substring library 0 (match-beginning 1)))) | |
0acffda1 RS |
225 | (let* ((filename (find-library-name library)) |
226 | (regexp-symbol (cdr (assq type find-function-regexp-alist)))) | |
78b6524e | 227 | (with-current-buffer (find-file-noselect filename) |
0acffda1 | 228 | (let ((regexp (format (symbol-value regexp-symbol) |
35b6eb23 NR |
229 | ;; Entry for ` (backquote) macro in loaddefs.el, |
230 | ;; (defalias (quote \`)..., has a \ but | |
231 | ;; (symbol-name symbol) doesn't. Add an | |
232 | ;; optional \ to catch this. | |
73584626 NR |
233 | (concat "\\\\?" |
234 | (regexp-quote (symbol-name symbol))))) | |
77186c62 | 235 | (case-fold-search)) |
fe75e6d0 MR |
236 | (with-syntax-table emacs-lisp-mode-syntax-table |
237 | (goto-char (point-min)) | |
238 | (if (or (re-search-forward regexp nil t) | |
a04e10bc SM |
239 | ;; `regexp' matches definitions using known forms like |
240 | ;; `defun', or `defvar'. But some functions/variables | |
241 | ;; are defined using special macros (or functions), so | |
242 | ;; if `regexp' can't find the definition, we look for | |
243 | ;; something of the form "(SOMETHING <symbol> ...)". | |
244 | ;; This fails to distinguish function definitions from | |
245 | ;; variable declarations (or even uses thereof), but is | |
246 | ;; a good pragmatic fallback. | |
fe75e6d0 | 247 | (re-search-forward |
2b690e5b | 248 | (concat "^([^ ]+" find-function-space-re "['(]?" |
fe75e6d0 | 249 | (regexp-quote (symbol-name symbol)) |
b0339ffd | 250 | "\\_>") |
fe75e6d0 MR |
251 | nil t)) |
252 | (progn | |
253 | (beginning-of-line) | |
254 | (cons (current-buffer) (point))) | |
39ff5f64 | 255 | (cons (current-buffer) nil)))))))) |
2cd6a032 | 256 | |
8a8a9abe | 257 | ;;;###autoload |
2cd6a032 | 258 | (defun find-function-noselect (function) |
d3d4fb62 | 259 | "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION. |
fffee8be | 260 | |
3bdedbcb | 261 | Finds the source file containing the definition of FUNCTION |
2cd6a032 | 262 | in a buffer and the point of the definition. The buffer is |
39ff5f64 RS |
263 | not selected. If the function definition can't be found in |
264 | the buffer, returns (BUFFER). | |
fffee8be | 265 | |
2cd6a032 | 266 | If the file where FUNCTION is defined is not known, then it is |
d3d4fb62 | 267 | searched for in `find-function-source-path' if non nil, otherwise |
2cd6a032 | 268 | in `load-path'." |
fffee8be DL |
269 | (if (not function) |
270 | (error "You didn't specify a function")) | |
271 | (let ((def (symbol-function function)) | |
2cd6a032 | 272 | aliases) |
fffee8be DL |
273 | (while (symbolp def) |
274 | (or (eq def function) | |
275 | (if aliases | |
276 | (setq aliases (concat aliases | |
2cd6a032 | 277 | (format ", which is an alias for `%s'" |
fffee8be | 278 | (symbol-name def)))) |
2cd6a032 RS |
279 | (setq aliases (format "`%s' an alias for `%s'" |
280 | function (symbol-name def))))) | |
fffee8be DL |
281 | (setq function (symbol-function function) |
282 | def (symbol-function function))) | |
283 | (if aliases | |
55aed120 | 284 | (message "%s" aliases)) |
2cd6a032 RS |
285 | (let ((library |
286 | (cond ((eq (car-safe def) 'autoload) | |
287 | (nth 1 def)) | |
6dfa731f MY |
288 | ((subrp def) |
289 | (help-C-file-name def 'subr)) | |
9f14330b | 290 | ((symbol-file function 'defun))))) |
2cd6a032 | 291 | (find-function-search-for-symbol function nil library)))) |
fffee8be | 292 | |
0acffda1 | 293 | (defun find-function-read (&optional type) |
2cd6a032 | 294 | "Read and return an interned symbol, defaulting to the one near point. |
fffee8be | 295 | |
0acffda1 RS |
296 | If TYPE is nil, insist on a symbol with a function definition. |
297 | Otherwise TYPE should be `defvar' or `defface'. | |
298 | If TYPE is nil, defaults using `function-called-at-point', | |
299 | otherwise uses `variable-at-point'." | |
300 | (let ((symb (if (null type) | |
301 | (function-called-at-point) | |
302 | (if (eq type 'defvar) | |
303 | (variable-at-point) | |
304 | (variable-at-point t)))) | |
305 | (predicate (cdr (assq type '((nil . fboundp) (defvar . boundp) | |
306 | (defface . facep))))) | |
307 | (prompt (cdr (assq type '((nil . "function") (defvar . "variable") | |
308 | (defface . "face"))))) | |
fffee8be DL |
309 | (enable-recursive-minibuffers t) |
310 | val) | |
2cd6a032 RS |
311 | (if (equal symb 0) |
312 | (setq symb nil)) | |
0acffda1 RS |
313 | (setq val (completing-read |
314 | (concat "Find " | |
315 | prompt | |
316 | (if symb | |
317 | (format " (default %s)" symb)) | |
318 | ": ") | |
319 | obarray predicate t nil)) | |
fffee8be | 320 | (list (if (equal val "") |
2cd6a032 RS |
321 | symb |
322 | (intern val))))) | |
fffee8be | 323 | |
0acffda1 | 324 | (defun find-function-do-it (symbol type switch-fn) |
d3d4fb62 | 325 | "Find Emacs Lisp SYMBOL in a buffer and display it. |
0acffda1 RS |
326 | TYPE is nil to search for a function definition, |
327 | or else `defvar' or `defface'. | |
328 | ||
329 | The variable `find-function-recenter-line' controls how | |
330 | to recenter the display. SWITCH-FN is the function to call | |
331 | to display and select the buffer. | |
332 | See also `find-function-after-hook'. | |
2cd6a032 | 333 | |
0acffda1 | 334 | Set mark before moving, if the buffer already existed." |
86f28125 RS |
335 | (let* ((orig-point (point)) |
336 | (orig-buf (window-buffer)) | |
2cd6a032 | 337 | (orig-buffers (buffer-list)) |
86f28125 | 338 | (buffer-point (save-excursion |
0acffda1 | 339 | (find-definition-noselect symbol type))) |
86f28125 RS |
340 | (new-buf (car buffer-point)) |
341 | (new-point (cdr buffer-point))) | |
2cd6a032 | 342 | (when buffer-point |
86f28125 | 343 | (when (memq new-buf orig-buffers) |
2cd6a032 | 344 | (push-mark orig-point)) |
86f28125 | 345 | (funcall switch-fn new-buf) |
39ff5f64 | 346 | (when new-point (goto-char new-point)) |
2cd6a032 | 347 | (recenter find-function-recenter-line) |
8d8ca3cf | 348 | (run-hooks 'find-function-after-hook)))) |
fffee8be | 349 | |
b07745ec | 350 | ;;;###autoload |
2cd6a032 | 351 | (defun find-function (function) |
c7efc289 | 352 | "Find the definition of the FUNCTION near point. |
fffee8be | 353 | |
3bdedbcb | 354 | Finds the source file containing the definition of the function |
436c08c2 | 355 | near point (selected by `function-called-at-point') in a buffer and |
0acffda1 RS |
356 | places point before the definition. |
357 | Set mark before moving, if the buffer already existed. | |
fffee8be | 358 | |
2cd6a032 | 359 | The library where FUNCTION is defined is searched for in |
d3d4fb62 | 360 | `find-function-source-path', if non nil, otherwise in `load-path'. |
2cd6a032 RS |
361 | See also `find-function-recenter-line' and `find-function-after-hook'." |
362 | (interactive (find-function-read)) | |
363 | (find-function-do-it function nil 'switch-to-buffer)) | |
fffee8be | 364 | |
b07745ec | 365 | ;;;###autoload |
2cd6a032 | 366 | (defun find-function-other-window (function) |
c7efc289 | 367 | "Find, in another window, the definition of FUNCTION near point. |
fffee8be | 368 | |
2cd6a032 RS |
369 | See `find-function' for more details." |
370 | (interactive (find-function-read)) | |
371 | (find-function-do-it function nil 'switch-to-buffer-other-window)) | |
fffee8be | 372 | |
b07745ec | 373 | ;;;###autoload |
2cd6a032 | 374 | (defun find-function-other-frame (function) |
3bdedbcb | 375 | "Find, in another frame, the definition of FUNCTION near point. |
fffee8be | 376 | |
2cd6a032 RS |
377 | See `find-function' for more details." |
378 | (interactive (find-function-read)) | |
379 | (find-function-do-it function nil 'switch-to-buffer-other-frame)) | |
380 | ||
e1acbda2 | 381 | ;;;###autoload |
1c787e34 | 382 | (defun find-variable-noselect (variable &optional file) |
3bdedbcb | 383 | "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE. |
2cd6a032 | 384 | |
3bdedbcb EZ |
385 | Finds the library containing the definition of VARIABLE in a buffer and |
386 | the point of the definition. The buffer is not selected. | |
39ff5f64 | 387 | If the variable's definition can't be found in the buffer, return (BUFFER). |
2cd6a032 | 388 | |
1c787e34 | 389 | The library where VARIABLE is defined is searched for in FILE or |
d3d4fb62 | 390 | `find-function-source-path', if non nil, otherwise in `load-path'." |
2cd6a032 | 391 | (if (not variable) |
3bdedbcb EZ |
392 | (error "You didn't specify a variable") |
393 | (let ((library (or file | |
394 | (symbol-file variable 'defvar) | |
395 | (help-C-file-name variable 'var)))) | |
396 | (find-function-search-for-symbol variable 'defvar library)))) | |
2cd6a032 RS |
397 | |
398 | ;;;###autoload | |
399 | (defun find-variable (variable) | |
c7efc289 | 400 | "Find the definition of the VARIABLE near point. |
2cd6a032 | 401 | |
3bdedbcb | 402 | Finds the library containing the definition of the variable |
2cd6a032 | 403 | near point (selected by `variable-at-point') in a buffer and |
0acffda1 RS |
404 | places point before the definition. |
405 | ||
406 | Set mark before moving, if the buffer already existed. | |
2cd6a032 RS |
407 | |
408 | The library where VARIABLE is defined is searched for in | |
d3d4fb62 | 409 | `find-function-source-path', if non nil, otherwise in `load-path'. |
2cd6a032 | 410 | See also `find-function-recenter-line' and `find-function-after-hook'." |
436c08c2 RS |
411 | (interactive (find-function-read 'defvar)) |
412 | (find-function-do-it variable 'defvar 'switch-to-buffer)) | |
fffee8be | 413 | |
2cd6a032 RS |
414 | ;;;###autoload |
415 | (defun find-variable-other-window (variable) | |
c7efc289 | 416 | "Find, in another window, the definition of VARIABLE near point. |
2cd6a032 RS |
417 | |
418 | See `find-variable' for more details." | |
436c08c2 RS |
419 | (interactive (find-function-read 'defvar)) |
420 | (find-function-do-it variable 'defvar 'switch-to-buffer-other-window)) | |
2cd6a032 RS |
421 | |
422 | ;;;###autoload | |
423 | (defun find-variable-other-frame (variable) | |
3bdedbcb | 424 | "Find, in another frame, the definition of VARIABLE near point. |
2cd6a032 RS |
425 | |
426 | See `find-variable' for more details." | |
436c08c2 RS |
427 | (interactive (find-function-read 'defvar)) |
428 | (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame)) | |
fffee8be | 429 | |
0acffda1 RS |
430 | ;;;###autoload |
431 | (defun find-definition-noselect (symbol type &optional file) | |
432 | "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL. | |
39ff5f64 | 433 | If the definition can't be found in the buffer, return (BUFFER). |
3bdedbcb | 434 | TYPE says what type of definition: nil for a function, `defvar' for a |
c67a6c48 | 435 | variable, `defface' for a face. This function does not switch to the |
3bdedbcb | 436 | buffer nor display it. |
0acffda1 RS |
437 | |
438 | The library where SYMBOL is defined is searched for in FILE or | |
439 | `find-function-source-path', if non nil, otherwise in `load-path'." | |
3bdedbcb EZ |
440 | (cond |
441 | ((not symbol) | |
442 | (error "You didn't specify a symbol")) | |
443 | ((null type) | |
444 | (find-function-noselect symbol)) | |
445 | ((eq type 'defvar) | |
446 | (find-variable-noselect symbol file)) | |
447 | (t | |
0acffda1 | 448 | (let ((library (or file (symbol-file symbol type)))) |
3bdedbcb | 449 | (find-function-search-for-symbol symbol type library))))) |
0acffda1 | 450 | |
c7ce7fc4 RS |
451 | ;; For symmetry, this should be called find-face; but some programs |
452 | ;; assume that, if that name is defined, it means something else. | |
0acffda1 | 453 | ;;;###autoload |
c7ce7fc4 | 454 | (defun find-face-definition (face) |
0acffda1 RS |
455 | "Find the definition of FACE. FACE defaults to the name near point. |
456 | ||
457 | Finds the Emacs Lisp library containing the definition of the face | |
458 | near point (selected by `variable-at-point') in a buffer and | |
459 | places point before the definition. | |
460 | ||
461 | Set mark before moving, if the buffer already existed. | |
462 | ||
463 | The library where FACE is defined is searched for in | |
464 | `find-function-source-path', if non nil, otherwise in `load-path'. | |
465 | See also `find-function-recenter-line' and `find-function-after-hook'." | |
466 | (interactive (find-function-read 'defface)) | |
467 | (find-function-do-it face 'defface 'switch-to-buffer)) | |
468 | ||
b07745ec | 469 | ;;;###autoload |
fffee8be DL |
470 | (defun find-function-on-key (key) |
471 | "Find the function that KEY invokes. KEY is a string. | |
0acffda1 | 472 | Set mark before moving, if the buffer already existed." |
fffee8be | 473 | (interactive "kFind function on key: ") |
cb4f3fc8 RS |
474 | (let (defn) |
475 | (save-excursion | |
476 | (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below. | |
477 | (start (event-start event)) | |
478 | (modifiers (event-modifiers event)) | |
479 | (window (and (or (memq 'click modifiers) (memq 'down modifiers) | |
480 | (memq 'drag modifiers)) | |
481 | (posn-window start)))) | |
482 | ;; For a mouse button event, go to the button it applies to | |
483 | ;; to get the right key bindings. And go to the right place | |
484 | ;; in case the keymap depends on where you clicked. | |
485 | (when (windowp window) | |
486 | (set-buffer (window-buffer window)) | |
487 | (goto-char (posn-point start))) | |
488 | (setq defn (key-binding key)))) | |
489 | (let ((key-desc (key-description key))) | |
490 | (if (or (null defn) (integerp defn)) | |
491 | (message "%s is unbound" key-desc) | |
492 | (if (consp defn) | |
493 | (message "%s runs %s" key-desc (prin1-to-string defn)) | |
494 | (find-function-other-window defn)))))) | |
2cd6a032 RS |
495 | |
496 | ;;;###autoload | |
497 | (defun find-function-at-point () | |
498 | "Find directly the function at point in the other window." | |
499 | (interactive) | |
436c08c2 | 500 | (let ((symb (function-called-at-point))) |
2cd6a032 RS |
501 | (when symb |
502 | (find-function-other-window symb)))) | |
503 | ||
504 | ;;;###autoload | |
505 | (defun find-variable-at-point () | |
3bdedbcb | 506 | "Find directly the variable at point in the other window." |
2cd6a032 RS |
507 | (interactive) |
508 | (let ((symb (variable-at-point))) | |
509 | (when (and symb (not (equal symb 0))) | |
510 | (find-variable-other-window symb)))) | |
fffee8be | 511 | |
86f28125 RS |
512 | ;;;###autoload |
513 | (defun find-function-setup-keys () | |
514 | "Define some key bindings for the find-function family of functions." | |
515 | (define-key ctl-x-map "F" 'find-function) | |
516 | (define-key ctl-x-4-map "F" 'find-function-other-window) | |
517 | (define-key ctl-x-5-map "F" 'find-function-other-frame) | |
518 | (define-key ctl-x-map "K" 'find-function-on-key) | |
519 | (define-key ctl-x-map "V" 'find-variable) | |
520 | (define-key ctl-x-4-map "V" 'find-variable-other-window) | |
521 | (define-key ctl-x-5-map "V" 'find-variable-other-frame)) | |
522 | ||
fffee8be DL |
523 | (provide 'find-func) |
524 | ||
1b60b25a | 525 | ;; arch-tag: 43ecd81c-74dc-4d9a-8f63-a61e55670d64 |
fffee8be | 526 | ;;; find-func.el ends here |