在Emacs Lisp中,如何找到符号的定义位置

时间:2013-08-10 22:12:22

标签: emacs elisp

当我执行C-h fC-h v时,“帮助”会告诉我符号定义的文件或自动加载的位置。如何以编程方式查找相同的信息?

2 个答案:

答案 0 :(得分:9)

一些挖掘揭示了

(find-lisp-object-file-name object type)

应该做的伎俩。举个例子:

(find-lisp-object-file-name 'goto-line 'function)
;; => "/usr/local/Cellar/emacs/24.3/share/emacs/24.3/lisp/simple.el"

编辑:我是如何发现这些信息的:

首先我做了C-h k C-h f来确定C-h f绑定的内容。结果是describe-function,所以让我们C-h f describe-function查看其来源。我注意到它本质上是围绕describe-function-1的交互式包装器,所以我跳到了源头。那里有很多东西,但相关的是:

(file-name (find-lisp-object-file-name function def))

显示find-lisp-object-file-name是用于在内部完成此工作的函数。

答案 1 :(得分:2)

添加詹姆斯波特的回答

;;; run from: emacs -q

(require 'cl) ; for incf

(print (list
        ;; goto-line is a function defined in simple.el
        (find-lisp-object-file-name 'goto-line (symbol-function 'goto-line))
        ;; print is a function defined in C
        (find-lisp-object-file-name 'print (symbol-function 'print))
        ;; rx is an autoload from rx.el
        (find-lisp-object-file-name 'rx (symbol-function 'rx))
        ;; incf is an alias for cl-incf defined in cl.el
        (find-lisp-object-file-name 'incf (symbol-function 'incf))
        ;; cl-incf is defined in cl-lib.el
        (find-lisp-object-file-name 'cl-incf (symbol-function 'cl-incf))))
;; => ("c:/run/Emacs/lisp/simple.el" C-source 
;;  "c:/run/Emacs/lisp/emacs-lisp/rx.el" "c:/run/Emacs/lisp/emacs-lisp/cl.el"
;;  "c:/run/Emacs/lisp/emacs-lisp/cl-lib.el")

(print (list
        ;; print-circle is a variable defined in C
        (find-lisp-object-file-name 'print-circle 'defvar)
        ;; indent-line-function is a variable defined in indent.el
        (find-lisp-object-file-name 'indent-line-function 'defvar)))
;; => (C-source "c:/run/Emacs/lisp/indent.el")