我在哪里可以找到所有 Emacs 的 elisp 脚本?我不是指用户开发或安装的脚本,而是已经存在的常见 elisp 脚本。
例如,
如果我有一个像 describe-char
或 insert-file
这样的函数,我该如何找到包含这些函数的文件?
Ctrl-h f
可以告诉你一个函数的解释以及它所在的位置。
如果你想要一个函数自动执行这个操作,可以参考以下草稿代码:
(defun my-find-lisp-object-file-name (function)
"Display the lisp file name of FUNCTION (a symbol)."
(interactive
(let ((fn (function-called-at-point))
(enable-recursive-minibuffers t)
val)
(setq val (completing-read (if fn
(format "Describe function (default %s): " fn)
"Describe function: ")
obarray 'fboundp t nil nil
(and fn (symbol-name fn))))
(list (if (equal val "")
fn (intern val)))))
(if (null function)
(message "You didn't specify a function")
(setq object-file-name (find-lisp-object-file-name function (symbol-function function)))
(if (eq object-file-name 'C-source)
(message "%s is in %s" function "C source code")
(setq buff (find-function-search-for-symbol function nil object-file-name))
(setq buf-name (buffer-name(car buff)))
(setq buf-pos (cdr buff))
(switch-to-buffer (car buff))
(message "%s is in %s(%s, %d)" function object-file-name buf-name buf-pos))))
describe-char
,你可以在 *Help*
缓冲区中获取 .el
文件的链接,并获取 .el
文件的位置。例如 /usr/local/share/emacs/24.3/lisp/descr-text.el.gz
中的 descr-text.el
。是这样吗? - songyuanyaoM-x locate-library RET RET
可能是一个答案,虽然需要库名而不是函数名。否则,M-x find-function-other-window
不仅会告诉您文件在哪里,而且还会打开文件,之后您可以使用M-x pwd
了解您的所在位置。C-h v load-path RET
以查看Emacs用于查找其库的目录,因此这应该能让您很好地了解所有捆绑的Elisp文件所在的位置。初稿(2014年3月25日):第一份草稿。
编辑(2014年3月26日):添加了一个global-set-key
。在最终消息中添加了一个额外的car
,以完全从按钮文本属性列表中提取文件名路径。对初始和最终消息进行了一些着色。在函数末尾添加了返回光标到缓冲区开头的选项。添加了find-variable-other-window
和find-function-other-window
的选项。添加了条件,以便在没有文件名时不生成任何消息。
*Help*
缓冲区中显示了*.el
文件,它会显示一个带文件路径的消息。
(global-set-key (kbd "C-h z") 'lawlist-describe-find-function-variable)
(defun lawlist-describe-find-function-variable ()
"Describe or find a function / variable. Displays the path of filename."
(interactive)
(message (concat
(propertize "Describe" 'face 'font-lock-keyword-face)
" ["
(propertize "f" 'face 'font-lock-warning-face)
"]unction / ["
(propertize "v" 'face 'font-lock-warning-face)
"]ariable | "
(propertize "Find" 'face 'font-lock-keyword-face)
" ["
(propertize "F" 'face 'font-lock-warning-face)
"]unction / ["
(propertize "V" 'face 'font-lock-warning-face)
"]ariable"))
(let* (
(select-f-or-v (read-char-exclusive))
function
variable)
(cond
((eq select-f-or-v ?f)
(setq function (read (read-string "Please enter a function name: ")))
(describe-function function)
(select-window (get-buffer-window "*Help*")))
((eq select-f-or-v ?v)
(setq variable (read (read-string "Please enter a variable name: ")))
(describe-variable variable)
(select-window (get-buffer-window "*Help*")))
((eq select-f-or-v ?F)
(setq function (read (read-string "Please enter a function name: ")))
(find-function-other-window function)
(when buffer-file-name
(message (propertize buffer-file-name 'face 'font-lock-warning-face))))
((eq select-f-or-v ?V)
(setq variable (read (read-string "Please enter a variable name: ")))
(find-variable-other-window variable)
(when buffer-file-name
(message (propertize buffer-file-name 'face 'font-lock-warning-face))))
(t
(message "Thanks and come again!")))
(when (and
(equal (buffer-name) "*Help*")
(save-excursion
(goto-char (point-max))
(re-search-backward "\\(`*[.]el'\\)" nil t)))
(goto-char (point-max))
(re-search-backward "\\(`*[.]el'\\)" nil t)
(message
(propertize
(car (cdr (car (nthcdr 1 (text-properties-at (point))))))
'face 'font-lock-warning-face) )
(goto-char (point-min))) ))
如果您使用lispy小模式:
你可以使用 g 键在当前目录中查找所有定义。
你会得到一个 helm
完成界面,其中包含所有 Elisp 文件中的所有标签。
每行的第一列是标签,第二列是文件名。
我的安装基础 Elisp 目录有 19838 个标签,并且完成速度足够快。
你可以使用 lispy-goto-recursive
在当前目录及其子目录中查找所有定义。
它需要几分钟来解析,但只需要几秒钟就能带出完成界面。
但它允许交互式地搜索 Emacs 源中的所有文件 - 共有 89675 个标签。
一个示例搜索:有 55 个顶级标签包含 insert-file
,分布在大约 20 个文件中。
其中大多数是函数,但顶级标签 (define-key ctl-x-map "i" 'insert-file)
也被匹配并可以在不打开文件的情况下查看。
ack
或find/grep
查找所有Lisp文件都在lisp
目录中。cd Emacs-24.4/lisp
ack 'defun some-function'
find . -name '*.el' | xargs grep 'defun some-function'
M-x find-function-other-window
和M-x find-variable-other-window
。我已经设置了(global-set-key (kbd "C-h F") 'find-function-other-window)
和(global-set-key (kbd "C-h V") 'find-variable-other-window)
。我的模式行设置为显示打开缓冲区中显示的文件路径。一旦文件打开,您还可以使用M-x describe-variable RET buffer-file-name
,这将为您提供完整的路径。 - lawlistC-h F
有一个相当有用的默认绑定,所以我建议选择一个未使用的绑定。我的选择是C-h C-f
;虽然它仍不是保留绑定,但目前没有被默认使用。 - phils