(defun my-project-root () (expand-file-name (project-root (project-current t)))) (defun my-gradle-command (root task) (let ((wrapper (expand-file-name (if (eq system-type 'windows-nt) "gradlew.bat" "gradlew") root))) (if (file-exists-p wrapper) (format "%s %s --console=plain --warning-mode=none" (shell-quote-argument wrapper) task) (format "gradle %s --console=plain --warning-mode=none" task)))) (defun my-java-gradle-task (file) (let ((path (replace-regexp-in-string "\\\\" "/" file))) (cond ((string-match-p "/src/test/java/" path) "compileTestJava") ((string-match-p "/src/main/java/" path) "compileJava") (t "compileJava")))) (defun my-java-compile-command (root file) (my-gradle-command root (my-java-gradle-task file))) (defun my-cpp-compile-command (root file) (if (eq system-type 'windows-nt) (format "cl /Zs /TP /std:c++latest /I%s /IC:\\VulkanSDK\\1.4.341.1\\include /Zc:preprocessor /MP /arch:AVX2 /EHs- /EHc- /GR- %s" (shell-quote-argument root) (shell-quote-argument file)) (format "g++ -x c++ -fsyntax-only -std=c++23 -I%s -fno-exceptions -fno-rtti -mavx2 -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-strict-aliasing %s" (shell-quote-argument root) (shell-quote-argument file)))) (defun my-compile () (interactive) (let* ((root (my-project-root)) (file (buffer-file-name)) (ext (file-name-extension file)) (command (cond ((member ext '("cpp" "cc" "cxx" "hpp" "hh" "hxx" "h")) (my-cpp-compile-command root file)) ((string= ext "java") (my-java-compile-command root file)) (t (user-error "Unsupported file type: %s" ext))))) (compile command))) (global-set-key (kbd "C-c c") #'my-compile) (provide 'my-compile)