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