In Elisp, how to get path string with slash properly inserted?

后端 未结 6 802
闹比i
闹比i 2021-01-02 05:46

I am manually constructing path strings in Elisp by concatenating partial paths and directory names. Unfortunately sometimes the paths end with slash, sometimes not. Theref

6条回答
  •  情话喂你
    2021-01-02 06:31

    The easiest way to assemble file names from parts of questionable content is with expand-file-name. For example:

    (expand-file-name "foo.txt")
    

    this common form will give you a full file name based on default-directory:

    /home/me/foo.txt
    

    but if you have a variable 'dir' whose content is "/home/them/subdir" and want to use that, do this:

    (expand-file-name "foo.txt" dir)
    

    it doesn't matter if dir ends in / or not. If you are on some other platform, and contains the other slash, it will do the right thing then too. Do you have a mix? Just stack them:

    (expand-file-name "foo.txt" (expand-file-name "somesubdir" dir))
    

提交回复
热议问题