17493
|
1 ;;; gnus-range.el --- range and sequence functions for Gnus
|
|
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
|
|
5 ;; Keywords: news
|
|
6
|
|
7 ;; This file is part of GNU Emacs.
|
|
8
|
|
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
10 ;; it under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 ;; GNU General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 ;; Boston, MA 02111-1307, USA.
|
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;;; Code:
|
|
27
|
|
28 ;;; List and range functions
|
|
29
|
|
30 (defun gnus-last-element (list)
|
|
31 "Return last element of LIST."
|
|
32 (while (cdr list)
|
|
33 (setq list (cdr list)))
|
|
34 (car list))
|
|
35
|
|
36 (defun gnus-copy-sequence (list)
|
|
37 "Do a complete, total copy of a list."
|
|
38 (let (out)
|
|
39 (while (consp list)
|
|
40 (if (consp (car list))
|
|
41 (push (gnus-copy-sequence (pop list)) out)
|
|
42 (push (pop list) out)))
|
|
43 (if list
|
|
44 (nconc (nreverse out) list)
|
|
45 (nreverse out))))
|
|
46
|
|
47 (defun gnus-set-difference (list1 list2)
|
|
48 "Return a list of elements of LIST1 that do not appear in LIST2."
|
|
49 (let ((list1 (copy-sequence list1)))
|
|
50 (while list2
|
|
51 (setq list1 (delq (car list2) list1))
|
|
52 (setq list2 (cdr list2)))
|
|
53 list1))
|
|
54
|
|
55 (defun gnus-sorted-complement (list1 list2)
|
|
56 "Return a list of elements of LIST1 that do not appear in LIST2.
|
|
57 Both lists have to be sorted over <."
|
|
58 (let (out)
|
|
59 (if (or (null list1) (null list2))
|
|
60 (or list1 list2)
|
|
61 (while (and list1 list2)
|
|
62 (cond ((= (car list1) (car list2))
|
|
63 (setq list1 (cdr list1)
|
|
64 list2 (cdr list2)))
|
|
65 ((< (car list1) (car list2))
|
|
66 (setq out (cons (car list1) out))
|
|
67 (setq list1 (cdr list1)))
|
|
68 (t
|
|
69 (setq out (cons (car list2) out))
|
|
70 (setq list2 (cdr list2)))))
|
|
71 (nconc (nreverse out) (or list1 list2)))))
|
|
72
|
|
73 (defun gnus-intersection (list1 list2)
|
|
74 (let ((result nil))
|
|
75 (while list2
|
|
76 (when (memq (car list2) list1)
|
|
77 (setq result (cons (car list2) result)))
|
|
78 (setq list2 (cdr list2)))
|
|
79 result))
|
|
80
|
|
81 (defun gnus-sorted-intersection (list1 list2)
|
|
82 ;; LIST1 and LIST2 have to be sorted over <.
|
|
83 (let (out)
|
|
84 (while (and list1 list2)
|
|
85 (cond ((= (car list1) (car list2))
|
|
86 (setq out (cons (car list1) out)
|
|
87 list1 (cdr list1)
|
|
88 list2 (cdr list2)))
|
|
89 ((< (car list1) (car list2))
|
|
90 (setq list1 (cdr list1)))
|
|
91 (t
|
|
92 (setq list2 (cdr list2)))))
|
|
93 (nreverse out)))
|
|
94
|
|
95 (defun gnus-set-sorted-intersection (list1 list2)
|
|
96 ;; LIST1 and LIST2 have to be sorted over <.
|
|
97 ;; This function modifies LIST1.
|
|
98 (let* ((top (cons nil list1))
|
|
99 (prev top))
|
|
100 (while (and list1 list2)
|
|
101 (cond ((= (car list1) (car list2))
|
|
102 (setq prev list1
|
|
103 list1 (cdr list1)
|
|
104 list2 (cdr list2)))
|
|
105 ((< (car list1) (car list2))
|
|
106 (setcdr prev (cdr list1))
|
|
107 (setq list1 (cdr list1)))
|
|
108 (t
|
|
109 (setq list2 (cdr list2)))))
|
|
110 (setcdr prev nil)
|
|
111 (cdr top)))
|
|
112
|
|
113 (defun gnus-compress-sequence (numbers &optional always-list)
|
|
114 "Convert list of numbers to a list of ranges or a single range.
|
|
115 If ALWAYS-LIST is non-nil, this function will always release a list of
|
|
116 ranges."
|
|
117 (let* ((first (car numbers))
|
|
118 (last (car numbers))
|
|
119 result)
|
|
120 (if (null numbers)
|
|
121 nil
|
|
122 (if (not (listp (cdr numbers)))
|
|
123 numbers
|
|
124 (while numbers
|
|
125 (cond ((= last (car numbers)) nil) ;Omit duplicated number
|
|
126 ((= (1+ last) (car numbers)) ;Still in sequence
|
|
127 (setq last (car numbers)))
|
|
128 (t ;End of one sequence
|
|
129 (setq result
|
|
130 (cons (if (= first last) first
|
|
131 (cons first last))
|
|
132 result))
|
|
133 (setq first (car numbers))
|
|
134 (setq last (car numbers))))
|
|
135 (setq numbers (cdr numbers)))
|
|
136 (if (and (not always-list) (null result))
|
|
137 (if (= first last) (list first) (cons first last))
|
|
138 (nreverse (cons (if (= first last) first (cons first last))
|
|
139 result)))))))
|
|
140
|
|
141 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
|
|
142 (defun gnus-uncompress-range (ranges)
|
|
143 "Expand a list of ranges into a list of numbers.
|
|
144 RANGES is either a single range on the form `(num . num)' or a list of
|
|
145 these ranges."
|
|
146 (let (first last result)
|
|
147 (cond
|
|
148 ((null ranges)
|
|
149 nil)
|
|
150 ((not (listp (cdr ranges)))
|
|
151 (setq first (car ranges))
|
|
152 (setq last (cdr ranges))
|
|
153 (while (<= first last)
|
|
154 (setq result (cons first result))
|
|
155 (setq first (1+ first)))
|
|
156 (nreverse result))
|
|
157 (t
|
|
158 (while ranges
|
|
159 (if (atom (car ranges))
|
|
160 (when (numberp (car ranges))
|
|
161 (setq result (cons (car ranges) result)))
|
|
162 (setq first (caar ranges))
|
|
163 (setq last (cdar ranges))
|
|
164 (while (<= first last)
|
|
165 (setq result (cons first result))
|
|
166 (setq first (1+ first))))
|
|
167 (setq ranges (cdr ranges)))
|
|
168 (nreverse result)))))
|
|
169
|
|
170 (defun gnus-add-to-range (ranges list)
|
|
171 "Return a list of ranges that has all articles from both RANGES and LIST.
|
|
172 Note: LIST has to be sorted over `<'."
|
|
173 (if (not ranges)
|
|
174 (gnus-compress-sequence list t)
|
|
175 (setq list (copy-sequence list))
|
|
176 (unless (listp (cdr ranges))
|
|
177 (setq ranges (list ranges)))
|
|
178 (let ((out ranges)
|
|
179 ilist lowest highest temp)
|
|
180 (while (and ranges list)
|
|
181 (setq ilist list)
|
|
182 (setq lowest (or (and (atom (car ranges)) (car ranges))
|
|
183 (caar ranges)))
|
|
184 (while (and list (cdr list) (< (cadr list) lowest))
|
|
185 (setq list (cdr list)))
|
|
186 (when (< (car ilist) lowest)
|
|
187 (setq temp list)
|
|
188 (setq list (cdr list))
|
|
189 (setcdr temp nil)
|
|
190 (setq out (nconc (gnus-compress-sequence ilist t) out)))
|
|
191 (setq highest (or (and (atom (car ranges)) (car ranges))
|
|
192 (cdar ranges)))
|
|
193 (while (and list (<= (car list) highest))
|
|
194 (setq list (cdr list)))
|
|
195 (setq ranges (cdr ranges)))
|
|
196 (when list
|
|
197 (setq out (nconc (gnus-compress-sequence list t) out)))
|
|
198 (setq out (sort out (lambda (r1 r2)
|
|
199 (< (or (and (atom r1) r1) (car r1))
|
|
200 (or (and (atom r2) r2) (car r2))))))
|
|
201 (setq ranges out)
|
|
202 (while ranges
|
|
203 (if (atom (car ranges))
|
|
204 (when (cdr ranges)
|
|
205 (if (atom (cadr ranges))
|
|
206 (when (= (1+ (car ranges)) (cadr ranges))
|
|
207 (setcar ranges (cons (car ranges)
|
|
208 (cadr ranges)))
|
|
209 (setcdr ranges (cddr ranges)))
|
|
210 (when (= (1+ (car ranges)) (caadr ranges))
|
|
211 (setcar (cadr ranges) (car ranges))
|
|
212 (setcar ranges (cadr ranges))
|
|
213 (setcdr ranges (cddr ranges)))))
|
|
214 (when (cdr ranges)
|
|
215 (if (atom (cadr ranges))
|
|
216 (when (= (1+ (cdar ranges)) (cadr ranges))
|
|
217 (setcdr (car ranges) (cadr ranges))
|
|
218 (setcdr ranges (cddr ranges)))
|
|
219 (when (= (1+ (cdar ranges)) (caadr ranges))
|
|
220 (setcdr (car ranges) (cdadr ranges))
|
|
221 (setcdr ranges (cddr ranges))))))
|
|
222 (setq ranges (cdr ranges)))
|
|
223 out)))
|
|
224
|
|
225 (defun gnus-remove-from-range (ranges list)
|
|
226 "Return a list of ranges that has all articles from LIST removed from RANGES.
|
|
227 Note: LIST has to be sorted over `<'."
|
|
228 ;; !!! This function shouldn't look like this, but I've got a headache.
|
|
229 (gnus-compress-sequence
|
|
230 (gnus-sorted-complement
|
|
231 (gnus-uncompress-range ranges) list)))
|
|
232
|
|
233 (defun gnus-member-of-range (number ranges)
|
|
234 (if (not (listp (cdr ranges)))
|
|
235 (and (>= number (car ranges))
|
|
236 (<= number (cdr ranges)))
|
|
237 (let ((not-stop t))
|
|
238 (while (and ranges
|
|
239 (if (numberp (car ranges))
|
|
240 (>= number (car ranges))
|
|
241 (>= number (caar ranges)))
|
|
242 not-stop)
|
|
243 (when (if (numberp (car ranges))
|
|
244 (= number (car ranges))
|
|
245 (and (>= number (caar ranges))
|
|
246 (<= number (cdar ranges))))
|
|
247 (setq not-stop nil))
|
|
248 (setq ranges (cdr ranges)))
|
|
249 (not not-stop))))
|
|
250
|
|
251 (defun gnus-range-length (range)
|
|
252 "Return the length RANGE would have if uncompressed."
|
|
253 (length (gnus-uncompress-range range)))
|
|
254
|
|
255 (defun gnus-sublist-p (list sublist)
|
|
256 "Test whether all elements in SUBLIST are members of LIST."
|
|
257 (let ((sublistp t))
|
|
258 (while sublist
|
|
259 (unless (memq (pop sublist) list)
|
|
260 (setq sublistp nil
|
|
261 sublist nil)))
|
|
262 sublistp))
|
|
263
|
|
264 (defun gnus-range-add (range1 range2)
|
|
265 "Add RANGE2 to RANGE1 destructively."
|
|
266 (cond
|
|
267 ;; If either are nil, then the job is quite easy.
|
|
268 ((or (null range1) (null range2))
|
|
269 (or range1 range2))
|
|
270 (t
|
|
271 ;; I don't like thinking.
|
|
272 (gnus-compress-sequence
|
|
273 (sort
|
|
274 (nconc
|
|
275 (gnus-uncompress-range range1)
|
|
276 (gnus-uncompress-range range2))
|
|
277 '<)))))
|
|
278
|
|
279 (provide 'gnus-range)
|
|
280
|
|
281 ;;; gnus-range.el ends here
|