|
;; (require 'projectile) |
|
|
|
(defun my/prompt-save-buffer () |
|
"Kinda like `save-some-buffers' except it only prompts for the current buffer." |
|
(when (buffer-modified-p) |
|
(map-y-or-n-p "Save %s ?" (lambda (_) (save-buffer)) (list (buffer-name))))) |
|
|
|
(defun my/projectile-compile-and-run (arg &optional dir) |
|
"Build and run projectile project. Concatenates |
|
`projectile-compilation-command' and `projectile-run-command' |
|
with a && like so: |
|
|
|
\"($projectile-compilation-command) && ($projectile-run-command)\" |
|
|
|
It's otherwise equivalent to the execution of those two |
|
functions. You have to confirm each command string." |
|
(interactive "P") |
|
(when (projectile-project-p) |
|
(let* ((project-root (projectile-project-root)) |
|
(default-directory (or dir (projectile-compilation-dir))) |
|
(default-compile-cmd (projectile-compilation-command default-directory)) |
|
(compile-cmd (projectile-maybe-read-command arg default-compile-cmd "Compile command: ")) |
|
(default-run-cmd (projectile-run-command project-root)) |
|
(run-cmd (projectile-maybe-read-command arg default-run-cmd "Run command: ")) |
|
(final-cmd (concat "("compile-cmd ") && (" run-cmd ")"))) |
|
(puthash default-directory compile-cmd projectile-compilation-cmd-map) |
|
(puthash project-root run-cmd projectile-run-cmd-map) |
|
(save-some-buffers (not compilation-ask-about-save) |
|
(lambda () |
|
(projectile-project-buffer-p (current-buffer) |
|
project-root))) |
|
;; Pass a lambda to projectile-run-compilation so that we can add |
|
;; the `t' parameter to `compilation-start', which ruyns the |
|
;; compilation buffer under `comint-mode' mode, so it can read |
|
;; keyboard input. |
|
(projectile-run-compilation (lambda () (compilation-start final-cmd t)))))) |
|
|
|
(defun my/try-build-run-project () |
|
"A sort of catch-all for build & run systems. Only handles |
|
projectile via `my/projectile-compile-and-run' right now, but could add more." |
|
(interactive) |
|
(hack-dir-local-variables-non-file-buffer) |
|
(let ((file-name (buffer-file-name))) |
|
(cond |
|
((and (projectile-project-p) (not (equal projectile-project-run-cmd 'project-self-run))) |
|
(let ((project-root (projectile-project-root))) |
|
(if (and (projectile-compilation-command project-root) |
|
(projectile-run-command project-root)) |
|
(call-interactively #'my/projectile-compile-and-run) |
|
(message "No compile/run commands defined for this project.")))) |
|
((and file-name (file-executable-p file-name)) |
|
(my/prompt-save-buffer) |
|
(compilation-start file-name t)) |
|
(nil (message "Not in a project and current buffer not executable"))))) |
|
|
|
(defun my/try-build-project () |
|
"Like `my/try-build-run-project', but only for building." |
|
(interactive) |
|
(hack-dir-local-variables-non-file-buffer) |
|
(if (not (projectile-project-p)) |
|
(message "Not in a project") |
|
(let ((project-root (projectile-project-root))) |
|
(if (projectile-compilation-command project-root) |
|
(call-interactively #'projectile-compile-project) |
|
(message "No compile command defined for this project."))))) |