Function: majutsu-jj-wash

majutsu-jj-wash is a natively compiled function defined in majutsu-jj.el.

Signature

(majutsu-jj-wash WASHER KEEP-ERROR &rest ARGS)

Documentation

Run jj with ARGS, insert output at point, then call WASHER.

KEEP-ERROR matches magit--git-wash: nil drops stderr on error, wash-anyway washes stdout even on non-zero exit and then appends stderr, and anything else keeps the error text without washing stdout. Output is optionally colorized based on majutsu-process-apply-ansi-colors.

Source Code

;; Defined in /nix/store/jagqw3nms546pad0gxi5f3wsckk6pji7-emacs-packages-deps/share/emacs/site-lisp/majutsu-jj.el
(defun majutsu-jj-wash (washer keep-error &rest args)
  "Run jj with ARGS, insert output at point, then call WASHER.
KEEP-ERROR matches `magit--git-wash': nil drops stderr on error,
`wash-anyway' washes stdout even on non-zero exit and then appends stderr,
and anything else keeps the error text without washing stdout.  Output is
optionally colorized based on `majutsu-process-apply-ansi-colors'."
  (declare (indent 2))
  (setq args (flatten-tree args))
  (let* ((beg (point))
         (err-file (make-nearby-temp-file "majutsu-jj-err")))
    (unwind-protect
        (let* ((exit
                ;; jj log's structured row protocol is temporarily inserted
                ;; as raw subprocess output.  Do not let it be redisplayed
                ;; before the washer replaces it with sections.
                (let ((inhibit-redisplay t))
                  (majutsu-process-jj (list t err-file) args)))
               (error-text
                (when (and keep-error (not (= exit 0)))
                  (let ((text
                         (ansi-color-filter-apply
                          (with-temp-buffer
                            (insert-file-contents err-file)
                            (buffer-string)))))
                    (unless (string-empty-p text)
                      text)))))
          (when (and (bound-and-true-p majutsu-process-apply-ansi-colors)
                     (> (point) beg))
            ;; Use text-properties instead of overlays so that subsequent
            ;; washing/parsing that uses `buffer-substring' preserves faces.
            (let ((ansi-color-apply-face-function
                   #'ansi-color-apply-text-property-face))
              (ansi-color-apply-on-region beg (point))))
          (cond
           ;; Command produced no output.
           ((= (point) beg)
            (if (= exit 0)
                (magit-cancel-section)
              (majutsu-jj--insert-failure-output args exit error-text)))
           ;; Failure path (unless we explicitly wash anyway).
           ((and (not (eq keep-error 'wash-anyway))
                 (not (= exit 0)))
            (goto-char beg)
            (majutsu-jj--insert-failure-output args exit error-text))
           ;; Success (or wash anyway).
           (t
            (unless (bolp)
              (insert "\n"))
            (when (or (= exit 0)
                      (eq keep-error 'wash-anyway))
              (let ((stdout-end (copy-marker (point) t)))
                (unwind-protect
                    (progn
                      ;; Structured washers such as
                      ;; `majutsu-diff-wash-diffs' must only ever see stdout.
                      (save-restriction
                        (narrow-to-region beg stdout-end)
                        (goto-char beg)
                        (funcall washer args))
                      (let ((washer-point (point)))
                        (goto-char stdout-end)
                        (if (and (not (= exit 0))
                                 (eq keep-error 'wash-anyway))
                            ;; Diagnostics are deliberately outside the
                            ;; washer's narrowed region.  A stderr line that
                            ;; resembles structured output cannot confuse the
                            ;; parser or prevent it from advancing.
                            (majutsu-jj--insert-failure-output
                             args exit error-text)
                          (when (or (= washer-point beg)
                                    (= washer-point (1+ beg)))
                            (magit-cancel-section)))))
                  (set-marker stdout-end nil))))))
          exit)
      (ignore-errors (delete-file err-file)))))