Having a relative path, how do I turn it into an absolute one from the location where the elisp file that I'm loading is. That is, I have an elisp file that I'm loading, it has an relative path and I need an absolute one.
The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).
To find the full absolute path of the current directory, use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file.
In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.
'file-truename
From the documentation:
Return the truename of FILENAME, which should be absolute. The truename of a file name is found by chasing symbolic links both at the level of the file and at the level of the directories containing it, until no links are left at any level.
The other solution proffered ('expand-file-name) leaves symbolic links in place, which may or may not be what you want. 'file-truename uses 'expand-file-name, so they both will determine the path relative default-directory for the buffer (which is what you're asking for).
After seeing the comment/question to a different answer, the problem is that the default-directory is that of the buffer that is calling 'load.
Luckily, there's a variable that 'load sets that stores the path to the file being loaded. Try this snippet of code out:
;; this is in the file being loaded
(let ((default-directory (file-name-directory load-file-name)))
(file-truename "blih"))
I ended up using:
(expand-file-name "relative/path" (file-name-directory load-file-name))
You can use the expand-file-name
function to convert a relative filename or path into an absolute filename/path. Look here for additional information.
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