Function: ansi-osc-apply-on-region

ansi-osc-apply-on-region is a byte-compiled function defined in ansi-osc.el.gz.

Signature

(ansi-osc-apply-on-region BEGIN END)

Documentation

Interpret OSC escape sequences in region between BEGIN and END.

This function searches for escape sequences of the forms

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

Every occurrence of such escape sequences is removed from the buffer. Then, if command is a key in the alist that is the value of the local variable ansi-osc-handlers, that key's value, which should be a function, is called with command and text as arguments, with point where the escape sequence was located. When an unfinished escape sequence is identified, it's hidden and the start position is saved to ansi-osc--marker. Later call will override BEGIN with the position pointed by ansi-osc--marker.

Source Code

;; Defined in /usr/src/emacs/lisp/ansi-osc.el.gz
(defun ansi-osc-apply-on-region (begin end)
  "Interpret OSC escape sequences in region between BEGIN and END.
This function searches for escape sequences of the forms

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

Every occurrence of such escape sequences is removed from the buffer.
Then, if `command' is a key in the alist that is the value of the local
variable `ansi-osc-handlers', that key's value, which should be a
function, is called with `command' and `text' as arguments, with point
where the escape sequence was located.  When an unfinished escape
sequence is identified, it's hidden and the start position is saved to
`ansi-osc--marker'.  Later call will override BEGIN with the position
pointed by `ansi-osc--marker'."
  (let ((end-marker (copy-marker end)))
    (save-excursion
      (goto-char (or ansi-osc--marker begin))
      (when (eq (char-before) ?\e) (backward-char))
      (while (re-search-forward "\e]" end-marker t)
        (let ((pos0 (match-beginning 0))
              (code (and
                     (re-search-forward "\\=\\([0-9A-Za-z]*\\);" end-marker t)
                     (match-string 1)))
              (pos1 (point)))
          (if (re-search-forward "\a\\|\e\\\\" end-marker t)
              (let ((text (buffer-substring-no-properties
                           pos1 (match-beginning 0))))
                (setq ansi-osc--marker nil)
                (delete-region pos0 (point))
                (when-let* ((fun (cdr (assoc-string code ansi-osc-handlers))))
                  (funcall fun code text)))
            (put-text-property pos0 end-marker 'invisible t)
            (setq ansi-osc--marker (copy-marker pos0))))))))