diff --git a/core/core-lib.el b/core/core-lib.el index 57fe6da0d..cbb4d16ea 100644 --- a/core/core-lib.el +++ b/core/core-lib.el @@ -348,5 +348,19 @@ Body forms can access the hook's arguments through the let-bound variable (t (user-error "associate! invalid rules for mode [%s] (modes %s) (match %s) (files %s)" mode modes match files)))))) +(defmacro file-exists-p! (spec &optional directory) + "Returns t if the files in SPEC all exist. + +SPEC can be a single file or a list of forms/files. It understands nested (and +...) and (or ...), as well. + +DIRECTORY is where to look for the files in SPEC if they aren't absolute. This +doesn't apply to variables, however. + +For example: + + (file-exists-p (or doom-core-dir \"~/.config\" \"some-file\") \"~\")" + (doom--resolve-path-forms spec directory)) + (provide 'core-lib) ;;; core-lib.el ends here diff --git a/core/core-projects.el b/core/core-projects.el index 2d9eb8197..5ad28773b 100644 --- a/core/core-projects.el +++ b/core/core-projects.el @@ -94,12 +94,12 @@ If NOCACHE, don't fetch a cached answer." (defalias 'doom-project-expand #'projectile-expand-root) -(defmacro doom-project-has! (files) +(defmacro project-file-exists-p! (files) "Checks if the project has the specified FILES. Paths are relative to the project root, unless they start with ./ or ../ (in which case they're relative to `default-directory'). If they start with a slash, they are absolute." - (doom--resolve-path-forms files (doom-project-root))) + (doom--resolve-path-forms files '(doom-project-root))) (defun doom-project-find-file (dir) "Fuzzy-find a file under DIR." diff --git a/core/test/core-lib.el b/core/test/core-lib.el index 2fff1fd6e..10a9d6ee3 100644 --- a/core/test/core-lib.el +++ b/core/test/core-lib.el @@ -7,8 +7,8 @@ (def-test! resolve-path-forms (should (equal (doom--resolve-path-forms '(and "fileA" "fileB")) - '(and (file-exists-p (expand-file-name "fileA" (doom-project-root))) - (file-exists-p (expand-file-name "fileB" (doom-project-root))))))) + '(and (file-exists-p (expand-file-name "fileA" nil)) + (file-exists-p (expand-file-name "fileB" nil)))))) ;; `doom--resolve-hook-forms' (def-test! resolve-hook-forms diff --git a/core/test/core-projects.el b/core/test/core-projects.el index d580b921e..47435061b 100644 --- a/core/test/core-projects.el +++ b/core/test/core-projects.el @@ -33,15 +33,15 @@ (should (equal (doom-project-expand "init.el") (expand-file-name "init.el" (doom-project-root)))))) -;; `doom-project-has!' +;; `project-file-exists-p!' (def-test! project-has! :minor-mode projectile-mode (let ((default-directory doom-core-dir)) ;; Resolve from project root - (should (doom-project-has! "init.el")) + (should (project-file-exists-p! "init.el")) ;; Chained file checks - (should (doom-project-has! (and "init.el" "LICENSE"))) - (should (doom-project-has! (or "init.el" "does-not-exist"))) - (should (doom-project-has! (and "init.el" (or "LICENSE" "does-not-exist")))) + (should (project-file-exists-p! (and "init.el" "LICENSE"))) + (should (project-file-exists-p! (or "init.el" "does-not-exist"))) + (should (project-file-exists-p! (and "init.el" (or "LICENSE" "does-not-exist")))) ;; Should resolve relative paths from `default-directory' - (should (doom-project-has! (and "./core.el" "../init.el"))))) + (should (project-file-exists-p! (and "core/core.el" "./init.el"))))) diff --git a/modules/lang/cc/autoload.el b/modules/lang/cc/autoload.el index d14f7c28e..cf6580425 100644 --- a/modules/lang/cc/autoload.el +++ b/modules/lang/cc/autoload.el @@ -6,7 +6,7 @@ (interactive) (unless (memq major-mode '(c-mode c++-mode objc-mode)) (user-error "Not a C/C++/ObjC buffer")) - (unless (doom-project-has! "compile_commands.json") + (unless (project-file-exists-p! "compile_commands.json") (user-error "No compile_commands.json file")) ;; first rtag (when (and (featurep 'rtags) diff --git a/modules/lang/java/autoload.el b/modules/lang/java/autoload.el index 3e7a73c78..fa1689503 100644 --- a/modules/lang/java/autoload.el +++ b/modules/lang/java/autoload.el @@ -24,9 +24,9 @@ ;;;###autoload (defun +java|android-mode-maybe () - (when (doom-project-has! (or "local.properties" - "AndroidManifest.xml" - "src/main/AndroidManifest.xml")) + (when (project-file-exists-p! (or "local.properties" + "AndroidManifest.xml" + "src/main/AndroidManifest.xml")) (android-mode +1) (doom/set-build-command "./gradlew %s" "build.gradle")))