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