2002-01-01 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-range.el
1 ;;; gnus-range.el --- range and sequence functions for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 ;;; List and range functions
33
34 (defsubst gnus-range-normalize (range)
35   "Normalize RANGE.
36 If RANGE is a single range, return (RANGE). Otherwise, return RANGE."
37   (if (listp (cdr range)) (list range) range))
38
39 (defun gnus-last-element (list)
40   "Return last element of LIST."
41   (while (cdr list)
42     (setq list (cdr list)))
43   (car list))
44
45 (defun gnus-copy-sequence (list)
46   "Do a complete, total copy of a list."
47   (let (out)
48     (while (consp list)
49       (if (consp (car list))
50           (push (gnus-copy-sequence (pop list)) out)
51         (push (pop list) out)))
52     (if list
53         (nconc (nreverse out) list)
54       (nreverse out))))
55
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)))
59     (while list2
60       (setq list1 (delq (car list2) list1))
61       (setq list2 (cdr list2)))
62     list1))
63
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 <."
67   (let (out)
68     (if (or (null list1) (null list2))
69         (or list1 list2)
70       (while (and list1 list2)
71         (cond ((= (car list1) (car list2))
72                (setq list1 (cdr list1)
73                      list2 (cdr list2)))
74               ((< (car list1) (car list2))
75                (setq out (cons (car list1) out))
76                (setq list1 (cdr list1)))
77               (t
78                (setq out (cons (car list2) out))
79                (setq list2 (cdr list2)))))
80       (nconc (nreverse out) (or list1 list2)))))
81
82 (defun gnus-intersection (list1 list2)
83   (let ((result nil))
84     (while list2
85       (when (memq (car list2) list1)
86         (setq result (cons (car list2) result)))
87       (setq list2 (cdr list2)))
88     result))
89
90 (defun gnus-sorted-intersection (list1 list2)
91   ;; LIST1 and LIST2 have to be sorted over <.
92   (let (out)
93     (while (and list1 list2)
94       (cond ((= (car list1) (car list2))
95              (setq out (cons (car list1) out)
96                    list1 (cdr list1)
97                    list2 (cdr list2)))
98             ((< (car list1) (car list2))
99              (setq list1 (cdr list1)))
100             (t
101              (setq list2 (cdr list2)))))
102     (nreverse out)))
103
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))
108          (prev top))
109     (while (and list1 list2)
110       (cond ((= (car list1) (car list2))
111              (setq prev list1
112                    list1 (cdr list1)
113                    list2 (cdr list2)))
114             ((< (car list1) (car list2))
115              (setcdr prev (cdr list1))
116              (setq list1 (cdr list1)))
117             (t
118              (setq list2 (cdr list2)))))
119     (setcdr prev nil)
120     (cdr top)))
121
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
125 ranges."
126   (let* ((first (car numbers))
127          (last (car numbers))
128          result)
129     (if (null numbers)
130         nil
131       (if (not (listp (cdr numbers)))
132           numbers
133         (while 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
138                  (setq result
139                        (cons (if (= first last) first
140                                (cons first last))
141                              result))
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))
148                           result)))))))
149
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
154 these ranges."
155   (let (first last result)
156     (cond
157      ((null ranges)
158       nil)
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)))
165       (nreverse result))
166      (t
167       (while ranges
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)))))
178
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 `<'."
182   (if (not ranges)
183       (gnus-compress-sequence list t)
184     (setq list (copy-sequence list))
185     (unless (listp (cdr ranges))
186       (setq ranges (list ranges)))
187     (let ((out ranges)
188           ilist lowest highest temp)
189       (while (and ranges list)
190         (setq ilist list)
191         (setq lowest (or (and (atom (car ranges)) (car ranges))
192                          (caar ranges)))
193         (while (and list (cdr list) (< (cadr list) lowest))
194           (setq list (cdr list)))
195         (when (< (car ilist) lowest)
196           (setq temp list)
197           (setq list (cdr list))
198           (setcdr temp nil)
199           (setq out (nconc (gnus-compress-sequence ilist t) out)))
200         (setq highest (or (and (atom (car ranges)) (car ranges))
201                           (cdar ranges)))
202         (while (and list (<= (car list) highest))
203           (setq list (cdr list)))
204         (setq ranges (cdr ranges)))
205       (when list
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))))))
210       (setq ranges out)
211       (while ranges
212         (if (atom (car ranges))
213             (when (cdr ranges)
214               (if (atom (cadr ranges))
215                   (when (= (1+ (car ranges)) (cadr ranges))
216                     (setcar ranges (cons (car ranges)
217                                          (cadr 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)))))
223           (when (cdr ranges)
224             (if (atom (cadr ranges))
225                 (when (= (1+ (cdar ranges)) (cadr ranges))