From 4c1d7acf81a4dcec97fa4daf9e9f414e1321bfdb Mon Sep 17 00:00:00 2001
From: Mark Oteiza <mvoteiza@udel.edu>
Date: Sun, 12 Feb 2017 20:25:57 -0500
Subject: [PATCH] Substitute leading $HOME/ in xdg-user-dirs

* lisp/xdg.el (xdg--substitute-home-env): New function.
(xdg--user-dirs-parse-line): Use it.
(xdg-user-dir): Expand ~/ in xdg-user-dirs values.
---
 lisp/xdg.el | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/lisp/xdg.el b/lisp/xdg.el
index b11e104e2b7..4973065f91a 100644
--- a/lisp/xdg.el
+++ b/lisp/xdg.el
@@ -109,6 +109,12 @@ file:///foo/bar.jpg"
 (defvar xdg-user-dirs nil
   "Alist of directory keys and values.")
 
+(defun xdg--substitute-home-env (str)
+  (if (file-name-absolute-p str) str
+    (save-match-data
+      (and (string-match "^$HOME/" str)
+           (replace-match "~/" t nil str 0)))))
+
 (defun xdg--user-dirs-parse-line ()
   "Return pair of user-dirs key to directory value in LINE, otherwise nil.
 This should be called at the beginning of a line."
@@ -117,7 +123,7 @@ This should be called at the beginning of a line."
              (looking-at xdg-line-regexp))
     (let ((k (match-string 1))
           (v (match-string 2)))
-      (when (and k v) (cons k v)))))
+      (when (and k v) (cons k (xdg--substitute-home-env v))))))
 
 (defun xdg--user-dirs-parse-file (filename)
   "Return alist of xdg-user-dirs from FILENAME."
@@ -137,7 +143,8 @@ This should be called at the beginning of a line."
     (setq xdg-user-dirs
           (xdg--user-dirs-parse-file
            (expand-file-name "user-dirs.dirs" (xdg-config-home)))))
-  (cdr (assoc name xdg-user-dirs)))
+  (let ((dir (cdr (assoc name xdg-user-dirs))))
+    (when dir (expand-file-name dir))))
 
 (provide 'xdg)
 
-- 
2.39.5