Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-14
[bpt/emacs.git] / lisp / gnus / nnheader.el
CommitLineData
eec82323 1;;; nnheader.el --- header access macros for Gnus and its backends
16409b0b
GM
2
3;; Copyright (C) 1987, 1988, 1989, 1990, 1993, 1994, 1995, 1996,
f4dd4ae8 4;; 1997, 1998, 2000, 2001, 2002, 2003, 2004
16409b0b 5;; 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
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
eec82323
LMI
30;;; Code:
31
7df7482d 32(eval-when-compile (require 'cl))
6f3b74c6
GM
33
34;; Requiring `gnus-util' at compile time creates a circular
35;; dependency between nnheader.el and gnus-util.el.
23f87bed 36;;(eval-when-compile (require 'gnus-util))
7df7482d 37
eec82323 38(require 'mail-utils)
16409b0b 39(require 'mm-util)
23f87bed 40(require 'gnus-util)
9e153fef 41(eval-and-compile
23f87bed 42 (autoload 'gnus-sorted-intersection "gnus-range")
9e153fef 43 (autoload 'gnus-intersection "gnus-range")
23f87bed
MB
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.
49The higher the number, the more messages the Gnus backends will flash
50to say what it's doing. At zero, the Gnus backends will be totally
51mute; at five, they will display most important messages; and at ten,
52they 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)
eec82323 60
f4dd4ae8 61(defvar nnheader-max-head-length 8192
23f87bed
MB
62 "*Max length of the head of articles.
63
64Value is an integer, nil, or t. nil means read in chunks of a file
65indefinitely until a complete head is found\; t means always read the
66entire file immediately, disregarding `nnheader-head-chop-length'.
67
68Integer values will in effect be rounded up to the nearest multiple of
69`nnheader-head-chop-length'.")
eec82323
LMI
70
71(defvar nnheader-head-chop-length 2048
72 "*Length of each read operation when trying to fetch HEAD headers.")
73
23f87bed
MB
74(defvar nnheader-read-timeout
75 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
76 (symbol-name system-type))
e62e7654
MB
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
23f87bed
MB
86 0.1)
87 "How long nntp should wait between checking for the end of output.
88Shorter values mean quicker response, but are more CPU intensive.")
89
c60ee5e7 90(defvar nnheader-file-name-translation-alist
3e7b2fa7
SZ
91 (let ((case-fold-search t))
92 (cond
23f87bed 93 ((string-match "windows-nt\\|os/2\\|emx\\|cygwin"
3e7b2fa7
SZ
94 (symbol-name system-type))
95 (append (mapcar (lambda (c) (cons c ?_))
96 '(?: ?* ?\" ?< ?> ??))
23f87bed 97 (if (string-match "windows-nt\\|cygwin"
3e7b2fa7
SZ
98 (symbol-name system-type))
99 nil
100 '((?+ . ?-)))))
101 (t nil)))
eec82323 102 "*Alist that says how to translate characters in file names.
16409b0b 103For instance, if \":\" is invalid as a file character in file names
eec82323
LMI
104on your system, you could say something like:
105
106\(setq nnheader-file-name-translation-alist '((?: . ?_)))")
107
23f87bed
MB
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
eec82323 112(eval-and-compile
16409b0b
GM
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")
16409b0b 117 (autoload 'gnus-buffer-live-p "gnus-util"))
eec82323
LMI
118
119;;; Header access macros.
120
16409b0b
GM
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
eec82323
LMI
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)
16409b0b 203 "Set article XREF of HEADER to xref."
eec82323
LMI
204 `(aset ,header 8 ,xref))
205
16409b0b
GM
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)
eec82323 215 "Create a new mail header structure initialized with INIT."
16409b0b 216 (make-vector 10 init))
eec82323 217
16409b0b
GM
218(defsubst make-full-mail-header (&optional number subject from date id
219 references chars lines xref
220 extra)
eec82323 221 "Create a new mail header structure initialized with the parameters given."
16409b0b 222 (vector number subject from date id references chars lines xref extra))
eec82323
LMI
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)
23f87bed 232 (save-match-data ; regular message-id's are <.*>
eec82323
LMI
233 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
234
235;; Parsing headers and NOV lines.
236
23f87bed
MB
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
eec82323 242(defsubst nnheader-header-value ()
23f87bed
MB
243 (skip-chars-forward " \t")
244 (buffer-substring (point) (gnus-point-at-eol)))
eec82323 245
23f87bed
MB
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.
eec82323 250 (let ((case-fold-search t)
eec82323 251 (buffer-read-only nil)
23f87bed
MB
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")
eec82323 260 (prog1
23f87bed
MB
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)))))
eec82323
LMI
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
16409b0b 375 (if (eq (char-after) ?\t)
eec82323 376 0
16409b0b
GM
377 (let ((num (condition-case nil
378 (read (current-buffer))
379 (error nil))))
eec82323
LMI
380 (if (numberp num) num 0)))
381 (or (eobp) (forward-char 1))))
382
16409b0b
GM
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))))
eec82323
LMI
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
16409b0b 406 (nnheader-nov-read-message-id) ; id
eec82323
LMI
407 (nnheader-nov-field) ; refs
408 (nnheader-nov-read-integer) ; chars
409 (nnheader-nov-read-integer) ; lines
16409b0b 410 (if (eq (char-after) ?\n)
eec82323 411 nil
8b93df01
DL
412 (if (looking-at "Xref: ")
413 (goto-char (match-end 0)))
414 (nnheader-nov-field)) ; Xref
16409b0b 415 (nnheader-nov-parse-extra)))) ; extra
eec82323
LMI
416
417(defun nnheader-insert-nov (header)
418 (princ (mail-header-number header) (current-buffer))
16409b0b
GM
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
23f87bed
MB
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
16409b0b 466(defun nnheader-insert-header (header)
eec82323 467 (insert
16409b0b
GM
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: ")
eec82323 474 (princ (or (mail-header-lines header) 0) (current-buffer))
16409b0b 475 (insert "\n\n"))
eec82323
LMI
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.
499If ARTICLE doesn't exist, put point where that line
500would have been. The function will return non-nil if
501the 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)
23f87bed 509 (goto-char (+ min (/ (- max min) 2)))
eec82323
LMI
510 (beginning-of-line)
511 (if (or (= (point) prev)
512 (eobp))
513 (setq found t)
514 (setq prev (point))
6748645f
LMI
515 (while (and (not (numberp (setq num (read cur))))
516 (not (eobp)))
517 (gnus-delete-line))
518 (cond ((> num article)
eec82323
LMI
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)
23f87bed 548(defvar nntp-process-response nil)
eec82323
LMI
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)
23c6089e 560 (mm-enable-multibyte)
eec82323
LMI
561 (erase-buffer)
562 (kill-all-local-variables)
563 (setq case-fold-search t) ;Should ignore case.
23f87bed 564 (set (make-local-variable 'nntp-process-response) nil)
eec82323
LMI
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))
16409b0b 607 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
eec82323
LMI
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))
16409b0b
GM
616 ;; This is invalid, but not all articles have Message-IDs.
617 ()
eec82323 618 (mail-position-on-field "References")
23f87bed 619 (let ((begin (gnus-point-at-bol))
eec82323
LMI
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
23f87bed
MB
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
eec82323
LMI
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))
16409b0b 662 (buffer-disable-undo)
eec82323
LMI
663 (unless noerase
664 (erase-buffer))
665 (current-buffer))
666
16409b0b 667(eval-when-compile (defvar jka-compr-compression-info-list))
eec82323
LMI
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)
35ef97a5 681 "Regexp that matches numerical full file names.")
eec82323
LMI
682
683(defsubst nnheader-file-to-number (file)
16409b0b 684 "Take a FILE name and return the article number."
6748645f 685 (if (string= nnheader-numerical-short-files "^[0-9]+$")
eec82323
LMI
686 (string-to-int file)
687 (string-match nnheader-numerical-short-files file)
688 (string-to-int (match-string 0 file))))
689
23f87bed
MB
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.
695It has been reported numerous times that `directory-files' fails with
696an alarming frequency on NFS mounted file systems. If it is nil,
697`nnheader-directory-files-safe' is used.")
698
eec82323 699(defun nnheader-directory-files-safe (&rest args)
23f87bed 700 "Execute `directory-files' twice and returns the longer result."
eec82323
LMI
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)
16409b0b 708 "Return a list of all article files in directory DIR."
eec82323 709 (mapcar 'nnheader-file-to-number
23f87bed
MB
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))))
eec82323
LMI
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))
23f87bed
MB
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))))
eec82323
LMI
724
725(defun nnheader-fold-continuation-lines ()
726 "Fold continuation lines in the current buffer."
727 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
728
6748645f
LMI
729(defun nnheader-translate-file-chars (file &optional full)
730 "Translate FILE into something that can be a file name.
731If FULL, translate everything."
eec82323
LMI
732 (if (null nnheader-file-name-translation-alist)
733 ;; No translation is necessary.
734 file
eec82323
LMI
735 (let* ((i 0)
736 trans leaf path len)
6748645f
LMI
737 (if full
738 ;; Do complete translation.
739 (setq leaf (copy-sequence file)
16409b0b
GM
740 path ""
741 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
742 2 0))
6748645f
LMI
743 ;; We translate -- but only the file name. We leave the directory
744 ;; alone.
df359f6c 745 (if (and (featurep 'xemacs)
23f87bed
MB
746 (memq system-type '(cygwin32 win32 w32 mswindows windows-nt
747 cygwin)))
df359f6c
DL
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"
c60ee5e7 752 ;;
df359f6c
DL
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.
6748645f
LMI
766 (setq leaf (file-name-nondirectory file)
767 path (file-name-directory file))))
eec82323
LMI
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.
778The 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 ()
6748645f 788 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
16409b0b 789 backend))))
6748645f 790 (error (nnheader-message 5 ""))))
eec82323
LMI
791
792(defun nnheader-insert (format &rest args)
793 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
794If FORMAT isn't a format string, it and all ARGS will be inserted
795without 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
23f87bed
MB
804(defsubst nnheader-replace-chars-in-string (string from to)
805 (mm-subst-char-in-string from to string))
16409b0b
GM
806
807(defun nnheader-replace-duplicate-chars-in-string (string from to)
eec82323
LMI
808 "Replace characters in STRING from FROM to TO."
809 (let ((string (substring string 0)) ;Copy string.
810 (len (length string))
16409b0b 811 (idx 0) prev i)
eec82323
LMI
812 ;; Replace all occurrences of FROM with TO.
813 (while (< idx len)
16409b0b
GM
814 (setq i (aref string idx))
815 (when (and (eq prev from) (= i from))
816 (aset string (1- idx) to)
eec82323 817 (aset string idx to))
16409b0b 818 (setq prev i)
eec82323
LMI
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 "")))
23f87bed 833 nnheader-directory-separator-character ?.))
eec82323
LMI
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
23f87bed
MB
847(defvar nnheader-pathname-coding-system 'iso-8859-1
848 "*Coding system for file name.")
e0b8fdf5 849
eec82323 850(defun nnheader-group-pathname (group dir &optional file)
35ef97a5 851 "Make file name for GROUP."
eec82323
LMI
852 (concat
853 (let ((dir (file-name-as-directory (expand-file-name dir))))
854 ;; If this directory exists, we use it directly.
16409b0b
GM
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 ?. ?/)
23f87bed 861 nnheader-pathname-coding-system)
16409b0b 862 dir))))
eec82323
LMI
863 (cond ((null file) "")
864 ((numberp file) (int-to-string file))
865 (t file))))
866
eec82323 867(defun nnheader-concat (dir &rest files)
16409b0b 868 "Concat DIR as directory to FILES."
eec82323
LMI
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
23f87bed 874 (nnheader-remove-cr-followed-by-lf)))
eec82323
LMI
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
23f87bed
MB
880(defun nnheader-find-etc-directory (package &optional file first)
881 "Go through `load-path' and find the \"../etc/PACKAGE\" directory.
882This function will look in the parent directory of each `load-path'
883entry, and look for the \"etc\" directory there.
884If FILE, find the \".../etc/PACKAGE\" file instead.
885If FIRST is non-nil, return the directory or the file found at the
886first. Otherwise, find the newest one, though it may take a time."
eec82323 887 (let ((path load-path)
23f87bed 888 dir results)
eec82323
LMI
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)))
23f87bed
MB
900 (progn
901 (or (member dir results)
902 (push dir results))
903 (setq path (if first nil (cdr path))))
eec82323 904 (setq path (cdr path))))
23f87bed
MB
905 (if (or first (not (cdr results)))
906 (car results)
907 (car (sort results 'file-newer-than-file-p)))))
eec82323 908
534aa266
DL
909(eval-when-compile
910 (defvar ange-ftp-path-format)
911 (defvar efs-path-regexp))
eec82323
LMI
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
6748645f
LMI
921(defvar nnheader-file-coding-system 'raw-text
922 "Coding system used in file backends of Gnus.")
923
eec82323
LMI
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.
926A buffer may be modified in several ways after reading into the buffer due
927to advanced Emacs features, such as file-name-handlers, format decoding,
928find-file-hooks, etc.
929 This function ensures that none of these modifications will take place."
16409b0b
GM
930 (let ((coding-system-for-read nnheader-file-coding-system))
931 (mm-insert-file-contents filename visit beg end replace)))
eec82323 932
23f87bed
MB
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
eec82323
LMI
953(defun nnheader-find-file-noselect (&rest args)
954 (let ((format-alist nil)
16409b0b 955 (auto-mode-alist (mm-auto-mode-alist))
eec82323
LMI
956 (default-major-mode 'fundamental-mode)
957 (enable-local-variables nil)
23f87bed 958 (after-insert-file-functions nil)
16409b0b 959 (enable-local-eval nil)
6748645f 960 (find-file-hooks nil)
e0b8fdf5 961 (coding-system-for-read nnheader-file-coding-system))
eec82323
LMI
962 (apply 'find-file-noselect args)))
963
eec82323
LMI
964(defun nnheader-directory-regular-files (dir)
965 "Return a list of all regular files in DIR."
966 (let ((files (directory-files dir t))
967 out)
968 (while files
969 (when (file-regular-p (car files))
970 (push (car files) out))
971 (pop files))
972 (nreverse out)))
973
6748645f
LMI
974(defun nnheader-directory-files (&rest args)
975 "Same as `directory-files', but prune \".\" and \"..\"."
976 (let ((files (apply 'directory-files args))
977 out)
978 (while files
979 (unless (member (file-name-nondirectory (car files)) '("." ".."))
980 (push (car files) out))
981 (pop files))
982 (nreverse out)))
983
eec82323
LMI
984(defmacro nnheader-skeleton-replace (from &optional to regexp)
985 `(let ((new (generate-new-buffer " *nnheader replace*"))
986 (cur (current-buffer))
987 (start (point-min)))
eec82323
LMI
988 (set-buffer cur)
989 (goto-char (point-min))
990 (while (,(if regexp 're-search-forward 'search-forward)
991 ,from nil t)
992 (insert-buffer-substring
993 cur start (prog1 (match-beginning 0) (set-buffer new)))
994 (goto-char (point-max))
995 ,(when to `(insert ,to))
996 (set-buffer cur)
997 (setq start (point)))
998 (insert-buffer-substring
999 cur start (prog1 (point-max) (set-buffer new)))
1000 (copy-to-buffer cur (point-min) (point-max))
1001 (kill-buffer (current-buffer))
1002 (set-buffer cur)))
1003
1004(defun nnheader-replace-string (from to)
16409b0b 1005 "Do a fast replacement of FROM to TO from point to `point-max'."
eec82323
LMI
1006 (nnheader-skeleton-replace from to))
1007
1008(defun nnheader-replace-regexp (from to)
16409b0b 1009 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
eec82323
LMI
1010 (nnheader-skeleton-replace from to t))
1011
1012(defun nnheader-strip-cr ()
1013 "Strip all \r's from the current buffer."
1014 (nnheader-skeleton-replace "\r"))
1015
16409b0b
GM
1016(defalias 'nnheader-run-at-time 'run-at-time)
1017(defalias 'nnheader-cancel-timer 'cancel-timer)
1018(defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
23f87bed
MB
1019(defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1020
1021(defun nnheader-accept-process-output (process)
1022 (accept-process-output
1023 process
1024 (truncate nnheader-read-timeout)
1025 (truncate (* (- nnheader-read-timeout
1026 (truncate nnheader-read-timeout))
1027 1000))))
eec82323 1028
df359f6c 1029(when (featurep 'xemacs)
eec82323
LMI
1030 (require 'nnheaderxm))
1031
1032(run-hooks 'nnheader-load-hook)
1033
1034(provide 'nnheader)
1035
ab5796a9 1036;;; arch-tag: a9c4b7d9-52ae-4ec9-b196-dfd93124d202
eec82323 1037;;; nnheader.el ends here