branch: elpa/nix-mode commit c18a24e9ac569a221e88ba9d74d52c7b02b6eb77 Author: Matthew Bauer <mjbaue...@gmail.com> Commit: Matthew Bauer <mjbaue...@gmail.com>
Revert "Replace custom nixfmt wrapping with reformatter" This reverts commit fb6c1ca5dd22580267142f19f78608a99de77552. --- flake.nix | 1 - nix-format.el | 47 ++++++++++++++++++++++++++++++++++++----------- nix-mode.el | 2 +- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/flake.nix b/flake.nix index 53aa5e9dbe..bffea7cd0d 100644 --- a/flake.nix +++ b/flake.nix @@ -16,7 +16,6 @@ f magit-section transient - reformatter ]); in stdenvNoCC.mkDerivation { pname = "nix-mode"; diff --git a/nix-format.el b/nix-format.el index 56558778cb..b478a1c687 100644 --- a/nix-format.el +++ b/nix-format.el @@ -7,23 +7,48 @@ ;;; Commentary: ;;; Code: -(require 'reformatter) (defcustom nix-nixfmt-bin "nixfmt" "Path to nixfmt executable." :group 'nix :type 'string) -;;;###autoload (autoload 'nixfmt-buffer "nix-format") -;;;###autoload (autoload 'nixfmt-region "nix-format") -;;;###autoload (autoload 'nixfmt-on-save-mode "nix-format") -(reformatter-define nixfmt - :program nix-nixfmt-bin - :args (list input-file) - :stdin nil - :stdout nil - :input-file (reformatter-temp-file-in-current-directory) - :group 'nix) +(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)) + (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)) + nixfmt-bin)) + +(defun nix-format-buffer () + "Format the current buffer using nixfmt." + (interactive) + (nix--format-call (current-buffer) (nix--find-nixfmt)) + (message "Formatted buffer with nixfmt.")) + +;;;###autoload +(defun nix-format-before-save () + "Add this to `before-save-hook' to run nixfmt when saving." + (when (derived-mode-p 'nix-mode) + (nix-format-buffer))) (provide 'nix-format) ;;; nix-format.el ends here diff --git a/nix-mode.el b/nix-mode.el index 5810b56285..2e42e238ea 100644 --- a/nix-mode.el +++ b/nix-mode.el @@ -4,7 +4,7 @@ ;; Homepage: https://github.com/NixOS/nix-mode ;; Version: 1.5.0 ;; Keywords: nix, languages, tools, unix -;; Package-Requires: ((emacs "25.1") magit-section (transient "0.3") (reformatter "0.6")) +;; Package-Requires: ((emacs "25.1") magit-section (transient "0.3")) ;; This file is NOT part of GNU Emacs.