From 32a962ab2d3f87bde0e12c4e8975fe73d8ba8579 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Sun, 26 Apr 2020 15:45:37 +0200 Subject: [PATCH] In emacs >= 26.2, use replace-buffer-contents after formatting This saves the position and the markers in the buffer, so there is no need to go to rustic-save-pos. This plays better with e.g. grep, as otherwise all grep markers would be destroyed when replacing the contents of the buffer with copy-to-buffer. While replace-buffer-contents was added in Emacs 26.1, it was broken for non-ASCII contents and would corrupt the buffer contents. This was fixed in 26.2, so we require version 26.2 for this. --- rustic-util.el | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/rustic-util.el b/rustic-util.el index 881ec3fb..ba07c7cf 100644 --- a/rustic-util.el +++ b/rustic-util.el @@ -103,10 +103,16 @@ Use `:command' when formatting files and `:stdin' for strings." (inhibit-read-only t)) (with-current-buffer proc-buffer (if (string-match-p "^finished" output) - (let ((file-buffer next-error-last-buffer)) - (copy-to-buffer file-buffer (point-min) (point-max)) + (let ((file-buffer next-error-last-buffer) + ;; replace-buffer-contents was in emacs 26.1, but it + ;; was broken for non-ASCII strings, so we need 26.2. + (use-replace (version<= "26.2" emacs-version))) + (unless use-replace + (copy-to-buffer file-buffer (point-min) (point-max))) (with-current-buffer file-buffer - (goto-char rustic-save-pos)) + (if use-replace + (replace-buffer-contents proc-buffer) + (goto-char rustic-save-pos))) (kill-buffer proc-buffer) (message "Formatted buffer with rustfmt.")) (goto-char (point-min))