如何在 Emacs Lisp 中引用当前正在加载的文件? [英] How to refer to the file currently being loaded in Emacs Lisp?
问题描述
我希望包含对非 elisp 文件(一个小型 Python 程序)的引用,并且希望能够说它与当前文件位于同一目录中,但具有不同的文件名."在许多脚本语言中,有诸如 __FILE__
(在 PHP 中)之类的东西,用于获取当前文件的绝对路径.
I am looking to include a reference to a non-elisp file (a small Python program), and would like to be able to say "it is in the same directory as the current file, but with a different file name." In many scripting languages, there are things like __FILE__
(in PHP) for getting an absolute path to the current file.
如果要包含的文件在 load-path
中,那么我可以用 (locate-library "file.py" t)
找到它,但是我如果文件不在加载路径中,我会卡住.
If the file to be included is in the load-path
, then I can find it with (locate-library "file.py" t)
, but I'm kind of stuck if the file is not in the load path.
那么有没有办法让 Emacs Lisp 文件找到自己的绝对路径(加载时,未访问时)?
So is there a way for an Emacs Lisp file to find out its own absolute path (when being loaded, not visited)?
推荐答案
M-x describe-variable load-file-name
load-file-name is a variable defined in `C source code'.
Documentation:
Full name of file being loaded by `load'.
您可能还对 symbol-file
函数感兴趣,它会告诉您定义指定函数或变量的绝对文件名.
You might also be interested in the symbol-file
function, which will tell you the absolute filename in which a specified function or variable was defined.
如果你想变得有趣,你可以检查 load-in-progress
变量.如果那是 nil
,则没有正在进行的加载(并且您可能在缓冲区中被 eval
处理).在这种情况下,您可以尝试 (buffer-file-name)
来获取文件名.
If you want to get fancy, you can check the load-in-progress
variable. If that's nil
, then no load is in progress (and you're presumably being eval
'd in a buffer). In that case, you could try (buffer-file-name)
to get the filename.
这篇关于如何在 Emacs Lisp 中引用当前正在加载的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!