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

后端 未结 6 837
闹比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

    (defun* tofilename (directorylist &optional (filename nil))
      "concatenate directory names into a path, with an optional file name as last part"
      (concat
       (mapconcat 'directory-file-name directorylist "/")
       "/"
       filename))
    
    
    (tofilename '("~/" "Temp/") "temp.txt")
    ;; => "~/Temp/temp.txt"
    
    (tofilename '("~/" "Temp/"))
    ;; => "~/Temp/"
    
    (tofilename '("~/" "Temp/" "test"))
    ;; => "~/Temp/temp/"
    

提交回复
热议问题