簡體   English   中英

如何查找哪個文件提供emacs elisp中的功能

[英]How to find which file provide(d) the feature in emacs elisp

目前,我正在使用load-history變量來查找功能來自的文件。

假設找到功能gnus的文件。

我在暫存緩沖區中執行以下代碼,該代碼連續在單獨的行中打印文件名和符號。

(dolist (var load-history)
  (princ (format "%s\n" (car var)))
  (princ (format "\t%s\n" (cdr var))))

然后搜索“(provide。gnus)”,然后將點移動到行的開頭(Ctrl + A)。 前一行的文件名是功能來自的文件。

這種方法有什么問題嗎,還是存在更好的方法?

我真的不知道您要怎么做,但是這里有一些注意事項。

  • 您的方法很好。 在我的書中,以任何方式破解您自己的問題解決方案都是好的。

  • @Tom是正確的,您實際上不需要執行此操作,因為幫助系統已為您解決了該問題。 Ch f

但這不是那么有趣。 假設您確實想要一個自動的,更優雅的解決方案。 您需要一個功能-具有此簽名的locate-feature

(defun locate-feature (feature)
  "Return file-name as string where `feature' was provided"
  ...)

方法1 load-history方法

我將描述為解決此問題而采取的步驟:

  1. 您已經擁有了最重要的部分-找到包含所需信息的變量。

  2. 我立即注意到該變量具有大量數據。 如果我將它作為一行插入緩沖區,Emacs將不會滿意,因為它在處理長行方面眾所周知是很糟糕的。 我知道prett-print軟件包將能夠很好地格式化此數據。 所以我打開我的*scratch*緩沖區並運行

    M- :(插入(pp到字符串的加載歷史記錄))

  3. 現在,我可以看到正在處理的數據結構。 似乎是(用偽代碼):

     ((file-name ((defun|t|provide . symbol)|symbol)*) ...) 
  4. 現在我只寫函數

     (eval-when-compile (require 'cl)) (defun locate-feature (feature) "Return file name as string where `feature' was provided" (interactive "Sfeature: ") (dolist (file-info load-history) (mapc (lambda (element) (when (and (consp element) (eq (car element) 'provide) (eq (cdr element) feature)) (when (called-interactively-p 'any) (message "%s defined in %s" feature (car file-info))) (return (car file-info)))) (cdr file-info)))) 

    這里的代碼非常簡單。 向Emacs詢問您不了解的功能。

方法2幫助方法

方法一適用於功能。 但是,如果我想知道在哪里定義了可用的功能,該怎么辦? 不只是功能。

Ch f已經告訴過我,但是我想要一個字符串中的文件名,而不是所有詳細的幫助文本。 我要這個:

(defun locate-function (func)
  "Return file-name as string where `func' was defined or will be autoloaded"
  ...)

開始了。

  1. Ch f是我的起點,但我真的很想閱讀定義describe-function的代碼。 我這樣做:

    Ch k Ch f Cx o 選項卡 輸入

  2. 現在,我在help-fns.el中對describe-function的定義。 我只想使用此函數定義。 所以縮小是為了:

    x

  3. 我有一種預感,有趣的命令的名稱中會包含“ find”或“ locate”,因此我使用occur搜索來搜索有趣的行:

    o 查找/定位 女士

    無匹配。 在此defun中沒有很多行。 describe-function-1似乎正在完成實際工作,因此我們嘗試這樣做。

  4. 我可以通過Ch f訪問describe-function-1的定義。 但是我已經打開了文件。 imenu現在可以使用:

    Cx nw Mx imenu desc * 1 選項卡 輸入

  5. 縮小並再次搜索:

    CX ND 女士O 最多 進入

    我看到find-lisp-object-file-name看起來很有希望。

  6. 閱讀Ch f find-lisp-object-file-name之后,我想到了:

     (defun locate-function (func) "Return file-name as string where `func' was defined or will be autoloaded" (interactive "Ccommand: ") (let ((res (find-lisp-object-file-name func (symbol-function func)))) (when (called-interactively-p 'any) (message "%s defined in %s" func res)) res)) 

現在去探索Emacs吧。

只需使用symbol-file 它掃描具有以下格式的load-history

Each entry has the form `(provide . FEATURE)',
`(require . FEATURE)', `(defun . FUNCTION)', `(autoload . SYMBOL)',
`(defface . SYMBOL)', or `(t . SYMBOL)'.  Entries like `(t . SYMBOL)'
may precede a `(defun . FUNCTION)' entry, and means that SYMBOL was an
autoload before this file redefined it as a function.  In addition,
entries may also be single symbols, which means that SYMBOL was
defined by `defvar' or `defconst'.

所以稱它為:

(symbol-file 'scheme 'provide)        ; Who provide feature.
(symbol-file 'nxml-mode-hook 'defvar) ; Where variable defined.
(symbol-file 'message-send 'defun)    ; Where function defined.
(symbol-file 'scheme)    ; Look for symbol despite its type.

locate-library

嘗試... M- :( (locate-library "my-feature")

例如:( (locate-library "gnus")

它沒有什么問題,但是為什么它比獲得鍵或函數的幫助要簡單呢? 例如,如果您使用gnus命令,並且想知道它來自何處,則可以使用Ch k ,它會告訴您其定義來自哪個elisp文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM