Elisp中绝对路径的相对路径

时间:2021-11-08 21:45:13

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.

有一个相对路径,如何从我正在加载的elisp文件的位置将其转换为绝对路径。也就是说,我有一个我正在加载的elisp文件,它有一个相对路径,我需要一个绝对的路径。

3 个解决方案

#1


21  

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

返回FILENAME的名称,这应该是绝对的。通过在文件级别和包含它的目录级别追逐符号链接来找到文件名的名称,直到在任何级别都没有链接。

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

递上另一种解决方案(“扩展文件名)离开到位符号链接,这可能是也可能不是你想要的。 “文件truename使用”扩展文件名,所以他们都将决定相对路径默认目录缓存(这是你问什么)。

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.

在看到对不同答案的评论/问题后,问题是default-directory是调用'load的缓冲区的目录。

Luckily, there's a variable that 'load sets that stores the path to the file being loaded. Try this snippet of code out:

幸运的是,有一个变量'load sets,它存储了正在加载的文件的路径。试试这段代码:

;; this is in the file being loaded
(let ((default-directory (file-name-directory load-file-name)))
   (file-truename "blih"))

#2


13  

I ended up using:

我最终使用:

(expand-file-name "relative/path" (file-name-directory load-file-name))

#3


8  

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.

您可以使用expand-file-name函数将相对文件名或路径转换为绝对文件名/路径。在这里查看更多信息。

#1


21  

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

返回FILENAME的名称,这应该是绝对的。通过在文件级别和包含它的目录级别追逐符号链接来找到文件名的名称,直到在任何级别都没有链接。

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

递上另一种解决方案(“扩展文件名)离开到位符号链接,这可能是也可能不是你想要的。 “文件truename使用”扩展文件名,所以他们都将决定相对路径默认目录缓存(这是你问什么)。

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.

在看到对不同答案的评论/问题后,问题是default-directory是调用'load的缓冲区的目录。

Luckily, there's a variable that 'load sets that stores the path to the file being loaded. Try this snippet of code out:

幸运的是,有一个变量'load sets,它存储了正在加载的文件的路径。试试这段代码:

;; this is in the file being loaded
(let ((default-directory (file-name-directory load-file-name)))
   (file-truename "blih"))

#2


13  

I ended up using:

我最终使用:

(expand-file-name "relative/path" (file-name-directory load-file-name))

#3


8  

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.

您可以使用expand-file-name函数将相对文件名或路径转换为绝对文件名/路径。在这里查看更多信息。