How to refer to the file currently being loaded in Emacs Lisp?

故事扮演 提交于 2019-11-29 16:46:46

问题


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.

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.

So is there a way for an Emacs Lisp file to find out its own absolute path (when being loaded, not visited)?


回答1:


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'.

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.

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.



来源:https://stackoverflow.com/questions/1344747/how-to-refer-to-the-file-currently-being-loaded-in-emacs-lisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!