branch: elpa/nix-mode commit 56748ac556d0406cc6c574f7347fe37decd8381e Merge: e61ecb95ef 1789c83b83 Author: Matthew Bauer <mjbaue...@gmail.com> Commit: GitHub <nore...@github.com>
Merge pull request #136 from bcc32/replace-buffer-contents Use replace-buffer-contents for better formatting experience --- nix-format.el | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/nix-format.el b/nix-format.el index 151c16bdd2..a31f577cbb 100644 --- a/nix-format.el +++ b/nix-format.el @@ -13,23 +13,29 @@ :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 () "Find the nixfmt binary, or error if it's missing." (let ((nixfmt-bin (executable-find nix-nixfmt-bin))) (unless nixfmt-bin - (error "Could not locate executable \"%s\"" nix-nixfmt-bin)) + (error "Could not locate executable %S" nix-nixfmt-bin)) nixfmt-bin)) (defun nix-format-buffer ()