Function: cl-replace

cl-replace is an autoloaded and byte-compiled function defined in cl-seq.el.gz.

Signature

(cl-replace SEQ1 SEQ2 [KEYWORD VALUE]...)

Documentation

Replace the elements of SEQ1 with the elements of SEQ2.

SEQ1 is destructively modified, then returned.

Keywords supported: :start1 :end1 :start2 :end2

View in manual

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-seq.el.gz
;;;###autoload
(defun cl-replace (seq1 seq2 &rest cl-keys)
  "Replace the elements of SEQ1 with the elements of SEQ2.
SEQ1 is destructively modified, then returned.
\nKeywords supported:  :start1 :end1 :start2 :end2
\n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
  (cl--parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) ()
    (if (and (eq seq1 seq2) (<= cl-start2 cl-start1))
	(or (= cl-start1 cl-start2)
            (let* ((len (length seq1))
                   (n (min (- (or cl-end1 len) cl-start1)
                           (- (or cl-end2 len) cl-start2))))
              (while (>= (setq n (1- n)) 0)
                (setf (elt seq1 (+ cl-start1 n))
                      (elt seq2 (+ cl-start2 n))))))
      (if (listp seq1)
          (let ((p1 (nthcdr cl-start1 seq1))
                (n1 (and cl-end1 (- cl-end1 cl-start1))))
            (if (listp seq2)
                (let ((p2 (nthcdr cl-start2 seq2))
                      (n (cond ((and n1 cl-end2)
                                (min n1 (- cl-end2 cl-start2)))
                               ((and n1 (null cl-end2)) n1)
                               ((and (null n1) cl-end2) (- cl-end2 cl-start2)))))
                  (while (and p1 p2 (or (null n) (>= (decf n) 0)))
                    (setcar p1 (car p2))
                    (setq p1 (cdr p1) p2 (cdr p2))))
              (setq cl-end2 (if (null n1)
                                (or cl-end2 (length seq2))
                              (min (or cl-end2 (length seq2))
                                   (+ cl-start2 n1))))
              (while (and p1 (< cl-start2 cl-end2))
                (setcar p1 (aref seq2 cl-start2))
                (setq p1 (cdr p1) cl-start2 (1+ cl-start2)))))
        (setq cl-end1 (min (or cl-end1 (length seq1))
                           (+ cl-start1 (- (or cl-end2 (length seq2))
					   cl-start2))))
        (if (listp seq2)
            (let ((p2 (nthcdr cl-start2 seq2)))
	      (while (< cl-start1 cl-end1)
                (aset seq1 cl-start1 (car p2))
                (setq p2 (cdr p2) cl-start1 (1+ cl-start1))))
	  (while (< cl-start1 cl-end1)
            (aset seq1 cl-start1 (aref seq2 cl-start2))
	    (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1))))))
    seq1))