branch: elpa/nix-mode
commit ef92acd96cce9ac5de3c5afc54b82c3f43c38f9f
Author: Aaron L. Zeng <[email protected]>
Commit: Aaron L. Zeng <[email protected]>
nix-format.el: Use replace-buffer-contents when available
`replace-buffer-contents`, introduced in Emacs 26.1, allows a buffer's
contents to be overwritten with another buffer's contents, but leaving
the destination buffer's state intact (point, scrolling, etc.). It
relocates point intelligently, and is designed precisely to be used
with reformatting functions like this.
---
nix-format.el | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/nix-format.el b/nix-format.el
index 1f97e8a086..8d66f88f8b 100644
--- a/nix-format.el
+++ b/nix-format.el
@@ -13,16 +13,22 @@
:group 'nix
:type 'string)
+(if (fboundp 'replace-buffer-contents)
+ (defun nix--replace-buffer-contents (src dst)
+ (with-current-buffer dst (replace-buffer-contents src)))
+ (defun nix--replace-buffer-contents (src dst)
+ (if (not (string= (with-current-buffer src (buffer-string))
+ (with-current-buffer dst (buffer-string))))
+ (with-current-buffer src
+ (copy-to-buffer dst (point-min) (point-max))))))
+
(defun nix--format-call (buf nixfmt-bin)
"Format BUF using nixfmt."
(with-current-buffer (get-buffer-create "*nixfmt*")
(erase-buffer)
(insert-buffer-substring buf)
(if (zerop (call-process-region (point-min) (point-max) nixfmt-bin t t
nil))
- (progn
- (if (not (string= (buffer-string) (with-current-buffer buf
(buffer-string))))
- (copy-to-buffer buf (point-min) (point-max)))
- (kill-buffer))
+ (nix--replace-buffer-contents (current-buffer) buf)
(error "Nixfmt failed, see *nixfmt* buffer for details"))))
(defun nix--find-nixfmt ()