Function: comint-osc-process-output

comint-osc-process-output is a byte-compiled function defined in comint.el.gz.

Signature

(comint-osc-process-output _)

Documentation

Interpret OSC escape sequences in comint output.

This function is intended to be added to comint-output-filter-functions in order to interpret escape sequences of the forms

    ESC ] command ; text BEL
    ESC ] command ; text ESC \

Specifically, every occurrence of such escape sequences is removed from the buffer. Then, if command is a key of the comint-osc-handlers alist, the corresponding value, which should be a function, is called with command and text as arguments, with point where the escape sequence was located.

Probably introduced at or before Emacs version 28.1.

Source Code

;; Defined in /usr/src/emacs/lisp/comint.el.gz
(defun comint-osc-process-output (_)
  "Interpret OSC escape sequences in comint output.
This function is intended to be added to
`comint-output-filter-functions' in order to interpret escape
sequences of the forms

    ESC ] command ; text BEL
    ESC ] command ; text ESC \\

Specifically, every occurrence of such escape sequences is
removed from the buffer.  Then, if `command' is a key of the
`comint-osc-handlers' alist, the corresponding value, which
should be a function, is called with `command' and `text' as
arguments, with point where the escape sequence was located."
  (let ((bound (process-mark (get-buffer-process (current-buffer)))))
    (save-excursion
      ;; Start one char before last output to catch a possibly stray ESC
      (goto-char (or comint-osc--marker (1- comint-last-output-start)))
      (when (eq (char-before) ?\e) (backward-char))
      (while (re-search-forward "\e]" bound t)
        (let ((pos0 (match-beginning 0))
              (code (and (re-search-forward "\\=\\([0-9A-Za-z]*\\);" bound t)
                         (match-string 1)))
              (pos1 (point)))
          (if (re-search-forward "\a\\|\e\\\\" bound t)
              (let ((text (buffer-substring-no-properties
                           pos1 (match-beginning 0))))
                (setq comint-osc--marker nil)
                (delete-region pos0 (point))
                (when-let ((fun (cdr (assoc-string code comint-osc-handlers))))
                  (funcall fun code text)))
            (put-text-property pos0 bound 'invisible t)
            (setq comint-osc--marker (copy-marker pos0))))))))