Function: format-subtract-regions

format-subtract-regions is a byte-compiled function defined in format.el.gz.

Signature

(format-subtract-regions MINUEND SUBTRAHEND)

Documentation

Remove from the regions in MINUEND the regions in SUBTRAHEND.

A region is a dotted pair (FROM . TO). Both parameters are lists of regions. Each list must contain nonoverlapping, noncontiguous regions, in descending order. The result is also nonoverlapping, noncontiguous, and in descending order. The first element of MINUEND can have a cdr of nil, indicating that the end of that region is not yet known.

Source Code

;; Defined in /usr/src/emacs/lisp/format.el.gz
(defun format-subtract-regions (minu subtra)
  "Remove from the regions in MINUEND the regions in SUBTRAHEND.
A region is a dotted pair (FROM . TO).  Both parameters are lists of
regions.  Each list must contain nonoverlapping, noncontiguous
regions, in descending order.  The result is also nonoverlapping,
noncontiguous, and in descending order.  The first element of MINUEND
can have a cdr of nil, indicating that the end of that region is not
yet known.

\(fn MINUEND SUBTRAHEND)"
  (let* ((minuend (copy-alist minu))
	 (subtrahend (copy-alist subtra))
	 (m (car minuend))
	 (s (car subtrahend))
	 results)
    (while (and minuend subtrahend)
      (cond
       ;; The minuend starts after the subtrahend ends; keep it.
       ((> (car m) (cdr s))
	(push m results)
	(setq minuend (cdr minuend)
	      m (car minuend)))
       ;; The minuend extends beyond the end of the subtrahend.  Chop it off.
       ((or (null (cdr m)) (> (cdr m) (cdr s)))
	(push (cons (1+ (cdr s)) (cdr m)) results)
	(setcdr m (cdr s)))
       ;; The subtrahend starts after the minuend ends; throw it away.
       ((< (cdr m) (car s))
	(setq subtrahend (cdr subtrahend) s (car subtrahend)))
       ;; The subtrahend extends beyond the end of the minuend.  Chop it off.
       (t	;(<= (cdr m) (cdr s)))
	(if (>= (car m) (car s))
	    (setq minuend (cdr minuend) m (car minuend))
	  (setcdr m (1- (car s)))
	  (setq subtrahend (cdr subtrahend) s (car subtrahend))))))
    (nconc (nreverse results) minuend)))