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)) `(file-exists-p ,filevar))
,filevar))))) ,filevar)))))
(defun doom--path (&rest segments) ;;;###autoload
(let ((segments (delq nil segments)) (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) dir)
(while segments (while segments
(setq dir (expand-file-name (car segments) dir) (setq segment (pop segments)
segments (cdr segments))) dir (expand-file-name
(if (listp segment)
(apply #'doom-path dir segment)
segment)
dir)))
dir)) dir))
;;;###autoload ;;;###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. "Construct a path from SEGMENTS and expand glob patterns.
Returns nil if the path doesn't exist. Returns nil if the path doesn't exist.
Ignores `nil' elements in SEGMENTS." Ignores `nil' elements in SEGMENTS."
(let* (case-fold-search (let (case-fold-search)
(dir (apply #'doom--path segments))) (file-expand-wildcards (apply #'doom-path segments) t)))
(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!)))
;;;###autoload ;;;###autoload
(defun doom-dir (&rest segments) (defun doom-dir (&rest segments)
"Constructs a path from SEGMENTS. "Constructs a path from SEGMENTS.
See `doom-path'. See `doom-path'.
Ignores `nil' elements in SEGMENTS." Ignores `nil' elements in SEGMENTS."
(when-let (path (apply #'doom-path segments)) (when-let (path (doom-path segments))
(directory-file-name (file-name-directory path)))) (directory-file-name path)))
;;;###autoload ;;;###autoload
(cl-defun doom-files-in (cl-defun doom-files-in

View file

@ -164,8 +164,8 @@ each package."
(mapc (fn! ((cat . mod)) (mapc (fn! ((cat . mod))
(if-let (packages-file (if-let (packages-file
(pcase cat (pcase cat
(:private (doom-glob doom-private-dir "packages.el")) (:private (car (doom-glob doom-private-dir "packages.el")))
(:core (doom-glob doom-core-dir "packages.el")) (:core (car (doom-glob doom-core-dir "packages.el")))
(_ (doom-module-locate-path cat mod "packages.el")))) (_ (doom-module-locate-path cat mod "packages.el"))))
(with-current-buffer (with-current-buffer
(or (get-file-buffer packages-file) (or (get-file-buffer packages-file)

View file

@ -52,7 +52,7 @@ one wants that.")
in (append (list doom-core-dir) in (append (list doom-core-dir)
(cdr (doom-module-load-path 'all-p)) (cdr (doom-module-load-path 'all-p))
(list doom-private-dir)) (list doom-private-dir))
if (doom-glob dir "autoload.el") collect it if (doom-glob dir "autoload.el") collect (car it)
if (doom-glob dir "autoload/*.el") append it) if (doom-glob dir "autoload/*.el") append it)
(mapcan #'doom-glob doom-autoloads-files)) (mapcan #'doom-glob doom-autoloads-files))
nil) nil)

View file

@ -16,11 +16,9 @@ and your private config files, respectively. To recompile your packages, use
'doom build' instead." 'doom build' instead."
(doom-cli-byte-compile (doom-cli-byte-compile
(if (or core-p private-p) (if (or core-p private-p)
(append (when core-p (append (if core-p (doom-glob doom-emacs-dir "init.el"))
(list (doom-glob doom-emacs-dir "init.el") (if core-p (list doom-core-dir))
doom-core-dir)) (if private-p (list doom-private-dir)))
(when private-p
(list doom-private-dir)))
(or (y-or-n-p (or (y-or-n-p
(concat "WARNING: Changes made to your config after compiling it won't take effect until\n" (concat "WARNING: Changes made to your config after compiling it won't take effect until\n"
"this command is rerun or you run 'doom clean'! It will also make error backtraces\n" "this command is rerun or you run 'doom clean'! It will also make error backtraces\n"
@ -28,11 +26,11 @@ and your private config files, respectively. To recompile your packages, use
"If you intend to use it anyway, remember this or it will come back to bite you!\n\n" "If you intend to use it anyway, remember this or it will come back to bite you!\n\n"
"Continue anyway?")) "Continue anyway?"))
(user-error "Aborted")) (user-error "Aborted"))
(append (list (doom-glob doom-emacs-dir "init.el") (append (doom-glob doom-emacs-dir "init.el")
doom-core-dir) (list doom-core-dir)
(cl-remove-if-not (seq-filter
;; Only compile Doom's modules ;; Only compile Doom's modules
(lambda (path) (file-in-directory-p path doom-emacs-dir)) (doom-rpartial #'file-in-directory-p doom-emacs-dir)
;; Omit `doom-private-dir', which is always first ;; Omit `doom-private-dir', which is always first
(cdr (doom-module-load-path))))) (cdr (doom-module-load-path)))))
recompile-p recompile-p

View file

@ -44,7 +44,7 @@
(doom-glob (or (getenv "XDG_CONFIG_HOME") (doom-glob (or (getenv "XDG_CONFIG_HOME")
"~/.config") "~/.config")
"isync/mbsyncrc")) "isync/mbsyncrc"))
(format "-c %S" config-file) (format "-c %S" (car config-file))
""))) "")))
(`offlineimap (`offlineimap
"offlineimap && notmuch new") "offlineimap && notmuch new")

View file

@ -6,7 +6,7 @@
:config :config
(when (and (featurep! +flutter) IS-LINUX) (when (and (featurep! +flutter) IS-LINUX)
(when-let (path (doom-glob "/opt/flutter/bin/cache/dart-sdk")) (when-let (path (doom-glob "/opt/flutter/bin/cache/dart-sdk"))
(setq flutter-sdk-path path))) (setq flutter-sdk-path (car path))))
(set-ligatures! '(dart-mode) (set-ligatures! '(dart-mode)
;; Functional ;; Functional
:def "Function" :def "Function"