Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #276: RCS patch for missing stdin on Windows #334

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
37 changes: 26 additions & 11 deletions apheleia-formatters.el
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,15 @@ See `make-temp-file' for a description of PREFIX, DIR-FLAG, and SUFFIX."
#'make-temp-file)
prefix dir-flag suffix))

(defun apheleia--make-temp-file-for-rcs-patch-windows (buffer &optional filename remote)
"Create a temporary file for the given BUFFER, using `apheleia--make-temp-file`.
If FILENAME is provided, it will be used as the name of the temp file.
If REMOTE is non-nil, the file will be created on a remote machine."
(let ((temp-file (apheleia--make-temp-file remote "apheleia-rcs-patch-" nil ".txt")))
(with-current-buffer buffer
(write-region (point-min) (point-max) temp-file))
temp-file))

(defun apheleia--create-rcs-patch (old-buffer new-buffer remote callback)
"Generate RCS patch from text in OLD-BUFFER to text in NEW-BUFFER.
Once finished, invoke CALLBACK with a buffer containing the patch
Expand Down Expand Up @@ -876,26 +885,32 @@ See `apheleia--run-formatters' for a description of REMOTE."
(unless (or old-fname new-fname)
(setq new-fname (apheleia--make-temp-file-for-rcs-patch new-buffer))))


(let ((ctx (apheleia-formatter--context)))
(setf (apheleia-formatter--name ctx) nil ; Skip logging on failure
(apheleia-formatter--arg1 ctx) "diff"
(apheleia-formatter--argv ctx) `("--rcs" "--strip-trailing-cr" "--"
,(or old-fname "-")
,(or new-fname "-"))
(apheleia-formatter--remote ctx) remote
(setf (apheleia-formatter--name ctx) nil ; Skip logging on failure
(apheleia-formatter--arg1 ctx) "diff" ; Set the first argument to "diff"
(apheleia-formatter--argv ctx)
(if (eq system-type 'windows-nt) ; Check if we're on Windows
(if (or old-fname new-fname) ; If files exist, use them
`("--rcs" "--strip-trailing-cr" "--"
,(or old-fname (apheleia--make-temp-file-for-rcs-patch-windows old-buffer)) ; Ensure temp file if none exists
,(or new-fname (apheleia--make-temp-file-for-rcs-patch-windows new-buffer)))) ; Same for new-fname
`("--rcs" "--strip-trailing-cr" "--"
,(or old-fname "-") ; Use "-" for stdin on Unix-like systems
,(or new-fname "-"))) ; Same for second file
(apheleia-formatter--remote ctx) remote ; Set the remote variable
(apheleia-formatter--stdin ctx)
(if new-fname old-buffer new-buffer))

(if new-fname old-buffer new-buffer)) ; Use buffers as needed
(apheleia--execute-formatter-process
:ctx ctx
:callback callback
:ensure
(lambda ()
(dolist (file clear-files)
(ignore-errors
(delete-file file))))
;; Exit status is 0 if no changes, 1 if some changes, and 2 if
;; error.
(delete-file file)))) ;; Clean up temporary files
;; Exit status is 0 if no changes, 1 if some changes, and 2 if error.
:exit-status (lambda (status) (memq status '(0 1)))))))

(defun apheleia--safe-buffer-name ()
Expand Down
Loading