Function: cider-repl--ansi-color-apply

cider-repl--ansi-color-apply is a byte-compiled function defined in cider-repl.el.

Signature

(cider-repl--ansi-color-apply STRING)

Documentation

Like ansi-color-apply, but does not withhold non-SGR seqs found in STRING.

Workaround for Emacs bug#53808 whereby partial ANSI control seqs present in the input stream may block the whole colorization process.

Source Code

;; Defined in ~/.emacs.d/elpa/cider-20260414.1619/cider-repl.el
(defun cider-repl--ansi-color-apply (string)
  "Like `ansi-color-apply', but does not withhold non-SGR seqs found in STRING.

Workaround for Emacs bug#53808 whereby partial ANSI control seqs present in
the input stream may block the whole colorization process."
  (let* ((result (ansi-color-apply string))

         ;; The STRING may end with a possible incomplete ANSI control seq which
         ;; the call to `ansi-color-apply' stores in the `ansi-color-context'
         ;; fragment. If the fragment is not an incomplete ANSI color control
         ;; sequence (aka SGR seq) though then flush it out and appended it to
         ;; the result.
         (fragment-flush?
          (when-let (fragment (and ansi-color-context (cadr ansi-color-context)))
            (save-match-data
              ;; Check if fragment is indeed an SGR seq in the making. The SGR
              ;; seq is defined as starting with ESC followed by [ followed by
              ;; zero or more [:digit:]+; followed by one or more digits and
              ;; ending with m.
              (when (string-match
                     (rx (sequence ?\e
                                   (? (and (or ?\[ eol)
                                           (or (+ (any (?0 . ?9))) eol)
                                           (* (sequence ?\; (+ (any (?0 . ?9)))))
                                           (or ?\; eol)))))
                     fragment)
                (let* ((sgr-end-pos (match-end 0))
                       (fragment-matches-whole? (or (= sgr-end-pos 0)
                                                    (= sgr-end-pos (length fragment)))))
                  (unless fragment-matches-whole?
                    ;; Definitely not an partial SGR seq, flush it out of
                    ;; `ansi-color-context'.
                    t)))))))

    (if (not fragment-flush?)
        result

      (progn
        ;; Temporarily replace the ESC char in the fragment so that is flushed
        ;; out of `ansi-color-context' by `ansi-color-apply' and append it to
        ;; the result.
        (aset (cadr ansi-color-context) 0 ?\0)
        (let ((result-fragment (ansi-color-apply "")))
          (aset result-fragment 0 ?\e)
          (concat result result-fragment))))))