Function: cl-substitute
cl-substitute is an autoloaded and byte-compiled function defined in
cl-seq.el.gz.
Signature
(cl-substitute NEW OLD SEQ [KEYWORD VALUE]...)
Documentation
Substitute NEW for OLD in SEQ.
This is a non-destructive function; it makes a copy of SEQ if necessary to avoid corrupting the original SEQ.
Keywords supported: :test :test-not :key :count :start :end :from-end
Aliases
substitute (obsolete since 27.1)
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/cl-seq.el.gz
;;;###autoload
(defun cl-substitute (new old seq &rest cl-keys)
"Substitute NEW for OLD in SEQ.
This is a non-destructive function; it makes a copy of SEQ if necessary
to avoid corrupting the original SEQ.
\nKeywords supported: :test :test-not :key :count :start :end :from-end
\n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
(declare (important-return-value t))
(cl--parsing-keywords ( :test :test-not :key :if :if-not :count
(:start 0) :end :from-end) ()
(if (or (eq old new)
(<= (or cl-count (setq cl-from-end nil
cl-count (length seq))) 0))
seq
(let ((i (cl--position old seq cl-start cl-end)))
(if (not i)
seq
(setq seq (copy-sequence seq))
(unless cl-from-end
(setf (elt seq i) new)
(incf i)
(decf cl-count))
(apply #'cl-nsubstitute new old seq :count cl-count
:start i cl-keys))))))