Skip to content

Commit

Permalink
Supply a callback that will create and clean up temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuenho committed Aug 30, 2020
1 parent 612825f commit d514444
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions reformatter.el
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,18 @@ the `reformatter-define' macro."
;; disruption to marker positions and the
;; undo list
(narrow-to-region beg end)
(let ((output-file (if stdout stdout-file input-file)))
(let* ((output-file (if stdout stdout-file input-file))
result-file
(result-callback (lambda (output)
(setq result-file
(and output
(reformatter-temp-file-in-current-directory nil output))))))
(if (functionp output-processor)
(let ((processed-output-file (funcall output-processor output-file)))
(when processed-output-file
(reformatter-replace-buffer-contents-from-file processed-output-file)))
(progn
(funcall output-processor output-file result-callback)
(when result-file
(reformatter-replace-buffer-contents-from-file result-file)
(delete-file result-file)))
(reformatter-replace-buffer-contents-from-file output-file))))
;; If there are no errors then we hide the error buffer
(delete-windows-on error-buffer))
Expand Down Expand Up @@ -309,7 +316,7 @@ DISPLAY-ERRORS, shows a buffer if the formatting fails."
(insert-file-contents file nil nil nil t))


(defun reformatter-temp-file-in-current-directory (&optional default-extension)
(defun reformatter-temp-file-in-current-directory (&optional default-extension text)
"Make a temp file in the current directory re-using the current extension.
If the current file is not backed by a file, then use
DEFAULT-EXTENSION, which should not contain a leading dot."
Expand All @@ -319,7 +326,8 @@ DEFAULT-EXTENSION, which should not contain a leading dot."
default-extension)))
(make-temp-file "reformatter" nil
(when extension
(concat "." extension)))))
(concat "." extension))
text)))

(provide 'reformatter)
;;; reformatter.el ends here

0 comments on commit d514444

Please sign in to comment.