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