Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path to absolute path in Elisp

Tags:

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.

like image 509
pupeno Avatar asked Nov 15 '08 01:11

pupeno


People also ask

How do you convert relative path to absolute path?

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.

How do you use absolute and relative paths?

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

How do you find the absolute path?

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.

Is relative path or absolute path?

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.


3 Answers

'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"))
like image 82
Trey Jackson Avatar answered Oct 10 '22 16:10

Trey Jackson


I ended up using:

(expand-file-name "relative/path" (file-name-directory load-file-name))
like image 39
pupeno Avatar answered Oct 10 '22 14:10

pupeno


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.

like image 24
Emerick Rogul Avatar answered Oct 10 '22 15:10

Emerick Rogul