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