summaryrefslogtreecommitdiff
path: root/.emacs.d/lisp/rust-cargo.el
diff options
context:
space:
mode:
Diffstat (limited to '.emacs.d/lisp/rust-cargo.el')
-rw-r--r--.emacs.d/lisp/rust-cargo.el55
1 files changed, 38 insertions, 17 deletions
diff --git a/.emacs.d/lisp/rust-cargo.el b/.emacs.d/lisp/rust-cargo.el
index bda23d8..d9f1be5 100644
--- a/.emacs.d/lisp/rust-cargo.el
+++ b/.emacs.d/lisp/rust-cargo.el
@@ -19,6 +19,13 @@
19 :type 'boolean 19 :type 'boolean
20 :group 'rust-mode) 20 :group 'rust-mode)
21 21
22(defcustom rust-cargo-locate-default-arguments '("--workspace")
23 "Arguments for `cargo locate-project`. Remove `--workspace` if you
24would prefer to use the local crate Cargo.toml instead of the
25worksapce for commands like `cargo check`."
26 :type '(repeat string)
27 :group 'rust-mode)
28
22(defcustom rust-cargo-default-arguments "" 29(defcustom rust-cargo-default-arguments ""
23 "Default arguments when running common cargo commands." 30 "Default arguments when running common cargo commands."
24 :type 'string 31 :type 'string
@@ -42,7 +49,13 @@
42 (setq-local process-environment env) 49 (setq-local process-environment env)
43 ;; Set PATH so we can find cargo. 50 ;; Set PATH so we can find cargo.
44 (setq-local exec-path path) 51 (setq-local exec-path path)
45 (let ((ret (process-file rust-cargo-bin nil (list (current-buffer) nil) nil "locate-project" "--workspace"))) 52 (let ((ret
53 (let ((args
54 (append
55 (list rust-cargo-bin nil (list (current-buffer) nil) nil
56 "locate-project")
57 rust-cargo-locate-default-arguments)))
58 (apply #'process-file args))))
46 (when (/= ret 0) 59 (when (/= ret 0)
47 (error "`cargo locate-project' returned %s status: %s" ret (buffer-string))) 60 (error "`cargo locate-project' returned %s status: %s" ret (buffer-string)))
48 (goto-char 0) 61 (goto-char 0)
@@ -67,46 +80,54 @@
67 80
68;;; Internal 81;;; Internal
69 82
70(defun rust--compile (format-string &rest args) 83(defun rust--compile (comint format-string &rest args)
71 (when (null rust-buffer-project) 84 (when (null rust-buffer-project)
72 (rust-update-buffer-project)) 85 (rust-update-buffer-project))
73 (let ((default-directory 86 (let ((default-directory
74 (or (and rust-buffer-project 87 (or (and rust-buffer-project
75 (file-name-directory rust-buffer-project)) 88 (file-name-directory rust-buffer-project))
76 default-directory))) 89 default-directory))
77 (compile (apply #'format format-string args)))) 90 ;; make sure comint is a boolean value
91 (comint (not (not comint))))
92 (compile (apply #'format format-string args) comint)))
78 93
79;;; Commands 94;;; Commands
80 95
81(defun rust-check () 96(defun rust-check ()
82 "Compile using `cargo check`" 97 "Compile using `cargo check`"
83 (interactive) 98 (interactive)
84 (rust--compile "%s check %s" rust-cargo-bin rust-cargo-default-arguments)) 99 (rust--compile nil "%s check %s" rust-cargo-bin rust-cargo-default-arguments))
85 100
86(defun rust-compile () 101(defun rust-compile ()
87 "Compile using `cargo build`" 102 "Compile using `cargo build`"
88 (interactive) 103 (interactive)
89 (rust--compile "%s build %s" rust-cargo-bin rust-cargo-default-arguments)) 104 (rust--compile nil "%s build %s" rust-cargo-bin rust-cargo-default-arguments))
90 105
91(defun rust-compile-release () 106(defun rust-compile-release ()
92 "Compile using `cargo build --release`" 107 "Compile using `cargo build --release`"
93 (interactive) 108 (interactive)
94 (rust--compile "%s build --release" rust-cargo-bin)) 109 (rust--compile nil "%s build --release" rust-cargo-bin))
95 110
96(defun rust-run () 111(defun rust-run (&optional comint)
97 "Run using `cargo run`" 112 "Run using `cargo run`
98 (interactive)
99 (rust--compile "%s run %s" rust-cargo-bin rust-cargo-default-arguments))
100 113
101(defun rust-run-release () 114If optional arg COMINT is t or invoked with universal prefix arg,
102 "Run using `cargo run --release`" 115output buffer will be in comint mode, i.e. interactive."
103 (interactive) 116 (interactive "P")
104 (rust--compile "%s run --release" rust-cargo-bin)) 117 (rust--compile comint "%s run %s" rust-cargo-bin rust-cargo-default-arguments))
118
119(defun rust-run-release (&optional comint)
120 "Run using `cargo run --release`
121
122If optional arg COMINT is t or invoked with universal prefix arg,
123output buffer will be in comint mode, i.e. interactive."
124 (interactive "P")
125 (rust--compile comint "%s run --release" rust-cargo-bin))
105 126
106(defun rust-test () 127(defun rust-test ()
107 "Test using `cargo test`" 128 "Test using `cargo test`"
108 (interactive) 129 (interactive)
109 (rust--compile "%s test %s" rust-cargo-bin rust-cargo-default-arguments)) 130 (rust--compile nil "%s test %s" rust-cargo-bin rust-cargo-default-arguments))
110 131
111(defun rust-run-clippy () 132(defun rust-run-clippy ()
112 "Run `cargo clippy'." 133 "Run `cargo clippy'."
@@ -118,7 +139,7 @@
118 ;; set `compile-command' temporarily so `compile' doesn't 139 ;; set `compile-command' temporarily so `compile' doesn't
119 ;; clobber the existing value 140 ;; clobber the existing value
120 (compile-command (mapconcat #'shell-quote-argument args " "))) 141 (compile-command (mapconcat #'shell-quote-argument args " ")))
121 (rust--compile compile-command))) 142 (rust--compile nil compile-command)))
122 143
123;;; _ 144;;; _
124(provide 'rust-cargo) 145(provide 'rust-cargo)