Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

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

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...