Function: apheleia--execute-formatter-process
apheleia--execute-formatter-process is a natively compiled function
defined in apheleia-formatters.el.
Signature
(apheleia--execute-formatter-process &key CTX CALLBACK ENSURE EXIT-STATUS)
Documentation
Wrapper for make-process that behaves a bit more nicely.
CTX is a formatter process context (see
apheleia-formatter--context(var)/apheleia-formatter--context(fun)). CALLBACK is invoked with two
arguments. The first is an error or nil. The second is the buffer
containing the text from stdout, when the process terminates (if
it succeeds). ENSURE is a callback that's invoked whether the
process exited successfully or not. EXIT-STATUS is a function
which is called with the exit status of the command; it should
return non-nil to indicate that the command succeeded. If
EXIT-STATUS is omitted, then the command succeeds provided that
its exit status is 0.
Source Code
;; Defined in /nix/store/lwcwryzabcmvjcwwzzs65jrwf7p7d7cs-emacs-packages-deps/share/emacs/site-lisp/elpa/apheleia-20260915.1628/apheleia-formatters.el
(cl-defun apheleia--execute-formatter-process
(&key ctx callback ensure exit-status)
"Wrapper for `make-process' that behaves a bit more nicely.
CTX is a formatter process context (see
`apheleia-formatter--context'). CALLBACK is invoked with two
arguments. The first is an error or nil. The second is the buffer
containing the text from stdout, when the process terminates (if
it succeeds). ENSURE is a callback that's invoked whether the
process exited successfully or not. EXIT-STATUS is a function
which is called with the exit status of the command; it should
return non-nil to indicate that the command succeeded. If
EXIT-STATUS is omitted, then the command succeeds provided that
its exit status is 0."
(apheleia--log
'process "Trying to execute formatter process %s with %S"
(apheleia-formatter--name ctx)
`(,(apheleia-formatter--arg1 ctx)
,@(apheleia-formatter--argv ctx)))
(when (process-live-p apheleia--current-process)
(apheleia--log
'process "Interrupting an existing process %S" apheleia--current-process)
(message "Interrupting %s" apheleia--current-process)
(process-put apheleia--current-process :interrupted t)
(interrupt-process apheleia--current-process)
(accept-process-output apheleia--current-process 0.1 nil 'just-this-one)
(when (process-live-p apheleia--current-process)
(kill-process apheleia--current-process)))
(let* ((name (file-name-nondirectory (apheleia-formatter--arg1 ctx)))
(stdout (generate-new-buffer
(format " *apheleia-%s-stdout*" name)))
(stderr (generate-new-buffer
(format " *apheleia-%s-stderr*" name)))
(log-name (apheleia-log--buffer-name name)))
(condition-case-unless-debug e
(progn
(setq apheleia--current-process
(funcall
(if (apheleia-formatter--remote ctx)
#'apheleia--call-process
#'apheleia--make-process)
:name (format "apheleia-%s" name)
:stdin (apheleia-formatter--stdin ctx)
:stdout stdout
:stderr stderr
:command `(,(apheleia-formatter--arg1 ctx)
,@(apheleia-formatter--argv ctx))
:remote (apheleia-formatter--remote ctx)
:connection-type 'pipe
:noquery t
:callback
(lambda (proc-exit-status proc-interrupted)
(apheleia--log
'process
"Process %s exited with status %S%s"
name
proc-exit-status
(if proc-interrupted
" (interrupted)"
" (not interrupted)"))
(setf (apheleia-formatter--exit-status ctx)
proc-exit-status)
(let ((exit-ok (and
(not proc-interrupted)
(or
(funcall
(or exit-status #'zerop)
(apheleia-formatter--exit-status ctx))
(apheleia-script--formatter-not-available-p
ctx
(with-current-buffer stderr
(string-trim (buffer-string))))))))
;; Append standard-error from current formatter
;; to log buffer when
;; `apheleia-log-only-errors' is nil or the
;; formatter failed. Every process output is
;; delimited by a line-feed character.
(unless (and exit-ok apheleia-log-only-errors)
(apheleia-log--formatter-result
ctx
log-name
exit-ok
(buffer-local-value 'default-directory stdout)
(with-current-buffer stderr
(string-trim (buffer-string)))))
(when (apheleia-formatter--name ctx)
(apheleia--log
'hook
"Invoking apheleia-formatter-exited-hook")
(run-hook-with-args
'apheleia-formatter-exited-hook
:formatter (apheleia-formatter--name ctx)
:error (not exit-ok)
:log (get-buffer log-name)))
(unwind-protect
(if exit-ok
(funcall callback nil stdout)
(let ((errmsg
(format
(concat
"Failed to run %s: exit status %s "
"(see %s %s)")
(apheleia-formatter--arg1 ctx)
proc-exit-status
(if (string-prefix-p " " log-name)
"hidden buffer"
"buffer")
(string-trim log-name))))
(message "%s" errmsg)
(when noninteractive
(message
"%s"
(concat
"(log buffer shown"
" below in batch mode)\n"
(with-current-buffer log-name
(buffer-string)))))
(funcall
callback (cons 'error errmsg) nil)))
(when ensure
(funcall ensure))
(ignore-errors
(kill-buffer stdout))
(ignore-errors
(kill-buffer stderr))))))))
(error
(ignore-errors
(kill-buffer stdout))
(ignore-errors
(kill-buffer stderr))
(message "Failed to run %s: %s" name (error-message-string e))))))