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