1 ;;; cl-seq.el --- Common Lisp features, part 3
3 ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; These are extensions to Emacs Lisp that provide a degree of
30 ;; Common Lisp compatibility, beyond what is already built-in
33 ;; This package was written by Dave Gillespie; it is a complete
34 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
36 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
38 ;; Bug reports, comments, and suggestions are welcome!
40 ;; This file contains the Common Lisp sequence and list functions
41 ;; which take keyword arguments.
43 ;; See cl.el for Change Log.
48 (or (memq 'cl-19 features)
49 (error "Tried to load `cl-seq' before `cl'!"))
52 ;;; Keyword parsing. This is special-cased here so that we can compile
53 ;;; this file independent from cl-macs.
55 (defmacro cl-parsing-keywords (kwords other-keys &rest body)
61 (let* ((var (if (consp x) (car x) x))
62 (mem (list 'car (list 'cdr (list 'memq (list 'quote var)
64 (if (eq var :test-not)
65 (setq mem (list 'and mem (list 'setq 'cl-test mem) t)))
67 (setq mem (list 'and mem (list 'setq 'cl-if mem) t)))
69 (format "cl-%s" (substring (symbol-name var) 1)))
70 (if (consp x) (list 'or mem (car (cdr x))) mem)))))
73 (and (not (eq other-keys t))
75 (list 'let '((cl-keys-temp cl-keys))
76 (list 'while 'cl-keys-temp
77 (list 'or (list 'memq '(car cl-keys-temp)
86 '(car (cdr (memq (quote :allow-other-keys)
88 '(error "Bad keyword argument %s"
90 '(setq cl-keys-temp (cdr (cdr cl-keys-temp)))))))
92 (put 'cl-parsing-keywords 'lisp-indent-function 2)
93 (put 'cl-parsing-keywords 'edebug-form-spec '(sexp sexp &rest form))
95 (defmacro cl-check-key (x)
96 (list 'if 'cl-key (list 'funcall 'cl-key x) x))
98 (defmacro cl-check-test-nokey (item x)
101 (list 'eq (list 'not (list 'funcall 'cl-test item x))
104 (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not))
105 (list 't (list 'if (list 'numberp item)
106 (list 'equal item x) (list 'eq item x)))))
108 (defmacro cl-check-test (item x)
109 (list 'cl-check-test-nokey item (list 'cl-check-key x)))
111 (defmacro cl-check-match (x y)
112 (setq x (list 'cl-check-key x) y (list 'cl-check-key y))
114 (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not)
115 (list 'if (list 'numberp x)
116 (list 'equal x y) (list 'eq x y))))
118 (put 'cl-check-key 'edebug-form-spec 'edebug-forms)
119 (put 'cl-check-test 'edebug-form-spec 'edebug-forms)
120 (put 'cl-check-test-nokey 'edebug-form-spec 'edebug-forms)
121 (put 'cl-check-match 'edebug-form-spec 'edebug-forms)
123 (defvar cl-test) (defvar cl-test-not)
124 (defvar cl-if) (defvar cl-if-not)
129 (defun reduce (cl-func cl-seq &rest cl-keys)
130 "Reduce two-argument FUNCTION across SEQ.
131 \nKeywords supported: :start :end :from-end :initial-value :key
132 \n(fn FUNCTION SEQ [KEYWORD VALUE]...)"
133 (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
134 (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
135 (setq cl-seq (subseq cl-seq cl-start cl-end))
136 (if cl-from-end (setq cl-seq (nreverse cl-seq)))
137 (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
138 (cl-seq (cl-check-key (pop cl-seq)))
139 (t (funcall cl-func)))))
142 (setq cl-accum (funcall cl-func (cl-check-key (pop cl-seq))
145 (setq cl-accum (funcall cl-func cl-accum
146 (cl-check-key (pop cl-seq))))))
150 (defun fill (seq item &rest cl-keys)
151 "Fill the elements of SEQ with ITEM.
152 \nKeywords supported: :start :end
153 \n(fn SEQ ITEM [KEYWORD VALUE]...)"
154 (cl-parsing-keywords ((:start 0) :end) ()
156 (let ((p (nthcdr cl-start seq))
157 (n (if cl-end (- cl-end cl-start) 8000000)))
158 (while (and p (>= (setq n (1- n)) 0))
161 (or cl-end (setq cl-end (length seq)))
162 (if (and (= cl-start 0) (= cl-end (length seq)))
164 (while (< cl-start cl-end)
165 (aset seq cl-start item)
166 (setq cl-start (1+ cl-start)))))
170 (defun replace (cl-seq1 cl-seq2 &rest cl-keys)
171 "Replace the elements of SEQ1 with the elements of SEQ2.
172 SEQ1 is destructively modified, then returned.
173 \nKeywords supported: :start1 :end1 :start2 :end2
174 \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
175 (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) ()
176 (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1))
177 (or (= cl-start1 cl-start2)
178 (let* ((cl-len (length cl-seq1))
179 (cl-n (min (- (or cl-end1 cl-len) cl-start1)
180 (- (or cl-end2 cl-len) cl-start2))))
181 (while (>= (setq cl-n (1- cl-n)) 0)
182 (cl-set-elt cl-seq1 (+ cl-start1 cl-n)
183 (elt cl-seq2 (+ cl-start2 cl-n))))))
185 (let ((cl-p1 (nthcdr cl-start1 cl-seq1))
186 (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000)))
188 (let ((cl-p2 (nthcdr cl-start2 cl-seq2))
190 (if cl-end2 (- cl-end2 cl-start2) 4000000))))
191 (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0))
192 (setcar cl-p1 (car cl-p2))
193 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))))
194 (setq cl-end2 (min (or cl-end2 (length cl-seq2))
195 (+ cl-start2 cl-n1)))
196 (while (and cl-p1 (< cl-start2 cl-end2))
197 (setcar cl-p1 (aref cl-seq2 cl-start2))
198 (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2)))))
199 (setq cl-end1 (min (or cl-end1 (length cl-seq1))
200 (+ cl-start1 (- (or cl-end2 (length cl-seq2))
203 (let ((cl-p2 (nthcdr cl-start2 cl-seq2)))
204 (while (< cl-start1 cl-end1)
205 (aset cl-seq1 cl-start1 (car cl-p2))
206 (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1))))
207 (while (< cl-start1 cl-end1)
208 (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2))
209 (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1))))))
213 (defun remove* (cl-item cl-seq &rest cl-keys)
214 "Remove all occurrences of ITEM in SEQ.
215 This is a non-destructive function; it makes a copy of SEQ if necessary
216 to avoid corrupting the original SEQ.
217 \nKeywords supported: :test :test-not :key :count :start :end :from-end
218 \n(fn ITEM SEQ [KEYWORD VALUE]...)"
219 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
221 (if (<= (or cl-count (setq cl-count 8000000)) 0)
223 (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000)))
224 (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end
227 (let ((cl-res (apply 'delete* cl-item (append cl-seq nil)
228 (append (if cl-from-end
229 (list :end (1+ cl-i))
232 (if (listp cl-seq) cl-res
233 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))
235 (setq cl-end (- (or cl-end 8000000) cl-start))
237 (while (and cl-seq (> cl-end 0)
238 (cl-check-test cl-item (car cl-seq))
239 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
240 (> (setq cl-count (1- cl-count)) 0))))
241 (if (and (> cl-count 0) (> cl-end 0))
242 (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq)
243 (setq cl-end (1- cl-end)) (cdr cl-seq))))
244 (while (and cl-p (> cl-end 0)
245 (not (cl-check-test cl-item (car cl-p))))
246 (setq cl-p (cdr cl-p) cl-end (1- cl-end)))
247 (if (and cl-p (> cl-end 0))
248 (nconc (ldiff cl-seq cl-p)
249 (if (= cl-count 1) (cdr cl-p)
251 (apply 'delete* cl-item
252 (copy-sequence (cdr cl-p))
253 :start 0 :end (1- cl-end)
254 :count (1- cl-count) cl-keys))))
259 (defun remove-if (cl-pred cl-list &rest cl-keys)
260 "Remove all items satisfying PREDICATE in SEQ.
261 This is a non-destructive function; it makes a copy of SEQ if necessary
262 to avoid corrupting the original SEQ.
263 \nKeywords supported: :key :count :start :end :from-end
264 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
265 (apply 'remove* nil cl-list :if cl-pred cl-keys))
268 (defun remove-if-not (cl-pred cl-list &rest cl-keys)
269 "Remove all items not satisfying PREDICATE in SEQ.
270 This is a non-destructive function; it makes a copy of SEQ if necessary
271 to avoid corrupting the original SEQ.
272 \nKeywords supported: :key :count :start :end :from-end
273 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
274 (apply 'remove* nil cl-list :if-not cl-pred cl-keys))
277 (defun delete* (cl-item cl-seq &rest cl-keys)
278 "Remove all occurrences of ITEM in SEQ.
279 This is a destructive function; it reuses the storage of SEQ whenever possible.
280 \nKeywords supported: :test :test-not :key :count :start :end :from-end
281 \n(fn ITEM SEQ [KEYWORD VALUE]...)"
282 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
284 (if (<= (or cl-count (setq cl-count 8000000)) 0)
287 (if (and cl-from-end (< cl-count 4000000))
289 (while (and (>= (setq cl-count (1- cl-count)) 0)
290 (setq cl-i (cl-position cl-item cl-seq cl-start
291 cl-end cl-from-end)))
292 (if (= cl-i 0) (setq cl-seq (cdr cl-seq))
293 (let ((cl-tail (nthcdr (1- cl-i) cl-seq)))
294 (setcdr cl-tail (cdr (cdr cl-tail)))))
297 (setq cl-end (- (or cl-end 8000000) cl-start))
302 (cl-check-test cl-item (car cl-seq))
303 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
304 (> (setq cl-count (1- cl-count)) 0)))
305 (setq cl-end (1- cl-end)))
306 (setq cl-start (1- cl-start)))
307 (if (and (> cl-count 0) (> cl-end 0))
308 (let ((cl-p (nthcdr cl-start cl-seq)))
309 (while (and (cdr cl-p) (> cl-end 0))
310 (if (cl-check-test cl-item (car (cdr cl-p)))
312 (setcdr cl-p (cdr (cdr cl-p)))
313 (if (= (setq cl-count (1- cl-count)) 0)
315 (setq cl-p (cdr cl-p)))
316 (setq cl-end (1- cl-end)))))
318 (apply 'remove* cl-item cl-seq cl-keys)))))
321 (defun delete-if (cl-pred cl-list &rest cl-keys)
322 "Remove all items satisfying PREDICATE in SEQ.
323 This is a destructive function; it reuses the storage of SEQ whenever possible.
324 \nKeywords supported: :key :count :start :end :from-end
325 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
326 (apply 'delete* nil cl-list :if cl-pred cl-keys))
329 (defun delete-if-not (cl-pred cl-list &rest cl-keys)
330 "Remove all items not satisfying PREDICATE in SEQ.
331 This is a destructive function; it reuses the storage of SEQ whenever possible.
332 \nKeywords supported: :key :count :start :end :from-end
333 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
334 (apply 'delete* nil cl-list :if-not cl-pred cl-keys))
337 (defun remove-duplicates (cl-seq &rest cl-keys)
338 "Return a copy of SEQ with all duplicate elements removed.
339 \nKeywords supported: :test :test-not :key :start :end :from-end
340 \n(fn SEQ [KEYWORD VALUE]...)"
341 (cl-delete-duplicates cl-seq cl-keys t))
344 (defun delete-duplicates (cl-seq &rest cl-keys)
345 "Remove all duplicate elements from SEQ (destructively).
346 \nKeywords supported: :test :test-not :key :start :end :from-end
347 \n(fn SEQ [KEYWORD VALUE]...)"
348 (cl-delete-duplicates cl-seq cl-keys nil))
350 (defun cl-delete-duplicates (cl-seq cl-keys cl-copy)
352 (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if)
355 (let ((cl-p (nthcdr cl-start cl-seq)) cl-i)
356 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
359 (while (setq cl-i (cl-position (cl-check-key (car cl-p))
360 (cdr cl-p) cl-i (1- cl-end)))
361 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
362 cl-p (nthcdr cl-start cl-seq) cl-copy nil))
363 (let ((cl-tail (nthcdr cl-i cl-p)))
364 (setcdr cl-tail (cdr (cdr cl-tail))))
365 (setq cl-end (1- cl-end)))
366 (setq cl-p (cdr cl-p) cl-end (1- cl-end)
367 cl-start (1+ cl-start)))
369 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
370 (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1)
371 (cl-position (cl-check-key (car cl-seq))
372 (cdr cl-seq) 0 (1- cl-end)))
373 (setq cl-seq (cdr cl-seq) cl-end (1- cl-end)))
374 (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq)
375 (setq cl-end (1- cl-end) cl-start 1) cl-seq)))
376 (while (and (cdr (cdr cl-p)) (> cl-end 1))
377 (if (cl-position (cl-check-key (car (cdr cl-p)))
378 (cdr (cdr cl-p)) 0 (1- cl-end))
380 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
381 cl-p (nthcdr (1- cl-start) cl-seq)
383 (setcdr cl-p (cdr (cdr cl-p))))
384 (setq cl-p (cdr cl-p)))
385 (setq cl-end (1- cl-end) cl-start (1+ cl-start)))
387 (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil)))
388 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))))
391 (defun substitute (cl-new cl-old cl-seq &rest cl-keys)
392 "Substitute NEW for OLD in SEQ.
393 This is a non-destructive function; it makes a copy of SEQ if necessary
394 to avoid corrupting the original SEQ.
395 \nKeywords supported: :test :test-not :key :count :start :end :from-end
396 \n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
397 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
398 (:start 0) :end :from-end) ()
399 (if (or (eq cl-old cl-new)
400 (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0))
402 (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end)))
405 (setq cl-seq (copy-sequence cl-seq))
407 (progn (cl-set-elt cl-seq cl-i cl-new)
408 (setq cl-i (1+ cl-i) cl-count (1- cl-count))))
409 (apply 'nsubstitute cl-new cl-old cl-seq :count cl-count
410 :start cl-i cl-keys))))))
413 (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys)
414 "Substitute NEW for all items satisfying PREDICATE in SEQ.
415 This is a non-destructive function; it makes a copy of SEQ if necessary
416 to avoid corrupting the original SEQ.
417 \nKeywords supported: :key :count :start :end :from-end
418 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
419 (apply 'substitute cl-new nil cl-list :if cl-pred cl-keys))
422 (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
423 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
424 This is a non-destructive function; it makes a copy of SEQ if necessary
425 to avoid corrupting the original SEQ.
426 \nKeywords supported: :key :count :start :end :from-end
427 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
428 (apply 'substitute cl-new nil cl-list :if-not cl-pred cl-keys))
431 (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys)
432 "Substitute NEW for OLD in SEQ.
433 This is a destructive function; it reuses the storage of SEQ whenever possible.
434 \nKeywords supported: :test :test-not :key :count :start :end :from-end
435 \n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
436 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
437 (:start 0) :end :from-end) ()
438 (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0)
439 (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000)))
440 (let ((cl-p (nthcdr cl-start cl-seq)))
441 (setq cl-end (- (or cl-end 8000000) cl-start))
442 (while (and cl-p (> cl-end 0) (> cl-count 0))
443 (if (cl-check-test cl-old (car cl-p))
446 (setq cl-count (1- cl-count))))
447 (setq cl-p (cdr cl-p) cl-end (1- cl-end))))
448 (or cl-end (setq cl-end (length cl-seq)))
450 (while (and (< cl-start cl-end) (> cl-count 0))
451 (setq cl-end (1- cl-end))
452 (if (cl-check-test cl-old (elt cl-seq cl-end))
454 (cl-set-elt cl-seq cl-end cl-new)
455 (setq cl-count (1- cl-count)))))
456 (while (and (< cl-start cl-end) (> cl-count 0))
457 (if (cl-check-test cl-old (aref cl-seq cl-start))
459 (aset cl-seq cl-start cl-new)
460 (setq cl-count (1- cl-count))))
461 (setq cl-start (1+ cl-start))))))
465 (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys)
466 "Substitute NEW for all items satisfying PREDICATE in SEQ.
467 This is a destructive function; it reuses the storage of SEQ whenever possible.
468 \nKeywords supported: :key :count :start :end :from-end
469 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
470 (apply 'nsubstitute cl-new nil cl-list :if cl-pred cl-keys))
473 (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
474 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
475 This is a destructive function; it reuses the storage of SEQ whenever possible.
476 \nKeywords supported: :key :count :start :end :from-end
477 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
478 (apply 'nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys))
481 (defun find (cl-item cl-seq &rest cl-keys)
482 "Find the first occurrence of ITEM in SEQ.
483 Return the matching ITEM, or nil if not found.
484 \nKeywords supported: :test :test-not :key :start :end :from-end
485 \n(fn ITEM SEQ [KEYWORD VALUE]...)"
486 (let ((cl-pos (apply 'position cl-item cl-seq cl-keys)))
487 (and cl-pos (elt cl-seq cl-pos))))
490 (defun find-if (cl-pred cl-list &rest cl-keys)
491 "Find the first item satisfying PREDICATE in SEQ.
492 Return the matching item, or nil if not found.
493 \nKeywords supported: :key :start :end :from-end
494 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
495 (apply 'find nil cl-list :if cl-pred cl-keys))
498 (defun find-if-not (cl-pred cl-list &rest cl-keys)
499 "Find the first item not satisfying PREDICATE in SEQ.
500 Return the matching item, or nil if not found.
501 \nKeywords supported: :key :start :end :from-end
502 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
503 (apply 'find nil cl-list :if-not cl-pred cl-keys))
506 (defun position (cl-item cl-seq &rest cl-keys)
507 "Find the first occurrence of ITEM in SEQ.
508 Return the index of the matching item, or nil if not found.
509 \nKeywords supported: :test :test-not :key :start :end :from-end
510 \n(fn ITEM SEQ [KEYWORD VALUE]...)"
511 (cl-parsing-keywords (:test :test-not :key :if :if-not
512 (:start 0) :end :from-end) ()
513 (cl-position cl-item cl-seq cl-start cl-end cl-from-end)))
515 (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end)
517 (let ((cl-p (nthcdr cl-start cl-seq)))
518 (or cl-end (setq cl-end 8000000))
520 (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end))
521 (if (cl-check-test cl-item (car cl-p))
522 (setq cl-res cl-start))
523 (setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
525 (or cl-end (setq cl-end (length cl-seq)))
528 (while (and (>= (setq cl-end (1- cl-end)) cl-start)
529 (not (cl-check-test cl-item (aref cl-seq cl-end)))))
530 (and (>= cl-end cl-start) cl-end))
531 (while (and (< cl-start cl-end)
532 (not (cl-check-test cl-item (aref cl-seq cl-start))))
533 (setq cl-start (1+ cl-start)))
534 (and (< cl-start cl-end) cl-start))))
537 (defun position-if (cl-pred cl-list &rest cl-keys)
538 "Find the first item satisfying PREDICATE in SEQ.
539 Return the index of the matching item, or nil if not found.
540 \nKeywords supported: :key :start :end :from-end
541 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
542 (apply 'position nil cl-list :if cl-pred cl-keys))
545 (defun position-if-not (cl-pred cl-list &rest cl-keys)
546 "Find the first item not satisfying PREDICATE in SEQ.
547 Return the index of the matching item, or nil if not found.
548 \nKeywords supported: :key :start :end :from-end
549 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
550 (apply 'position nil cl-list :if-not cl-pred cl-keys))
553 (defun count (cl-item cl-seq &rest cl-keys)
554 "Count the number of occurrences of ITEM in SEQ.
555 \nKeywords supported: :test :test-not :key :start :end
556 \n(fn ITEM SEQ [KEYWORD VALUE]...)"
557 (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) ()
558 (let ((cl-count 0) cl-x)
559 (or cl-end (setq cl-end (length cl-seq)))
560 (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
561 (while (< cl-start cl-end)
562 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
563 (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count)))
564 (setq cl-start (1+ cl-start)))
568 (defun count-if (cl-pred cl-list &rest cl-keys)
569 "Count the number of items satisfying PREDICATE in SEQ.
570 \nKeywords supported: :key :start :end
571 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
572 (apply 'count nil cl-list :if cl-pred cl-keys))
575 (defun count-if-not (cl-pred cl-list &rest cl-keys)
576 "Count the number of items not satisfying PREDICATE in SEQ.
577 \nKeywords supported: :key :start :end
578 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
579 (apply 'count nil cl-list :if-not cl-pred cl-keys))
582 (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys)
583 "Compare SEQ1 with SEQ2, return index of first mismatching element.
584 Return nil if the sequences match. If one sequence is a prefix of the
585 other, the return value indicates the end of the shorter sequence.
586 \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end
587 \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
588 (cl-parsing-keywords (:test :test-not :key :from-end
589 (:start1 0) :end1 (:start2 0) :end2) ()
590 (or cl-end1 (setq cl-end1 (length cl-seq1)))
591 (or cl-end2 (setq cl-end2 (length cl-seq2)))
594 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
595 (cl-check-match (elt cl-seq1 (1- cl-end1))
596 (elt cl-seq2 (1- cl-end2))))
597 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2)))
598 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
600 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1)))
601 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2))))
602 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
603 (cl-check-match (if cl-p1 (car cl-p1)
604 (aref cl-seq1 cl-start1))
605 (if cl-p2 (car cl-p2)
606 (aref cl-seq2 cl-start2))))
607 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)
608 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2)))
609 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
613 (defun search (cl-seq1 cl-seq2 &rest cl-keys)
614 "Search for SEQ1 as a subsequence of SEQ2.
615 Return the index of the leftmost element of the first match found;
616 return nil if there are no matches.
617 \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end
618 \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
619 (cl-parsing-keywords (:test :test-not :key :from-end
620 (:start1 0) :end1 (:start2 0) :end2) ()
621 (or cl-end1 (setq cl-end1 (length cl-seq1)))
622 (or cl-end2 (setq cl-end2 (length cl-seq2)))
623 (if (>= cl-start1 cl-end1)
624 (if cl-from-end cl-end2 cl-start2)
625 (let* ((cl-len (- cl-end1 cl-start1))
626 (cl-first (cl-check-key (elt cl-seq1 cl-start1)))
628 (setq cl-end2 (- cl-end2 (1- cl-len)))
629 (while (and (< cl-start2 cl-end2)
630 (setq cl-pos (cl-position cl-first cl-seq2
631 cl-start2 cl-end2 cl-from-end))
632 (apply 'mismatch cl-seq1 cl-seq2
633 :start1 (1+ cl-start1) :end1 cl-end1
634 :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len)
635 :from-end nil cl-keys))
636 (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos))))
637 (and (< cl-start2 cl-end2) cl-pos)))))
640 (defun sort* (cl-seq cl-pred &rest cl-keys)
641 "Sort the argument SEQ according to PREDICATE.
642 This is a destructive function; it reuses the storage of SEQ if possible.
643 \nKeywords supported: :key
644 \n(fn SEQ PREDICATE [KEYWORD VALUE]...)"
646 (replace cl-seq (apply 'sort* (append cl-seq nil) cl-pred cl-keys))
647 (cl-parsing-keywords (:key) ()
648 (if (memq cl-key '(nil identity))
649 (sort cl-seq cl-pred)
650 (sort cl-seq (function (lambda (cl-x cl-y)
651 (funcall cl-pred (funcall cl-key cl-x)
652 (funcall cl-key cl-y)))))))))
655 (defun stable-sort (cl-seq cl-pred &rest cl-keys)
656 "Sort the argument SEQ stably according to PREDICATE.
657 This is a destructive function; it reuses the storage of SEQ if possible.
658 \nKeywords supported: :key
659 \n(fn SEQ PREDICATE [KEYWORD VALUE]...)"
660 (apply 'sort* cl-seq cl-pred cl-keys))
663 (defun merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys)
664 "Destructively merge the two sequences to produce a new sequence.
665 TYPE is the sequence type to return, SEQ1 and SEQ2 are the two argument
666 sequences, and PREDICATE is a `less-than' predicate on the elements.
667 \nKeywords supported: :key
668 \n(fn TYPE SEQ1 SEQ2 PREDICATE [KEYWORD VALUE]...)"
669 (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil)))
670 (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil)))
671 (cl-parsing-keywords (:key) ()
673 (while (and cl-seq1 cl-seq2)
674 (if (funcall cl-pred (cl-check-key (car cl-seq2))
675 (cl-check-key (car cl-seq1)))
676 (push (pop cl-seq2) cl-res)
677 (push (pop cl-seq1) cl-res)))
678 (coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type))))
680 ;;; See compiler macro in cl-macs.el
682 (defun member* (cl-item cl-list &rest cl-keys)
683 "Find the first occurrence of ITEM in LIST.
684 Return the sublist of LIST whose car is ITEM.
685 \nKeywords supported: :test :test-not :key
686 \n(fn ITEM LIST [KEYWORD VALUE]...)"
688 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
689 (while (and cl-list (not (cl-check-test cl-item (car cl-list))))
690 (setq cl-list (cdr cl-list)))
692 (if (and (numberp cl-item) (not (integerp cl-item)))
693 (member cl-item cl-list)
694 (memq cl-item cl-list))))
697 (defun member-if (cl-pred cl-list &rest cl-keys)
698 "Find the first item satisfying PREDICATE in LIST.
699 Return the sublist of LIST whose car matches.
700 \nKeywords supported: :key
701 \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
702 (apply 'member* nil cl-list :if cl-pred cl-keys))
705 (defun member-if-not (cl-pred cl-list &rest cl-keys)
706 "Find the first item not satisfying PREDICATE in LIST.
707 Return the sublist of LIST whose car matches.
708 \nKeywords supported: :key
709 \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
710 (apply 'member* nil cl-list :if-not cl-pred cl-keys))
713 (defun cl-adjoin (cl-item cl-list &rest cl-keys)
714 (if (cl-parsing-keywords (:key) t
715 (apply 'member* (cl-check-key cl-item) cl-list cl-keys))
717 (cons cl-item cl-list)))
719 ;;; See compiler macro in cl-macs.el
721 (defun assoc* (cl-item cl-alist &rest cl-keys)
722 "Find the first item whose car matches ITEM in LIST.
723 \nKeywords supported: :test :test-not :key
724 \n(fn ITEM LIST [KEYWORD VALUE]...)"
726 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
728 (or (not (consp (car cl-alist)))
729 (not (cl-check-test cl-item (car (car cl-alist))))))
730 (setq cl-alist (cdr cl-alist)))
731 (and cl-alist (car cl-alist)))
732 (if (and (numberp cl-item) (not (integerp cl-item)))
733 (assoc cl-item cl-alist)
734 (assq cl-item cl-alist))))
737 (defun assoc-if (cl-pred cl-list &rest cl-keys)
738 "Find the first item whose car satisfies PREDICATE in LIST.
739 \nKeywords supported: :key
740 \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
741 (apply 'assoc* nil cl-list :if cl-pred cl-keys))
744 (defun assoc-if-not (cl-pred cl-list &rest cl-keys)
745 "Find the first item whose car does not satisfy PREDICATE in LIST.
746 \nKeywords supported: :key
747 \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
748 (apply 'assoc* nil cl-list :if-not cl-pred cl-keys))
751 (defun rassoc* (cl-item cl-alist &rest cl-keys)
752 "Find the first item whose cdr matches ITEM in LIST.
753 \nKeywords supported: :test :test-not :key
754 \n(fn ITEM LIST [KEYWORD VALUE]...)"
755 (if (or cl-keys (numberp cl-item))
756 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
758 (or (not (consp (car cl-alist)))
759 (not (cl-check-test cl-item (cdr (car cl-alist))))))
760 (setq cl-alist (cdr cl-alist)))
761 (and cl-alist (car cl-alist)))
762 (rassq cl-item cl-alist)))
765 (defun rassoc-if (cl-pred cl-list &rest cl-keys)
766 "Find the first item whose cdr satisfies PREDICATE in LIST.
767 \nKeywords supported: :key
768 \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
769 (apply 'rassoc* nil cl-list :if cl-pred cl-keys))
772 (defun rassoc-if-not (cl-pred cl-list &rest cl-keys)
773 "Find the first item whose cdr does not satisfy PREDICATE in LIST.
774 \nKeywords supported: :key
775 \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
776 (apply 'rassoc* nil cl-list :if-not cl-pred cl-keys))
779 (defun union (cl-list1 cl-list2 &rest cl-keys)
780 "Combine LIST1 and LIST2 using a set-union operation.
781 The result list contains all items that appear in either LIST1 or LIST2.
782 This is a non-destructive function; it makes a copy of the data if necessary
783 to avoid corrupting the original LIST1 and LIST2.
784 \nKeywords supported: :test :test-not :key
785 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
786 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
787 ((equal cl-list1 cl-list2) cl-list1)
789 (or (>= (length cl-list1) (length cl-list2))
790 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
792 (if (or cl-keys (numberp (car cl-list2)))
793 (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys))
794 (or (memq (car cl-list2) cl-list1)
795 (push (car cl-list2) cl-list1)))
800 (defun nunion (cl-list1 cl-list2 &rest cl-keys)
801 "Combine LIST1 and LIST2 using a set-union operation.
802 The result list contains all items that appear in either LIST1 or LIST2.
803 This is a destructive function; it reuses the storage of LIST1 and LIST2
805 \nKeywords supported: :test :test-not :key
806 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
807 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
808 (t (apply 'union cl-list1 cl-list2 cl-keys))))
811 (defun intersection (cl-list1 cl-list2 &rest cl-keys)
812 "Combine LIST1 and LIST2 using a set-intersection operation.
813 The result list contains all items that appear in both LIST1 and LIST2.
814 This is a non-destructive function; it makes a copy of the data if necessary
815 to avoid corrupting the original LIST1 and LIST2.
816 \nKeywords supported: :test :test-not :key
817 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
818 (and cl-list1 cl-list2
819 (if (equal cl-list1 cl-list2) cl-list1
820 (cl-parsing-keywords (:key) (:test :test-not)
822 (or (>= (length cl-list1) (length cl-list2))
823 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
825 (if (if (or cl-keys (numberp (car cl-list2)))
826 (apply 'member* (cl-check-key (car cl-list2))
828 (memq (car cl-list2) cl-list1))
829 (push (car cl-list2) cl-res))
834 (defun nintersection (cl-list1 cl-list2 &rest cl-keys)
835 "Combine LIST1 and LIST2 using a set-intersection operation.
836 The result list contains all items that appear in both LIST1 and LIST2.
837 This is a destructive function; it reuses the storage of LIST1 and LIST2
839 \nKeywords supported: :test :test-not :key
840 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
841 (and cl-list1 cl-list2 (apply 'intersection cl-list1 cl-list2 cl-keys)))
844 (defun set-difference (cl-list1 cl-list2 &rest cl-keys)
845 "Combine LIST1 and LIST2 using a set-difference operation.
846 The result list contains all items that appear in LIST1 but not LIST2.
847 This is a non-destructive function; it makes a copy of the data if necessary
848 to avoid corrupting the original LIST1 and LIST2.
849 \nKeywords supported: :test :test-not :key
850 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
851 (if (or (null cl-list1) (null cl-list2)) cl-list1
852 (cl-parsing-keywords (:key) (:test :test-not)
855 (or (if (or cl-keys (numberp (car cl-list1)))
856 (apply 'member* (cl-check-key (car cl-list1))
858 (memq (car cl-list1) cl-list2))
859 (push (car cl-list1) cl-res))
864 (defun nset-difference (cl-list1 cl-list2 &rest cl-keys)
865 "Combine LIST1 and LIST2 using a set-difference operation.
866 The result list contains all items that appear in LIST1 but not LIST2.
867 This is a destructive function; it reuses the storage of LIST1 and LIST2
869 \nKeywords supported: :test :test-not :key
870 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
871 (if (or (null cl-list1) (null cl-list2)) cl-list1
872 (apply 'set-difference cl-list1 cl-list2 cl-keys)))
875 (defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
876 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
877 The result list contains all items that appear in exactly one of LIST1, LIST2.
878 This is a non-destructive function; it makes a copy of the data if necessary
879 to avoid corrupting the original LIST1 and LIST2.
880 \nKeywords supported: :test :test-not :key
881 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
882 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
883 ((equal cl-list1 cl-list2) nil)
884 (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys)
885 (apply 'set-difference cl-list2 cl-list1 cl-keys)))))
888 (defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
889 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
890 The result list contains all items that appear in exactly one of LIST1, LIST2.
891 This is a destructive function; it reuses the storage of LIST1 and LIST2
893 \nKeywords supported: :test :test-not :key
894 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
895 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
896 ((equal cl-list1 cl-list2) nil)
897 (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys)
898 (apply 'nset-difference cl-list2 cl-list1 cl-keys)))))
901 (defun subsetp (cl-list1 cl-list2 &rest cl-keys)
902 "Return true if LIST1 is a subset of LIST2.
903 I.e., if every element of LIST1 also appears in LIST2.
904 \nKeywords supported: :test :test-not :key
905 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
906 (cond ((null cl-list1) t) ((null cl-list2) nil)
907 ((equal cl-list1 cl-list2) t)
908 (t (cl-parsing-keywords (:key) (:test :test-not)
910 (apply 'member* (cl-check-key (car cl-list1))
916 (defun subst-if (cl-new cl-pred cl-tree &rest cl-keys)
917 "Substitute NEW for elements matching PREDICATE in TREE (non-destructively).
918 Return a copy of TREE with all matching elements replaced by NEW.
919 \nKeywords supported: :key
920 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
921 (apply 'sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
924 (defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
925 "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively).
926 Return a copy of TREE with all non-matching elements replaced by NEW.
927 \nKeywords supported: :key
928 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
929 (apply 'sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
932 (defun nsubst (cl-new cl-old cl-tree &rest cl-keys)
933 "Substitute NEW for OLD everywhere in TREE (destructively).
934 Any element of TREE which is `eql' to OLD is changed to NEW (via a call
936 \nKeywords supported: :test :test-not :key
937 \n(fn NEW OLD TREE [KEYWORD VALUE]...)"
938 (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys))
941 (defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys)
942 "Substitute NEW for elements matching PREDICATE in TREE (destructively).
943 Any element of TREE which matches is changed to NEW (via a call to `setcar').
944 \nKeywords supported: :key
945 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
946 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
949 (defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
950 "Substitute NEW for elements not matching PREDICATE in TREE (destructively).
951 Any element of TREE which matches is changed to NEW (via a call to `setcar').
952 \nKeywords supported: :key
953 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
954 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
957 (defun sublis (cl-alist cl-tree &rest cl-keys)
958 "Perform substitutions indicated by ALIST in TREE (non-destructively).
959 Return a copy of TREE with all matching elements replaced.
960 \nKeywords supported: :test :test-not :key
961 \n(fn ALIST TREE [KEYWORD VALUE]...)"
962 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
963 (cl-sublis-rec cl-tree)))
966 (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if*
967 (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist))
968 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
969 (setq cl-p (cdr cl-p)))
970 (if cl-p (cdr (car cl-p))
972 (let ((cl-a (cl-sublis-rec (car cl-tree)))
973 (cl-d (cl-sublis-rec (cdr cl-tree))))
974 (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree)))
980 (defun nsublis (cl-alist cl-tree &rest cl-keys)
981 "Perform substitutions indicated by ALIST in TREE (destructively).
982 Any matching element of TREE is changed via a call to `setcar'.
983 \nKeywords supported: :test :test-not :key
984 \n(fn ALIST TREE [KEYWORD VALUE]...)"
985 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
986 (let ((cl-hold (list cl-tree)))
987 (cl-nsublis-rec cl-hold)
990 (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if*
991 (while (consp cl-tree)
992 (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist))
993 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
994 (setq cl-p (cdr cl-p)))
995 (if cl-p (setcar cl-tree (cdr (car cl-p)))
996 (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree))))
997 (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist)
998 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
999 (setq cl-p (cdr cl-p)))
1001 (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil))
1002 (setq cl-tree (cdr cl-tree))))))
1005 (defun tree-equal (cl-x cl-y &rest cl-keys)
1006 "Return t if trees TREE1 and TREE2 have `eql' leaves.
1007 Atoms are compared by `eql'; cons cells are compared recursively.
1008 \nKeywords supported: :test :test-not :key
1009 \n(fn TREE1 TREE2 [KEYWORD VALUE]...)"
1010 (cl-parsing-keywords (:test :test-not :key) ()
1011 (cl-tree-equal-rec cl-x cl-y)))
1013 (defun cl-tree-equal-rec (cl-x cl-y)
1014 (while (and (consp cl-x) (consp cl-y)
1015 (cl-tree-equal-rec (car cl-x) (car cl-y)))
1016 (setq cl-x (cdr cl-x) cl-y (cdr cl-y)))
1017 (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y)))
1020 (run-hooks 'cl-seq-load-hook)
1023 ;; byte-compile-dynamic: t
1024 ;; byte-compile-warnings: (not cl-functions)
1025 ;; generated-autoload-file: "cl-loaddefs.el"
1028 ;; arch-tag: ec1cc072-9006-4225-b6ba-d6b07ed1710c
1029 ;;; cl-seq.el ends here