(woman-mode-line-format): Delete constant.
[bpt/emacs.git] / lisp / gnus / nnheader.el
1 ;;; nnheader.el --- header access macros for Gnus and its backends
2
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1993, 1994, 1995, 1996,
4 ;; 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Keywords: news
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (eval-when-compile (require 'cl))
33
34 ;; Requiring `gnus-util' at compile time creates a circular
35 ;; dependency between nnheader.el and gnus-util.el.
36 ;;(eval-when-compile (require 'gnus-util))
37
38 (require 'mail-utils)
39 (require 'mm-util)
40 (require 'gnus-util)
41 (eval-and-compile
42 (autoload 'gnus-sorted-intersection "gnus-range")
43 (autoload 'gnus-intersection "gnus-range")
44 (autoload 'gnus-sorted-complement "gnus-range")
45 (autoload 'gnus-sorted-difference "gnus-range"))
46
47 (defcustom gnus-verbose-backends 7
48 "Integer that says how verbose the Gnus backends should be.
49 The higher the number, the more messages the Gnus backends will flash
50 to say what it's doing. At zero, the Gnus backends will be totally
51 mute; at five, they will display most important messages; and at ten,
52 they will keep on jabbering all the time."
53 :group 'gnus-start
54 :type 'integer)
55
56 (defcustom gnus-nov-is-evil nil
57 "If non-nil, Gnus backends will never output headers in the NOV format."
58 :group 'gnus-server
59 :type 'boolean)
60
61 (defvar nnheader-max-head-length 8192
62 "*Max length of the head of articles.
63
64 Value is an integer, nil, or t. nil means read in chunks of a file
65 indefinitely until a complete head is found\; t means always read the
66 entire file immediately, disregarding `nnheader-head-chop-length'.
67
68 Integer values will in effect be rounded up to the nearest multiple of
69 `nnheader-head-chop-length'.")
70
71 (defvar nnheader-head-chop-length 2048
72 "*Length of each read operation when trying to fetch HEAD headers.")
73
74 (defvar nnheader-read-timeout
75 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
76 (symbol-name system-type))
77 ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
78 ;;
79 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
80 ;;
81 ;; There should probably be a runtime test to determine the timing
82 ;; resolution, or a primitive to report it. I don't know off-hand
83 ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
84 ;; could round up non-zero timeouts to a minimum of 1.0?
85 1.0
86 0.1)
87 "How long nntp should wait between checking for the end of output.
88 Shorter values mean quicker response, but are more CPU intensive.")
89
90 (defvar nnheader-file-name-translation-alist
91 (let ((case-fold-search t))
92 (cond
93 ((string-match "windows-nt\\|os/2\\|emx\\|cygwin"
94 (symbol-name system-type))
95 (append (mapcar (lambda (c) (cons c ?_))
96 '(?: ?* ?\" ?< ?> ??))
97 (if (string-match "windows-nt\\|cygwin"
98 (symbol-name system-type))
99 nil
100 '((?+ . ?-)))))
101 (t nil)))
102 "*Alist that says how to translate characters in file names.
103 For instance, if \":\" is invalid as a file character in file names
104 on your system, you could say something like:
105
106 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
107
108 (defvar nnheader-directory-separator-character
109 (string-to-char (substring (file-name-as-directory ".") -1))
110 "*A character used to a directory separator.")
111
112 (eval-and-compile
113 (autoload 'nnmail-message-id "nnmail")
114 (autoload 'mail-position-on-field "sendmail")
115 (autoload 'message-remove-header "message")
116 (autoload 'gnus-point-at-eol "gnus-util")
117 (autoload 'gnus-buffer-live-p "gnus-util"))
118
119 ;;; Header access macros.
120
121 ;; These macros may look very much like the ones in GNUS 4.1. They
122 ;; are, in a way, but you should note that the indices they use have
123 ;; been changed from the internal GNUS format to the NOV format. The
124 ;; makes it possible to read headers from XOVER much faster.
125 ;;
126 ;; The format of a header is now:
127 ;; [number subject from date id references chars lines xref extra]
128 ;;
129 ;; (That next-to-last entry is defined as "misc" in the NOV format,
130 ;; but Gnus uses it for xrefs.)
131
132 (defmacro mail-header-number (header)
133 "Return article number in HEADER."
134 `(aref ,header 0))
135
136 (defmacro mail-header-set-number (header number)
137 "Set article number of HEADER to NUMBER."
138 `(aset ,header 0 ,number))
139
140 (defmacro mail-header-subject (header)
141 "Return subject string in HEADER."
142 `(aref ,header 1))
143
144 (defmacro mail-header-set-subject (header subject)
145 "Set article subject of HEADER to SUBJECT."
146 `(aset ,header 1 ,subject))
147
148 (defmacro mail-header-from (header)
149 "Return author string in HEADER."
150 `(aref ,header 2))
151
152 (defmacro mail-header-set-from (header from)
153 "Set article author of HEADER to FROM."
154 `(aset ,header 2 ,from))
155
156 (defmacro mail-header-date (header)
157 "Return date in HEADER."
158 `(aref ,header 3))
159
160 (defmacro mail-header-set-date (header date)
161 "Set article date of HEADER to DATE."
162 `(aset ,header 3 ,date))
163
164 (defalias 'mail-header-message-id 'mail-header-id)
165 (defmacro mail-header-id (header)
166 "Return Id in HEADER."
167 `(aref ,header 4))
168
169 (defalias 'mail-header-set-message-id 'mail-header-set-id)
170 (defmacro mail-header-set-id (header id)
171 "Set article Id of HEADER to ID."
172 `(aset ,header 4 ,id))
173
174 (defmacro mail-header-references (header)
175 "Return references in HEADER."
176 `(aref ,header 5))
177
178 (defmacro mail-header-set-references (header ref)
179 "Set article references of HEADER to REF."
180 `(aset ,header 5 ,ref))
181
182 (defmacro mail-header-chars (header)
183 "Return number of chars of article in HEADER."
184 `(aref ,header 6))
185
186 (defmacro mail-header-set-chars (header chars)
187 "Set number of chars in article of HEADER to CHARS."
188 `(aset ,header 6 ,chars))
189
190 (defmacro mail-header-lines (header)
191 "Return lines in HEADER."
192 `(aref ,header 7))
193
194 (defmacro mail-header-set-lines (header lines)
195 "Set article lines of HEADER to LINES."
196 `(aset ,header 7 ,lines))
197
198 (defmacro mail-header-xref (header)
199 "Return xref string in HEADER."
200 `(aref ,header 8))
201
202 (defmacro mail-header-set-xref (header xref)
203 "Set article XREF of HEADER to xref."
204 `(aset ,header 8 ,xref))
205
206 (defmacro mail-header-extra (header)
207 "Return the extra headers in HEADER."
208 `(aref ,header 9))
209
210 (defmacro mail-header-set-extra (header extra)
211 "Set the extra headers in HEADER to EXTRA."
212 `(aset ,header 9 ',extra))
213
214 (defsubst make-mail-header (&optional init)
215 "Create a new mail header structure initialized with INIT."
216 (make-vector 10 init))
217
218 (defsubst make-full-mail-header (&optional number subject from date id
219 references chars lines xref
220 extra)
221 "Create a new mail header structure initialized with the parameters given."
222 (vector number subject from date id references chars lines xref extra))
223
224 ;; fake message-ids: generation and detection
225
226 (defvar nnheader-fake-message-id 1)
227
228 (defsubst nnheader-generate-fake-message-id ()
229 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
230
231 (defsubst nnheader-fake-message-id-p (id)
232 (save-match-data ; regular message-id's are <.*>
233 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
234
235 ;; Parsing headers and NOV lines.
236
237 (defsubst nnheader-remove-cr-followed-by-lf ()
238 (goto-char (point-max))
239 (while (search-backward "\r\n" nil t)
240 (delete-char 1)))
241
242 (defsubst nnheader-header-value ()
243 (skip-chars-forward " \t")
244 (buffer-substring (point) (gnus-point-at-eol)))
245
246 (defun nnheader-parse-naked-head (&optional number)
247 ;; This function unfolds continuation lines in this buffer
248 ;; destructively. When this side effect is unwanted, use
249 ;; `nnheader-parse-head' instead of this function.
250 (let ((case-fold-search t)
251 (buffer-read-only nil)
252 (cur (current-buffer))
253 (p (point-min))
254 in-reply-to lines ref)
255 (nnheader-remove-cr-followed-by-lf)
256 (ietf-drums-unfold-fws)
257 (subst-char-in-region (point-min) (point-max) ?\t ? )
258 (goto-char p)
259 (insert "\n")
260 (prog1
261 ;; This implementation of this function, with nine
262 ;; search-forwards instead of the one re-search-forward and a
263 ;; case (which basically was the old function) is actually
264 ;; about twice as fast, even though it looks messier. You
265 ;; can't have everything, I guess. Speed and elegance don't
266 ;; always go hand in hand.
267 (vector
268 ;; Number.
269 (or number 0)
270 ;; Subject.
271 (progn
272 (goto-char p)
273 (if (search-forward "\nsubject:" nil t)
274 (nnheader-header-value) "(none)"))
275 ;; From.
276 (progn
277 (goto-char p)
278 (if (search-forward "\nfrom:" nil t)
279 (nnheader-header-value) "(nobody)"))
280 ;; Date.
281 (progn
282 (goto-char p)
283 (if (search-forward "\ndate:" nil t)
284 (nnheader-header-value) ""))
285 ;; Message-ID.
286 (progn
287 (goto-char p)
288 (if (search-forward "\nmessage-id:" nil t)
289 (buffer-substring
290 (1- (or (search-forward "<" (gnus-point-at-eol) t)
291 (point)))
292 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
293 ;; If there was no message-id, we just fake one to make
294 ;; subsequent routines simpler.
295 (nnheader-generate-fake-message-id)))
296 ;; References.
297 (progn
298 (goto-char p)
299 (if (search-forward "\nreferences:" nil t)
300 (nnheader-header-value)
301 ;; Get the references from the in-reply-to header if
302 ;; there were no references and the in-reply-to header
303 ;; looks promising.
304 (if (and (search-forward "\nin-reply-to:" nil t)
305 (setq in-reply-to (nnheader-header-value))
306 (string-match "<[^\n>]+>" in-reply-to))
307 (let (ref2)
308 (setq ref (substring in-reply-to (match-beginning 0)
309 (match-end 0)))
310 (while (string-match "<[^\n>]+>"
311 in-reply-to (match-end 0))
312 (setq ref2 (substring in-reply-to (match-beginning 0)
313 (match-end 0)))
314 (when (> (length ref2) (length ref))
315 (setq ref ref2)))
316 ref)
317 nil)))
318 ;; Chars.
319 0
320 ;; Lines.
321 (progn
322 (goto-char p)
323 (if (search-forward "\nlines: " nil t)
324 (if (numberp (setq lines (read cur)))
325 lines 0)
326 0))
327 ;; Xref.
328 (progn
329 (goto-char p)
330 (and (search-forward "\nxref:" nil t)
331 (nnheader-header-value)))
332 ;; Extra.
333 (when nnmail-extra-headers
334 (let ((extra nnmail-extra-headers)
335 out)
336 (while extra
337 (goto-char p)
338 (when (search-forward
339 (concat "\n" (symbol-name (car extra)) ":") nil t)
340 (push (cons (car extra) (nnheader-header-value))
341 out))
342 (pop extra))
343 out)))
344 (goto-char p)
345 (delete-char 1))))
346
347 (defun nnheader-parse-head (&optional naked)
348 (let ((cur (current-buffer)) num beg end)
349 (when (if naked
350 (setq num 0
351 beg (point-min)
352 end (point-max))
353 (goto-char (point-min))
354 ;; Search to the beginning of the next header. Error
355 ;; messages do not begin with 2 or 3.
356 (when (re-search-forward "^[23][0-9]+ " nil t)
357 (end-of-line)
358 (setq num (read cur)
359 beg (point)
360 end (if (search-forward "\n.\n" nil t)
361 (- (point) 2)
362 (point)))))
363 (with-temp-buffer
364 (insert-buffer-substring cur beg end)
365 (nnheader-parse-naked-head num)))))
366
367 (defmacro nnheader-nov-skip-field ()
368 '(search-forward "\t" eol 'move))
369
370 (defmacro nnheader-nov-field ()
371 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
372
373 (defmacro nnheader-nov-read-integer ()
374 '(prog1
375 (if (eq (char-after) ?\t)
376 0
377 (let ((num (condition-case nil
378 (read (current-buffer))
379 (error nil))))
380 (if (numberp num) num 0)))
381 (or (eobp) (forward-char 1))))
382
383 (defmacro nnheader-nov-parse-extra ()
384 '(let (out string)
385 (while (not (memq (char-after) '(?\n nil)))
386 (setq string (nnheader-nov-field))
387 (when (string-match "^\\([^ :]+\\): " string)
388 (push (cons (intern (match-string 1 string))
389 (substring string (match-end 0)))
390 out)))
391 out))
392
393 (defmacro nnheader-nov-read-message-id ()
394 '(let ((id (nnheader-nov-field)))
395 (if (string-match "^<[^>]+>$" id)
396 id
397 (nnheader-generate-fake-message-id))))
398
399 (defun nnheader-parse-nov ()
400 (let ((eol (gnus-point-at-eol)))
401 (vector
402 (nnheader-nov-read-integer) ; number
403 (nnheader-nov-field) ; subject
404 (nnheader-nov-field) ; from
405 (nnheader-nov-field) ; date
406 (nnheader-nov-read-message-id) ; id
407 (nnheader-nov-field) ; refs
408 (nnheader-nov-read-integer) ; chars
409 (nnheader-nov-read-integer) ; lines
410 (if (eq (char-after) ?\n)
411 nil
412 (if (looking-at "Xref: ")
413 (goto-char (match-end 0)))
414 (nnheader-nov-field)) ; Xref
415 (nnheader-nov-parse-extra)))) ; extra
416
417 (defun nnheader-insert-nov (header)
418 (princ (mail-header-number header) (current-buffer))
419 (let ((p (point)))
420 (insert
421 "\t"
422 (or (mail-header-subject header) "(none)") "\t"
423 (or (mail-header-from header) "(nobody)") "\t"
424 (or (mail-header-date header) "") "\t"
425 (or (mail-header-id header)
426 (nnmail-message-id))
427 "\t"
428 (or (mail-header-references header) "") "\t")
429 (princ (or (mail-header-chars header) 0) (current-buffer))
430 (insert "\t")
431 (princ (or (mail-header-lines header) 0) (current-buffer))
432 (insert "\t")
433 (when (mail-header-xref header)
434 (insert "Xref: " (mail-header-xref header)))
435 (when (or (mail-header-xref header)
436 (mail-header-extra header))
437 (insert "\t"))
438 (when (mail-header-extra header)
439 (let ((extra (mail-header-extra header)))
440 (while extra
441 (insert (symbol-name (caar extra))
442 ": " (cdar extra) "\t")
443 (pop extra))))
444 (insert "\n")
445 (backward-char 1)
446 (while (search-backward "\n" p t)
447 (delete-char 1))
448 (forward-line 1)))
449
450 (defun nnheader-parse-overview-file (file)
451 "Parse FILE and return a list of headers."
452 (mm-with-unibyte-buffer
453 (nnheader-insert-file-contents file)
454 (goto-char (point-min))
455 (let (headers)
456 (while (not (eobp))
457 (push (nnheader-parse-nov) headers)
458 (forward-line 1))
459 (nreverse headers))))
460
461 (defun nnheader-write-overview-file (file headers)
462 "Write HEADERS to FILE."
463 (with-temp-file file
464 (mapcar 'nnheader-insert-nov headers)))
465
466 (defun nnheader-insert-header (header)
467 (insert
468 "Subject: " (or (mail-header-subject header) "(none)") "\n"
469 "From: " (or (mail-header-from header) "(nobody)") "\n"
470 "Date: " (or (mail-header-date header) "") "\n"
471 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
472 "References: " (or (mail-header-references header) "") "\n"
473 "Lines: ")
474 (princ (or (mail-header-lines header) 0) (current-buffer))
475 (insert "\n\n"))
476
477 (defun nnheader-insert-article-line (article)
478 (goto-char (point-min))
479 (insert "220 ")
480 (princ article (current-buffer))
481 (insert " Article retrieved.\n")
482 (search-forward "\n\n" nil 'move)
483 (delete-region (point) (point-max))
484 (forward-char -1)
485 (insert "."))
486
487 (defun nnheader-nov-delete-outside-range (beg end)
488 "Delete all NOV lines that lie outside the BEG to END range."
489 ;; First we find the first wanted line.
490 (nnheader-find-nov-line beg)
491 (delete-region (point-min) (point))
492 ;; Then we find the last wanted line.
493 (when (nnheader-find-nov-line end)
494 (forward-line 1))
495 (delete-region (point) (point-max)))
496
497 (defun nnheader-find-nov-line (article)
498 "Put point at the NOV line that start with ARTICLE.
499 If ARTICLE doesn't exist, put point where that line
500 would have been. The function will return non-nil if
501 the line could be found."
502 ;; This function basically does a binary search.
503 (let ((max (point-max))
504 (min (goto-char (point-min)))
505 (cur (current-buffer))
506 (prev (point-min))
507 num found)
508 (while (not found)
509 (goto-char (+ min (/ (- max min) 2)))
510 (beginning-of-line)
511 (if (or (= (point) prev)
512 (eobp))
513 (setq found t)
514 (setq prev (point))
515 (while (and (not (numberp (setq num (read cur))))
516 (not (eobp)))
517 (gnus-delete-line))
518 (cond ((> num article)
519 (setq max (point)))
520 ((< num article)
521 (setq min (point)))
522 (t
523 (setq found 'yes)))))
524 ;; We may be at the first line.
525 (when (and (not num)
526 (not (eobp)))
527 (setq num (read cur)))
528 ;; Now we may have found the article we're looking for, or we
529 ;; may be somewhere near it.
530 (when (and (not (eq found 'yes))
531 (not (eq num article)))
532 (setq found (point))
533 (while (and (< (point) max)
534 (or (not (numberp num))
535 (< num article)))
536 (forward-line 1)
537 (setq found (point))
538 (or (eobp)
539 (= (setq num (read cur)) article)))
540 (unless (eq num article)
541 (goto-char found)))
542 (beginning-of-line)
543 (eq num article)))
544
545 ;; Various cruft the backends and Gnus need to communicate.
546
547 (defvar nntp-server-buffer nil)
548 (defvar nntp-process-response nil)
549 (defvar news-reply-yank-from nil)
550 (defvar news-reply-yank-message-id nil)
551
552 (defvar nnheader-callback-function nil)
553
554 (defun nnheader-init-server-buffer ()
555 "Initialize the Gnus-backend communication buffer."
556 (save-excursion
557 (unless (gnus-buffer-live-p nntp-server-buffer)
558 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
559 (set-buffer nntp-server-buffer)
560 (mm-enable-multibyte)
561 (erase-buffer)
562 (kill-all-local-variables)
563 (setq case-fold-search t) ;Should ignore case.
564 (set (make-local-variable 'nntp-process-response) nil)
565 t))
566
567 ;;; Various functions the backends use.
568
569 (defun nnheader-file-error (file)
570 "Return a string that says what is wrong with FILE."
571 (format
572 (cond
573 ((not (file-exists-p file))
574 "%s does not exist")
575 ((file-directory-p file)
576 "%s is a directory")
577 ((not (file-readable-p file))
578 "%s is not readable"))
579 file))
580
581 (defun nnheader-insert-head (file)
582 "Insert the head of the article."
583 (when (file-exists-p file)
584 (if (eq nnheader-max-head-length t)
585 ;; Just read the entire file.
586 (nnheader-insert-file-contents file)
587 ;; Read 1K blocks until we find a separator.
588 (let ((beg 0)
589 format-alist)
590 (while (and (eq nnheader-head-chop-length
591 (nth 1 (nnheader-insert-file-contents
592 file nil beg
593 (incf beg nnheader-head-chop-length))))
594 (prog1 (not (search-forward "\n\n" nil t))
595 (goto-char (point-max)))
596 (or (null nnheader-max-head-length)
597 (< beg nnheader-max-head-length))))))
598 t))
599
600 (defun nnheader-article-p ()
601 "Say whether the current buffer looks like an article."
602 (goto-char (point-min))
603 (if (not (search-forward "\n\n" nil t))
604 nil
605 (narrow-to-region (point-min) (1- (point)))
606 (goto-char (point-min))
607 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
608 (goto-char (match-end 0)))
609 (prog1
610 (eobp)
611 (widen))))
612
613 (defun nnheader-insert-references (references message-id)
614 "Insert a References header based on REFERENCES and MESSAGE-ID."
615 (if (and (not references) (not message-id))
616 ;; This is invalid, but not all articles have Message-IDs.
617 ()
618 (mail-position-on-field "References")
619 (let ((begin (gnus-point-at-bol))
620 (fill-column 78)
621 (fill-prefix "\t"))
622 (when references
623 (insert references))
624 (when (and references message-id)
625 (insert " "))
626 (when message-id
627 (insert message-id))
628 ;; Fold long References lines to conform to RFC1036 (sort of).
629 ;; The region must end with a newline to fill the region
630 ;; without inserting extra newline.
631 (fill-region-as-paragraph begin (1+ (point))))))
632
633 (defun nnheader-replace-header (header new-value)
634 "Remove HEADER and insert the NEW-VALUE."
635 (save-excursion
636 (save-restriction
637 (nnheader-narrow-to-headers)
638 (prog1
639 (message-remove-header header)
640 (goto-char (point-max))
641 (insert header ": " new-value "\n")))))
642
643 (defun nnheader-narrow-to-headers ()
644 "Narrow to the head of an article."
645 (widen)
646 (narrow-to-region
647 (goto-char (point-min))
648 (if (search-forward "\n\n" nil t)
649 (1- (point))
650 (point-max)))
651 (goto-char (point-min)))
652
653 (defun nnheader-remove-body ()
654 "Remove the body from an article in this current buffer."
655 (goto-char (point-min))
656 (when (re-search-forward "\n\r?\n" nil t)
657 (delete-region (point) (point-max))))
658
659 (defun nnheader-set-temp-buffer (name &optional noerase)
660 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
661 (set-buffer (get-buffer-create name))
662 (buffer-disable-undo)
663 (unless noerase
664 (erase-buffer))
665 (current-buffer))
666
667 (eval-when-compile (defvar jka-compr-compression-info-list))
668 (defvar nnheader-numerical-files
669 (if (boundp 'jka-compr-compression-info-list)
670 (concat "\\([0-9]+\\)\\("
671 (mapconcat (lambda (i) (aref i 0))
672 jka-compr-compression-info-list "\\|")
673 "\\)?")
674 "[0-9]+$")
675 "Regexp that match numerical files.")
676
677 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
678 "Regexp that matches numerical file names.")
679
680 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
681 "Regexp that matches numerical full file names.")
682
683 (defsubst nnheader-file-to-number (file)
684 "Take a FILE name and return the article number."
685 (if (string= nnheader-numerical-short-files "^[0-9]+$")
686 (string-to-number file)
687 (string-match nnheader-numerical-short-files file)
688 (string-to-number (match-string 0 file))))
689
690 (defvar nnheader-directory-files-is-safe
691 (or (eq system-type 'windows-nt)
692 (and (not (featurep 'xemacs))
693 (> emacs-major-version 20)))
694 "If non-nil, Gnus believes `directory-files' is safe.
695 It has been reported numerous times that `directory-files' fails with
696 an alarming frequency on NFS mounted file systems. If it is nil,
697 `nnheader-directory-files-safe' is used.")
698
699 (defun nnheader-directory-files-safe (&rest args)
700 "Execute `directory-files' twice and returns the longer result."
701 (let ((first (apply 'directory-files args))
702 (second (apply 'directory-files args)))
703 (if (> (length first) (length second))
704 first
705 second)))
706
707 (defun nnheader-directory-articles (dir)
708 "Return a list of all article files in directory DIR."
709 (mapcar 'nnheader-file-to-number
710 (if nnheader-directory-files-is-safe
711 (directory-files
712 dir nil nnheader-numerical-short-files t)
713 (nnheader-directory-files-safe
714 dir nil nnheader-numerical-short-files t))))
715
716 (defun nnheader-article-to-file-alist (dir)
717 "Return an alist of article/file pairs in DIR."
718 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
719 (if nnheader-directory-files-is-safe
720 (directory-files
721 dir nil nnheader-numerical-short-files t)
722 (nnheader-directory-files-safe
723 dir nil nnheader-numerical-short-files t))))
724
725 (defun nnheader-fold-continuation-lines ()
726 "Fold continuation lines in the current buffer."
727 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
728
729 (defun nnheader-translate-file-chars (file &optional full)
730 "Translate FILE into something that can be a file name.
731 If FULL, translate everything."
732 (if (null nnheader-file-name-translation-alist)
733 ;; No translation is necessary.
734 file
735 (let* ((i 0)
736 trans leaf path len)
737 (if full
738 ;; Do complete translation.
739 (setq leaf (copy-sequence file)
740 path ""
741 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
742 2 0))
743 ;; We translate -- but only the file name. We leave the directory
744 ;; alone.
745 (if (and (featurep 'xemacs)
746 (memq system-type '(cygwin32 win32 w32 mswindows windows-nt
747 cygwin)))
748 ;; This is needed on NT and stuff, because
749 ;; file-name-nondirectory is not enough to split
750 ;; file names, containing ':', e.g.
751 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
752 ;;
753 ;; we are trying to correctly split such names:
754 ;; "d:file.name" -> "a:" "file.name"
755 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
756 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
757 ;; etc.
758 ;; to translate then only the file name part.
759 (progn
760 (setq leaf file
761 path "")
762 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
763 (setq leaf (substring file (match-beginning 2))
764 path (substring file 0 (match-beginning 2)))))
765 ;; Emacs DTRT, says andrewi.
766 (setq leaf (file-name-nondirectory file)
767 path (file-name-directory file))))
768 (setq len (length leaf))
769 (while (< i len)
770 (when (setq trans (cdr (assq (aref leaf i)
771 nnheader-file-name-translation-alist)))
772 (aset leaf i trans))
773 (incf i))
774 (concat path leaf))))
775
776 (defun nnheader-report (backend &rest args)
777 "Report an error from the BACKEND.
778 The first string in ARGS can be a format string."
779 (set (intern (format "%s-status-string" backend))
780 (if (< (length args) 2)
781 (car args)
782 (apply 'format args)))
783 nil)
784
785 (defun nnheader-get-report (backend)
786 "Get the most recent report from BACKEND."
787 (condition-case ()
788 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
789 backend))))
790 (error (nnheader-message 5 ""))))
791
792 (defun nnheader-insert (format &rest args)
793 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
794 If FORMAT isn't a format string, it and all ARGS will be inserted
795 without formatting."
796 (save-excursion
797 (set-buffer nntp-server-buffer)
798 (erase-buffer)
799 (if (string-match "%" format)
800 (insert (apply 'format format args))
801 (apply 'insert format args))
802 t))
803
804 (defsubst nnheader-replace-chars-in-string (string from to)
805 (mm-subst-char-in-string from to string))
806
807 (defun nnheader-replace-duplicate-chars-in-string (string from to)
808 "Replace characters in STRING from FROM to TO."
809 (let ((string (substring string 0)) ;Copy string.
810 (len (length string))
811 (idx 0) prev i)
812 ;; Replace all occurrences of FROM with TO.
813 (while (< idx len)
814 (setq i (aref string idx))
815 (when (and (eq prev from) (= i from))
816 (aset string (1- idx) to)
817 (aset string idx to))
818 (setq prev i)
819 (setq idx (1+ idx)))
820 string))
821
822 (defun nnheader-file-to-group (file &optional top)
823 "Return a group name based on FILE and TOP."
824 (nnheader-replace-chars-in-string
825 (if (not top)
826 file
827 (condition-case ()
828 (substring (expand-file-name file)
829 (length
830 (expand-file-name
831 (file-name-as-directory top))))
832 (error "")))
833 nnheader-directory-separator-character ?.))
834
835 (defun nnheader-message (level &rest args)
836 "Message if the Gnus backends are talkative."
837 (if (or (not (numberp gnus-verbose-backends))
838 (<= level gnus-verbose-backends))
839 (apply 'message args)
840 (apply 'format args)))
841
842 (defun nnheader-be-verbose (level)
843 "Return whether the backends should be verbose on LEVEL."
844 (or (not (numberp gnus-verbose-backends))
845 (<= level gnus-verbose-backends)))
846
847 (defvar nnheader-pathname-coding-system 'iso-8859-1
848 "*Coding system for file name.")
849
850 (defun nnheader-group-pathname (group dir &optional file)
851 "Make file name for GROUP."
852 (concat
853 (let ((dir (file-name-as-directory (expand-file-name dir))))
854 ;; If this directory exists, we use it directly.
855 (file-name-as-directory
856 (if (file-directory-p (concat dir group))
857 (expand-file-name group dir)
858 ;; If not, we translate dots into slashes.
859 (expand-file-name (mm-encode-coding-string
860 (nnheader-replace-chars-in-string group ?. ?/)
861 nnheader-pathname-coding-system)
862 dir))))
863 (cond ((null file) "")
864 ((numberp file) (int-to-string file))
865 (t file))))
866
867 (defun nnheader-concat (dir &rest files)
868 "Concat DIR as directory to FILES."
869 (apply 'concat (file-name-as-directory dir) files))
870
871 (defun nnheader-ms-strip-cr ()
872 "Strip ^M from the end of all lines."
873 (save-excursion
874 (nnheader-remove-cr-followed-by-lf)))
875
876 (defun nnheader-file-size (file)
877 "Return the file size of FILE or 0."
878 (or (nth 7 (file-attributes file)) 0))
879
880 (defun nnheader-find-etc-directory (package &optional file first)
881 "Go through `load-path' and find the \"../etc/PACKAGE\" directory.
882 This function will look in the parent directory of each `load-path'
883 entry, and look for the \"etc\" directory there.
884 If FILE, find the \".../etc/PACKAGE\" file instead.
885 If FIRST is non-nil, return the directory or the file found at the
886 first. Otherwise, find the newest one, though it may take a time."
887 (let ((path load-path)
888 dir results)
889 ;; We try to find the dir by looking at the load path,
890 ;; stripping away the last component and adding "etc/".
891 (while path
892 (if (and (car path)
893 (file-exists-p
894 (setq dir (concat
895 (file-name-directory
896 (directory-file-name (car path)))
897 "etc/" package
898 (if file "" "/"))))
899 (or file (file-directory-p dir)))
900 (progn
901 (or (member dir results)
902 (push dir results))
903 (setq path (if first nil (cdr path))))
904 (setq path (cdr path))))
905 (if (or first (not (cdr results)))
906 (car results)
907 (car (sort results 'file-newer-than-file-p)))))
908
909 (eval-when-compile
910 (defvar ange-ftp-path-format)
911 (defvar efs-path-regexp))
912 (defun nnheader-re-read-dir (path)
913 "Re-read directory PATH if PATH is on a remote system."
914 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
915 (when (string-match efs-path-regexp path)
916 (efs-re-read-dir path))
917 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
918 (when (string-match (car ange-ftp-path-format) path)
919 (ange-ftp-re-read-dir path)))))
920
921 (defvar nnheader-file-coding-system 'raw-text
922 "Coding system used in file backends of Gnus.")
923
924 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
925 "Like `insert-file-contents', q.v., but only reads in the file.
926 A buffer may be modified in several ways after reading into the buffer due
927 to advanced Emacs features, such as file-name-handlers, format decoding,
928 find-file-hooks, etc.
929 This function ensures that none of these modifications will take place."
930 (let ((coding-system-for-read nnheader-file-coding-system))
931 (mm-insert-file-contents filename visit beg end replace)))
932
933 (defun nnheader-insert-nov-file (file first)
934 (let ((size (nth 7 (file-attributes file)))
935 (cutoff (* 32 1024)))
936 (when size
937 (if (< size cutoff)
938 ;; If the file is small, we just load it.
939 (nnheader-insert-file-contents file)
940 ;; We start on the assumption that FIRST is pretty recent. If
941 ;; not, we just insert the rest of the file as well.
942 (let (current)
943 (nnheader-insert-file-contents file nil (- size cutoff) size)
944 (goto-char (point-min))
945 (delete-region (point) (or (search-forward "\n" nil 'move) (point)))
946 (setq current (ignore-errors (read (current-buffer))))
947 (if (and (numberp current)
948 (< current first))
949 t
950 (delete-region (point-min) (point-max))
951 (nnheader-insert-file-contents file)))))))
952
953 (defun nnheader-find-file-noselect (&rest args)
954 "Open a file with some variables bound.
955 See `find-file-noselect' for the arguments."
956 (let* ((format-alist nil)
957 (auto-mode-alist (mm-auto-mode-alist))
958 (default-major-mode 'fundamental-mode)
959 (enable-local-variables nil)
960 (after-insert-file-functions nil)
961 (enable-local-eval nil)
962 (coding-system-for-read nnheader-file-coding-system)
963 (ffh (if (boundp 'find-file-hook)
964 'find-file-hook
965 'find-file-hooks))
966 (val (symbol-value ffh)))
967 (set ffh nil)
968 (unwind-protect
969 (apply 'find-file-noselect args)
970 (set ffh val))))
971
972 (defun nnheader-directory-regular-files (dir)
973 "Return a list of all regular files in DIR."
974 (let ((files (directory-files dir t))
975 out)
976 (while files
977 (when (file-regular-p (car files))
978 (push (car files) out))
979 (pop files))
980 (nreverse out)))
981
982 (defun nnheader-directory-files (&rest args)
983 "Same as `directory-files', but prune \".\" and \"..\"."
984 (let ((files (apply 'directory-files args))
985 out)
986 (while files
987 (unless (member (file-name-nondirectory (car files)) '("." ".."))
988 (push (car files) out))
989 (pop files))
990 (nreverse out)))
991
992 (defmacro nnheader-skeleton-replace (from &optional to regexp)
993 `(let ((new (generate-new-buffer " *nnheader replace*"))
994 (cur (current-buffer))
995 (start (point-min)))
996 (set-buffer cur)
997 (goto-char (point-min))
998 (while (,(if regexp 're-search-forward 'search-forward)
999 ,from nil t)
1000 (insert-buffer-substring
1001 cur start (prog1 (match-beginning 0) (set-buffer new)))
1002 (goto-char (point-max))
1003 ,(when to `(insert ,to))
1004 (set-buffer cur)
1005 (setq start (point)))
1006 (insert-buffer-substring
1007 cur start (prog1 (point-max) (set-buffer new)))
1008 (copy-to-buffer cur (point-min) (point-max))
1009 (kill-buffer (current-buffer))
1010 (set-buffer cur)))
1011
1012 (defun nnheader-replace-string (from to)
1013 "Do a fast replacement of FROM to TO from point to `point-max'."
1014 (nnheader-skeleton-replace from to))
1015
1016 (defun nnheader-replace-regexp (from to)
1017 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1018 (nnheader-skeleton-replace from to t))
1019
1020 (defun nnheader-strip-cr ()
1021 "Strip all \r's from the current buffer."
1022 (nnheader-skeleton-replace "\r"))
1023
1024 (defalias 'nnheader-run-at-time 'run-at-time)
1025 (defalias 'nnheader-cancel-timer 'cancel-timer)
1026 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1027 (defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1028
1029 (defun nnheader-accept-process-output (process)
1030 (accept-process-output
1031 process
1032 (truncate nnheader-read-timeout)
1033 (truncate (* (- nnheader-read-timeout
1034 (truncate nnheader-read-timeout))
1035 1000))))
1036
1037 (when (featurep 'xemacs)
1038 (require 'nnheaderxm))
1039
1040 (run-hooks 'nnheader-load-hook)
1041
1042 (provide 'nnheader)
1043
1044 ;;; arch-tag: a9c4b7d9-52ae-4ec9-b196-dfd93124d202
1045 ;;; nnheader.el ends here