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