comparison lisp/emacs-lisp/find-func.el @ 55231:7fc34677dcb8

(find-function-C-source-directory): New var. (find-function-C-source): New fun. (find-function-search-for-symbol): Use it.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Thu, 29 Apr 2004 18:39:14 +0000
parents 695cf19ef79e
children 212733e53508 4c90ffeb71c5
comparison
equal deleted inserted replaced
55230:c41874c7d876 55231:7fc34677dcb8
1 ;;; find-func.el --- find the definition of the Emacs Lisp function near point 1 ;;; find-func.el --- find the definition of the Emacs Lisp function near point
2 2
3 ;; Copyright (C) 1997, 1999, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1997, 1999, 2001, 2004 Free Software Foundation, Inc.
4 4
5 ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp> 5 ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp>
6 ;; Maintainer: petersen@kurims.kyoto-u.ac.jp 6 ;; Maintainer: petersen@kurims.kyoto-u.ac.jp
7 ;; Keywords: emacs-lisp, functions, variables 7 ;; Keywords: emacs-lisp, functions, variables
8 ;; Created: 97/07/25 8 ;; Created: 97/07/25
126 (or (locate-file library 126 (or (locate-file library
127 (or find-function-source-path load-path) 127 (or find-function-source-path load-path)
128 (append (find-library-suffixes) '(""))) 128 (append (find-library-suffixes) '("")))
129 (error "Can't find library %s" library))) 129 (error "Can't find library %s" library)))
130 130
131 (defvar find-function-C-source-directory
132 (let ((dir (expand-file-name "src" source-directory)))
133 (when (and (file-directory-p dir) (file-readable-p dir))
134 dir))
135 "Directory where the C source files of Emacs can be found.
136 If nil, do not try to find the source code of functions and variables
137 defined in C.")
138
139 (defun find-function-C-source (fun-or-var file variable-p)
140 "Find the source location where SUBR-OR-VAR is defined in FILE.
141 VARIABLE-P should be non-nil for a variable or nil for a subroutine."
142 (unless find-function-C-source-directory
143 (setq find-function-C-source-directory
144 (read-directory-name "Emacs C source dir: " nil nil t)))
145 (setq file (expand-file-name file find-function-C-source-directory))
146 (unless (file-readable-p file)
147 (error "The C source file %s is not available"
148 (file-name-nondirectory file)))
149 (unless variable-p
150 (setq fun-or-var (indirect-function fun-or-var)))
151 (with-current-buffer (find-file-noselect file)
152 (goto-char (point-min))
153 (unless (re-search-forward
154 (if variable-p
155 (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
156 (regexp-quote (symbol-name fun-or-var))
157 "\"")
158 (concat "DEFUN[ \t\n]*([ \t\n]*\""
159 (regexp-quote (subr-name fun-or-var))
160 "\""))
161 nil t)
162 (error "Can't find source for %s" fun-or-var))
163 (cons (current-buffer) (match-beginning 0))))
164
131 ;;;###autoload 165 ;;;###autoload
132 (defun find-library (library) 166 (defun find-library (library)
133 "Find the elisp source of LIBRARY." 167 "Find the elisp source of LIBRARY."
134 (interactive 168 (interactive
135 (list 169 (list
147 `find-variable-regexp' is used. The search is done in library LIBRARY." 181 `find-variable-regexp' is used. The search is done in library LIBRARY."
148 (if (null library) 182 (if (null library)
149 (error "Don't know where `%s' is defined" symbol)) 183 (error "Don't know where `%s' is defined" symbol))
150 ;; Some functions are defined as part of the construct 184 ;; Some functions are defined as part of the construct
151 ;; that defines something else. 185 ;; that defines something else.
152 (while (get symbol 'definition-name) 186 (while (and (symbolp symbol) (get symbol 'definition-name))
153 (setq symbol (get symbol 'definition-name))) 187 (setq symbol (get symbol 'definition-name)))
154 (save-match-data 188 (if (string-match "\\`src/\\(.*\\.c\\)\\'" library)
189 (find-function-C-source symbol (match-string 1 library) variable-p)
155 (if (string-match "\\.el\\(c\\)\\'" library) 190 (if (string-match "\\.el\\(c\\)\\'" library)
156 (setq library (substring library 0 (match-beginning 1)))) 191 (setq library (substring library 0 (match-beginning 1))))
157 (let* ((filename (find-library-name library))) 192 (let* ((filename (find-library-name library)))
158 (with-current-buffer (find-file-noselect filename) 193 (with-current-buffer (find-file-noselect filename)
159 (let ((regexp (format (if variable-p 194 (let ((regexp (format (if variable-p