Refactor doom-{path,file,dir,glob}

Breaking change: doom-glob would formerly return a string (if only one
match) or a list. Now it always returns a list.
This commit is contained in:
Henrik Lissner 2021-05-23 21:44:06 -04:00
parent 13f316e645
commit a8e57438dc
6 changed files with 28 additions and 34 deletions

View file

@ -36,12 +36,20 @@ This is used by `file-exists-p!' and `project-file-exists-p!'."
`(file-exists-p ,filevar))
,filevar)))))
(defun doom--path (&rest segments)
(let ((segments (delq nil segments))
;;;###autoload
(defun doom-path (&rest segments)
"Constructs a file path from SEGMENTS.
Ignores `nil' elements in SEGMENTS."
(let ((segments (remq nil segments))
file-name-handler-alist
dir)
(while segments
(setq dir (expand-file-name (car segments) dir)
segments (cdr segments)))
(setq segment (pop segments)
dir (expand-file-name
(if (listp segment)
(apply #'doom-path dir segment)
segment)
dir)))
dir))
;;;###autoload
@ -49,28 +57,16 @@ This is used by `file-exists-p!' and `project-file-exists-p!'."
"Construct a path from SEGMENTS and expand glob patterns.
Returns nil if the path doesn't exist.
Ignores `nil' elements in SEGMENTS."
(let* (case-fold-search
(dir (apply #'doom--path segments)))
(if (string-match-p "[[*?]" dir)
(file-expand-wildcards dir t)
(if (file-exists-p dir)
dir))))
;;;###autoload
(defun doom-path (&rest segments)
"Constructs a file path from SEGMENTS.
Ignores `nil' elements in SEGMENTS."
(if segments
(apply #'doom--path segments)
(file!)))
(let (case-fold-search)
(file-expand-wildcards (apply #'doom-path segments) t)))
;;;###autoload
(defun doom-dir (&rest segments)
"Constructs a path from SEGMENTS.
See `doom-path'.
Ignores `nil' elements in SEGMENTS."
(when-let (path (apply #'doom-path segments))
(directory-file-name (file-name-directory path))))
(when-let (path (doom-path segments))
(directory-file-name path)))
;;;###autoload
(cl-defun doom-files-in