Mercurial > emacs
annotate lisp/gnus/gnus-range.el @ 26340:f1ae5b57d9db
Changed the mail addess of the maintainer.
author | Francesco Potortì <pot@gnu.org> |
---|---|
date | Wed, 03 Nov 1999 23:13:10 +0000 |
parents | 15fc6acbae7a |
children | 9968f55ad26e |
rev | line source |
---|---|
17493 | 1 ;;; gnus-range.el --- range and sequence functions for Gnus |
24357
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19969
diff
changeset
|
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc. |
17493 | 3 |
24357
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19969
diff
changeset
|
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
17493 | 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 | |
19493
8d840c4548c0
Require cl at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
17493
diff
changeset
|
28 (eval-when-compile (require 'cl)) |
8d840c4548c0
Require cl at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
17493
diff
changeset
|
29 |
24357
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19969
diff
changeset
|
30 (eval-when-compile (require 'cl)) |
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19969
diff
changeset
|
31 |
17493 | 32 ;;; List and range functions |
33 | |
34 (defun gnus-last-element (list) | |
35 "Return last element of LIST." | |
36 (while (cdr list) | |
37 (setq list (cdr list))) | |
38 (car list)) | |
39 | |
40 (defun gnus-copy-sequence (list) | |
41 "Do a complete, total copy of a list." | |
42 (let (out) | |
43 (while (consp list) | |
44 (if (consp (car list)) | |
45 (push (gnus-copy-sequence (pop list)) out) | |
46 (push (pop list) out))) | |
47 (if list | |
48 (nconc (nreverse out) list) | |
49 (nreverse out)))) | |
50 | |
51 (defun gnus-set-difference (list1 list2) | |
52 "Return a list of elements of LIST1 that do not appear in LIST2." | |
53 (let ((list1 (copy-sequence list1))) | |
54 (while list2 | |
55 (setq list1 (delq (car list2) list1)) | |
56 (setq list2 (cdr list2))) | |
57 list1)) | |
58 | |
59 (defun gnus-sorted-complement (list1 list2) | |
24357
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19969
diff
changeset
|
60 "Return a list of elements that are in LIST1 or LIST2 but not both. |
17493 | 61 Both lists have to be sorted over <." |
62 (let (out) | |
63 (if (or (null list1) (null list2)) | |
64 (or list1 list2) | |
65 (while (and list1 list2) | |
66 (cond ((= (car list1) (car list2)) | |
67 (setq list1 (cdr list1) | |
68 list2 (cdr list2))) | |
69 ((< (car list1) (car list2)) | |
70 (setq out (cons (car list1) out)) | |
71 (setq list1 (cdr list1))) | |
72 (t | |
73 (setq out (cons (car list2) out)) | |
74 (setq list2 (cdr list2))))) | |
75 (nconc (nreverse out) (or list1 list2))))) | |
76 | |
77 (defun gnus-intersection (list1 list2) | |
78 (let ((result nil)) | |
79 (while list2 | |
80 (when (memq (car list2) list1) | |
81 (setq result (cons (car list2) result))) | |
82 (setq list2 (cdr list2))) | |
83 result)) | |
84 | |
85 (defun gnus-sorted-intersection (list1 list2) | |
86 ;; LIST1 and LIST2 have to be sorted over <. | |
87 (let (out) | |
88 (while (and list1 list2) | |
89 (cond ((= (car list1) (car list2)) | |
90 (setq out (cons (car list1) out) | |
91 list1 (cdr list1) | |
92 list2 (cdr list2))) | |
93 ((< (car list1) (car list2)) | |
94 (setq list1 (cdr list1))) | |
95 (t | |
96 (setq list2 (cdr list2))))) | |
97 (nreverse out))) | |
98 | |
99 (defun gnus-set-sorted-intersection (list1 list2) | |
100 ;; LIST1 and LIST2 have to be sorted over <. | |
101 ;; This function modifies LIST1. | |
102 (let* ((top (cons nil list1)) | |
103 (prev top)) | |
104 (while (and list1 list2) | |
105 (cond ((= (car list1) (car list2)) | |
106 (setq prev list1 | |
107 list1 (cdr list1) | |
108 list2 (cdr list2))) | |
109 ((< (car list1) (car list2)) | |
110 (setcdr prev (cdr list1)) | |
111 (setq list1 (cdr list1))) | |
112 (t | |
113 (setq list2 (cdr list2))))) | |
114 (setcdr prev nil) | |
115 (cdr top))) | |
116 | |
117 (defun gnus-compress-sequence (numbers &optional always-list) | |
118 "Convert list of numbers to a list of ranges or a single range. | |
119 If ALWAYS-LIST is non-nil, this function will always release a list of | |
120 ranges." | |
121 (let* ((first (car numbers)) | |
122 (last (car numbers)) | |
123 result) | |
124 (if (null numbers) | |
125 nil | |
126 (if (not (listp (cdr numbers))) | |
127 numbers | |
128 (while numbers | |
129 (cond ((= last (car numbers)) nil) ;Omit duplicated number | |
130 ((= (1+ last) (car numbers)) ;Still in sequence | |
131 (setq last (car numbers))) | |
132 (t ;End of one sequence | |
133 (setq result | |
134 (cons (if (= first last) first | |
135 (cons first last)) | |
136 result)) | |
137 (setq first (car numbers)) | |
138 (setq last (car numbers)))) | |
139 (setq numbers (cdr numbers))) | |
140 (if (and (not always-list) (null result)) | |
141 (if (= first last) (list first) (cons first last)) | |
142 (nreverse (cons (if (= first last) first (cons first last)) | |
143 result))))))) | |
144 | |
145 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range) | |
146 (defun gnus-uncompress-range (ranges) | |
147 "Expand a list of ranges into a list of numbers. | |
148 RANGES is either a single range on the form `(num . num)' or a list of | |
149 these ranges." | |
150 (let (first last result) | |
151 (cond | |
152 ((null ranges) | |
153 nil) | |
154 ((not (listp (cdr ranges))) | |
155 (setq first (car ranges)) | |
156 (setq last (cdr ranges)) | |
157 (while (<= first last) | |
158 (setq result (cons first result)) | |
159 (setq first (1+ first))) | |
160 (nreverse result)) | |
161 (t | |
162 (while ranges | |
163 (if (atom (car ranges)) | |
164 (when (numberp (car ranges)) | |
165 (setq result (cons (car ranges) result))) | |
166 (setq first (caar ranges)) | |
167 (setq last (cdar ranges)) | |
168 (while (<= first last) | |
169 (setq result (cons first result)) | |
170 (setq first (1+ first)))) | |
171 (setq ranges (cdr ranges))) | |
172 (nreverse result))))) | |
173 | |
174 (defun gnus-add-to-range (ranges list) | |
175 "Return a list of ranges that has all articles from both RANGES and LIST. | |
176 Note: LIST has to be sorted over `<'." | |
177 (if (not ranges) | |
178 (gnus-compress-sequence list t) | |
179 (setq list (copy-sequence list)) | |
180 (unless (listp (cdr ranges)) | |
181 (setq ranges (list ranges))) | |
182 (let ((out ranges) | |
183 ilist lowest highest temp) | |
184 (while (and ranges list) | |
185 (setq ilist list) | |
186 (setq lowest (or (and (atom (car ranges)) (car ranges)) | |
187 (caar ranges))) | |
188 (while (and list (cdr list) (< (cadr list) lowest)) | |
189 (setq list (cdr list))) | |
190 (when (< (car ilist) lowest) | |
191 (setq temp list) | |
192 (setq list (cdr list)) | |
193 (setcdr temp nil) | |
194 (setq out (nconc (gnus-compress-sequence ilist t) out))) | |
195 (setq highest (or (and (atom (car ranges)) (car ranges)) | |
196 (cdar ranges))) | |
197 (while (and list (<= (car list) highest)) | |
198 (setq list (cdr list))) | |
199 (setq ranges (cdr ranges))) | |
200 (when list | |
201 (setq out (nconc (gnus-compress-sequence list t) out))) | |
202 (setq out (sort out (lambda (r1 r2) | |
203 (< (or (and (atom r1) r1) (car r1)) | |
204 (or (and (atom r2) r2) (car r2)))))) | |
205 (setq ranges out) | |
206 (while ranges | |
207 (if (atom (car ranges)) | |
208 (when (cdr ranges) | |
209 (if (atom (cadr ranges)) | |
210 (when (= (1+ (car ranges)) (cadr ranges)) | |
211 (setcar ranges (cons (car ranges) | |
212 (cadr ranges))) | |
213 (setcdr ranges (cddr ranges))) | |
19969
5f1ab3dd344d
*** empty log message ***
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19493
diff
changeset
|
214 (when (= (1+ (car ranges)) (caadr ranges)) |
17493 | 215 (setcar (cadr ranges) (car ranges)) |
216 (setcar ranges (cadr ranges)) | |
217 (setcdr ranges (cddr ranges))))) | |
218 (when (cdr ranges) | |
219 (if (atom (cadr ranges)) | |
220 (when (= (1+ (cdar ranges)) (cadr ranges)) | |
221 (setcdr (car ranges) (cadr ranges)) | |
222 (setcdr ranges (cddr ranges))) | |
19969
5f1ab3dd344d
*** empty log message ***
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19493
diff
changeset
|
223 (when (= (1+ (cdar ranges)) (caadr ranges)) |
5f1ab3dd344d
*** empty log message ***
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19493
diff
changeset
|
224 (setcdr (car ranges) (cdadr ranges)) |
17493 | 225 (setcdr ranges (cddr ranges)))))) |
226 (setq ranges (cdr ranges))) | |
227 out))) | |
228 | |
229 (defun gnus-remove-from-range (ranges list) | |
230 "Return a list of ranges that has all articles from LIST removed from RANGES. | |
231 Note: LIST has to be sorted over `<'." | |
232 ;; !!! This function shouldn't look like this, but I've got a headache. | |
233 (gnus-compress-sequence | |
234 (gnus-sorted-complement | |
235 (gnus-uncompress-range ranges) list))) | |
236 | |
237 (defun gnus-member-of-range (number ranges) | |
238 (if (not (listp (cdr ranges))) | |
239 (and (>= number (car ranges)) | |
240 (<= number (cdr ranges))) | |
241 (let ((not-stop t)) | |
242 (while (and ranges | |
243 (if (numberp (car ranges)) | |
244 (>= number (car ranges)) | |
245 (>= number (caar ranges))) | |
246 not-stop) | |
247 (when (if (numberp (car ranges)) | |
248 (= number (car ranges)) | |
249 (and (>= number (caar ranges)) | |
250 (<= number (cdar ranges)))) | |
251 (setq not-stop nil)) | |
252 (setq ranges (cdr ranges))) | |
253 (not not-stop)))) | |
254 | |
255 (defun gnus-range-length (range) | |
256 "Return the length RANGE would have if uncompressed." | |
257 (length (gnus-uncompress-range range))) | |
258 | |
259 (defun gnus-sublist-p (list sublist) | |
260 "Test whether all elements in SUBLIST are members of LIST." | |
261 (let ((sublistp t)) | |
262 (while sublist | |
263 (unless (memq (pop sublist) list) | |
264 (setq sublistp nil | |
265 sublist nil))) | |
266 sublistp)) | |
267 | |
268 (defun gnus-range-add (range1 range2) | |
269 "Add RANGE2 to RANGE1 destructively." | |
270 (cond | |
271 ;; If either are nil, then the job is quite easy. | |
272 ((or (null range1) (null range2)) | |
273 (or range1 range2)) | |
274 (t | |
275 ;; I don't like thinking. | |
276 (gnus-compress-sequence | |
277 (sort | |
278 (nconc | |
279 (gnus-uncompress-range range1) | |
280 (gnus-uncompress-range range2)) | |
281 '<))))) | |
282 | |
283 (provide 'gnus-range) | |
284 | |
285 ;;; gnus-range.el ends here |