1 ;;; gnus-range.el --- range and sequence functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
32 ;;; List and range functions
34 (defsubst gnus-range-normalize (range)
36 If RANGE is a single range, return (RANGE). Otherwise, return RANGE."
37 (if (listp (cdr range)) (list range) range))
39 (defun gnus-last-element (list)
40 "Return last element of LIST."
42 (setq list (cdr list)))
45 (defun gnus-copy-sequence (list)
46 "Do a complete, total copy of a list."
49 (if (consp (car list))
50 (push (gnus-copy-sequence (pop list)) out)
51 (push (pop list) out)))
53 (nconc (nreverse out) list)
56 (defun gnus-set-difference (list1 list2)
57 "Return a list of elements of LIST1 that do not appear in LIST2."
58 (let ((list1 (copy-sequence list1)))
60 (setq list1 (delq (car list2) list1))
61 (setq list2 (cdr list2)))
64 (defun gnus-sorted-complement (list1 list2)
65 "Return a list of elements that are in LIST1 or LIST2 but not both.
66 Both lists have to be sorted over <."
68 (if (or (null list1) (null list2))
70 (while (and list1 list2)
71 (cond ((= (car list1) (car list2))
72 (setq list1 (cdr list1)
74 ((< (car list1) (car list2))
75 (setq out (cons (car list1) out))
76 (setq list1 (cdr list1)))
78 (setq out (cons (car list2) out))
79 (setq list2 (cdr list2)))))
80 (nconc (nreverse out) (or list1 list2)))))
82 (defun gnus-intersection (list1 list2)
85 (when (memq (car list2) list1)
86 (setq result (cons (car list2) result)))
87 (setq list2 (cdr list2)))
90 (defun gnus-sorted-intersection (list1 list2)
91 ;; LIST1 and LIST2 have to be sorted over <.
93 (while (and list1 list2)
94 (cond ((= (car list1) (car list2))
95 (setq out (cons (car list1) out)
98 ((< (car list1) (car list2))
99 (setq list1 (cdr list1)))
101 (setq list2 (cdr list2)))))
104 (defun gnus-set-sorted-intersection (list1 list2)
105 ;; LIST1 and LIST2 have to be sorted over <.
106 ;; This function modifies LIST1.
107 (let* ((top (cons nil list1))
109 (while (and list1 list2)
110 (cond ((= (car list1) (car list2))
114 ((< (car list1) (car list2))
115 (setcdr prev (cdr list1))
116 (setq list1 (cdr list1)))
118 (setq list2 (cdr list2)))))
122 (defun gnus-compress-sequence (numbers &optional always-list)
123 "Convert list of numbers to a list of ranges or a single range.
124 If ALWAYS-LIST is non-nil, this function will always release a list of
126 (let* ((first (car numbers))
131 (if (not (listp (cdr numbers)))
134 (cond ((= last (car numbers)) nil) ;Omit duplicated number
135 ((= (1+ last) (car numbers)) ;Still in sequence
136 (setq last (car numbers)))
137 (t ;End of one sequence
139 (cons (if (= first last) first
142 (setq first (car numbers))
143 (setq last (car numbers))))
144 (setq numbers (cdr numbers)))
145 (if (and (not always-list) (null result))
146 (if (= first last) (list first) (cons first last))
147 (nreverse (cons (if (= first last) first (cons first last))
150 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
151 (defun gnus-uncompress-range (ranges)
152 "Expand a list of ranges into a list of numbers.
153 RANGES is either a single range on the form `(num . num)' or a list of
155 (let (first last result)
159 ((not (listp (cdr ranges)))
160 (setq first (car ranges))
161 (setq last (cdr ranges))
162 (while (<= first last)
163 (setq result (cons first result))
164 (setq first (1+ first)))
168 (if (atom (car ranges))
169 (when (numberp (car ranges))
170 (setq result (cons (car ranges) result)))
171 (setq first (caar ranges))
172 (setq last (cdar ranges))
173 (while (<= first last)
174 (setq result (cons first result))
175 (setq first (1+ first))))
176 (setq ranges (cdr ranges)))
177 (nreverse result)))))
179 (defun gnus-add-to-range (ranges list)
180 "Return a list of ranges that has all articles from both RANGES and LIST.
181 Note: LIST has to be sorted over `<'."
183 (gnus-compress-sequence list t)
184 (setq list (copy-sequence list))
185 (unless (listp (cdr ranges))
186 (setq ranges (list ranges)))
188 ilist lowest highest temp)
189 (while (and ranges list)
191 (setq lowest (or (and (atom (car ranges)) (car ranges))
193 (while (and list (cdr list) (< (cadr list) lowest))
194 (setq list (cdr list)))
195 (when (< (car ilist) lowest)
197 (setq list (cdr list))
199 (setq out (nconc (gnus-compress-sequence ilist t) out)))
200 (setq highest (or (and (atom (car ranges)) (car ranges))
202 (while (and list (<= (car list) highest))
203 (setq list (cdr list)))
204 (setq ranges (cdr ranges)))
206 (setq out (nconc (gnus-compress-sequence list t) out)))
207 (setq out (sort out (lambda (r1 r2)
208 (< (or (and (atom r1) r1) (car r1))
209 (or (and (atom r2) r2) (car r2))))))
212 (if (atom (car ranges))
214 (if (atom (cadr ranges))
215 (when (= (1+ (car ranges)) (cadr ranges))
216 (setcar ranges (cons (car ranges)
218 (setcdr ranges (cddr ranges)))
219 (when (= (1+ (car ranges)) (caadr ranges))
220 (setcar (cadr ranges) (car ranges))
221 (setcar ranges (cadr ranges))
222 (setcdr ranges (cddr ranges)))))
224 (if (atom (cadr ranges))
225 (when (= (1+ (cdar ranges)) (cadr ranges))