emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/rust-mode 39f32cc 447/486: In emacs >= 26.2, use replace-b


From: ELPA Syncer
Subject: [nongnu] elpa/rust-mode 39f32cc 447/486: In emacs >= 26.2, use replace-buffer-contents after formatting
Date: Sat, 7 Aug 2021 09:26:12 -0400 (EDT)

branch: elpa/rust-mode
commit 39f32ccd6a1183df26a223a23db255a67076cd2d
Author: Trevor Spiteri <tspiteri@ieee.org>
Commit: Nathan Moreau <nathan.moreau@m4x.org>

    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 save positions of direct and indirect buffers and windows.
    This also 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.
---
 rust-mode.el | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/rust-mode.el b/rust-mode.el
index 1aa4e2e..9d6040b 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -1463,7 +1463,13 @@ This is written mainly to be used as 
`end-of-defun-function' for Rust."
              ((zerop ret)
               (if (not (string= (buffer-string)
                                 (with-current-buffer buf (buffer-string))))
-                  (copy-to-buffer buf (point-min) (point-max)))
+                  ;; replace-buffer-contents was in emacs 26.1, but it
+                  ;; was broken for non-ASCII strings, so we need 26.2.
+                  (if (and (fboundp 'replace-buffer-contents)
+                           (version<= "26.2" emacs-version))
+                      (with-current-buffer buf
+                        (replace-buffer-contents rust-rustfmt-buffername))
+                    (copy-to-buffer buf (point-min) (point-max))))
               (kill-buffer))
              ((= ret 3)
               (if (not (string= (buffer-string)
@@ -1661,12 +1667,16 @@ Return the created process."
           (pop-to-buffer (current-buffer)))
       (message "rustfmt check passed."))))
 
-(defun rust-format-buffer ()
-  "Format the current buffer using rustfmt."
-  (interactive)
-  (unless (executable-find rust-rustfmt-bin)
-    (error "Could not locate executable \"%s\"" rust-rustfmt-bin))
+(defun rust--format-buffer-using-replace-buffer-contents ()
+  (condition-case err
+      (progn
+        (rust--format-call (current-buffer))
+        (message "Formatted buffer with rustfmt."))
+    (error
+     (or (rust--format-error-handler)
+         (signal (car err) (cdr err))))))
 
+(defun rust--format-buffer-saving-position-manually ()
   (let* ((current (current-buffer))
          (base (or (buffer-base-buffer current) current))
          buffer-loc
@@ -1714,6 +1724,18 @@ Return the created process."
        (or (rust--format-error-handler)
            (signal (car err) (cdr err)))))))
 
+(defun rust-format-buffer ()
+  "Format the current buffer using rustfmt."
+  (interactive)
+  (unless (executable-find rust-rustfmt-bin)
+    (error "Could not locate executable \"%s\"" rust-rustfmt-bin))
+  ;; If emacs version >= 26.2, we can use replace-buffer-contents to
+  ;; preserve location and markers in buffer, otherwise we can try to
+  ;; save locations as best we can, though we still lose markers.
+  (if (version<= "26.2" emacs-version)
+      (rust--format-buffer-using-replace-buffer-contents)
+    (rust--format-buffer-saving-position-manually)))
+
 (defun rust-enable-format-on-save ()
   "Enable formatting using rustfmt when saving buffer."
   (interactive)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]