如何显示模块的完整路径

How to show full path of module

如果我这样做:

(message (format "===> %s loaded" (file-name-base load-file-name)))

我只得到文件的基本名称。

===> 0100-start loaded

如果我这样做:

(message (format "===> %s loaded" 'load-file-name))

我得到:

===> load-file-name loaded

不是我想要的...

如果我这样做:

(message (format "===> %s loaded" load-file-name))

我得到:

===> nil loaded

不知道那里发生了什么。我虽然变量 load-file-name 具有使用它的模块的值。它以某种方式拥有它,否则我无法获得 (file-name-base load-file-name),但它自己使用是行不通的。我可能需要在变量名前加反引号和前引号,但不知道该怎么做。 Elisp 巫术。

我想得到的是:

===> /home/user1/.emacs.d/conf/0100-start.el loaded

如何获取 lisp 模块的完整绝对路径?我以为这是 load-file-name 的工作。如何显示?

如何在 elisp 中使用变量?

首先,message can do formatting, so format不需要。

其次,当你引用一个变量时,比如 'foo,它不会被计算,所以你没有得到它的值。

第三,C-h f file-name-base RET 应该解释你的第一个输出。

最后,正在加载的文件中的正确格式

(message "===> %s loaded" load-file-name)

您看到 nil 的原因是 在加载 期间未对它进行评估。请尝试 C-h v load-file-name RET.

Scoping Rules for Variable Binding1:

Local bindings in Emacs Lisp have indefinite scope and dynamic extent. [...]

  • Indefinite scope means that any part of the program can potentially access the variable binding. Extent refers to when, as the program is executing, the binding exists.
  • Dynamic extent means that the binding lasts as long as the activation of the construct that established it.

(Emacs也可以执行lexical binding)

当你load一个文件时,会发生这样的事情:

(defun load (file)
  (let ((load-file-name file))
    (do-load file)))

因此,load-file-name 仅在加载文件期间绑定到 file。当代码存在 let 范围时,绑定不再有效。

请注意,如果您没有 load 而是 require a module, then upon loading Emacs will also register the module as being provided and will not reload it when calling require another time. This might be the reason you get nil the third time (see unload-feature)。

在你的模块中,你可以为你自己的符号定义一个全局绑定:

(defvar jeckyll2hide/module-path load-file-name)

然后,您第一次 load(或 require)您的模块时,您将定义一个变量,该变量将全局绑定到在本地绑定到 load-file-name 的值加载过程中。


1. 格式化和强调我的