-(defun describe-simplify-lib-file-name (file)
- "Simplify a library name FILE to a relative name, and make it a source file."
- (if file
- ;; Try converting the absolute file name to a library name.
- (let ((libname (file-name-nondirectory file)))
- ;; Now convert that back to a file name and see if we get
- ;; the original one. If so, they are equivalent.
- (if (equal file (locate-file libname load-path '("")))
- (if (string-match "[.]elc\\'" libname)
- (substring libname 0 -1)
- libname)
- file))))
-
-(defun find-source-lisp-file (file-name)
- (let* ((elc-file (locate-file (concat file-name
- (if (string-match "\\.el" file-name)
- "c"
- ".elc"))
- load-path))
- (str (if (and elc-file (file-readable-p elc-file))
- (with-temp-buffer
- (insert-file-contents-literally elc-file nil 0 256)
- (buffer-string))))
- (src-file (and str
- (string-match ";;; from file \\(.*\\.el\\)" str)
- (match-string 1 str))))
- (if (and src-file (file-readable-p src-file))
- src-file
- file-name)))
+(defun find-lisp-object-file-name (object type)
+ "Guess the file that defined the Lisp object OBJECT, of type TYPE.
+OBJECT should be a symbol associated with a function, variable, or face;
+ alternatively, it can be a function definition.
+If TYPE is `variable', search for a variable definition.
+If TYPE is `face', search for a face definition.
+If TYPE is the value returned by `symbol-function' for a function symbol,
+ search for a function definition.
+
+The return value is the absolute name of a readable file where OBJECT is
+defined. If several such files exist, preference is given to a file
+found via `load-path'. The return value can also be `C-source', which
+means that OBJECT is a function or variable defined in C. If no
+suitable file is found, return nil."
+ (let* ((autoloaded (eq (car-safe type) 'autoload))
+ (file-name (or (and autoloaded (nth 1 type))
+ (symbol-file
+ object (if (memq type (list 'defvar 'defface))
+ type
+ 'defun)))))
+ (cond
+ (autoloaded
+ ;; An autoloaded function: Locate the file since `symbol-function'
+ ;; has only returned a bare string here.
+ (setq file-name
+ (locate-file file-name load-path '(".el" ".elc") 'readable)))
+ ((and (stringp file-name)
+ (string-match "[.]*loaddefs.el\\'" file-name))
+ ;; An autoloaded variable or face. Visit loaddefs.el in a buffer
+ ;; and try to extract the defining file. The following form is
+ ;; from `describe-function-1' and `describe-variable'.
+ (let ((location
+ (condition-case nil
+ (find-function-search-for-symbol object nil file-name)
+ (error nil))))
+ (when location
+ (with-current-buffer (car location)
+ (goto-char (cdr location))
+ (when (re-search-backward
+ "^;;; Generated autoloads from \\(.*\\)" nil t)
+ (setq file-name
+ (locate-file
+ (match-string-no-properties 1)
+ load-path nil 'readable))))))))
+
+ (cond
+ ((and (not file-name) (subrp type))
+ ;; A built-in function. The form is from `describe-function-1'.
+ (if (get-buffer " *DOC*")
+ (help-C-file-name type 'subr)
+ 'C-source))
+ ((and (not file-name) (symbolp object)
+ (integerp (get object 'variable-documentation)))
+ ;; A variable defined in C. The form is from `describe-variable'.
+ (if (get-buffer " *DOC*")
+ (help-C-file-name object 'var)
+ 'C-source))
+ ((not (stringp file-name))
+ ;; If we don't have a file-name string by now, we lost.
+ nil)
+ ((let ((lib-name
+ (if (string-match "[.]elc\\'" file-name)
+ (substring-no-properties file-name 0 -1)
+ file-name)))
+ ;; When the Elisp source file can be found in the install
+ ;; directory return the name of that file - `file-name' should
+ ;; have become an absolute file name ny now.
+ (or (and (file-readable-p lib-name) lib-name)
+ ;; The library might be compressed.
+ (and (file-readable-p (concat lib-name ".gz")) lib-name))))
+ ((let* ((lib-name (file-name-nondirectory file-name))
+ ;; The next form is from `describe-simplify-lib-file-name'.
+ (file-name
+ ;; Try converting the absolute file name to a library
+ ;; name, convert that back to a file name and see if we
+ ;; get the original one. If so, they are equivalent.
+ (if (equal file-name (locate-file lib-name load-path '("")))
+ (if (string-match "[.]elc\\'" lib-name)
+ (substring-no-properties lib-name 0 -1)
+ lib-name)
+ file-name))
+ ;; The next three forms are from `find-source-lisp-file'.
+ (elc-file (locate-file
+ (concat file-name
+ (if (string-match "\\.el\\'" file-name)
+ "c"
+ ".elc"))
+ load-path nil 'readable))
+ (str (when elc-file
+ (with-temp-buffer
+ (insert-file-contents-literally elc-file nil 0 256)
+ (buffer-string))))
+ (src-file (and str
+ (string-match ";;; from file \\(.*\\.el\\)" str)
+ (match-string 1 str))))
+ (and src-file (file-readable-p src-file) src-file))))))