New macros file-exists-p! & project-file-exists-p!

The latter replaces the doom-project-has! macro
This commit is contained in:
Henrik Lissner 2018-05-24 18:35:42 +02:00
parent b2186745b7
commit 4ee0b5ba6d
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
6 changed files with 28 additions and 14 deletions

View file

@ -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)" (t (user-error "associate! invalid rules for mode [%s] (modes %s) (match %s) (files %s)"
mode modes match files)))))) 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) (provide 'core-lib)
;;; core-lib.el ends here ;;; core-lib.el ends here

View file

@ -94,12 +94,12 @@ If NOCACHE, don't fetch a cached answer."
(defalias 'doom-project-expand #'projectile-expand-root) (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. "Checks if the project has the specified FILES.
Paths are relative to the project root, unless they start with ./ or ../ (in 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, which case they're relative to `default-directory'). If they start with a slash,
they are absolute." 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) (defun doom-project-find-file (dir)
"Fuzzy-find a file under DIR." "Fuzzy-find a file under DIR."

View file

@ -7,8 +7,8 @@
(def-test! resolve-path-forms (def-test! resolve-path-forms
(should (should
(equal (doom--resolve-path-forms '(and "fileA" "fileB")) (equal (doom--resolve-path-forms '(and "fileA" "fileB"))
'(and (file-exists-p (expand-file-name "fileA" (doom-project-root))) '(and (file-exists-p (expand-file-name "fileA" nil))
(file-exists-p (expand-file-name "fileB" (doom-project-root))))))) (file-exists-p (expand-file-name "fileB" nil))))))
;; `doom--resolve-hook-forms' ;; `doom--resolve-hook-forms'
(def-test! resolve-hook-forms (def-test! resolve-hook-forms

View file

@ -33,15 +33,15 @@
(should (equal (doom-project-expand "init.el") (should (equal (doom-project-expand "init.el")
(expand-file-name "init.el" (doom-project-root)))))) (expand-file-name "init.el" (doom-project-root))))))
;; `doom-project-has!' ;; `project-file-exists-p!'
(def-test! project-has! (def-test! project-has!
:minor-mode projectile-mode :minor-mode projectile-mode
(let ((default-directory doom-core-dir)) (let ((default-directory doom-core-dir))
;; Resolve from project root ;; Resolve from project root
(should (doom-project-has! "init.el")) (should (project-file-exists-p! "init.el"))
;; Chained file checks ;; Chained file checks
(should (doom-project-has! (and "init.el" "LICENSE"))) (should (project-file-exists-p! (and "init.el" "LICENSE")))
(should (doom-project-has! (or "init.el" "does-not-exist"))) (should (project-file-exists-p! (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" (or "LICENSE" "does-not-exist"))))
;; Should resolve relative paths from `default-directory' ;; 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")))))

View file

@ -6,7 +6,7 @@
(interactive) (interactive)
(unless (memq major-mode '(c-mode c++-mode objc-mode)) (unless (memq major-mode '(c-mode c++-mode objc-mode))
(user-error "Not a C/C++/ObjC buffer")) (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")) (user-error "No compile_commands.json file"))
;; first rtag ;; first rtag
(when (and (featurep 'rtags) (when (and (featurep 'rtags)

View file

@ -24,9 +24,9 @@
;;;###autoload ;;;###autoload
(defun +java|android-mode-maybe () (defun +java|android-mode-maybe ()
(when (doom-project-has! (or "local.properties" (when (project-file-exists-p! (or "local.properties"
"AndroidManifest.xml" "AndroidManifest.xml"
"src/main/AndroidManifest.xml")) "src/main/AndroidManifest.xml"))
(android-mode +1) (android-mode +1)
(doom/set-build-command "./gradlew %s" "build.gradle"))) (doom/set-build-command "./gradlew %s" "build.gradle")))