tags:

views:

586

answers:

3

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.

+3  A: 

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.

Emerick Rogul
The problem is that when I load ~/blah/bleh.el and it evaluates (expand-file-name "blih") I end up with ~/blih instead of ~/blah/blih. I need it relative to where the file is, not to where I happen to be when I load the file.Thanks.
J. Pablo Fernández
+3  A: 

I ended up using:

(expand-file-name "relative/path" (file-name-directory load-file-name))
J. Pablo Fernández
+5  A: 
'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"))
Trey Jackson
Does it work with directories too?
J. Pablo Fernández