1 ;;; cl-seq.el --- Common Lisp extensions for GNU Emacs Lisp (part three)
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainer: XEmacs Development Team
8 ;; Keywords: extensions, dumped
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 ;;; Synched up with: FSF 19.34.
31 ;; This file is dumped with XEmacs.
33 ;; These are extensions to Emacs Lisp that provide a degree of
34 ;; Common Lisp compatibility, beyond what is already built-in
37 ;; This package was written by Dave Gillespie; it is a complete
38 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
40 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
42 ;; Bug reports, comments, and suggestions are welcome!
44 ;; This file contains the Common Lisp sequence and list functions
45 ;; which take keyword arguments.
47 ;; See cl.el for Change Log.
52 (or (memq 'cl-19 features)
53 (error "Tried to load `cl-seq' before `cl'!"))
56 ;;; We define these here so that this file can compile without having
57 ;;; loaded the cl.el file already.
59 (defmacro cl-push (x place) (list 'setq place (list 'cons x place)))
60 (defmacro cl-pop (place)
61 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place)))))
64 ;;; Keyword parsing. This is special-cased here so that we can compile
65 ;;; this file independent from cl-macs.
67 (defmacro cl-parsing-keywords (kwords other-keys &rest body)
68 "Helper macro for functions with keyword arguments.
69 This is a temporary solution, until keyword arguments are natively supported.
70 Declare your function ending with (... &rest cl-keys), then wrap the
71 function body in a call to `cl-parsing-keywords'.
73 KWORDS is a list of keyword definitions. Each definition should be
74 either a keyword or a list (KEYWORD DEFAULT-VALUE). In the former case,
75 the default value is nil. The keywords are available in BODY as the name
76 of the keyword, minus its initial colon and prepended with `cl-'.
78 OTHER-KEYS specifies other keywords that are accepted but ignored. It
79 is either the value 't' (ignore all other keys, equivalent to the
80 &allow-other-keys argument declaration in Common Lisp) or a list in the
81 same format as KWORDS. If keywords are given that are not in KWORDS
82 and not allowed by OTHER-KEYS, an error will normally be signalled; but
83 the caller can override this by specifying a non-nil value for the
84 keyword :allow-other-keys (which defaults to t)."
90 (let* ((var (if (consp x) (car x) x))
91 (mem (list 'car (list 'cdr (list 'memq (list 'quote var)
93 (if (eq var ':test-not)
94 (setq mem (list 'and mem (list 'setq 'cl-test mem) t)))
96 (setq mem (list 'and mem (list 'setq 'cl-if mem) t)))
98 (format "cl-%s" (substring (symbol-name var) 1)))
99 (if (consp x) (list 'or mem (car (cdr x))) mem)))))
102 (and (not (eq other-keys t))
104 (list 'let '((cl-keys-temp cl-keys))
105 (list 'while 'cl-keys-temp
106 (list 'or (list 'memq '(car cl-keys-temp)
115 '(car (cdr (memq (quote :allow-other-keys)
117 '(error "Bad keyword argument %s"
119 '(setq cl-keys-temp (cdr (cdr cl-keys-temp)))))))
121 (put 'cl-parsing-keywords 'lisp-indent-function 2)
122 (put 'cl-parsing-keywords 'edebug-form-spec '(sexp sexp &rest form))
124 (defmacro cl-check-key (x)
125 (list 'if 'cl-key (list 'funcall 'cl-key x) x))
127 (defmacro cl-check-test-nokey (item x)
130 (list 'eq (list 'not (list 'funcall 'cl-test item x))
133 (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not))
134 (list 't (list 'if (list 'numberp item)
135 (list 'equal item x) (list 'eq item x)))))
137 (defmacro cl-check-test (item x)
138 (list 'cl-check-test-nokey item (list 'cl-check-key x)))
140 (defmacro cl-check-match (x y)
141 (setq x (list 'cl-check-key x) y (list 'cl-check-key y))
143 (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not)
144 (list 'if (list 'numberp x)
145 (list 'equal x y) (list 'eq x y))))
147 (put 'cl-check-key 'edebug-form-spec 'edebug-forms)
148 (put 'cl-check-test 'edebug-form-spec 'edebug-forms)
149 (put 'cl-check-test-nokey 'edebug-form-spec 'edebug-forms)
150 (put 'cl-check-match 'edebug-form-spec 'edebug-forms)
152 (defvar cl-test) (defvar cl-test-not)
153 (defvar cl-if) (defvar cl-if-not)
157 (defun reduce (cl-func cl-seq &rest cl-keys)
158 "Reduce two-argument FUNCTION across SEQUENCE.
159 Keywords supported: :start :end :from-end :initial-value :key"
160 (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
161 (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
162 (setq cl-seq (subseq cl-seq cl-start cl-end))
163 (if cl-from-end (setq cl-seq (nreverse cl-seq)))
164 (let ((cl-accum (cond ((memq ':initial-value cl-keys) cl-initial-value)
165 (cl-seq (cl-check-key (cl-pop cl-seq)))
166 (t (funcall cl-func)))))
169 (setq cl-accum (funcall cl-func (cl-check-key (cl-pop cl-seq))
172 (setq cl-accum (funcall cl-func cl-accum
173 (cl-check-key (cl-pop cl-seq))))))
176 (defun fill (seq item &rest cl-keys)
177 "Fill the elements of SEQ with ITEM.
178 Keywords supported: :start :end"
179 (cl-parsing-keywords ((:start 0) :end) ()
181 (let ((p (nthcdr cl-start seq))
182 (n (if cl-end (- cl-end cl-start) 8000000)))
183 (while (and p (>= (setq n (1- n)) 0))
186 (or cl-end (setq cl-end (length seq)))
187 (if (and (= cl-start 0) (= cl-end (length seq)))
189 (while (< cl-start cl-end)
190 (aset seq cl-start item)
191 (setq cl-start (1+ cl-start)))))
194 (defun replace (cl-seq1 cl-seq2 &rest cl-keys)
195 "Replace the elements of SEQ1 with the elements of SEQ2.
196 SEQ1 is destructively modified, then returned.
197 Keywords supported: :start1 :end1 :start2 :end2"
198 (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) ()
199 (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1))
200 (or (= cl-start1 cl-start2)
201 (let* ((cl-len (length cl-seq1))
202 (cl-n (min (- (or cl-end1 cl-len) cl-start1)
203 (- (or cl-end2 cl-len) cl-start2))))
204 (while (>= (setq cl-n (1- cl-n)) 0)
205 (cl-set-elt cl-seq1 (+ cl-start1 cl-n)
206 (elt cl-seq2 (+ cl-start2 cl-n))))))
208 (let ((cl-p1 (nthcdr cl-start1 cl-seq1))
209 (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000)))
211 (let ((cl-p2 (nthcdr cl-start2 cl-seq2))
213 (if cl-end2 (- cl-end2 cl-start2) 4000000))))
214 (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0))
215 (setcar cl-p1 (car cl-p2))
216 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))))
217 (setq cl-end2 (min (or cl-end2 (length cl-seq2))
218 (+ cl-start2 cl-n1)))
219 (while (and cl-p1 (< cl-start2 cl-end2))
220 (setcar cl-p1 (aref cl-seq2 cl-start2))
221 (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2)))))
222 (setq cl-end1 (min (or cl-end1 (length cl-seq1))
223 (+ cl-start1 (- (or cl-end2 (length cl-seq2))
226 (let ((cl-p2 (nthcdr cl-start2 cl-seq2)))
227 (while (< cl-start1 cl-end1)
228 (aset cl-seq1 cl-start1 (car cl-p2))
229 (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1))))
230 (while (< cl-start1 cl-end1)
231 (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2))
232 (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1))))))
235 (defun remove* (cl-item cl-seq &rest cl-keys)
236 "Remove all occurrences of ITEM in SEQ.
237 This is a non-destructive function; it makes a copy of SEQ if necessary
238 to avoid corrupting the original SEQ.
239 Keywords supported: :test :test-not :key :count :start :end :from-end"
240 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
242 (if (<= (or cl-count (setq cl-count 8000000)) 0)
244 (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000)))
245 (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end
248 (let ((cl-res (apply 'delete* cl-item (append cl-seq nil)
249 (append (if cl-from-end
250 (list ':end (1+ cl-i))
253 (if (listp cl-seq) cl-res
254 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))
256 (setq cl-end (- (or cl-end 8000000) cl-start))
258 (while (and cl-seq (> cl-end 0)
259 (cl-check-test cl-item (car cl-seq))
260 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
261 (> (setq cl-count (1- cl-count)) 0))))
262 (if (and (> cl-count 0) (> cl-end 0))
263 (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq)
264 (setq cl-end (1- cl-end)) (cdr cl-seq))))
265 (while (and cl-p (> cl-end 0)
266 (not (cl-check-test cl-item (car cl-p))))
267 (setq cl-p (cdr cl-p) cl-end (1- cl-end)))
268 (if (and cl-p (> cl-end 0))
269 (nconc (ldiff cl-seq cl-p)
270 (if (= cl-count 1) (cdr cl-p)
272 (apply 'delete* cl-item
273 (copy-sequence (cdr cl-p))
274 ':start 0 ':end (1- cl-end)
275 ':count (1- cl-count) cl-keys))))
279 (defun remove-if (cl-pred cl-list &rest cl-keys)
280 "Remove all items satisfying PREDICATE in SEQ.
281 This is a non-destructive function; it makes a copy of SEQ if necessary
282 to avoid corrupting the original SEQ.
283 Keywords supported: :key :count :start :end :from-end"
284 (apply 'remove* nil cl-list ':if cl-pred cl-keys))
286 (defun remove-if-not (cl-pred cl-list &rest cl-keys)
287 "Remove all items not satisfying PREDICATE in SEQ.
288 This is a non-destructive function; it makes a copy of SEQ if necessary
289 to avoid corrupting the original SEQ.
290 Keywords supported: :key :count :start :end :from-end"
291 (apply 'remove* nil cl-list ':if-not cl-pred cl-keys))
293 (defun delete* (cl-item cl-seq &rest cl-keys)
294 "Remove all occurrences of ITEM in SEQ.
295 This is a destructive function; it reuses the storage of SEQ whenever possible.
296 Keywords supported: :test :test-not :key :count :start :end :from-end"
297 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
299 (if (<= (or cl-count (setq cl-count 8000000)) 0)
302 (if (and cl-from-end (< cl-count 4000000))
304 (while (and (>= (setq cl-count (1- cl-count)) 0)
305 (setq cl-i (cl-position cl-item cl-seq cl-start
306 cl-end cl-from-end)))
307 (if (= cl-i 0) (setq cl-seq (cdr cl-seq))
308 (let ((cl-tail (nthcdr (1- cl-i) cl-seq)))
309 (setcdr cl-tail (cdr (cdr cl-tail)))))
312 (setq cl-end (- (or cl-end 8000000) cl-start))
317 (cl-check-test cl-item (car cl-seq))
318 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
319 (> (setq cl-count (1- cl-count)) 0)))
320 (setq cl-end (1- cl-end)))
321 (setq cl-start (1- cl-start)))
322 (if (and (> cl-count 0) (> cl-end 0))
323 (let ((cl-p (nthcdr cl-start cl-seq)))
324 (while (and (cdr cl-p) (> cl-end 0))
325 (if (cl-check-test cl-item (car (cdr cl-p)))
327 (setcdr cl-p (cdr (cdr cl-p)))
328 (if (= (setq cl-count (1- cl-count)) 0)
330 (setq cl-p (cdr cl-p)))
331 (setq cl-end (1- cl-end)))))
333 (apply 'remove* cl-item cl-seq cl-keys)))))
335 (defun delete-if (cl-pred cl-list &rest cl-keys)
336 "Remove all items satisfying PREDICATE in SEQ.
337 This is a destructive function; it reuses the storage of SEQ whenever possible.
338 Keywords supported: :key :count :start :end :from-end"
339 (apply 'delete* nil cl-list ':if cl-pred cl-keys))
341 (defun delete-if-not (cl-pred cl-list &rest cl-keys)
342 "Remove all items not satisfying PREDICATE in SEQ.
343 This is a destructive function; it reuses the storage of SEQ whenever possible.
344 Keywords supported: :key :count :start :end :from-end"
345 (apply 'delete* nil cl-list ':if-not cl-pred cl-keys))
347 (or (and (fboundp 'delete) (subrp (symbol-function 'delete)))
348 (defalias 'delete (function (lambda (x y) (delete* x y ':test 'equal)))))
350 (defun remove (cl-item cl-seq)
351 "Remove all occurrences of ITEM in SEQ, testing with `equal'
352 This is a non-destructive function; it makes a copy of SEQ if necessary
353 to avoid corrupting the original SEQ.
354 Also see: `remove*', `delete', `delete*'"
355 (remove* cl-item cl-seq ':test 'equal))
357 (defun remq (cl-elt cl-list)
358 "Remove all occurrences of ELT in LIST, comparing with `eq'.
359 This is a non-destructive function; it makes a copy of LIST to avoid
360 corrupting the original LIST.
361 Also see: `delq', `delete', `delete*', `remove', `remove*'."
362 (if (memq cl-elt cl-list)
363 (delq cl-elt (copy-list cl-list))
366 (defun remove-duplicates (cl-seq &rest cl-keys)
367 "Return a copy of SEQ with all duplicate elements removed.
368 Keywords supported: :test :test-not :key :start :end :from-end"
369 (cl-delete-duplicates cl-seq cl-keys t))
371 (defun delete-duplicates (cl-seq &rest cl-keys)
372 "Remove all duplicate elements from SEQ (destructively).
373 Keywords supported: :test :test-not :key :start :end :from-end"
374 (cl-delete-duplicates cl-seq cl-keys nil))
376 (defun cl-delete-duplicates (cl-seq cl-keys cl-copy)
378 (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if)
381 (let ((cl-p (nthcdr cl-start cl-seq)) cl-i)
382 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
385 (while (setq cl-i (cl-position (cl-check-key (car cl-p))
386 (cdr cl-p) cl-i (1- cl-end)))
387 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
388 cl-p (nthcdr cl-start cl-seq) cl-copy nil))
389 (let ((cl-tail (nthcdr cl-i cl-p)))
390 (setcdr cl-tail (cdr (cdr cl-tail))))
391 (setq cl-end (1- cl-end)))
392 (setq cl-p (cdr cl-p) cl-end (1- cl-end)
393 cl-start (1+ cl-start)))
395 (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
396 (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1)
397 (cl-position (cl-check-key (car cl-seq))
398 (cdr cl-seq) 0 (1- cl-end)))
399 (setq cl-seq (cdr cl-seq) cl-end (1- cl-end)))
400 (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq)
401 (setq cl-end (1- cl-end) cl-start 1) cl-seq)))
402 (while (and (cdr (cdr cl-p)) (> cl-end 1))
403 (if (cl-position (cl-check-key (car (cdr cl-p)))
404 (cdr (cdr cl-p)) 0 (1- cl-end))
406 (if cl-copy (setq cl-seq (copy-sequence cl-seq)
407 cl-p (nthcdr (1- cl-start) cl-seq)
409 (setcdr cl-p (cdr (cdr cl-p))))
410 (setq cl-p (cdr cl-p)))
411 (setq cl-end (1- cl-end) cl-start (1+ cl-start)))
413 (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil)))
414 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))))
416 (defun substitute (cl-new cl-old cl-seq &rest cl-keys)
417 "Substitute NEW for OLD in SEQ.
418 This is a non-destructive function; it makes a copy of SEQ if necessary
419 to avoid corrupting the original SEQ.
420 Keywords supported: :test :test-not :key :count :start :end :from-end"
421 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
422 (:start 0) :end :from-end) ()
423 (if (or (eq cl-old cl-new)
424 (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0))
426 (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end)))
429 (setq cl-seq (copy-sequence cl-seq))
431 (progn (cl-set-elt cl-seq cl-i cl-new)
432 (setq cl-i (1+ cl-i) cl-count (1- cl-count))))
433 (apply 'nsubstitute cl-new cl-old cl-seq ':count cl-count
434 ':start cl-i cl-keys))))))
436 (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys)
437 "Substitute NEW for all items satisfying PREDICATE in SEQ.
438 This is a non-destructive function; it makes a copy of SEQ if necessary
439 to avoid corrupting the original SEQ.
440 Keywords supported: :key :count :start :end :from-end"
441 (apply 'substitute cl-new nil cl-list ':if cl-pred cl-keys))
443 (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
444 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
445 This is a non-destructive function; it makes a copy of SEQ if necessary
446 to avoid corrupting the original SEQ.
447 Keywords supported: :key :count :start :end :from-end"
448 (apply 'substitute cl-new nil cl-list ':if-not cl-pred cl-keys))
450 (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys)
451 "Substitute NEW for OLD in SEQ.
452 This is a destructive function; it reuses the storage of SEQ whenever possible.
453 Keywords supported: :test :test-not :key :count :start :end :from-end"
454 (cl-parsing-keywords (:test :test-not :key :if :if-not :count
455 (:start 0) :end :from-end) ()
456 (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0)
457 (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000)))
458 (let ((cl-p (nthcdr cl-start cl-seq)))
459 (setq cl-end (- (or cl-end 8000000) cl-start))
460 (while (and cl-p (> cl-end 0) (> cl-count 0))
461 (if (cl-check-test cl-old (car cl-p))
464 (setq cl-count (1- cl-count))))
465 (setq cl-p (cdr cl-p) cl-end (1- cl-end))))
466 (or cl-end (setq cl-end (length cl-seq)))
468 (while (and (< cl-start cl-end) (> cl-count 0))
469 (setq cl-end (1- cl-end))
470 (if (cl-check-test cl-old (elt cl-seq cl-end))
472 (cl-set-elt cl-seq cl-end cl-new)
473 (setq cl-count (1- cl-count)))))
474 (while (and (< cl-start cl-end) (> cl-count 0))
475 (if (cl-check-test cl-old (aref cl-seq cl-start))
477 (aset cl-seq cl-start cl-new)
478 (setq cl-count (1- cl-count))))
479 (setq cl-start (1+ cl-start))))))
482 (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys)
483 "Substitute NEW for all items satisfying PREDICATE in SEQ.
484 This is a destructive function; it reuses the storage of SEQ whenever possible.
485 Keywords supported: :key :count :start :end :from-end"
486 (apply 'nsubstitute cl-new nil cl-list ':if cl-pred cl-keys))
488 (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
489 "Substitute NEW for all items not satisfying PREDICATE in SEQ.
490 This is a destructive function; it reuses the storage of SEQ whenever possible.
491 Keywords supported: :key :count :start :end :from-end"
492 (apply 'nsubstitute cl-new nil cl-list ':if-not cl-pred cl-keys))
494 (defun find (cl-item cl-seq &rest cl-keys)
495 "Find the first occurrence of ITEM in LIST.
496 Return the matching ITEM, or nil if not found.
497 Keywords supported: :test :test-not :key :start :end :from-end"
498 (let ((cl-pos (apply 'position cl-item cl-seq cl-keys)))
499 (and cl-pos (elt cl-seq cl-pos))))
501 (defun find-if (cl-pred cl-list &rest cl-keys)
502 "Find the first item satisfying PREDICATE in LIST.
503 Return the matching ITEM, or nil if not found.
504 Keywords supported: :key :start :end :from-end"
505 (apply 'find nil cl-list ':if cl-pred cl-keys))
507 (defun find-if-not (cl-pred cl-list &rest cl-keys)
508 "Find the first item not satisfying PREDICATE in LIST.
509 Return the matching ITEM, or nil if not found.
510 Keywords supported: :key :start :end :from-end"
511 (apply 'find nil cl-list ':if-not cl-pred cl-keys))
513 (defun position (cl-item cl-seq &rest cl-keys)
514 "Find the first occurrence of ITEM in LIST.
515 Return the index of the matching item, or nil if not found.
516 Keywords supported: :test :test-not :key :start :end :from-end"
517 (cl-parsing-keywords (:test :test-not :key :if :if-not
518 (:start 0) :end :from-end) ()
519 (cl-position cl-item cl-seq cl-start cl-end cl-from-end)))
521 (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end)
523 (let ((cl-p (nthcdr cl-start cl-seq)))
524 (or cl-end (setq cl-end 8000000))
526 (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end))
527 (if (cl-check-test cl-item (car cl-p))
528 (setq cl-res cl-start))
529 (setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
531 (or cl-end (setq cl-end (length cl-seq)))
534 (while (and (>= (setq cl-end (1- cl-end)) cl-start)
535 (not (cl-check-test cl-item (aref cl-seq cl-end)))))
536 (and (>= cl-end cl-start) cl-end))
537 (while (and (< cl-start cl-end)
538 (not (cl-check-test cl-item (aref cl-seq cl-start))))
539 (setq cl-start (1+ cl-start)))
540 (and (< cl-start cl-end) cl-start))))
542 (defun position-if (cl-pred cl-list &rest cl-keys)
543 "Find the first item satisfying PREDICATE in LIST.
544 Return the index of the matching item, or nil if not found.
545 Keywords supported: :key :start :end :from-end"
546 (apply 'position nil cl-list ':if cl-pred cl-keys))
548 (defun position-if-not (cl-pred cl-list &rest cl-keys)
549 "Find the first item not satisfying PREDICATE in LIST.
550 Return the index of the matching item, or nil if not found.
551 Keywords supported: :key :start :end :from-end"
552 (apply 'position nil cl-list ':if-not cl-pred cl-keys))
554 (defun count (cl-item cl-seq &rest cl-keys)
555 "Count the number of occurrences of ITEM in LIST.
556 Keywords supported: :test :test-not :key :start :end"
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) (cl-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)))
567 (defun count-if (cl-pred cl-list &rest cl-keys)
568 "Count the number of items satisfying PREDICATE in LIST.
569 Keywords supported: :key :start :end"
570 (apply 'count nil cl-list ':if cl-pred cl-keys))
572 (defun count-if-not (cl-pred cl-list &rest cl-keys)
573 "Count the number of items not satisfying PREDICATE in LIST.
574 Keywords supported: :key :start :end"
575 (apply 'count nil cl-list ':if-not cl-pred cl-keys))
577 (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys)
578 "Compare SEQ1 with SEQ2, return index of first mismatching element.
579 Return nil if the sequences match. If one sequence is a prefix of the
580 other, the return value indicates the end of the shorted sequence.
581 Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end"
582 (cl-parsing-keywords (:test :test-not :key :from-end
583 (:start1 0) :end1 (:start2 0) :end2) ()
584 (or cl-end1 (setq cl-end1 (length cl-seq1)))
585 (or cl-end2 (setq cl-end2 (length cl-seq2)))
588 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
589 (cl-check-match (elt cl-seq1 (1- cl-end1))
590 (elt cl-seq2 (1- cl-end2))))
591 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2)))
592 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
594 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1)))
595 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2))))
596 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
597 (cl-check-match (if cl-p1 (car cl-p1)
598 (aref cl-seq1 cl-start1))
599 (if cl-p2 (car cl-p2)
600 (aref cl-seq2 cl-start2))))
601 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)
602 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2)))
603 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
606 (defun search (cl-seq1 cl-seq2 &rest cl-keys)
607 "Search for SEQ1 as a subsequence of SEQ2.
608 Return the index of the leftmost element of the first match found;
609 return nil if there are no matches.
610 Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end"
611 (cl-parsing-keywords (:test :test-not :key :from-end
612 (:start1 0) :end1 (:start2 0) :end2) ()
613 (or cl-end1 (setq cl-end1 (length cl-seq1)))
614 (or cl-end2 (setq cl-end2 (length cl-seq2)))
615 (if (>= cl-start1 cl-end1)
616 (if cl-from-end cl-end2 cl-start2)
617 (let* ((cl-len (- cl-end1 cl-start1))
618 (cl-first (cl-check-key (elt cl-seq1 cl-start1)))
620 (setq cl-end2 (- cl-end2 (1- cl-len)))
621 (while (and (< cl-start2 cl-end2)
622 (setq cl-pos (cl-position cl-first cl-seq2
623 cl-start2 cl-end2 cl-from-end))
624 (apply 'mismatch cl-seq1 cl-seq2
625 ':start1 (1+ cl-start1) ':end1 cl-end1
626 ':start2 (1+ cl-pos) ':end2 (+ cl-pos cl-len)
627 ':from-end nil cl-keys))
628 (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos))))
629 (and (< cl-start2 cl-end2) cl-pos)))))
631 (defun sort* (cl-seq cl-pred &rest cl-keys)
632 "Sort the argument SEQUENCE according to PREDICATE.
633 This is a destructive function; it reuses the storage of SEQUENCE if possible.
634 Keywords supported: :key"
636 (replace cl-seq (apply 'sort* (append cl-seq nil) cl-pred cl-keys))
637 (cl-parsing-keywords (:key) ()
638 (if (memq cl-key '(nil identity))
639 (sort cl-seq cl-pred)
640 (sort cl-seq (function (lambda (cl-x cl-y)
641 (funcall cl-pred (funcall cl-key cl-x)
642 (funcall cl-key cl-y)))))))))
644 (defun stable-sort (cl-seq cl-pred &rest cl-keys)
645 "Sort the argument SEQUENCE stably according to PREDICATE.
646 This is a destructive function; it reuses the storage of SEQUENCE if possible.
647 Keywords supported: :key"
648 (apply 'sort* cl-seq cl-pred cl-keys))
650 (defun merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys)
651 "Destructively merge the two sequences to produce a new sequence.
652 TYPE is the sequence type to return, SEQ1 and SEQ2 are the two
653 argument sequences, and PRED is a `less-than' predicate on the elements.
654 Keywords supported: :key"
655 (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil)))
656 (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil)))
657 (cl-parsing-keywords (:key) ()
659 (while (and cl-seq1 cl-seq2)
660 (if (funcall cl-pred (cl-check-key (car cl-seq2))
661 (cl-check-key (car cl-seq1)))
662 (cl-push (cl-pop cl-seq2) cl-res)
663 (cl-push (cl-pop cl-seq1) cl-res)))
664 (coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type))))
666 ;;; See compiler macro in cl-macs.el
667 (defun member* (cl-item cl-list &rest cl-keys)
668 "Find the first occurrence of ITEM in LIST.
669 Return the sublist of LIST whose car is ITEM.
670 Keywords supported: :test :test-not :key"
672 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
673 (while (and cl-list (not (cl-check-test cl-item (car cl-list))))
674 (setq cl-list (cdr cl-list)))
676 (if (and (numberp cl-item) (not (integerp cl-item)))
677 (member cl-item cl-list)
678 (memq cl-item cl-list))))
680 (defun member-if (cl-pred cl-list &rest cl-keys)
681 "Find the first item satisfying PREDICATE in LIST.
682 Return the sublist of LIST whose car matches.
683 Keywords supported: :key"
684 (apply 'member* nil cl-list ':if cl-pred cl-keys))
686 (defun member-if-not (cl-pred cl-list &rest cl-keys)
687 "Find the first item not satisfying PREDICATE in LIST.
688 Return the sublist of LIST whose car matches.
689 Keywords supported: :key"
690 (apply 'member* nil cl-list ':if-not cl-pred cl-keys))
692 (defun cl-adjoin (cl-item cl-list &rest cl-keys)
693 (if (cl-parsing-keywords (:key) t
694 (apply 'member* (cl-check-key cl-item) cl-list cl-keys))
696 (cons cl-item cl-list)))
698 ;;; See compiler macro in cl-macs.el
699 (defun assoc* (cl-item cl-alist &rest cl-keys)
700 "Find the first item whose car matches ITEM in LIST.
701 Keywords supported: :test :test-not :key"
703 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
705 (or (not (consp (car cl-alist)))
706 (not (cl-check-test cl-item (car (car cl-alist))))))
707 (setq cl-alist (cdr cl-alist)))
708 (and cl-alist (car cl-alist)))
709 (if (and (numberp cl-item) (not (integerp cl-item)))
710 (assoc cl-item cl-alist)
711 (assq cl-item cl-alist))))
713 (defun assoc-if (cl-pred cl-list &rest cl-keys)
714 "Find the first item whose car satisfies PREDICATE in LIST.
715 Keywords supported: :key"
716 (apply 'assoc* nil cl-list ':if cl-pred cl-keys))
718 (defun assoc-if-not (cl-pred cl-list &rest cl-keys)
719 "Find the first item whose car does not satisfy PREDICATE in LIST.
720 Keywords supported: :key"
721 (apply 'assoc* nil cl-list ':if-not cl-pred cl-keys))
723 (defun rassoc* (cl-item cl-alist &rest cl-keys)
724 "Find the first item whose cdr matches ITEM in LIST.
725 Keywords supported: :test :test-not :key"
726 (if (or cl-keys (numberp cl-item))
727 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
729 (or (not (consp (car cl-alist)))
730 (not (cl-check-test cl-item (cdr (car cl-alist))))))
731 (setq cl-alist (cdr cl-alist)))
732 (and cl-alist (car cl-alist)))
733 (rassq cl-item cl-alist)))
735 (defun rassoc-if (cl-pred cl-list &rest cl-keys)
736 "Find the first item whose cdr satisfies PREDICATE in LIST.
737 Keywords supported: :key"
738 (apply 'rassoc* nil cl-list ':if cl-pred cl-keys))
740 (defun rassoc-if-not (cl-pred cl-list &rest cl-keys)
741 "Find the first item whose cdr does not satisfy PREDICATE in LIST.
742 Keywords supported: :key"
743 (apply 'rassoc* nil cl-list ':if-not cl-pred cl-keys))
745 (defun union (cl-list1 cl-list2 &rest cl-keys)
746 "Combine LIST1 and LIST2 using a set-union operation.
747 The result list contains all items that appear in either LIST1 or LIST2.
748 This is a non-destructive function; it makes a copy of the data if necessary
749 to avoid corrupting the original LIST1 and LIST2.
750 Keywords supported: :test :test-not :key"
751 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
752 ((equal cl-list1 cl-list2) cl-list1)
754 (or (>= (length cl-list1) (length cl-list2))
755 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
757 (if (or cl-keys (numberp (car cl-list2)))
758 (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys))
759 (or (memq (car cl-list2) cl-list1)
760 (cl-push (car cl-list2) cl-list1)))
764 (defun nunion (cl-list1 cl-list2 &rest cl-keys)
765 "Combine LIST1 and LIST2 using a set-union operation.
766 The result list contains all items that appear in either LIST1 or LIST2.
767 This is a destructive function; it reuses the storage of LIST1 and LIST2
769 Keywords supported: :test :test-not :key"
770 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
771 (t (apply 'union cl-list1 cl-list2 cl-keys))))
773 (defun intersection (cl-list1 cl-list2 &rest cl-keys)
774 "Combine LIST1 and LIST2 using a set-intersection operation.
775 The result list contains all items that appear in both LIST1 and LIST2.
776 This is a non-destructive function; it makes a copy of the data if necessary
777 to avoid corrupting the original LIST1 and LIST2.
778 Keywords supported: :test :test-not :key"
779 (and cl-list1 cl-list2
780 (if (equal cl-list1 cl-list2) cl-list1
781 (cl-parsing-keywords (:key) (:test :test-not)
783 (or (>= (length cl-list1) (length cl-list2))
784 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
786 (if (if (or cl-keys (numberp (car cl-list2)))
787 (apply 'member* (cl-check-key (car cl-list2))
789 (memq (car cl-list2) cl-list1))
790 (cl-push (car cl-list2) cl-res))
794 (defun nintersection (cl-list1 cl-list2 &rest cl-keys)
795 "Combine LIST1 and LIST2 using a set-intersection operation.
796 The result list contains all items that appear in both LIST1 and LIST2.
797 This is a destructive function; it reuses the storage of LIST1 and LIST2
799 Keywords supported: :test :test-not :key"
800 (and cl-list1 cl-list2 (apply 'intersection cl-list1 cl-list2 cl-keys)))
802 (defun set-difference (cl-list1 cl-list2 &rest cl-keys)
803 "Combine LIST1 and LIST2 using a set-difference operation.
804 The result list contains all items that appear in LIST1 but not LIST2.
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.
807 Keywords supported: :test :test-not :key"
808 (if (or (null cl-list1) (null cl-list2)) cl-list1
809 (cl-parsing-keywords (:key) (:test :test-not)
812 (or (if (or cl-keys (numberp (car cl-list1)))
813 (apply 'member* (cl-check-key (car cl-list1))
815 (memq (car cl-list1) cl-list2))
816 (cl-push (car cl-list1) cl-res))
820 (defun nset-difference (cl-list1 cl-list2 &rest cl-keys)
821 "Combine LIST1 and LIST2 using a set-difference operation.
822 The result list contains all items that appear in LIST1 but not LIST2.
823 This is a destructive function; it reuses the storage of LIST1 and LIST2
825 Keywords supported: :test :test-not :key"
826 (if (or (null cl-list1) (null cl-list2)) cl-list1
827 (apply 'set-difference cl-list1 cl-list2 cl-keys)))
829 (defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
830 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
831 The result list contains all items that appear in exactly one of LIST1, LIST2.
832 This is a non-destructive function; it makes a copy of the data if necessary
833 to avoid corrupting the original LIST1 and LIST2.
834 Keywords supported: :test :test-not :key"
835 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
836 ((equal cl-list1 cl-list2) nil)
837 (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys)
838 (apply 'set-difference cl-list2 cl-list1 cl-keys)))))
840 (defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
841 "Combine LIST1 and LIST2 using a set-exclusive-or operation.
842 The result list contains all items that appear in exactly one of LIST1, LIST2.
843 This is a destructive function; it reuses the storage of LIST1 and LIST2
845 Keywords supported: :test :test-not :key"
846 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
847 ((equal cl-list1 cl-list2) nil)
848 (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys)
849 (apply 'nset-difference cl-list2 cl-list1 cl-keys)))))
851 (defun subsetp (cl-list1 cl-list2 &rest cl-keys)
852 "True if LIST1 is a subset of LIST2.
853 I.e., if every element of LIST1 also appears in LIST2.
854 Keywords supported: :test :test-not :key"
855 (cond ((null cl-list1) t) ((null cl-list2) nil)
856 ((equal cl-list1 cl-list2) t)
857 (t (cl-parsing-keywords (:key) (:test :test-not)
859 (apply 'member* (cl-check-key (car cl-list1))
864 (defun subst-if (cl-new cl-pred cl-tree &rest cl-keys)
865 "Substitute NEW for elements matching PREDICATE in TREE (non-destructively).
866 Return a copy of TREE with all matching elements replaced by NEW.
867 Keywords supported: :key"
868 (apply 'sublis (list (cons nil cl-new)) cl-tree ':if cl-pred cl-keys))
870 (defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
871 "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively).
872 Return a copy of TREE with all non-matching elements replaced by NEW.
873 Keywords supported: :key"
874 (apply 'sublis (list (cons nil cl-new)) cl-tree ':if-not cl-pred cl-keys))
876 (defun nsubst (cl-new cl-old cl-tree &rest cl-keys)
877 "Substitute NEW for OLD everywhere in TREE (destructively).
878 Any element of TREE which is `eql' to OLD is changed to NEW (via a call
880 Keywords supported: :test :test-not :key"
881 (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys))
883 (defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys)
884 "Substitute NEW for elements matching PREDICATE in TREE (destructively).
885 Any element of TREE which matches is changed to NEW (via a call to `setcar').
886 Keywords supported: :key"
887 (apply 'nsublis (list (cons nil cl-new)) cl-tree ':if cl-pred cl-keys))
889 (defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
890 "Substitute NEW for elements not matching PREDICATE in TREE (destructively).
891 Any element of TREE which matches is changed to NEW (via a call to `setcar').
892 Keywords supported: :key"
893 (apply 'nsublis (list (cons nil cl-new)) cl-tree ':if-not cl-pred cl-keys))
895 (defun sublis (cl-alist cl-tree &rest cl-keys)
896 "Perform substitutions indicated by ALIST in TREE (non-destructively).
897 Return a copy of TREE with all matching elements replaced.
898 Keywords supported: :test :test-not :key"
899 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
900 (cl-sublis-rec cl-tree)))
903 (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if*
904 (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist))
905 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
906 (setq cl-p (cdr cl-p)))
907 (if cl-p (cdr (car cl-p))
909 (let ((cl-a (cl-sublis-rec (car cl-tree)))
910 (cl-d (cl-sublis-rec (cdr cl-tree))))
911 (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree)))
916 (defun nsublis (cl-alist cl-tree &rest cl-keys)
917 "Perform substitutions indicated by ALIST in TREE (destructively).
918 Any matching element of TREE is changed via a call to `setcar'.
919 Keywords supported: :test :test-not :key"
920 (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
921 (let ((cl-hold (list cl-tree)))
922 (cl-nsublis-rec cl-hold)
925 (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if*
926 (while (consp cl-tree)
927 (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist))
928 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
929 (setq cl-p (cdr cl-p)))
930 (if cl-p (setcar cl-tree (cdr (car cl-p)))
931 (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree))))
932 (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist)
933 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
934 (setq cl-p (cdr cl-p)))
936 (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil))
937 (setq cl-tree (cdr cl-tree))))))
939 (defun tree-equal (cl-x cl-y &rest cl-keys)
940 "Return t if trees X and Y have `eql' leaves.
941 Atoms are compared by `eql'; cons cells are compared recursively.
942 Keywords supported: :test :test-not :key"
943 (cl-parsing-keywords (:test :test-not :key) ()
944 (cl-tree-equal-rec cl-x cl-y)))
946 (defun cl-tree-equal-rec (cl-x cl-y)
947 (while (and (consp cl-x) (consp cl-y)
948 (cl-tree-equal-rec (car cl-x) (car cl-y)))
949 (setq cl-x (cdr cl-x) cl-y (cdr cl-y)))
950 (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y)))
953 (run-hooks 'cl-seq-load-hook)
955 ;;; cl-seq.el ends here