Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions nix-format.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@
(defun nix--replace-buffer-contents (src dst)
(if (fboundp 'replace-buffer-contents)
(with-current-buffer dst (replace-buffer-contents src))
(unless (string= (with-current-buffer src (buffer-string))
(with-current-buffer dst (buffer-string)))
(unless (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))))))
(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))
(nix--replace-buffer-contents (current-buffer) buf)
(error "Nixfmt failed, see *nixfmt* buffer for details"))))
"Format BUF using program NIXFMT-BIN."
(let ((temp-file (make-nearby-temp-file "nixfmt")))
(unwind-protect
(let ((out-buf (get-buffer-create "*nixfmt*")))
(with-current-buffer out-buf
(erase-buffer))
(with-current-buffer buf
(write-region (point-min) (point-max) temp-file nil 'no-visit nil nil))
(if (zerop (process-file nixfmt-bin temp-file out-buf))
(nix--replace-buffer-contents out-buf buf)
(error "Nixfmt failed, see *nixfmt* buffer for details")))
(delete-file temp-file))))

(defun nix--find-nixfmt ()
"Find the nixfmt binary, or error if it's missing."
(let ((nixfmt-bin (executable-find nix-nixfmt-bin)))
(let ((nixfmt-bin (executable-find nix-nixfmt-bin t)))
(unless nixfmt-bin
(error "Could not locate executable %S" nix-nixfmt-bin))
nixfmt-bin))
Expand Down