Function: comint--intersect-regions

comint--intersect-regions is a byte-compiled function defined in comint.el.gz.

Signature

(comint--intersect-regions FUN-OUTPUT FUN-INPUT BEG END)

Documentation

Iterate over comint output and input regions between BEG and END.

Divide the region specified by BEG and END into smaller regions that cover either process output (its field property is output) or input (all remaining text). Interchangeably call FUN-OUTPUT on each output region, and FUN-INPUT on each input region.

FUN-OUTPUT and FUN-INPUT are passed two arguments, the beginning and end of the smaller region. Before calling each function, narrow the buffer to the surrounding process output or input. You can also pass nil as either function to skip its corresponding regions.

Return a cons cell of return values of the first and last function called, or nil, if no function was called (if BEG = END).

Source Code

;; Defined in /usr/src/emacs/lisp/comint.el.gz
(defun comint--intersect-regions (fun-output fun-input beg end)
  "Iterate over comint output and input regions between BEG and END.
Divide the region specified by BEG and END into smaller regions
that cover either process output (its `field' property is `output')
or input (all remaining text).  Interchangeably call FUN-OUTPUT
on each output region, and FUN-INPUT on each input region.

FUN-OUTPUT and FUN-INPUT are passed two arguments, the beginning
and end of the smaller region.  Before calling each function,
narrow the buffer to the surrounding process output or input.  You
can also pass nil as either function to skip its corresponding
regions.

Return a cons cell of return values of the first and last
function called, or nil, if no function was called (if BEG = END)."
  (let ((beg1 beg)
        (end1 (copy-marker nil t))
        (return-beg nil) (return-end nil)
        (is-output (eq (get-text-property beg 'field) 'output)))
    (setq end (copy-marker end t))

    (while (< beg1 end)
      (set-marker
       end1 (or (if is-output
                    (text-property-not-all beg1 end 'field 'output)
                  (text-property-any beg1 end 'field 'output))
                end))
      (when-let ((fun (if is-output fun-output fun-input)))
        (save-restriction
          (let ((beg2 beg1)
                (end2 end1))
            (when (and (= beg2 beg)
                       (> beg2 (point-min))
                       (eq is-output
                           (eq (get-text-property (1- beg2) 'field) 'output)))
              (setq beg2 (field-beginning beg2)))
            (when (and (= end2 end)
                       (< end2 (point-max))
                       (eq is-output
                           (eq (get-text-property (1+ end2) 'field) 'output)))
              (setq end2 (field-end end2)))
            ;; Narrow to the whole field surrounding the region
            (narrow-to-region beg2 end2))
          (setq return-end (list (funcall fun beg1
                                          (marker-position end1)))))
        (unless return-beg
          (setq return-beg return-end)))
      (setq beg1 (marker-position end1))
      (setq is-output (not is-output)))

    (set-marker end nil)
    (set-marker end1 nil)
    (when return-beg
      (cons (car return-beg) (car return-end)))))