Function: range-concat

range-concat is a byte-compiled function defined in range.el.gz.

Signature

(range-concat RANGE1 RANGE2)

Documentation

Add RANGE2 to RANGE1 (nondestructively).

Aliases

gnus-range-add (obsolete since 29.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/range.el.gz
(defun range-concat (range1 range2)
  "Add RANGE2 to RANGE1 (nondestructively)."
  (unless (listp (cdr range1))
    (setq range1 (list range1)))
  (unless (listp (cdr range2))
    (setq range2 (list range2)))
  (let ((item1 (pop range1))
	(item2 (pop range2))
	range item selector)
    (while (or item1 item2)
      (setq selector
	    (cond
	     ((null item1) nil)
	     ((null item2) t)
	     ((and (numberp item1) (numberp item2)) (< item1 item2))
	     ((numberp item1) (< item1 (car item2)))
	     ((numberp item2) (< (car item1) item2))
	     (t (< (car item1) (car item2)))))
      (setq item
	    (or
	     (let ((tmp1 item) (tmp2 (if selector item1 item2)))
	       (cond
		((null tmp1) tmp2)
		((null tmp2) tmp1)
		((and (numberp tmp1) (numberp tmp2))
		 (cond
		  ((eq tmp1 tmp2) tmp1)
		  ((eq (1+ tmp1) tmp2) (cons tmp1 tmp2))
		  ((eq (1+ tmp2) tmp1) (cons tmp2 tmp1))
		  (t nil)))
		((numberp tmp1)
		 (cond
		  ((and (>= tmp1 (car tmp2)) (<= tmp1 (cdr tmp2))) tmp2)
		  ((eq (1+ tmp1) (car tmp2)) (cons tmp1 (cdr tmp2)))
		  ((eq (1- tmp1) (cdr tmp2)) (cons (car tmp2) tmp1))
		  (t nil)))
		((numberp tmp2)
		 (cond
		  ((and (>= tmp2 (car tmp1)) (<= tmp2 (cdr tmp1))) tmp1)
		  ((eq (1+ tmp2) (car tmp1)) (cons tmp2 (cdr tmp1)))
		  ((eq (1- tmp2) (cdr tmp1)) (cons (car tmp1) tmp2))
		  (t nil)))
		((< (1+ (cdr tmp1)) (car tmp2)) nil)
		((< (1+ (cdr tmp2)) (car tmp1)) nil)
		(t (cons (min (car tmp1) (car tmp2))
			 (max (cdr tmp1) (cdr tmp2))))))
	     (progn
	       (if item (push item range))
	       (if selector item1 item2))))
      (if selector
	  (setq item1 (pop range1))
	(setq item2 (pop range2))))
    (if item (push item range))
    (reverse range)))