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