Function: symbol-file

symbol-file is a byte-compiled function defined in subr.el.gz.

Signature

(symbol-file SYMBOL &optional TYPE)

Documentation

Return the name of the file that defined SYMBOL.

The value is normally an absolute file name. It can also be nil, if the definition is not associated with any file. If SYMBOL specifies an autoloaded function, the value can be a relative file name without extension.

If TYPE is nil, then any kind of definition is acceptable. If TYPE is defun, defvar, or defface, that specifies function definition, variable definition, or face definition only. Otherwise TYPE is assumed to be a symbol property.

This function only works for symbols defined in Lisp files. For symbols that are defined in C files, use help-C-file-name instead.

Probably introduced at or before Emacs version 22.1.

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun symbol-file (symbol &optional type)
  "Return the name of the file that defined SYMBOL.
The value is normally an absolute file name.  It can also be nil,
if the definition is not associated with any file.  If SYMBOL
specifies an autoloaded function, the value can be a relative
file name without extension.

If TYPE is nil, then any kind of definition is acceptable.  If
TYPE is `defun', `defvar', or `defface', that specifies function
definition, variable definition, or face definition only.
Otherwise TYPE is assumed to be a symbol property.

This function only works for symbols defined in Lisp files.  For
symbols that are defined in C files, use `help-C-file-name'
instead."
  (if (and (or (null type) (eq type 'defun))
	   (symbolp symbol)
	   (autoloadp (symbol-function symbol)))
      (nth 1 (symbol-function symbol))
    (catch 'found
      (pcase-dolist (`(,file . ,elems) load-history)
	(when (if type
		  (if (eq type 'defvar)
		      ;; Variables are present just as their names.
		      (member symbol elems)
		    ;; Many other types are represented as (TYPE . NAME).
		    (or (member (cons type symbol) elems)
                        (memq symbol (alist-get type
                                                (alist-get 'define-symbol-props
                                                           elems)))))
                ;; We accept all types, so look for variable def
                ;; and then for any other kind.
                (or (member symbol elems)
                    (let ((match (rassq symbol elems)))
		      (and match
                           (not (eq 'require (car match)))))))
          (throw 'found file))))))