Commit | Line | Data |
---|---|---|
bb3faf5b | 1 | ;;; cl-seq.el --- Common Lisp features, part 3 -*- lexical-binding: t -*- |
fcd73769 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1993, 2001-2014 Free Software Foundation, Inc. |
fcd73769 RS |
4 | |
5 | ;; Author: Dave Gillespie <daveg@synaptics.com> | |
12059709 | 6 | ;; Old-Version: 2.02 |
fcd73769 | 7 | ;; Keywords: extensions |
bd78fa1d | 8 | ;; Package: emacs |
fcd73769 RS |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
d6cba7ae | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
fcd73769 | 13 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
fcd73769 RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
fcd73769 | 24 | |
07b3798c | 25 | ;;; Commentary: |
fcd73769 RS |
26 | |
27 | ;; These are extensions to Emacs Lisp that provide a degree of | |
28 | ;; Common Lisp compatibility, beyond what is already built-in | |
29 | ;; in Emacs Lisp. | |
30 | ;; | |
31 | ;; This package was written by Dave Gillespie; it is a complete | |
32 | ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
33 | ;; | |
fcd73769 RS |
34 | ;; Bug reports, comments, and suggestions are welcome! |
35 | ||
36 | ;; This file contains the Common Lisp sequence and list functions | |
37 | ;; which take keyword arguments. | |
38 | ||
39 | ;; See cl.el for Change Log. | |
40 | ||
41 | ||
07b3798c | 42 | ;;; Code: |
fcd73769 | 43 | |
7c1898a7 | 44 | (require 'cl-lib) |
fcd73769 | 45 | |
bb3faf5b SM |
46 | ;; Keyword parsing. |
47 | ;; This is special-cased here so that we can compile | |
48 | ;; this file independent from cl-macs. | |
fcd73769 | 49 | |
bb3faf5b | 50 | (defmacro cl--parsing-keywords (kwords other-keys &rest body) |
f291fe60 | 51 | (declare (indent 2) (debug (sexp sexp &rest form))) |
bb3faf5b SM |
52 | `(let* ,(mapcar |
53 | (lambda (x) | |
54 | (let* ((var (if (consp x) (car x) x)) | |
55 | (mem `(car (cdr (memq ',var cl-keys))))) | |
56 | (if (eq var :test-not) | |
57 | (setq mem `(and ,mem (setq cl-test ,mem) t))) | |
58 | (if (eq var :if-not) | |
59 | (setq mem `(and ,mem (setq cl-if ,mem) t))) | |
60 | (list (intern | |
61 | (format "cl-%s" (substring (symbol-name var) 1))) | |
62 | (if (consp x) `(or ,mem ,(car (cdr x))) mem)))) | |
63 | kwords) | |
64 | ,@(append | |
65 | (and (not (eq other-keys t)) | |
66 | (list | |
67 | (list 'let '((cl-keys-temp cl-keys)) | |
68 | (list 'while 'cl-keys-temp | |
69 | (list 'or (list 'memq '(car cl-keys-temp) | |
70 | (list 'quote | |
71 | (mapcar | |
72 | (function | |
73 | (lambda (x) | |
74 | (if (consp x) | |
75 | (car x) x))) | |
76 | (append kwords | |
77 | other-keys)))) | |
78 | '(car (cdr (memq (quote :allow-other-keys) | |
79 | cl-keys))) | |
80 | '(error "Bad keyword argument %s" | |
81 | (car cl-keys-temp))) | |
82 | '(setq cl-keys-temp (cdr (cdr cl-keys-temp))))))) | |
83 | body))) | |
84 | ||
85 | (defmacro cl--check-key (x) ;Expects `cl-key' in context of generated code. | |
f291fe60 | 86 | (declare (debug edebug-forms)) |
bb3faf5b | 87 | `(if cl-key (funcall cl-key ,x) ,x)) |
fcd73769 | 88 | |
bb3faf5b | 89 | (defmacro cl--check-test-nokey (item x) ;cl-test cl-if cl-test-not cl-if-not. |
f291fe60 | 90 | (declare (debug edebug-forms)) |
bb3faf5b SM |
91 | `(cond |
92 | (cl-test (eq (not (funcall cl-test ,item ,x)) | |
93 | cl-test-not)) | |
94 | (cl-if (eq (not (funcall cl-if ,x)) cl-if-not)) | |
95 | (t (eql ,item ,x)))) | |
96 | ||
97 | (defmacro cl--check-test (item x) ;all of the above. | |
f291fe60 | 98 | (declare (debug edebug-forms)) |
bb3faf5b | 99 | `(cl--check-test-nokey ,item (cl--check-key ,x))) |
fcd73769 | 100 | |
bb3faf5b | 101 | (defmacro cl--check-match (x y) ;cl-key cl-test cl-test-not |
f291fe60 | 102 | (declare (debug edebug-forms)) |
bb3faf5b SM |
103 | (setq x `(cl--check-key ,x) y `(cl--check-key ,y)) |
104 | `(if cl-test | |
105 | (eq (not (funcall cl-test ,x ,y)) cl-test-not) | |
106 | (eql ,x ,y))) | |
fcd73769 | 107 | |
338bfefa SM |
108 | ;; Yuck! These vars are set/bound by cl--parsing-keywords to match :if :test |
109 | ;; and :key keyword args, and they are also accessed (sometimes) via dynamic | |
110 | ;; scoping (and some of those accesses are from macro-expanded code). | |
fcd73769 RS |
111 | (defvar cl-test) (defvar cl-test-not) |
112 | (defvar cl-if) (defvar cl-if-not) | |
113 | (defvar cl-key) | |
114 | ||
323698cc | 115 | ;;;###autoload |
7c1898a7 | 116 | (defun cl-reduce (cl-func cl-seq &rest cl-keys) |
47bc4b3f JB |
117 | "Reduce two-argument FUNCTION across SEQ. |
118 | \nKeywords supported: :start :end :from-end :initial-value :key | |
119 | \n(fn FUNCTION SEQ [KEYWORD VALUE]...)" | |
bb3faf5b | 120 | (cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) () |
fcd73769 | 121 | (or (listp cl-seq) (setq cl-seq (append cl-seq nil))) |
7c1898a7 | 122 | (setq cl-seq (cl-subseq cl-seq cl-start cl-end)) |
fcd73769 | 123 | (if cl-from-end (setq cl-seq (nreverse cl-seq))) |
64a4c526 | 124 | (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value) |
bb3faf5b | 125 | (cl-seq (cl--check-key (pop cl-seq))) |
fcd73769 RS |
126 | (t (funcall cl-func))))) |
127 | (if cl-from-end | |
128 | (while cl-seq | |
bb3faf5b | 129 | (setq cl-accum (funcall cl-func (cl--check-key (pop cl-seq)) |
fcd73769 RS |
130 | cl-accum))) |
131 | (while cl-seq | |
132 | (setq cl-accum (funcall cl-func cl-accum | |
bb3faf5b | 133 | (cl--check-key (pop cl-seq)))))) |
fcd73769 RS |
134 | cl-accum))) |
135 | ||
323698cc | 136 | ;;;###autoload |
7c1898a7 | 137 | (defun cl-fill (seq item &rest cl-keys) |
fcd73769 | 138 | "Fill the elements of SEQ with ITEM. |
47bc4b3f JB |
139 | \nKeywords supported: :start :end |
140 | \n(fn SEQ ITEM [KEYWORD VALUE]...)" | |
bb3faf5b | 141 | (cl--parsing-keywords ((:start 0) :end) () |
fcd73769 RS |
142 | (if (listp seq) |
143 | (let ((p (nthcdr cl-start seq)) | |
144 | (n (if cl-end (- cl-end cl-start) 8000000))) | |
145 | (while (and p (>= (setq n (1- n)) 0)) | |
146 | (setcar p item) | |
147 | (setq p (cdr p)))) | |
148 | (or cl-end (setq cl-end (length seq))) | |
149 | (if (and (= cl-start 0) (= cl-end (length seq))) | |
150 | (fillarray seq item) | |
151 | (while (< cl-start cl-end) | |
152 | (aset seq cl-start item) | |
153 | (setq cl-start (1+ cl-start))))) | |
154 | seq)) | |
155 | ||
323698cc | 156 | ;;;###autoload |
7c1898a7 | 157 | (defun cl-replace (cl-seq1 cl-seq2 &rest cl-keys) |
fcd73769 RS |
158 | "Replace the elements of SEQ1 with the elements of SEQ2. |
159 | SEQ1 is destructively modified, then returned. | |
47bc4b3f JB |
160 | \nKeywords supported: :start1 :end1 :start2 :end2 |
161 | \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)" | |
bb3faf5b | 162 | (cl--parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) () |
fcd73769 RS |
163 | (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1)) |
164 | (or (= cl-start1 cl-start2) | |
165 | (let* ((cl-len (length cl-seq1)) | |
166 | (cl-n (min (- (or cl-end1 cl-len) cl-start1) | |
167 | (- (or cl-end2 cl-len) cl-start2)))) | |
168 | (while (>= (setq cl-n (1- cl-n)) 0) | |
d6f14ca7 | 169 | (setf (elt cl-seq1 (+ cl-start1 cl-n)) |
fcd73769 RS |
170 | (elt cl-seq2 (+ cl-start2 cl-n)))))) |
171 | (if (listp cl-seq1) | |
172 | (let ((cl-p1 (nthcdr cl-start1 cl-seq1)) | |
173 | (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000))) | |
174 | (if (listp cl-seq2) | |
175 | (let ((cl-p2 (nthcdr cl-start2 cl-seq2)) | |
176 | (cl-n (min cl-n1 | |
177 | (if cl-end2 (- cl-end2 cl-start2) 4000000)))) | |
178 | (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0)) | |
179 | (setcar cl-p1 (car cl-p2)) | |
180 | (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))) | |
181 | (setq cl-end2 (min (or cl-end2 (length cl-seq2)) | |
182 | (+ cl-start2 cl-n1))) | |
183 | (while (and cl-p1 (< cl-start2 cl-end2)) | |
184 | (setcar cl-p1 (aref cl-seq2 cl-start2)) | |
185 | (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2))))) | |
186 | (setq cl-end1 (min (or cl-end1 (length cl-seq1)) | |
187 | (+ cl-start1 (- (or cl-end2 (length cl-seq2)) | |
188 | cl-start2)))) | |
189 | (if (listp cl-seq2) | |
190 | (let ((cl-p2 (nthcdr cl-start2 cl-seq2))) | |
191 | (while (< cl-start1 cl-end1) | |
192 | (aset cl-seq1 cl-start1 (car cl-p2)) | |
193 | (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1)))) | |
194 | (while (< cl-start1 cl-end1) | |
195 | (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2)) | |
196 | (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1)))))) | |
197 | cl-seq1)) | |
198 | ||
323698cc | 199 | ;;;###autoload |
7c1898a7 | 200 | (defun cl-remove (cl-item cl-seq &rest cl-keys) |
fcd73769 RS |
201 | "Remove all occurrences of ITEM in SEQ. |
202 | This is a non-destructive function; it makes a copy of SEQ if necessary | |
203 | to avoid corrupting the original SEQ. | |
47bc4b3f JB |
204 | \nKeywords supported: :test :test-not :key :count :start :end :from-end |
205 | \n(fn ITEM SEQ [KEYWORD VALUE]...)" | |
bb3faf5b | 206 | (cl--parsing-keywords (:test :test-not :key :if :if-not :count :from-end |
fcd73769 RS |
207 | (:start 0) :end) () |
208 | (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
209 | cl-seq | |
210 | (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000))) | |
4735906a SM |
211 | (let ((cl-i (cl--position cl-item cl-seq cl-start cl-end |
212 | cl-from-end))) | |
fcd73769 | 213 | (if cl-i |
7c1898a7 | 214 | (let ((cl-res (apply 'cl-delete cl-item (append cl-seq nil) |
fcd73769 | 215 | (append (if cl-from-end |
64a4c526 DL |
216 | (list :end (1+ cl-i)) |
217 | (list :start cl-i)) | |
fcd73769 RS |
218 | cl-keys)))) |
219 | (if (listp cl-seq) cl-res | |
220 | (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))) | |
221 | cl-seq)) | |
222 | (setq cl-end (- (or cl-end 8000000) cl-start)) | |
223 | (if (= cl-start 0) | |
224 | (while (and cl-seq (> cl-end 0) | |
bb3faf5b | 225 | (cl--check-test cl-item (car cl-seq)) |
fcd73769 RS |
226 | (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) |
227 | (> (setq cl-count (1- cl-count)) 0)))) | |
228 | (if (and (> cl-count 0) (> cl-end 0)) | |
229 | (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq) | |
230 | (setq cl-end (1- cl-end)) (cdr cl-seq)))) | |
231 | (while (and cl-p (> cl-end 0) | |
bb3faf5b | 232 | (not (cl--check-test cl-item (car cl-p)))) |
fcd73769 RS |
233 | (setq cl-p (cdr cl-p) cl-end (1- cl-end))) |
234 | (if (and cl-p (> cl-end 0)) | |
7c1898a7 | 235 | (nconc (cl-ldiff cl-seq cl-p) |
fcd73769 RS |
236 | (if (= cl-count 1) (cdr cl-p) |
237 | (and (cdr cl-p) | |
7c1898a7 | 238 | (apply 'cl-delete cl-item |
fcd73769 | 239 | (copy-sequence (cdr cl-p)) |
64a4c526 DL |
240 | :start 0 :end (1- cl-end) |
241 | :count (1- cl-count) cl-keys)))) | |
fcd73769 RS |
242 | cl-seq)) |
243 | cl-seq))))) | |
244 | ||
323698cc | 245 | ;;;###autoload |
7c1898a7 | 246 | (defun cl-remove-if (cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
247 | "Remove all items satisfying PREDICATE in SEQ. |
248 | This is a non-destructive function; it makes a copy of SEQ if necessary | |
249 | to avoid corrupting the original SEQ. | |
47bc4b3f JB |
250 | \nKeywords supported: :key :count :start :end :from-end |
251 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 252 | (apply 'cl-remove nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 253 | |
323698cc | 254 | ;;;###autoload |
7c1898a7 | 255 | (defun cl-remove-if-not (cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
256 | "Remove all items not satisfying PREDICATE in SEQ. |
257 | This is a non-destructive function; it makes a copy of SEQ if necessary | |
258 | to avoid corrupting the original SEQ. | |
47bc4b3f JB |
259 | \nKeywords supported: :key :count :start :end :from-end |
260 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 261 | (apply 'cl-remove nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 262 | |
323698cc | 263 | ;;;###autoload |
7c1898a7 | 264 | (defun cl-delete (cl-item cl-seq &rest cl-keys) |
fcd73769 RS |
265 | "Remove all occurrences of ITEM in SEQ. |
266 | This is a destructive function; it reuses the storage of SEQ whenever possible. | |
47bc4b3f JB |
267 | \nKeywords supported: :test :test-not :key :count :start :end :from-end |
268 | \n(fn ITEM SEQ [KEYWORD VALUE]...)" | |
bb3faf5b | 269 | (cl--parsing-keywords (:test :test-not :key :if :if-not :count :from-end |
fcd73769 RS |
270 | (:start 0) :end) () |
271 | (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
272 | cl-seq | |
273 | (if (listp cl-seq) | |
274 | (if (and cl-from-end (< cl-count 4000000)) | |
275 | (let (cl-i) | |
276 | (while (and (>= (setq cl-count (1- cl-count)) 0) | |
4735906a SM |
277 | (setq cl-i (cl--position cl-item cl-seq cl-start |
278 | cl-end cl-from-end))) | |
fcd73769 RS |
279 | (if (= cl-i 0) (setq cl-seq (cdr cl-seq)) |
280 | (let ((cl-tail (nthcdr (1- cl-i) cl-seq))) | |
281 | (setcdr cl-tail (cdr (cdr cl-tail))))) | |
282 | (setq cl-end cl-i)) | |
283 | cl-seq) | |
284 | (setq cl-end (- (or cl-end 8000000) cl-start)) | |
285 | (if (= cl-start 0) | |
286 | (progn | |
287 | (while (and cl-seq | |
288 | (> cl-end 0) | |
bb3faf5b | 289 | (cl--check-test cl-item (car cl-seq)) |
fcd73769 RS |
290 | (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) |
291 | (> (setq cl-count (1- cl-count)) 0))) | |
292 | (setq cl-end (1- cl-end))) | |
293 | (setq cl-start (1- cl-start))) | |
294 | (if (and (> cl-count 0) (> cl-end 0)) | |
295 | (let ((cl-p (nthcdr cl-start cl-seq))) | |
296 | (while (and (cdr cl-p) (> cl-end 0)) | |
bb3faf5b | 297 | (if (cl--check-test cl-item (car (cdr cl-p))) |
fcd73769 RS |
298 | (progn |
299 | (setcdr cl-p (cdr (cdr cl-p))) | |
300 | (if (= (setq cl-count (1- cl-count)) 0) | |
301 | (setq cl-end 1))) | |
302 | (setq cl-p (cdr cl-p))) | |
303 | (setq cl-end (1- cl-end))))) | |
304 | cl-seq) | |
7c1898a7 | 305 | (apply 'cl-remove cl-item cl-seq cl-keys))))) |
fcd73769 | 306 | |
323698cc | 307 | ;;;###autoload |
7c1898a7 | 308 | (defun cl-delete-if (cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
309 | "Remove all items satisfying PREDICATE in SEQ. |
310 | This is a destructive function; it reuses the storage of SEQ whenever possible. | |
47bc4b3f JB |
311 | \nKeywords supported: :key :count :start :end :from-end |
312 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 313 | (apply 'cl-delete nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 314 | |
323698cc | 315 | ;;;###autoload |
7c1898a7 | 316 | (defun cl-delete-if-not (cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
317 | "Remove all items not satisfying PREDICATE in SEQ. |
318 | This is a destructive function; it reuses the storage of SEQ whenever possible. | |
47bc4b3f JB |
319 | \nKeywords supported: :key :count :start :end :from-end |
320 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 321 | (apply 'cl-delete nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 322 | |
323698cc | 323 | ;;;###autoload |
7c1898a7 | 324 | (defun cl-remove-duplicates (cl-seq &rest cl-keys) |
fcd73769 | 325 | "Return a copy of SEQ with all duplicate elements removed. |
47bc4b3f JB |
326 | \nKeywords supported: :test :test-not :key :start :end :from-end |
327 | \n(fn SEQ [KEYWORD VALUE]...)" | |
4735906a | 328 | (cl--delete-duplicates cl-seq cl-keys t)) |
fcd73769 | 329 | |
323698cc | 330 | ;;;###autoload |
7c1898a7 | 331 | (defun cl-delete-duplicates (cl-seq &rest cl-keys) |
fcd73769 | 332 | "Remove all duplicate elements from SEQ (destructively). |
47bc4b3f JB |
333 | \nKeywords supported: :test :test-not :key :start :end :from-end |
334 | \n(fn SEQ [KEYWORD VALUE]...)" | |
4735906a | 335 | (cl--delete-duplicates cl-seq cl-keys nil)) |
fcd73769 | 336 | |
4735906a | 337 | (defun cl--delete-duplicates (cl-seq cl-keys cl-copy) |
fcd73769 | 338 | (if (listp cl-seq) |
338bfefa SM |
339 | (cl--parsing-keywords |
340 | (:test :test-not :key (:start 0) :end :from-end :if) | |
fcd73769 RS |
341 | () |
342 | (if cl-from-end | |
343 | (let ((cl-p (nthcdr cl-start cl-seq)) cl-i) | |
344 | (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
345 | (while (> cl-end 1) | |
346 | (setq cl-i 0) | |
bb3faf5b | 347 | (while (setq cl-i (cl--position (cl--check-key (car cl-p)) |
4735906a | 348 | (cdr cl-p) cl-i (1- cl-end))) |
fcd73769 RS |
349 | (if cl-copy (setq cl-seq (copy-sequence cl-seq) |
350 | cl-p (nthcdr cl-start cl-seq) cl-copy nil)) | |
351 | (let ((cl-tail (nthcdr cl-i cl-p))) | |
352 | (setcdr cl-tail (cdr (cdr cl-tail)))) | |
353 | (setq cl-end (1- cl-end))) | |
354 | (setq cl-p (cdr cl-p) cl-end (1- cl-end) | |
355 | cl-start (1+ cl-start))) | |
356 | cl-seq) | |
357 | (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
358 | (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1) | |
bb3faf5b | 359 | (cl--position (cl--check-key (car cl-seq)) |
4735906a | 360 | (cdr cl-seq) 0 (1- cl-end))) |
fcd73769 RS |
361 | (setq cl-seq (cdr cl-seq) cl-end (1- cl-end))) |
362 | (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq) | |
363 | (setq cl-end (1- cl-end) cl-start 1) cl-seq))) | |
364 | (while (and (cdr (cdr cl-p)) (> cl-end 1)) | |
bb3faf5b | 365 | (if (cl--position (cl--check-key (car (cdr cl-p))) |
4735906a | 366 | (cdr (cdr cl-p)) 0 (1- cl-end)) |
fcd73769 RS |
367 | (progn |
368 | (if cl-copy (setq cl-seq (copy-sequence cl-seq) | |
369 | cl-p (nthcdr (1- cl-start) cl-seq) | |
370 | cl-copy nil)) | |
371 | (setcdr cl-p (cdr (cdr cl-p)))) | |
372 | (setq cl-p (cdr cl-p))) | |
373 | (setq cl-end (1- cl-end) cl-start (1+ cl-start))) | |
374 | cl-seq))) | |
4735906a | 375 | (let ((cl-res (cl--delete-duplicates (append cl-seq nil) cl-keys nil))) |
fcd73769 RS |
376 | (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))) |
377 | ||
323698cc | 378 | ;;;###autoload |
7c1898a7 | 379 | (defun cl-substitute (cl-new cl-old cl-seq &rest cl-keys) |
fcd73769 RS |
380 | "Substitute NEW for OLD in SEQ. |
381 | This is a non-destructive function; it makes a copy of SEQ if necessary | |
382 | to avoid corrupting the original SEQ. | |
47bc4b3f JB |
383 | \nKeywords supported: :test :test-not :key :count :start :end :from-end |
384 | \n(fn NEW OLD SEQ [KEYWORD VALUE]...)" | |
bb3faf5b | 385 | (cl--parsing-keywords (:test :test-not :key :if :if-not :count |
fcd73769 RS |
386 | (:start 0) :end :from-end) () |
387 | (if (or (eq cl-old cl-new) | |
388 | (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0)) | |
389 | cl-seq | |
4735906a | 390 | (let ((cl-i (cl--position cl-old cl-seq cl-start cl-end))) |
fcd73769 RS |
391 | (if (not cl-i) |
392 | cl-seq | |
393 | (setq cl-seq (copy-sequence cl-seq)) | |
394 | (or cl-from-end | |
d6f14ca7 | 395 | (progn (setf (elt cl-seq cl-i) cl-new) |
fcd73769 | 396 | (setq cl-i (1+ cl-i) cl-count (1- cl-count)))) |
7c1898a7 | 397 | (apply 'cl-nsubstitute cl-new cl-old cl-seq :count cl-count |
64a4c526 | 398 | :start cl-i cl-keys)))))) |
fcd73769 | 399 | |
323698cc | 400 | ;;;###autoload |
7c1898a7 | 401 | (defun cl-substitute-if (cl-new cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
402 | "Substitute NEW for all items satisfying PREDICATE in SEQ. |
403 | This is a non-destructive function; it makes a copy of SEQ if necessary | |
404 | to avoid corrupting the original SEQ. | |
47bc4b3f JB |
405 | \nKeywords supported: :key :count :start :end :from-end |
406 | \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 407 | (apply 'cl-substitute cl-new nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 408 | |
323698cc | 409 | ;;;###autoload |
7c1898a7 | 410 | (defun cl-substitute-if-not (cl-new cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
411 | "Substitute NEW for all items not satisfying PREDICATE in SEQ. |
412 | This is a non-destructive function; it makes a copy of SEQ if necessary | |
413 | to avoid corrupting the original SEQ. | |
47bc4b3f JB |
414 | \nKeywords supported: :key :count :start :end :from-end |
415 | \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 416 | (apply 'cl-substitute cl-new nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 417 | |
323698cc | 418 | ;;;###autoload |
7c1898a7 | 419 | (defun cl-nsubstitute (cl-new cl-old cl-seq &rest cl-keys) |
fcd73769 RS |
420 | "Substitute NEW for OLD in SEQ. |
421 | This is a destructive function; it reuses the storage of SEQ whenever possible. | |
47bc4b3f JB |
422 | \nKeywords supported: :test :test-not :key :count :start :end :from-end |
423 | \n(fn NEW OLD SEQ [KEYWORD VALUE]...)" | |
bb3faf5b | 424 | (cl--parsing-keywords (:test :test-not :key :if :if-not :count |
fcd73769 RS |
425 | (:start 0) :end :from-end) () |
426 | (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0) | |
427 | (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000))) | |
428 | (let ((cl-p (nthcdr cl-start cl-seq))) | |
429 | (setq cl-end (- (or cl-end 8000000) cl-start)) | |
430 | (while (and cl-p (> cl-end 0) (> cl-count 0)) | |
bb3faf5b | 431 | (if (cl--check-test cl-old (car cl-p)) |
fcd73769 RS |
432 | (progn |
433 | (setcar cl-p cl-new) | |
434 | (setq cl-count (1- cl-count)))) | |
435 | (setq cl-p (cdr cl-p) cl-end (1- cl-end)))) | |
436 | (or cl-end (setq cl-end (length cl-seq))) | |
437 | (if cl-from-end | |
438 | (while (and (< cl-start cl-end) (> cl-count 0)) | |
439 | (setq cl-end (1- cl-end)) | |
bb3faf5b | 440 | (if (cl--check-test cl-old (elt cl-seq cl-end)) |
fcd73769 | 441 | (progn |
d6f14ca7 | 442 | (setf (elt cl-seq cl-end) cl-new) |
fcd73769 RS |
443 | (setq cl-count (1- cl-count))))) |
444 | (while (and (< cl-start cl-end) (> cl-count 0)) | |
bb3faf5b | 445 | (if (cl--check-test cl-old (aref cl-seq cl-start)) |
fcd73769 RS |
446 | (progn |
447 | (aset cl-seq cl-start cl-new) | |
448 | (setq cl-count (1- cl-count)))) | |
449 | (setq cl-start (1+ cl-start)))))) | |
450 | cl-seq)) | |
451 | ||
323698cc | 452 | ;;;###autoload |
7c1898a7 | 453 | (defun cl-nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
454 | "Substitute NEW for all items satisfying PREDICATE in SEQ. |
455 | This is a destructive function; it reuses the storage of SEQ whenever possible. | |
47bc4b3f JB |
456 | \nKeywords supported: :key :count :start :end :from-end |
457 | \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 458 | (apply 'cl-nsubstitute cl-new nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 459 | |
323698cc | 460 | ;;;###autoload |
7c1898a7 | 461 | (defun cl-nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
462 | "Substitute NEW for all items not satisfying PREDICATE in SEQ. |
463 | This is a destructive function; it reuses the storage of SEQ whenever possible. | |
47bc4b3f JB |
464 | \nKeywords supported: :key :count :start :end :from-end |
465 | \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 466 | (apply 'cl-nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 467 | |
323698cc | 468 | ;;;###autoload |
7c1898a7 | 469 | (defun cl-find (cl-item cl-seq &rest cl-keys) |
47bc4b3f | 470 | "Find the first occurrence of ITEM in SEQ. |
fcd73769 | 471 | Return the matching ITEM, or nil if not found. |
47bc4b3f JB |
472 | \nKeywords supported: :test :test-not :key :start :end :from-end |
473 | \n(fn ITEM SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 474 | (let ((cl-pos (apply 'cl-position cl-item cl-seq cl-keys))) |
fcd73769 RS |
475 | (and cl-pos (elt cl-seq cl-pos)))) |
476 | ||
323698cc | 477 | ;;;###autoload |
7c1898a7 | 478 | (defun cl-find-if (cl-pred cl-list &rest cl-keys) |
47bc4b3f JB |
479 | "Find the first item satisfying PREDICATE in SEQ. |
480 | Return the matching item, or nil if not found. | |
481 | \nKeywords supported: :key :start :end :from-end | |
482 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 483 | (apply 'cl-find nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 484 | |
323698cc | 485 | ;;;###autoload |
7c1898a7 | 486 | (defun cl-find-if-not (cl-pred cl-list &rest cl-keys) |
47bc4b3f JB |
487 | "Find the first item not satisfying PREDICATE in SEQ. |
488 | Return the matching item, or nil if not found. | |
489 | \nKeywords supported: :key :start :end :from-end | |
490 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 491 | (apply 'cl-find nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 492 | |
323698cc | 493 | ;;;###autoload |
7c1898a7 | 494 | (defun cl-position (cl-item cl-seq &rest cl-keys) |
47bc4b3f | 495 | "Find the first occurrence of ITEM in SEQ. |
fcd73769 | 496 | Return the index of the matching item, or nil if not found. |
47bc4b3f JB |
497 | \nKeywords supported: :test :test-not :key :start :end :from-end |
498 | \n(fn ITEM SEQ [KEYWORD VALUE]...)" | |
bb3faf5b | 499 | (cl--parsing-keywords (:test :test-not :key :if :if-not |
fcd73769 | 500 | (:start 0) :end :from-end) () |
4735906a | 501 | (cl--position cl-item cl-seq cl-start cl-end cl-from-end))) |
fcd73769 | 502 | |
4735906a | 503 | (defun cl--position (cl-item cl-seq cl-start &optional cl-end cl-from-end) |
fcd73769 RS |
504 | (if (listp cl-seq) |
505 | (let ((cl-p (nthcdr cl-start cl-seq))) | |
506 | (or cl-end (setq cl-end 8000000)) | |
507 | (let ((cl-res nil)) | |
508 | (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end)) | |
bb3faf5b | 509 | (if (cl--check-test cl-item (car cl-p)) |
fcd73769 RS |
510 | (setq cl-res cl-start)) |
511 | (setq cl-p (cdr cl-p) cl-start (1+ cl-start))) | |
512 | cl-res)) | |
513 | (or cl-end (setq cl-end (length cl-seq))) | |
514 | (if cl-from-end | |
515 | (progn | |
516 | (while (and (>= (setq cl-end (1- cl-end)) cl-start) | |
bb3faf5b | 517 | (not (cl--check-test cl-item (aref cl-seq cl-end))))) |
fcd73769 RS |
518 | (and (>= cl-end cl-start) cl-end)) |
519 | (while (and (< cl-start cl-end) | |
bb3faf5b | 520 | (not (cl--check-test cl-item (aref cl-seq cl-start)))) |
fcd73769 RS |
521 | (setq cl-start (1+ cl-start))) |
522 | (and (< cl-start cl-end) cl-start)))) | |
523 | ||
323698cc | 524 | ;;;###autoload |
7c1898a7 | 525 | (defun cl-position-if (cl-pred cl-list &rest cl-keys) |
47bc4b3f | 526 | "Find the first item satisfying PREDICATE in SEQ. |
fcd73769 | 527 | Return the index of the matching item, or nil if not found. |
47bc4b3f JB |
528 | \nKeywords supported: :key :start :end :from-end |
529 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 530 | (apply 'cl-position nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 531 | |
323698cc | 532 | ;;;###autoload |
7c1898a7 | 533 | (defun cl-position-if-not (cl-pred cl-list &rest cl-keys) |
47bc4b3f | 534 | "Find the first item not satisfying PREDICATE in SEQ. |
fcd73769 | 535 | Return the index of the matching item, or nil if not found. |
47bc4b3f JB |
536 | \nKeywords supported: :key :start :end :from-end |
537 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 538 | (apply 'cl-position nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 539 | |
323698cc | 540 | ;;;###autoload |
7c1898a7 | 541 | (defun cl-count (cl-item cl-seq &rest cl-keys) |
47bc4b3f JB |
542 | "Count the number of occurrences of ITEM in SEQ. |
543 | \nKeywords supported: :test :test-not :key :start :end | |
544 | \n(fn ITEM SEQ [KEYWORD VALUE]...)" | |
bb3faf5b | 545 | (cl--parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) () |
fcd73769 RS |
546 | (let ((cl-count 0) cl-x) |
547 | (or cl-end (setq cl-end (length cl-seq))) | |
548 | (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq))) | |
549 | (while (< cl-start cl-end) | |
ca50d9e6 | 550 | (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start))) |
bb3faf5b | 551 | (if (cl--check-test cl-item cl-x) (setq cl-count (1+ cl-count))) |
fcd73769 RS |
552 | (setq cl-start (1+ cl-start))) |
553 | cl-count))) | |
554 | ||
323698cc | 555 | ;;;###autoload |
7c1898a7 | 556 | (defun cl-count-if (cl-pred cl-list &rest cl-keys) |
47bc4b3f JB |
557 | "Count the number of items satisfying PREDICATE in SEQ. |
558 | \nKeywords supported: :key :start :end | |
559 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 560 | (apply 'cl-count nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 561 | |
323698cc | 562 | ;;;###autoload |
7c1898a7 | 563 | (defun cl-count-if-not (cl-pred cl-list &rest cl-keys) |
47bc4b3f JB |
564 | "Count the number of items not satisfying PREDICATE in SEQ. |
565 | \nKeywords supported: :key :start :end | |
566 | \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" | |
7c1898a7 | 567 | (apply 'cl-count nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 568 | |
323698cc | 569 | ;;;###autoload |
7c1898a7 | 570 | (defun cl-mismatch (cl-seq1 cl-seq2 &rest cl-keys) |
fcd73769 RS |
571 | "Compare SEQ1 with SEQ2, return index of first mismatching element. |
572 | Return nil if the sequences match. If one sequence is a prefix of the | |
d22f8da4 | 573 | other, the return value indicates the end of the shorter sequence. |
47bc4b3f JB |
574 | \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end |
575 | \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)" | |
bb3faf5b | 576 | (cl--parsing-keywords (:test :test-not :key :from-end |
fcd73769 RS |
577 | (:start1 0) :end1 (:start2 0) :end2) () |
578 | (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
579 | (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
580 | (if cl-from-end | |
581 | (progn | |
582 | (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
bb3faf5b | 583 | (cl--check-match (elt cl-seq1 (1- cl-end1)) |
fcd73769 RS |
584 | (elt cl-seq2 (1- cl-end2)))) |
585 | (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2))) | |
586 | (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
587 | (1- cl-end1))) | |
588 | (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1))) | |
589 | (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2)))) | |
590 | (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
bb3faf5b | 591 | (cl--check-match (if cl-p1 (car cl-p1) |
fcd73769 RS |
592 | (aref cl-seq1 cl-start1)) |
593 | (if cl-p2 (car cl-p2) | |
594 | (aref cl-seq2 cl-start2)))) | |
595 | (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2) | |
596 | cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2))) | |
597 | (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
598 | cl-start1))))) | |
599 | ||
323698cc | 600 | ;;;###autoload |
7c1898a7 | 601 | (defun cl-search (cl-seq1 cl-seq2 &rest cl-keys) |
fcd73769 RS |
602 | "Search for SEQ1 as a subsequence of SEQ2. |
603 | Return the index of the leftmost element of the first match found; | |
604 | return nil if there are no matches. | |
47bc4b3f JB |
605 | \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end |
606 | \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)" | |
bb3faf5b | 607 | (cl--parsing-keywords (:test :test-not :key :from-end |
fcd73769 RS |
608 | (:start1 0) :end1 (:start2 0) :end2) () |
609 | (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
610 | (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
611 | (if (>= cl-start1 cl-end1) | |
612 | (if cl-from-end cl-end2 cl-start2) | |
613 | (let* ((cl-len (- cl-end1 cl-start1)) | |
bb3faf5b | 614 | (cl-first (cl--check-key (elt cl-seq1 cl-start1))) |
fcd73769 RS |
615 | (cl-if nil) cl-pos) |
616 | (setq cl-end2 (- cl-end2 (1- cl-len))) | |
617 | (while (and (< cl-start2 cl-end2) | |
4735906a SM |
618 | (setq cl-pos (cl--position cl-first cl-seq2 |
619 | cl-start2 cl-end2 cl-from-end)) | |
7c1898a7 | 620 | (apply 'cl-mismatch cl-seq1 cl-seq2 |
64a4c526 DL |
621 | :start1 (1+ cl-start1) :end1 cl-end1 |
622 | :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len) | |
623 | :from-end nil cl-keys)) | |
fcd73769 RS |
624 | (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos)))) |
625 | (and (< cl-start2 cl-end2) cl-pos))))) | |
626 | ||
323698cc | 627 | ;;;###autoload |
7c1898a7 | 628 | (defun cl-sort (cl-seq cl-pred &rest cl-keys) |
47bc4b3f JB |
629 | "Sort the argument SEQ according to PREDICATE. |
630 | This is a destructive function; it reuses the storage of SEQ if possible. | |
631 | \nKeywords supported: :key | |
632 | \n(fn SEQ PREDICATE [KEYWORD VALUE]...)" | |
fcd73769 | 633 | (if (nlistp cl-seq) |
7c1898a7 | 634 | (cl-replace cl-seq (apply 'cl-sort (append cl-seq nil) cl-pred cl-keys)) |
bb3faf5b | 635 | (cl--parsing-keywords (:key) () |
fcd73769 RS |
636 | (if (memq cl-key '(nil identity)) |
637 | (sort cl-seq cl-pred) | |
638 | (sort cl-seq (function (lambda (cl-x cl-y) | |
639 | (funcall cl-pred (funcall cl-key cl-x) | |
640 | (funcall cl-key cl-y))))))))) | |
641 | ||
323698cc | 642 | ;;;###autoload |
7c1898a7 | 643 | (defun cl-stable-sort (cl-seq cl-pred &rest cl-keys) |
47bc4b3f JB |
644 | "Sort the argument SEQ stably according to PREDICATE. |
645 | This is a destructive function; it reuses the storage of SEQ if possible. | |
646 | \nKeywords supported: :key | |
647 | \n(fn SEQ PREDICATE [KEYWORD VALUE]...)" | |
7c1898a7 | 648 | (apply 'cl-sort cl-seq cl-pred cl-keys)) |
fcd73769 | 649 | |
323698cc | 650 | ;;;###autoload |
7c1898a7 | 651 | (defun cl-merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys) |
fcd73769 | 652 | "Destructively merge the two sequences to produce a new sequence. |
47bc4b3f JB |
653 | TYPE is the sequence type to return, SEQ1 and SEQ2 are the two argument |
654 | sequences, and PREDICATE is a `less-than' predicate on the elements. | |
655 | \nKeywords supported: :key | |
656 | \n(fn TYPE SEQ1 SEQ2 PREDICATE [KEYWORD VALUE]...)" | |
fcd73769 RS |
657 | (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil))) |
658 | (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil))) | |
bb3faf5b | 659 | (cl--parsing-keywords (:key) () |
fcd73769 RS |
660 | (let ((cl-res nil)) |
661 | (while (and cl-seq1 cl-seq2) | |
bb3faf5b SM |
662 | (if (funcall cl-pred (cl--check-key (car cl-seq2)) |
663 | (cl--check-key (car cl-seq1))) | |
ca50d9e6 SM |
664 | (push (pop cl-seq2) cl-res) |
665 | (push (pop cl-seq1) cl-res))) | |
7c1898a7 | 666 | (cl-coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type)))) |
fcd73769 | 667 | |
323698cc | 668 | ;;;###autoload |
7c1898a7 | 669 | (defun cl-member (cl-item cl-list &rest cl-keys) |
fcd73769 RS |
670 | "Find the first occurrence of ITEM in LIST. |
671 | Return the sublist of LIST whose car is ITEM. | |
47bc4b3f JB |
672 | \nKeywords supported: :test :test-not :key |
673 | \n(fn ITEM LIST [KEYWORD VALUE]...)" | |
d9857e53 | 674 | (declare (compiler-macro cl--compiler-macro-member)) |
fcd73769 | 675 | (if cl-keys |
bb3faf5b SM |
676 | (cl--parsing-keywords (:test :test-not :key :if :if-not) () |
677 | (while (and cl-list (not (cl--check-test cl-item (car cl-list)))) | |
fcd73769 RS |
678 | (setq cl-list (cdr cl-list))) |
679 | cl-list) | |
680 | (if (and (numberp cl-item) (not (integerp cl-item))) | |
681 | (member cl-item cl-list) | |
682 | (memq cl-item cl-list)))) | |
d9857e53 | 683 | (autoload 'cl--compiler-macro-member "cl-macs") |
fcd73769 | 684 | |
323698cc | 685 | ;;;###autoload |
7c1898a7 | 686 | (defun cl-member-if (cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
687 | "Find the first item satisfying PREDICATE in LIST. |
688 | Return the sublist of LIST whose car matches. | |
47bc4b3f JB |
689 | \nKeywords supported: :key |
690 | \n(fn PREDICATE LIST [KEYWORD VALUE]...)" | |
7c1898a7 | 691 | (apply 'cl-member nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 692 | |
323698cc | 693 | ;;;###autoload |
7c1898a7 | 694 | (defun cl-member-if-not (cl-pred cl-list &rest cl-keys) |
fcd73769 RS |
695 | "Find the first item not satisfying PREDICATE in LIST. |
696 | Return the sublist of LIST whose car matches. | |
47bc4b3f JB |
697 | \nKeywords supported: :key |
698 | \n(fn PREDICATE LIST [KEYWORD VALUE]...)" | |
7c1898a7 | 699 | (apply 'cl-member nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 700 | |
323698cc | 701 | ;;;###autoload |
4735906a | 702 | (defun cl--adjoin (cl-item cl-list &rest cl-keys) |
bb3faf5b SM |
703 | (if (cl--parsing-keywords (:key) t |
704 | (apply 'cl-member (cl--check-key cl-item) cl-list cl-keys)) | |
fcd73769 RS |
705 | cl-list |
706 | (cons cl-item cl-list))) | |
707 | ||
323698cc | 708 | ;;;###autoload |
7c1898a7 | 709 | (defun cl-assoc (cl-item cl-alist &rest cl-keys) |
fcd73769 | 710 | "Find the first item whose car matches ITEM in LIST. |
47bc4b3f JB |
711 | \nKeywords supported: :test :test-not :key |
712 | \n(fn ITEM LIST [KEYWORD VALUE]...)" | |
d9857e53 | 713 | (declare (compiler-macro cl--compiler-macro-assoc)) |
fcd73769 | 714 | (if cl-keys |
bb3faf5b | 715 | (cl--parsing-keywords (:test :test-not :key :if :if-not) () |
fcd73769 RS |
716 | (while (and cl-alist |
717 | (or (not (consp (car cl-alist))) | |
bb3faf5b | 718 | (not (cl--check-test cl-item (car (car cl-alist)))))) |
fcd73769 RS |
719 | (setq cl-alist (cdr cl-alist))) |
720 | (and cl-alist (car cl-alist))) | |
721 | (if (and (numberp cl-item) (not (integerp cl-item))) | |
722 | (assoc cl-item cl-alist) | |
723 | (assq cl-item cl-alist)))) | |
d9857e53 | 724 | (autoload 'cl--compiler-macro-assoc "cl-macs") |
fcd73769 | 725 | |
323698cc | 726 | ;;;###autoload |
7c1898a7 | 727 | (defun cl-assoc-if (cl-pred cl-list &rest cl-keys) |
fcd73769 | 728 | "Find the first item whose car satisfies PREDICATE in LIST. |
47bc4b3f JB |
729 | \nKeywords supported: :key |
730 | \n(fn PREDICATE LIST [KEYWORD VALUE]...)" | |
7c1898a7 | 731 | (apply 'cl-assoc nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 732 | |
323698cc | 733 | ;;;###autoload |
7c1898a7 | 734 | (defun cl-assoc-if-not (cl-pred cl-list &rest cl-keys) |
fcd73769 | 735 | "Find the first item whose car does not satisfy PREDICATE in LIST. |
47bc4b3f JB |
736 | \nKeywords supported: :key |
737 | \n(fn PREDICATE LIST [KEYWORD VALUE]...)" | |
7c1898a7 | 738 | (apply 'cl-assoc nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 739 | |
323698cc | 740 | ;;;###autoload |
7c1898a7 | 741 | (defun cl-rassoc (cl-item cl-alist &rest cl-keys) |
fcd73769 | 742 | "Find the first item whose cdr matches ITEM in LIST. |
47bc4b3f JB |
743 | \nKeywords supported: :test :test-not :key |
744 | \n(fn ITEM LIST [KEYWORD VALUE]...)" | |
fcd73769 | 745 | (if (or cl-keys (numberp cl-item)) |
bb3faf5b | 746 | (cl--parsing-keywords (:test :test-not :key :if :if-not) () |
fcd73769 RS |
747 | (while (and cl-alist |
748 | (or (not (consp (car cl-alist))) | |
bb3faf5b | 749 | (not (cl--check-test cl-item (cdr (car cl-alist)))))) |
fcd73769 RS |
750 | (setq cl-alist (cdr cl-alist))) |
751 | (and cl-alist (car cl-alist))) | |
752 | (rassq cl-item cl-alist))) | |
753 | ||
323698cc | 754 | ;;;###autoload |
7c1898a7 | 755 | (defun cl-rassoc-if (cl-pred cl-list &rest cl-keys) |
fcd73769 | 756 | "Find the first item whose cdr satisfies PREDICATE in LIST. |
47bc4b3f JB |
757 | \nKeywords supported: :key |
758 | \n(fn PREDICATE LIST [KEYWORD VALUE]...)" | |
7c1898a7 | 759 | (apply 'cl-rassoc nil cl-list :if cl-pred cl-keys)) |
fcd73769 | 760 | |
323698cc | 761 | ;;;###autoload |
7c1898a7 | 762 | (defun cl-rassoc-if-not (cl-pred cl-list &rest cl-keys) |
fcd73769 | 763 | "Find the first item whose cdr does not satisfy PREDICATE in LIST. |
47bc4b3f JB |
764 | \nKeywords supported: :key |
765 | \n(fn PREDICATE LIST [KEYWORD VALUE]...)" | |
7c1898a7 | 766 | (apply 'cl-rassoc nil cl-list :if-not cl-pred cl-keys)) |
fcd73769 | 767 | |
323698cc | 768 | ;;;###autoload |
7c1898a7 | 769 | (defun cl-union (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 770 | "Combine LIST1 and LIST2 using a set-union operation. |
86361e1e | 771 | The resulting list contains all items that appear in either LIST1 or LIST2. |
fcd73769 RS |
772 | This is a non-destructive function; it makes a copy of the data if necessary |
773 | to avoid corrupting the original LIST1 and LIST2. | |
47bc4b3f JB |
774 | \nKeywords supported: :test :test-not :key |
775 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 RS |
776 | (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
777 | ((equal cl-list1 cl-list2) cl-list1) | |
778 | (t | |
779 | (or (>= (length cl-list1) (length cl-list2)) | |
780 | (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) | |
781 | (while cl-list2 | |
782 | (if (or cl-keys (numberp (car cl-list2))) | |
338bfefa SM |
783 | (setq cl-list1 |
784 | (apply 'cl-adjoin (car cl-list2) cl-list1 cl-keys)) | |
fcd73769 | 785 | (or (memq (car cl-list2) cl-list1) |
ca50d9e6 SM |
786 | (push (car cl-list2) cl-list1))) |
787 | (pop cl-list2)) | |
fcd73769 RS |
788 | cl-list1))) |
789 | ||
323698cc | 790 | ;;;###autoload |
7c1898a7 | 791 | (defun cl-nunion (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 792 | "Combine LIST1 and LIST2 using a set-union operation. |
86361e1e | 793 | The resulting list contains all items that appear in either LIST1 or LIST2. |
fcd73769 RS |
794 | This is a destructive function; it reuses the storage of LIST1 and LIST2 |
795 | whenever possible. | |
47bc4b3f JB |
796 | \nKeywords supported: :test :test-not :key |
797 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 | 798 | (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
7c1898a7 | 799 | (t (apply 'cl-union cl-list1 cl-list2 cl-keys)))) |
fcd73769 | 800 | |
323698cc | 801 | ;;;###autoload |
7c1898a7 | 802 | (defun cl-intersection (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 803 | "Combine LIST1 and LIST2 using a set-intersection operation. |
86361e1e | 804 | The resulting list contains all items that appear in both LIST1 and LIST2. |
fcd73769 RS |
805 | This is a non-destructive function; it makes a copy of the data if necessary |
806 | to avoid corrupting the original LIST1 and LIST2. | |
47bc4b3f JB |
807 | \nKeywords supported: :test :test-not :key |
808 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 RS |
809 | (and cl-list1 cl-list2 |
810 | (if (equal cl-list1 cl-list2) cl-list1 | |
bb3faf5b | 811 | (cl--parsing-keywords (:key) (:test :test-not) |
fcd73769 RS |
812 | (let ((cl-res nil)) |
813 | (or (>= (length cl-list1) (length cl-list2)) | |
814 | (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) | |
815 | (while cl-list2 | |
816 | (if (if (or cl-keys (numberp (car cl-list2))) | |
bb3faf5b | 817 | (apply 'cl-member (cl--check-key (car cl-list2)) |
fcd73769 RS |
818 | cl-list1 cl-keys) |
819 | (memq (car cl-list2) cl-list1)) | |
ca50d9e6 SM |
820 | (push (car cl-list2) cl-res)) |
821 | (pop cl-list2)) | |
fcd73769 RS |
822 | cl-res))))) |
823 | ||
323698cc | 824 | ;;;###autoload |
7c1898a7 | 825 | (defun cl-nintersection (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 826 | "Combine LIST1 and LIST2 using a set-intersection operation. |
86361e1e | 827 | The resulting list contains all items that appear in both LIST1 and LIST2. |
fcd73769 RS |
828 | This is a destructive function; it reuses the storage of LIST1 and LIST2 |
829 | whenever possible. | |
47bc4b3f JB |
830 | \nKeywords supported: :test :test-not :key |
831 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
7c1898a7 | 832 | (and cl-list1 cl-list2 (apply 'cl-intersection cl-list1 cl-list2 cl-keys))) |
fcd73769 | 833 | |
323698cc | 834 | ;;;###autoload |
7c1898a7 | 835 | (defun cl-set-difference (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 836 | "Combine LIST1 and LIST2 using a set-difference operation. |
86361e1e | 837 | The resulting list contains all items that appear in LIST1 but not LIST2. |
fcd73769 RS |
838 | This is a non-destructive function; it makes a copy of the data if necessary |
839 | to avoid corrupting the original LIST1 and LIST2. | |
47bc4b3f JB |
840 | \nKeywords supported: :test :test-not :key |
841 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 | 842 | (if (or (null cl-list1) (null cl-list2)) cl-list1 |
bb3faf5b | 843 | (cl--parsing-keywords (:key) (:test :test-not) |
fcd73769 RS |
844 | (let ((cl-res nil)) |
845 | (while cl-list1 | |
846 | (or (if (or cl-keys (numberp (car cl-list1))) | |
bb3faf5b | 847 | (apply 'cl-member (cl--check-key (car cl-list1)) |
fcd73769 RS |
848 | cl-list2 cl-keys) |
849 | (memq (car cl-list1) cl-list2)) | |
ca50d9e6 SM |
850 | (push (car cl-list1) cl-res)) |
851 | (pop cl-list1)) | |
fcd73769 RS |
852 | cl-res)))) |
853 | ||
323698cc | 854 | ;;;###autoload |
7c1898a7 | 855 | (defun cl-nset-difference (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 856 | "Combine LIST1 and LIST2 using a set-difference operation. |
86361e1e | 857 | The resulting list contains all items that appear in LIST1 but not LIST2. |
fcd73769 RS |
858 | This is a destructive function; it reuses the storage of LIST1 and LIST2 |
859 | whenever possible. | |
47bc4b3f JB |
860 | \nKeywords supported: :test :test-not :key |
861 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 | 862 | (if (or (null cl-list1) (null cl-list2)) cl-list1 |
7c1898a7 | 863 | (apply 'cl-set-difference cl-list1 cl-list2 cl-keys))) |
fcd73769 | 864 | |
323698cc | 865 | ;;;###autoload |
7c1898a7 | 866 | (defun cl-set-exclusive-or (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 867 | "Combine LIST1 and LIST2 using a set-exclusive-or operation. |
86361e1e | 868 | The resulting list contains all items appearing in exactly one of LIST1, LIST2. |
fcd73769 RS |
869 | This is a non-destructive function; it makes a copy of the data if necessary |
870 | to avoid corrupting the original LIST1 and LIST2. | |
47bc4b3f JB |
871 | \nKeywords supported: :test :test-not :key |
872 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 RS |
873 | (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
874 | ((equal cl-list1 cl-list2) nil) | |
7c1898a7 SM |
875 | (t (append (apply 'cl-set-difference cl-list1 cl-list2 cl-keys) |
876 | (apply 'cl-set-difference cl-list2 cl-list1 cl-keys))))) | |
fcd73769 | 877 | |
323698cc | 878 | ;;;###autoload |
7c1898a7 | 879 | (defun cl-nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys) |
fcd73769 | 880 | "Combine LIST1 and LIST2 using a set-exclusive-or operation. |
86361e1e | 881 | The resulting list contains all items appearing in exactly one of LIST1, LIST2. |
fcd73769 RS |
882 | This is a destructive function; it reuses the storage of LIST1 and LIST2 |
883 | whenever possible. | |
47bc4b3f JB |
884 | \nKeywords supported: :test :test-not :key |
885 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 RS |
886 | (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
887 | ((equal cl-list1 cl-list2) nil) | |
7c1898a7 SM |
888 | (t (nconc (apply 'cl-nset-difference cl-list1 cl-list2 cl-keys) |
889 | (apply 'cl-nset-difference cl-list2 cl-list1 cl-keys))))) | |
fcd73769 | 890 | |
323698cc | 891 | ;;;###autoload |
7c1898a7 | 892 | (defun cl-subsetp (cl-list1 cl-list2 &rest cl-keys) |
213233f0 | 893 | "Return true if LIST1 is a subset of LIST2. |
fcd73769 | 894 | I.e., if every element of LIST1 also appears in LIST2. |
47bc4b3f JB |
895 | \nKeywords supported: :test :test-not :key |
896 | \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" | |
fcd73769 RS |
897 | (cond ((null cl-list1) t) ((null cl-list2) nil) |
898 | ((equal cl-list1 cl-list2) t) | |
bb3faf5b | 899 | (t (cl--parsing-keywords (:key) (:test :test-not) |
fcd73769 | 900 | (while (and cl-list1 |
bb3faf5b | 901 | (apply 'cl-member (cl--check-key (car cl-list1)) |
fcd73769 | 902 | cl-list2 cl-keys)) |
ca50d9e6 | 903 | (pop cl-list1)) |
fcd73769 RS |
904 | (null cl-list1))))) |
905 | ||
323698cc | 906 | ;;;###autoload |
7c1898a7 | 907 | (defun cl-subst-if (cl-new cl-pred cl-tree &rest cl-keys) |
fcd73769 RS |
908 | "Substitute NEW for elements matching PREDICATE in TREE (non-destructively). |
909 | Return a copy of TREE with all matching elements replaced by NEW. | |
47bc4b3f JB |
910 | \nKeywords supported: :key |
911 | \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" | |
7c1898a7 | 912 | (apply 'cl-sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys)) |
fcd73769 | 913 | |
323698cc | 914 | ;;;###autoload |
7c1898a7 | 915 | (defun cl-subst-if-not (cl-new cl-pred cl-tree &rest cl-keys) |
fcd73769 RS |
916 | "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively). |
917 | Return a copy of TREE with all non-matching elements replaced by NEW. | |
47bc4b3f JB |
918 | \nKeywords supported: :key |
919 | \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" | |
7c1898a7 | 920 | (apply 'cl-sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys)) |
fcd73769 | 921 | |
323698cc | 922 | ;;;###autoload |
7c1898a7 | 923 | (defun cl-nsubst (cl-new cl-old cl-tree &rest cl-keys) |
fcd73769 RS |
924 | "Substitute NEW for OLD everywhere in TREE (destructively). |
925 | Any element of TREE which is `eql' to OLD is changed to NEW (via a call | |
926 | to `setcar'). | |
47bc4b3f JB |
927 | \nKeywords supported: :test :test-not :key |
928 | \n(fn NEW OLD TREE [KEYWORD VALUE]...)" | |
7c1898a7 | 929 | (apply 'cl-nsublis (list (cons cl-old cl-new)) cl-tree cl-keys)) |
fcd73769 | 930 | |
323698cc | 931 | ;;;###autoload |
7c1898a7 | 932 | (defun cl-nsubst-if (cl-new cl-pred cl-tree &rest cl-keys) |
fcd73769 RS |
933 | "Substitute NEW for elements matching PREDICATE in TREE (destructively). |
934 | Any element of TREE which matches is changed to NEW (via a call to `setcar'). | |
47bc4b3f JB |
935 | \nKeywords supported: :key |
936 | \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" | |
7c1898a7 | 937 | (apply 'cl-nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys)) |
fcd73769 | 938 | |
323698cc | 939 | ;;;###autoload |
7c1898a7 | 940 | (defun cl-nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys) |
fcd73769 RS |
941 | "Substitute NEW for elements not matching PREDICATE in TREE (destructively). |
942 | Any element of TREE which matches is changed to NEW (via a call to `setcar'). | |
47bc4b3f JB |
943 | \nKeywords supported: :key |
944 | \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" | |
7c1898a7 | 945 | (apply 'cl-nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys)) |
fcd73769 | 946 | |
bb3faf5b SM |
947 | (defvar cl--alist) |
948 | ||
323698cc | 949 | ;;;###autoload |
7c1898a7 | 950 | (defun cl-sublis (cl-alist cl-tree &rest cl-keys) |
fcd73769 RS |
951 | "Perform substitutions indicated by ALIST in TREE (non-destructively). |
952 | Return a copy of TREE with all matching elements replaced. | |
47bc4b3f JB |
953 | \nKeywords supported: :test :test-not :key |
954 | \n(fn ALIST TREE [KEYWORD VALUE]...)" | |
bb3faf5b SM |
955 | (cl--parsing-keywords (:test :test-not :key :if :if-not) () |
956 | (let ((cl--alist cl-alist)) | |
957 | (cl--sublis-rec cl-tree)))) | |
fcd73769 | 958 | |
bb3faf5b SM |
959 | (defun cl--sublis-rec (cl-tree) ;Uses cl--alist cl-key/test*/if*. |
960 | (let ((cl-temp (cl--check-key cl-tree)) (cl-p cl--alist)) | |
961 | (while (and cl-p (not (cl--check-test-nokey (car (car cl-p)) cl-temp))) | |
fcd73769 RS |
962 | (setq cl-p (cdr cl-p))) |
963 | (if cl-p (cdr (car cl-p)) | |
964 | (if (consp cl-tree) | |
bb3faf5b SM |
965 | (let ((cl-a (cl--sublis-rec (car cl-tree))) |
966 | (cl-d (cl--sublis-rec (cdr cl-tree)))) | |
fcd73769 RS |
967 | (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree))) |
968 | cl-tree | |
969 | (cons cl-a cl-d))) | |
970 | cl-tree)))) | |
971 | ||
323698cc | 972 | ;;;###autoload |
7c1898a7 | 973 | (defun cl-nsublis (cl-alist cl-tree &rest cl-keys) |
fcd73769 RS |
974 | "Perform substitutions indicated by ALIST in TREE (destructively). |
975 | Any matching element of TREE is changed via a call to `setcar'. | |
47bc4b3f JB |
976 | \nKeywords supported: :test :test-not :key |
977 | \n(fn ALIST TREE [KEYWORD VALUE]...)" | |
bb3faf5b SM |
978 | (cl--parsing-keywords (:test :test-not :key :if :if-not) () |
979 | (let ((cl-hold (list cl-tree)) | |
980 | (cl--alist cl-alist)) | |
981 | (cl--nsublis-rec cl-hold) | |
fcd73769 RS |
982 | (car cl-hold)))) |
983 | ||
bb3faf5b | 984 | (defun cl--nsublis-rec (cl-tree) ;Uses cl--alist cl-key/test*/if*. |
fcd73769 | 985 | (while (consp cl-tree) |
bb3faf5b SM |
986 | (let ((cl-temp (cl--check-key (car cl-tree))) (cl-p cl--alist)) |
987 | (while (and cl-p (not (cl--check-test-nokey (car (car cl-p)) cl-temp))) | |
fcd73769 RS |
988 | (setq cl-p (cdr cl-p))) |
989 | (if cl-p (setcar cl-tree (cdr (car cl-p))) | |
bb3faf5b SM |
990 | (if (consp (car cl-tree)) (cl--nsublis-rec (car cl-tree)))) |
991 | (setq cl-temp (cl--check-key (cdr cl-tree)) cl-p cl--alist) | |
992 | (while (and cl-p (not (cl--check-test-nokey (car (car cl-p)) cl-temp))) | |
fcd73769 RS |
993 | (setq cl-p (cdr cl-p))) |
994 | (if cl-p | |
995 | (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil)) | |
996 | (setq cl-tree (cdr cl-tree)))))) | |
997 | ||
323698cc | 998 | ;;;###autoload |
7c1898a7 | 999 | (defun cl-tree-equal (cl-x cl-y &rest cl-keys) |
47bc4b3f | 1000 | "Return t if trees TREE1 and TREE2 have `eql' leaves. |
fcd73769 | 1001 | Atoms are compared by `eql'; cons cells are compared recursively. |
47bc4b3f JB |
1002 | \nKeywords supported: :test :test-not :key |
1003 | \n(fn TREE1 TREE2 [KEYWORD VALUE]...)" | |
bb3faf5b SM |
1004 | (cl--parsing-keywords (:test :test-not :key) () |
1005 | (cl--tree-equal-rec cl-x cl-y))) | |
fcd73769 | 1006 | |
bb3faf5b | 1007 | (defun cl--tree-equal-rec (cl-x cl-y) ;Uses cl-key/test*. |
fcd73769 | 1008 | (while (and (consp cl-x) (consp cl-y) |
bb3faf5b | 1009 | (cl--tree-equal-rec (car cl-x) (car cl-y))) |
fcd73769 | 1010 | (setq cl-x (cdr cl-x) cl-y (cdr cl-y))) |
bb3faf5b | 1011 | (and (not (consp cl-x)) (not (consp cl-y)) (cl--check-match cl-x cl-y))) |
fcd73769 RS |
1012 | |
1013 | ||
1014 | (run-hooks 'cl-seq-load-hook) | |
1015 | ||
323698cc | 1016 | ;; Local variables: |
08f5e965 | 1017 | ;; byte-compile-dynamic: t |
323698cc SM |
1018 | ;; generated-autoload-file: "cl-loaddefs.el" |
1019 | ;; End: | |
1020 | ||
fcd73769 | 1021 | ;;; cl-seq.el ends here |