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)?
On GNU/Linux, the default value of 'load-path' includes two special directories and their descendants: /usr/local/share/emacs/VERSION/site-lisp and /usr/local/share/emacs/site-lisp . The first directory contains packages for a particular Emacs version; the second contains packages for all installed versions of Emacs.
To load an Emacs Lisp file, type M-x load-file . This command reads a file name using the minibuffer, and executes the contents of that file as Emacs Lisp code. It is not necessary to visit the file first; this command reads the file directly from disk, not from an existing Emacs buffer.
Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With