Skip to content

Commit

Permalink
use compilation-ask-about-save instead of buffer-save-without-query
Browse files Browse the repository at this point in the history
for rustic-save-some-buffers

close #199
  • Loading branch information
brotzeit committed Jan 6, 2022
1 parent 6cb6aee commit 7f251b9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,14 @@ formatting, turn off `rustic-format-on-save` and set
`rustic-lsp-format`to `t`.

To save buffers automatically, you can change the value of
`buffer-save-without-query`:
`compilation-ask-about-save`, it has higher precedence than
`buffer-save-without-query` when compiling.

```elisp
(defun rustic-mode-auto-save-hook ()
"Enable auto-saving in rustic-mode buffers."
(when buffer-file-name
(setq-local buffer-save-without-query t)))
(setq-local compilation-ask-about-save nil)))
(add-hook 'rustic-mode-hook 'rustic-mode-auto-save-hook)
```

Expand Down
4 changes: 2 additions & 2 deletions rustic-compile.el
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ If NO-ERROR is t, don't throw error if user chooses not to kill running process.
(defun rustic-save-some-buffers ()
"Unlike `save-some-buffers', only consider project related files.
The variable `buffer-save-without-query' can be used for customization and
The variable `compilation-ask-about-save' can be used for customization and
buffers are formatted after saving if turned on by `rustic-format-trigger'."
(let ((buffers (cl-remove-if-not
#'buffer-file-name
Expand All @@ -387,7 +387,7 @@ buffers are formatted after saving if turned on by `rustic-format-trigger'."
(let ((rustic-format-trigger nil)
(rustic-format-on-save nil))
(setq saved-p
(if buffer-save-without-query
(if (not compilation-ask-about-save)
(progn (save-buffer) t)
(if (yes-or-no-p (format "Save file %s ? "
(buffer-file-name buffer)))
Expand Down
30 changes: 30 additions & 0 deletions test/rustic-compile-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@
(kill-buffer buffer1)
(kill-buffer buffer2)))

;; test if `compilation-ask-about-save' overrides `buffer-save-without-query'
(ert-deftest rustic-test-save-some-buffers-compilation-ask-about-save ()
(let* ((buffer1 (get-buffer-create "b1"))
(buffer2 (get-buffer-create "b2"))
(string "fn main() {}")
(formatted-string "fn main() {}\n")
(dir (rustic-babel-generate-project t))
(rustic-format-trigger 'on-save)
(buffer-save-without-query nil)
(compilation-ask-about-save t))
(let* ((default-directory dir)
(src (concat dir "/src"))
(file1 (expand-file-name "main.rs" src))
(file2 (progn (shell-command-to-string "touch src/test.rs")
(expand-file-name "test.rs" src))))
(with-current-buffer buffer1
(write-file file1)
(insert string))
(with-current-buffer buffer2
(write-file file2)
(insert string))
(rustic-save-some-buffers)
(sit-for 1)
(with-current-buffer buffer1
(should (string= (buffer-string) formatted-string)))
(with-current-buffer buffer2
(should (string= (buffer-string) formatted-string))))
(kill-buffer buffer1)
(kill-buffer buffer2)))

(ert-deftest rustic-test-compile ()
(let* ((dir (rustic-babel-generate-project t)))
(should-not compilation-directory)
Expand Down
2 changes: 1 addition & 1 deletion test/test-helper.el
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ list of substrings of `STR' each followed by its face."
(defun rustic-mode-auto-save-hook ()
"Enable auto-saving in rustic-mode buffers."
(when buffer-file-name
(setq-local buffer-save-without-query t)))
(setq-local compilation-ask-about-save nil)))
(add-hook 'rustic-mode-hook 'rustic-mode-auto-save-hook)

0 comments on commit 7f251b9

Please sign in to comment.