annotate lisp/emacs-lisp/find-func.el @ 22180:485917486caf

Indent `with-standard-io-syntax' correctly.
author Richard M. Stallman <rms@gnu.org>
date Fri, 22 May 1998 04:42:48 +0000
parents 92d6cd3eebfa
children 65cdb6a99775
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
1 ;;; find-func.el --- find the definition of the elisp function near point
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
2
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
4
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
5 ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp>
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
6 ;; Maintainer: petersen@kurims.kyoto-u.ac.jp
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
7 ;; Keywords: emacs-lisp, help, functions
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
8 ;; Created: 97/07/25
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
9
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
11
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
15 ;; any later version.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
16
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
20 ;; GNU General Public License for more details.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
21
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
25 ;; Boston, MA 02111-1307, USA.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
26
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
27 ;;; Commentary:
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
28 ;;
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
29 ;; The funniest thing about this is that I can't imagine why a package
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
30 ;; so obviously useful as this hasn't been written before!!
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
31 ;; This probably belongs in "help.el" or somewhere like that.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
32 ;;
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
33 ;; Put this file in your `load-path', byte-compile it and add the
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
34 ;; following code in your init file:
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
35 ;;
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
36 ;; ;;; find-func
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
37 ;; (load "find-function")
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
38 ;; (global-set-key [(control ?c) ?f] 'find-function)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
39 ;; (global-set-key [(control ?c) ?4 ?f] 'find-function-other-window)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
40 ;; (global-set-key [(control ?c) ?5 ?f] 'find-function-other-frame)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
41 ;; (global-set-key [(control ?c) ?k] 'find-function-on-key)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
42 ;;
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
43 ;; and away you go! It does pretty much what you would expect,
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
44 ;; putting the cursor at the definition of the function at point.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
45 ;;
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
46 ;; The code is adapted from `describe-function', `describe-key'
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
47 ;; ("help.el") and `fff-find-loaded-emacs-lisp-function' (Noah Friedman's
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
48 ;; "fff.el").
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
49
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
50 ;;; To do:
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
51 ;;
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
52 ;; o custom?
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
53 ;;
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
54 ;; o improve handling of advice'd functions? (at the moment it goes to
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
55 ;; the advice, not the actual definition)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
56
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
57 ;;;; Code:
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
58
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
59 ;;; User variables:
20962
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
60 (defgroup find-function nil
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
61 "Find the definition of the elisp function near point."
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
62 :prefix "find-function"
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
63 :group 'lisp)
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
64
20962
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
65 (defcustom find-function-function 'function-at-point
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
66 "*The function used by `find-function' to select the function near
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
67 point.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
68
20962
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
69 For example `function-at-point' or `function-called-at-point'."
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
70 :type 'function
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
71 :group 'find-function)
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
72
20962
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
73 (defcustom find-function-source-path nil
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
74 "The default list of directories where find-function searches.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
75
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
76 If this variable is `nil' then find-function searches `load-path' by
20962
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
77 default."
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
78 :type '(repeat directory)
54413501e4a9 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 20184
diff changeset
79 :group 'find-function)
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
80
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
81
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
82 ;;; Functions:
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
83
22007
92d6cd3eebfa (find-function-noselect): Autoload it.
Richard M. Stallman <rms@gnu.org>
parents: 21586
diff changeset
84 ;;;###autoload
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
85 (defun find-function-noselect (function &optional path)
21586
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
86 "Returns list (BUFFER POINT) pointing to the definition of FUNCTION.
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
87
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
88 Finds the Emacs Lisp library containing the definition of FUNCTION
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
89 in a buffer and places point before the definition. The buffer is
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
90 not selected.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
91
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
92 If the optional argument PATH is given, the library where FUNCTION is
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
93 defined is searched in PATH instead of `load-path' (see
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
94 `find-function-source-path')."
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
95 (and (subrp (symbol-function function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
96 (error "%s is a primitive function" function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
97 (if (not function)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
98 (error "You didn't specify a function"))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
99 (let ((def (symbol-function function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
100 library aliases)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
101 (while (symbolp def)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
102 (or (eq def function)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
103 (if aliases
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
104 (setq aliases (concat aliases
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
105 (format ", which is an alias for %s"
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
106 (symbol-name def))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
107 (setq aliases (format "an alias for %s" (symbol-name
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
108 def)))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
109 (setq function (symbol-function function)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
110 def (symbol-function function)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
111 (if aliases
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
112 (message aliases))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
113 (setq library
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
114 (cond ((eq (car-safe def) 'autoload)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
115 (nth 1 def))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
116 ((describe-function-find-file function))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
117 (if (null library)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
118 (error (format "`%s' is not in `load-history'" function)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
119 (if (string-match "\\(\\.elc?\\'\\)" library)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
120 (setq library (substring library 0 (match-beginning 1))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
121 (let* ((path (or path find-function-source-path))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
122 (compression (or (rassq 'jka-compr-handler file-name-handler-alist)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
123 (member 'crypt-find-file-hook find-file-hooks)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
124 (filename (or (locate-library (concat library ".el")
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
125 t path)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
126 (locate-library library t path)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
127 (if compression
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
128 (or (locate-library (concat library ".el.gz")
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
129 t path)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
130 (locate-library (concat library ".gz")
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
131 t path))))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
132 (if (not filename)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
133 (error "The library \"%s\" is not in the path." library))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
134 (save-excursion
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
135 (set-buffer (find-file-noselect filename))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
136 (save-match-data
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
137 (let (;; avoid defconst, defgroup, defvar (any others?)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
138 (regexp (format "^\\s-*(def[^cgv\W]\\w+\\s-+%s\\s-" function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
139 (syntable (syntax-table)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
140 (set-syntax-table emacs-lisp-mode-syntax-table)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
141 (goto-char (point-min))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
142 (if (prog1
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
143 (re-search-forward regexp nil t)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
144 (set-syntax-table syntable))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
145 (progn
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
146 (beginning-of-line)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
147 (list (current-buffer) (point)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
148 (error "Cannot find definition of %s" function))))))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
149
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
150 (defun function-at-point ()
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
151 (or (condition-case ()
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
152 (let ((stab (syntax-table)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
153 (unwind-protect
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
154 (save-excursion
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
155 (set-syntax-table emacs-lisp-mode-syntax-table)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
156 (or (not (zerop (skip-syntax-backward "_w")))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
157 (eq (char-syntax (char-after (point))) ?w)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
158 (eq (char-syntax (char-after (point))) ?_)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
159 (forward-sexp -1))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
160 (skip-chars-forward "`'")
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
161 (let ((obj (read (current-buffer))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
162 (and (symbolp obj) (fboundp obj) obj)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
163 (set-syntax-table stab)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
164 (error nil))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
165 (condition-case ()
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
166 (save-excursion
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
167 (save-restriction
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
168 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
169 (backward-up-list 1)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
170 (forward-char 1)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
171 (let (obj)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
172 (setq obj (read (current-buffer)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
173 (and (symbolp obj) (fboundp obj) obj))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
174 (error nil))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
175
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
176 (defun find-function-read-function ()
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
177 "Read and return a function, defaulting to the one near point.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
178
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
179 The function named by `find-function-function' is used to select the
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
180 default function."
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
181 (let ((fn (funcall find-function-function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
182 (enable-recursive-minibuffers t)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
183 val)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
184 (setq val (completing-read
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
185 (if fn
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
186 (format "Find function (default %s): " fn)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
187 "Find function: ")
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
188 obarray 'fboundp t))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
189 (list (if (equal val "")
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
190 fn (intern val)))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
191
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
192 (defun find-function-do-it (function path switch-fn)
21586
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
193 "Find Emacs Lisp FUNCTION in PATH and display it with SWITCH-FN.
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
194 Point is saved if FUNCTION is in the current buffer."
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
195 (let ((orig-point (point))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
196 (buffer-point (find-function-noselect function path)))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
197 (if buffer-point
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
198 (progn
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
199 (if (eq (current-buffer) (car buffer-point))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
200 (push-mark orig-point))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
201 (funcall switch-fn (car buffer-point))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
202 (goto-char (elt buffer-point 1))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
203 (recenter 0)))))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
204
20184
c8d4024e07de (find-function, find-function-other-window, find-function-other-frame,
Dave Love <fx@gnu.org>
parents: 20183
diff changeset
205 ;;;###autoload
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
206 (defun find-function (function &optional path)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
207 "Find the definition of the function near point in the current window.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
208
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
209 Finds the Emacs Lisp library containing the definition of the function
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
210 near point (selected by `find-function-function') and places point
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
211 before the definition. Point is saved if FUNCTION is in the current
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
212 buffer.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
213
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
214 If the optional argument PATH is given, the library where FUNCTION is
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
215 defined is searched in PATH instead of `load-path'"
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
216 (interactive (find-function-read-function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
217 (find-function-do-it function path 'switch-to-buffer))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
218
20184
c8d4024e07de (find-function, find-function-other-window, find-function-other-frame,
Dave Love <fx@gnu.org>
parents: 20183
diff changeset
219 ;;;###autoload
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
220 (defun find-function-other-window (function &optional path)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
221 "Find the definition of the function near point in the other window.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
222
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
223 Finds the Emacs Lisp package containing the definition of the function
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
224 near point (selected by `find-function-function') and places point
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
225 before the definition. Point is saved if FUNCTION is in the current
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
226 buffer.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
227
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
228 If the optional argument PATH is given, the package where FUNCTION is
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
229 defined is searched in PATH instead of `load-path'"
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
230 (interactive (find-function-read-function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
231 (find-function-do-it function path 'switch-to-buffer-other-window))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
232
20184
c8d4024e07de (find-function, find-function-other-window, find-function-other-frame,
Dave Love <fx@gnu.org>
parents: 20183
diff changeset
233 ;;;###autoload
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
234 (defun find-function-other-frame (function &optional path)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
235 "Find the definition of the function near point in the another frame.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
236
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
237 Finds the Emacs Lisp package containing the definition of the function
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
238 near point (selected by `find-function-function') and places point
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
239 before the definition. Point is saved if FUNCTION is in the current
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
240 buffer.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
241
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
242 If the optional argument PATH is given, the package where FUNCTION is
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
243 defined is searched in PATH instead of `load-path'"
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
244 (interactive (find-function-read-function))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
245 (find-function-do-it function path 'switch-to-buffer-other-frame))
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
246
20184
c8d4024e07de (find-function, find-function-other-window, find-function-other-frame,
Dave Love <fx@gnu.org>
parents: 20183
diff changeset
247 ;;;###autoload
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
248 (defun find-function-on-key (key)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
249 "Find the function that KEY invokes. KEY is a string.
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
250 Point is saved if FUNCTION is in the current buffer."
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
251 (interactive "kFind function on key: ")
21586
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
252 ;; If this key seq ends with a down event, discard the
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
253 ;; following click or drag event. Otherwise that would
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
254 ;; erase an eventual message.
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
255 (let ((type (aref key (1- (length key)))))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
256 (if (listp type) (setq type (car type)))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
257 (and (symbolp type)
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
258 (memq 'down (event-modifiers type))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
259 (read-event)))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
260 (save-excursion
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
261 (let ((modifiers (event-modifiers (aref key 0)))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
262 window position)
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
263 ;; For a mouse button event, go to the button it applies to
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
264 ;; to get the right key bindings. And go to the right place
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
265 ;; in case the keymap depends on where you clicked.
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
266 (if (or (memq 'click modifiers) (memq 'down modifiers)
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
267 (memq 'drag modifiers))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
268 (setq window (posn-window (event-start (aref key 0)))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
269 position (posn-point (event-start (aref key 0)))))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
270 (if (windowp window)
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
271 (progn
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
272 (set-buffer (window-buffer window))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
273 (goto-char position)))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
274 ;; Ok, now look up the key and name the command.
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
275 (let ((defn (key-binding key)))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
276 (if (or (null defn) (integerp defn))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
277 (message "%s is undefined" (key-description key))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
278 (if (consp defn)
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
279 (message (if (windowp window)
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
280 "%s at that spot runs %s"
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
281 "%s runs %s")
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
282 (key-description key) (prin1-to-string defn))
f6a01ea8441e (find-function-on-key):
Richard M. Stallman <rms@gnu.org>
parents: 20962
diff changeset
283 (find-function-other-window defn)))))))
20183
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
284
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
285 (provide 'find-func)
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
286
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
287 ;;; find-func.el ends here
add8b7b3a7da Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
288