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 Free Software Foundation, Inc.
7 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
32 (eval-when-compile (require 'cl))
34 (defvar nnmail-extra-headers)
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))
44 (autoload 'gnus-sorted-intersection "gnus-range")
45 (autoload 'gnus-intersection "gnus-range")
46 (autoload 'gnus-sorted-complement "gnus-range")
47 (autoload 'gnus-sorted-difference "gnus-range"))
49 (defcustom gnus-verbose-backends 7
50 "Integer that says how verbose the Gnus backends should be.
51 The higher the number, the more messages the Gnus backends will flash
52 to say what it's doing. At zero, the Gnus backends will be totally
53 mute; at five, they will display most important messages; and at ten,
54 they will keep on jabbering all the time."
58 (defcustom gnus-nov-is-evil nil
59 "If non-nil, Gnus backends will never output headers in the NOV format."
63 (defvar nnheader-max-head-length 8192
64 "*Max length of the head of articles.
66 Value is an integer, nil, or t. nil means read in chunks of a file
67 indefinitely until a complete head is found\; t means always read the
68 entire file immediately, disregarding `nnheader-head-chop-length'.
70 Integer values will in effect be rounded up to the nearest multiple of
71 `nnheader-head-chop-length'.")
73 (defvar nnheader-head-chop-length 2048
74 "*Length of each read operation when trying to fetch HEAD headers.")
76 (defvar nnheader-read-timeout
77 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
78 (symbol-name system-type))
79 ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
81 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
83 ;; There should probably be a runtime test to determine the timing
84 ;; resolution, or a primitive to report it. I don't know off-hand
85 ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
86 ;; could round up non-zero timeouts to a minimum of 1.0?
89 "How long nntp should wait between checking for the end of output.
90 Shorter values mean quicker response, but are more CPU intensive.")
92 (defvar nnheader-file-name-translation-alist
93 (let ((case-fold-search t))
95 ((string-match "windows-nt\\|os/2\\|emx\\|cygwin"
96 (symbol-name system-type))
97 (append (mapcar (lambda (c) (cons c ?_))
98 '(?: ?* ?\" ?< ?> ??))
99 (if (string-match "windows-nt\\|cygwin"
100 (symbol-name system-type))
104 "*Alist that says how to translate characters in file names.
105 For instance, if \":\" is invalid as a file character in file names
106 on your system, you could say something like:
108 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
110 (defvar nnheader-directory-separator-character
111 (string-to-char (substring (file-name-as-directory ".") -1))
112 "*A character used to a directory separator.")
115 (autoload 'nnmail-message-id "nnmail")
116 (autoload 'mail-position-on-field "sendmail")
117 (autoload 'message-remove-header "message")
118 (autoload 'gnus-buffer-live-p "gnus-util"))
120 ;;; Header access macros.
122 ;; These macros may look very much like the ones in GNUS 4.1. They
123 ;; are, in a way, but you should note that the indices they use have
124 ;; been changed from the internal GNUS format to the NOV format. The
125 ;; makes it possible to read headers from XOVER much faster.
127 ;; The format of a header is now:
128 ;; [number subject from date id references chars lines xref extra]
130 ;; (That next-to-last entry is defined as "misc" in the NOV format,
131 ;; but Gnus uses it for xrefs.)
133 (defmacro mail-header-number (header)
134 "Return article number in HEADER."
137 (defmacro mail-header-set-number (header number)
138 "Set article number of HEADER to NUMBER."
139 `(aset ,header 0 ,number))
141 (defmacro mail-header-subject (header)
142 "Return subject string in HEADER."
145 (defmacro mail-header-set-subject (header subject)
146 "Set article subject of HEADER to SUBJECT."
147 `(aset ,header 1 ,subject))
149 (defmacro mail-header-from (header)
150 "Return author string in HEADER."
153 (defmacro mail-header-set-from (header from)
154 "Set article author of HEADER to FROM."
155 `(aset ,header 2 ,from))
157 (defmacro mail-header-date (header)
158 "Return date in HEADER."
161 (defmacro mail-header-set-date (header date)
162 "Set article date of HEADER to DATE."
163 `(aset ,header 3 ,date))
165 (defalias 'mail-header-message-id 'mail-header-id)
166 (defmacro mail-header-id (header)
167 "Return Id in HEADER."
170 (defalias 'mail-header-set-message-id 'mail-header-set-id)
171 (defmacro mail-header-set-id (header id)
172 "Set article Id of HEADER to ID."
173 `(aset ,header 4 ,id))
175 (defmacro mail-header-references (header)
176 "Return references in HEADER."
179 (defmacro mail-header-set-references (header ref)
180 "Set article references of HEADER to REF."
181 `(aset ,header 5 ,ref))
183 (defmacro mail-header-chars (header)
184 "Return number of chars of article in HEADER."
187 (defmacro mail-header-set-chars (header chars)
188 "Set number of chars in article of HEADER to CHARS."
189 `(aset ,header 6 ,chars))
191 (defmacro mail-header-lines (header)
192 "Return lines in HEADER."
195 (defmacro mail-header-set-lines (header lines)
196 "Set article lines of HEADER to LINES."
197 `(aset ,header 7 ,lines))
199 (defmacro mail-header-xref (header)
200 "Return xref string in HEADER."
203 (defmacro mail-header-set-xref (header xref)
204 "Set article XREF of HEADER to xref."
205 `(aset ,header 8 ,xref))
207 (defmacro mail-header-extra (header)
208 "Return the extra headers in HEADER."
211 (defun mail-header-set-extra (header extra)
212 "Set the extra headers in HEADER to EXTRA."
213 (aset header 9 extra))
215 (defsubst make-mail-header (&optional init)
216 "Create a new mail header structure initialized with INIT."
217 (make-vector 10 init))
219 (defsubst make-full-mail-header (&optional number subject from date id
220 references chars lines xref
222 "Create a new mail header structure initialized with the parameters given."
223 (vector number subject from date id references chars lines xref extra))
225 ;; fake message-ids: generation and detection
227 (defvar nnheader-fake-message-id 1)
229 (defsubst nnheader-generate-fake-message-id (&optional number)
231 (format "fake+none+%s+%d" gnus-newsgroup-name number)
232 (format "fake+none+%s+%s"
234 (int-to-string (incf nnheader-fake-message-id)))))
236 (defsubst nnheader-fake-message-id-p (id)
237 (save-match-data ; regular message-id's are <.*>
238 (string-match "\\`fake\\+none\\+.*\\+[0-9]+\\'" id)))
240 ;; Parsing headers and NOV lines.
242 (defsubst nnheader-remove-cr-followed-by-lf ()
243 (goto-char (point-max))
244 (while (search-backward "\r\n" nil t)
247 (defsubst nnheader-header-value ()
248 (skip-chars-forward " \t")
249 (buffer-substring (point) (point-at-eol)))
251 (defun nnheader-parse-naked-head (&optional number)
252 ;; This function unfolds continuation lines in this buffer
253 ;; destructively. When this side effect is unwanted, use
254 ;; `nnheader-parse-head' instead of this function.
255 (let ((case-fold-search t)
256 (buffer-read-only nil)
257 (cur (current-buffer))
259 in-reply-to lines ref)
260 (nnheader-remove-cr-followed-by-lf)
261 (ietf-drums-unfold-fws)
262 (subst-char-in-region (point-min) (point-max) ?\t ? )
266 ;; This implementation of this function, with nine
267 ;; search-forwards instead of the one re-search-forward and a
268 ;; case (which basically was the old function) is actually
269 ;; about twice as fast, even though it looks messier. You
270 ;; can't have everything, I guess. Speed and elegance don't
271 ;; always go hand in hand.
278 (if (search-forward "\nsubject:" nil t)
279 (nnheader-header-value) "(none)"))
283 (if (search-forward "\nfrom:" nil t)
284 (nnheader-header-value) "(nobody)"))
288 (if (search-forward "\ndate:" nil t)
289 (nnheader-header-value) ""))
293 (if (search-forward "\nmessage-id:" nil t)
295 (1- (or (search-forward "<" (point-at-eol) t)
297 (or (search-forward ">" (point-at-eol) t) (point)))
298 ;; If there was no message-id, we just fake one to make
299 ;; subsequent routines simpler.
300 (nnheader-generate-fake-message-id number)))
304 (if (search-forward "\nreferences:" nil t)
305 (nnheader-header-value)
306 ;; Get the references from the in-reply-to header if
307 ;; there were no references and the in-reply-to header
309 (if (and (search-forward "\nin-reply-to:" nil t)
310 (setq in-reply-to (nnheader-header-value))
311 (string-match "<[^\n>]+>" in-reply-to))
313 (setq ref (substring in-reply-to (match-beginning 0)
315 (while (string-match "<[^\n>]+>"
316 in-reply-to (match-end 0))
317 (setq ref2 (substring in-reply-to (match-beginning 0)
319 (when (> (length ref2) (length ref))
328 (if (search-forward "\nlines: " nil t)
329 (if (numberp (setq lines (read cur)))
335 (and (search-forward "\nxref:" nil t)
336 (nnheader-header-value)))
338 (when nnmail-extra-headers
339 (let ((extra nnmail-extra-headers)
343 (when (search-forward
344 (concat "\n" (symbol-name (car extra)) ":") nil t)
345 (push (cons (car extra) (nnheader-header-value))
352 (defun nnheader-parse-head (&optional naked)
353 (let ((cur (current-buffer)) num beg end)
358 (goto-char (point-min))
359 ;; Search to the beginning of the next header. Error
360 ;; messages do not begin with 2 or 3.
361 (when (re-search-forward "^[23][0-9]+ " nil t)
365 end (if (search-forward "\n.\n" nil t)
369 (insert-buffer-substring cur beg end)
370 (nnheader-parse-naked-head num)))))
372 (defmacro nnheader-nov-skip-field ()
373 '(search-forward "\t" eol 'move))
375 (defmacro nnheader-nov-field ()
376 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
378 (defmacro nnheader-nov-read-integer ()
380 (if (eq (char-after) ?\t)
382 (let ((num (condition-case nil
383 (read (current-buffer))
385 (if (numberp num) num 0)))
386 (or (eobp) (forward-char 1))))
388 (defmacro nnheader-nov-parse-extra ()
390 (while (not (memq (char-after) '(?\n nil)))
391 (setq string (nnheader-nov-field))
392 (when (string-match "^\\([^ :]+\\): " string)
393 (push (cons (intern (match-string 1 string))
394 (substring string (match-end 0)))
398 (defvar nnheader-uniquify-message-id nil)
400 (defmacro nnheader-nov-read-message-id (&optional number)
401 `(let ((id (nnheader-nov-field)))
402 (if (string-match "^<[^>]+>$" id)
403 ,(if nnheader-uniquify-message-id
404 `(if (string-match "__[^@]+@" id)
405 (concat (substring id 0 (match-beginning 0))
406 (substring id (1- (match-end 0))))
409 (nnheader-generate-fake-message-id ,number))))
411 (defun nnheader-parse-nov ()
412 (let ((eol (point-at-eol))
413 (number (nnheader-nov-read-integer)))
416 (nnheader-nov-field) ; subject
417 (nnheader-nov-field) ; from
418 (nnheader-nov-field) ; date
419 (nnheader-nov-read-message-id number) ; id
420 (nnheader-nov-field) ; refs
421 (nnheader-nov-read-integer) ; chars
422 (nnheader-nov-read-integer) ; lines
423 (if (eq (char-after) ?\n)
425 (if (looking-at "Xref: ")
426 (goto-char (match-end 0)))
427 (nnheader-nov-field)) ; Xref
428 (nnheader-nov-parse-extra)))) ; extra
430 (defun nnheader-insert-nov (header)
431 (princ (mail-header-number header) (current-buffer))
435 (or (mail-header-subject header) "(none)") "\t"
436 (or (mail-header-from header) "(nobody)") "\t"
437 (or (mail-header-date header) "") "\t"
438 (or (mail-header-id header)
441 (or (mail-header-references header) "") "\t")
442 (princ (or (mail-header-chars header) 0) (current-buffer))
444 (princ (or (mail-header-lines header) 0) (current-buffer))
446 (when (mail-header-xref header)
447 (insert "Xref: " (mail-header-xref header)))
448 (when (or (mail-header-xref header)
449 (mail-header-extra header))
451 (when (mail-header-extra header)
452 (let ((extra (mail-header-extra header)))
454 (insert (symbol-name (caar extra))
455 ": " (cdar extra) "\t")
459 (while (search-backward "\n" p t)
463 (defun nnheader-parse-overview-file (file)
464 "Parse FILE and return a list of headers."
465 (mm-with-unibyte-buffer
466 (nnheader-insert-file-contents file)
467 (goto-char (point-min))
470 (push (nnheader-parse-nov) headers)
472 (nreverse headers))))
474 (defun nnheader-write-overview-file (file headers)
475 "Write HEADERS to FILE."
477 (mapcar 'nnheader-insert-nov headers)))
479 (defun nnheader-insert-header (header)
481 "Subject: " (or (mail-header-subject header) "(none)") "\n"
482 "From: " (or (mail-header-from header) "(nobody)") "\n"
483 "Date: " (or (mail-header-date header) "") "\n"
484 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
485 "References: " (or (mail-header-references header) "") "\n"
487 (princ (or (mail-header-lines header) 0) (current-buffer))
490 (defun nnheader-insert-article-line (article)
491 (goto-char (point-min))
493 (princ article (current-buffer))
494 (insert " Article retrieved.\n")
495 (search-forward "\n\n" nil 'move)
496 (delete-region (point) (point-max))
500 (defun nnheader-nov-delete-outside-range (beg end)
501 "Delete all NOV lines that lie outside the BEG to END range."
502 ;; First we find the first wanted line.
503 (nnheader-find-nov-line beg)
504 (delete-region (point-min) (point))
505 ;; Then we find the last wanted line.
506 (when (nnheader-find-nov-line end)
508 (delete-region (point) (point-max)))
510 (defun nnheader-find-nov-line (article)
511 "Put point at the NOV line that start with ARTICLE.
512 If ARTICLE doesn't exist, put point where that line
513 would have been. The function will return non-nil if
514 the line could be found."
515 ;; This function basically does a binary search.
516 (let ((max (point-max))
517 (min (goto-char (point-min)))
518 (cur (current-buffer))
522 (goto-char (+ min (/ (- max min) 2)))
524 (if (or (= (point) prev)
528 (while (and (not (numberp (setq num (read cur))))
531 (cond ((> num article)
536 (setq found 'yes)))))
537 ;; We may be at the first line.
540 (setq num (read cur)))
541 ;; Now we may have found the article we're looking for, or we
542 ;; may be somewhere near it.
543 (when (and (not (eq found 'yes))
544 (not (eq num article)))
546 (while (and (< (point) max)
547 (or (not (numberp num))
552 (= (setq num (read cur)) article)))
553 (unless (eq num article)
558 ;; Various cruft the backends and Gnus need to communicate.
560 (defvar nntp-server-buffer nil)
561 (defvar nntp-process-response nil)
562 (defvar news-reply-yank-from nil)
563 (defvar news-reply-yank-message-id nil)
565 (defvar nnheader-callback-function nil)
567 (defun nnheader-init-server-buffer ()
568 "Initialize the Gnus-backend communication buffer."
570 (unless (gnus-buffer-live-p nntp-server-buffer)
571 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
572 (set-buffer nntp-server-buffer)
573 (mm-enable-multibyte)
575 (kill-all-local-variables)
576 (setq case-fold-search t) ;Should ignore case.
577 (set (make-local-variable 'nntp-process-response) nil)
580 ;;; Various functions the backends use.
582 (defun nnheader-file-error (file)
583 "Return a string that says what is wrong with FILE."
586 ((not (file-exists-p file))
588 ((file-directory-p file)
590 ((not (file-readable-p file))
591 "%s is not readable"))
594 (defun nnheader-insert-head (file)
595 "Insert the head of the article."
596 (when (file-exists-p file)
597 (if (eq nnheader-max-head-length t)
598 ;; Just read the entire file.
599 (nnheader-insert-file-contents file)
600 ;; Read 1K blocks until we find a separator.
603 (while (and (eq nnheader-head-chop-length
604 (nth 1 (nnheader-insert-file-contents
606 (incf beg nnheader-head-chop-length))))
607 (prog1 (not (search-forward "\n\n" nil t))
608 (goto-char (point-max)))
609 (or (null nnheader-max-head-length)
610 (< beg nnheader-max-head-length))))))
613 (defun nnheader-article-p ()
614 "Say whether the current buffer looks like an article."
615 (goto-char (point-min))
616 (if (not (search-forward "\n\n" nil t))
618 (narrow-to-region (point-min) (1- (point)))
619 (goto-char (point-min))
620 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
621 (goto-char (match-end 0)))
626 (defun nnheader-insert-references (references message-id)
627 "Insert a References header based on REFERENCES and MESSAGE-ID."
628 (if (and (not references) (not message-id))
629 ;; This is invalid, but not all articles have Message-IDs.
631 (mail-position-on-field "References")
632 (let ((begin (point-at-bol))
637 (when (and references message-id)
641 ;; Fold long References lines to conform to RFC1036 (sort of).
642 ;; The region must end with a newline to fill the region
643 ;; without inserting extra newline.
644 (fill-region-as-paragraph begin (1+ (point))))))
646 (defun nnheader-replace-header (header new-value)
647 "Remove HEADER and insert the NEW-VALUE."
650 (nnheader-narrow-to-headers)
652 (message-remove-header header)
653 (goto-char (point-max))
654 (insert header ": " new-value "\n")))))
656 (defun nnheader-narrow-to-headers ()
657 "Narrow to the head of an article."
660 (goto-char (point-min))
661 (if (search-forward "\n\n" nil t)
664 (goto-char (point-min)))
666 (defun nnheader-get-lines-and-char ()
667 "Return the number of lines and chars in the article body."
668 (goto-char (point-min))
669 (if (not (re-search-forward "\n\r?\n" nil t))
671 (list (count-lines (point) (point-max))
672 (- (point-max) (point)))))
674 (defun nnheader-remove-body ()
675 "Remove the body from an article in this current buffer."
676 (goto-char (point-min))
677 (when (re-search-forward "\n\r?\n" nil t)
678 (delete-region (point) (point-max))))
680 (defun nnheader-set-temp-buffer (name &optional noerase)
681 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
682 (set-buffer (get-buffer-create name))
683 (buffer-disable-undo)
688 (eval-when-compile (defvar jka-compr-compression-info-list))
689 (defvar nnheader-numerical-files
690 (if (boundp 'jka-compr-compression-info-list)
691 (concat "\\([0-9]+\\)\\("
692 (mapconcat (lambda (i) (aref i 0))
693 jka-compr-compression-info-list "\\|")
696 "Regexp that match numerical files.")
698 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
699 "Regexp that matches numerical file names.")
701 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
702 "Regexp that matches numerical full file names.")
704 (defsubst nnheader-file-to-number (file)
705 "Take a FILE name and return the article number."
706 (if (string= nnheader-numerical-short-files "^[0-9]+$")
707 (string-to-number file)
708 (string-match nnheader-numerical-short-files file)
709 (string-to-number (match-string 0 file))))
711 (defvar nnheader-directory-files-is-safe
712 (or (eq system-type 'windows-nt)
713 (not (featurep 'xemacs)))
714 "If non-nil, Gnus believes `directory-files' is safe.
715 It has been reported numerous times that `directory-files' fails with
716 an alarming frequency on NFS mounted file systems. If it is nil,
717 `nnheader-directory-files-safe' is used.")
719 (defun nnheader-directory-files-safe (&rest args)
720 "Execute `directory-files' twice and returns the longer result."
721 (let ((first (apply 'directory-files args))
722 (second (apply 'directory-files args)))
723 (if (> (length first) (length second))
727 (defun nnheader-directory-articles (dir)
728 "Return a list of all article files in directory DIR."
729 (mapcar 'nnheader-file-to-number
730 (if nnheader-directory-files-is-safe
732 dir nil nnheader-numerical-short-files t)
733 (nnheader-directory-files-safe
734 dir nil nnheader-numerical-short-files t))))
736 (defun nnheader-article-to-file-alist (dir)
737 "Return an alist of article/file pairs in DIR."
738 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
739 (if nnheader-directory-files-is-safe
741 dir nil nnheader-numerical-short-files t)
742 (nnheader-directory-files-safe
743 dir nil nnheader-numerical-short-files t))))
745 (defun nnheader-fold-continuation-lines ()
746 "Fold continuation lines in the current buffer."
747 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
749 (defun nnheader-translate-file-chars (file &optional full)
750 "Translate FILE into something that can be a file name.
751 If FULL, translate everything."
752 (if (null nnheader-file-name-translation-alist)
753 ;; No translation is necessary.
758 ;; Do complete translation.
759 (setq leaf (copy-sequence file)
761 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
763 ;; We translate -- but only the file name. We leave the directory
765 (if (and (featurep 'xemacs)
766 (memq system-type '(cygwin32 win32 w32 mswindows windows-nt
768 ;; This is needed on NT and stuff, because
769 ;; file-name-nondirectory is not enough to split
770 ;; file names, containing ':', e.g.
771 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
773 ;; we are trying to correctly split such names:
774 ;; "d:file.name" -> "a:" "file.name"
775 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
776 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
778 ;; to translate then only the file name part.
782 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
783 (setq leaf (substring file (match-beginning 2))
784 path (substring file 0 (match-beginning 2)))))
785 ;; Emacs DTRT, says andrewi.
786 (setq leaf (file-name-nondirectory file)
787 path (file-name-directory file))))
788 (setq len (length leaf))
790 (when (setq trans (cdr (assq (aref leaf i)
791 nnheader-file-name-translation-alist)))
794 (concat path leaf))))
796 (defun nnheader-report (backend &rest args)
797 "Report an error from the BACKEND.
798 The first string in ARGS can be a format string."
799 (set (intern (format "%s-status-string" backend))
800 (if (< (length args) 2)
802 (apply 'format args)))
805 (defun nnheader-get-report (backend)
806 "Get the most recent report from BACKEND."
808 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
810 (error (nnheader-message 5 ""))))
812 (defun nnheader-insert (format &rest args)
813 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
814 If FORMAT isn't a format string, it and all ARGS will be inserted
817 (set-buffer nntp-server-buffer)
819 (if (string-match "%" format)
820 (insert (apply 'format format args))
821 (apply 'insert format args))
824 (defsubst nnheader-replace-chars-in-string (string from to)
825 (mm-subst-char-in-string from to string))
827 (defun nnheader-replace-duplicate-chars-in-string (string from to)
828 "Replace characters in STRING from FROM to TO."
829 (let ((string (substring string 0)) ;Copy string.
830 (len (length string))
832 ;; Replace all occurrences of FROM with TO.
834 (setq i (aref string idx))
835 (when (and (eq prev from) (= i from))
836 (aset string (1- idx) to)
837 (aset string idx to))
842 (defun nnheader-file-to-group (file &optional top)
843 "Return a group name based on FILE and TOP."
844 (nnheader-replace-chars-in-string
848 (substring (expand-file-name file)
851 (file-name-as-directory top))))
853 nnheader-directory-separator-character ?.))
855 (defun nnheader-message (level &rest args)
856 "Message if the Gnus backends are talkative."
857 (if (or (not (numberp gnus-verbose-backends))
858 (<= level gnus-verbose-backends))
859 (apply 'message args)
860 (apply 'format args)))
862 (defun nnheader-be-verbose (level)
863 "Return whether the backends should be verbose on LEVEL."
864 (or (not (numberp gnus-verbose-backends))
865 (<= level gnus-verbose-backends)))
867 (defvar nnheader-pathname-coding-system 'iso-8859-1
868 "*Coding system for file name.")
870 (defun nnheader-group-pathname (group dir &optional file)
871 "Make file name for GROUP."
873 (let ((dir (file-name-as-directory (expand-file-name dir))))
874 ;; If this directory exists, we use it directly.
875 (file-name-as-directory
876 (if (file-directory-p (concat dir group))
877 (expand-file-name group dir)
878 ;; If not, we translate dots into slashes.
879 (expand-file-name (mm-encode-coding-string
880 (nnheader-replace-chars-in-string group ?. ?/)
881 nnheader-pathname-coding-system)
883 (cond ((null file) "")
884 ((numberp file) (int-to-string file))
887 (defun nnheader-concat (dir &rest files)
888 "Concat DIR as directory to FILES."
889 (apply 'concat (file-name-as-directory dir) files))
891 (defun nnheader-ms-strip-cr ()
892 "Strip ^M from the end of all lines."
894 (nnheader-remove-cr-followed-by-lf)))
896 (defun nnheader-file-size (file)
897 "Return the file size of FILE or 0."
898 (or (nth 7 (file-attributes file)) 0))
900 (defun nnheader-find-etc-directory (package &optional file first)
901 "Go through `load-path' and find the \"../etc/PACKAGE\" directory.
902 This function will look in the parent directory of each `load-path'
903 entry, and look for the \"etc\" directory there.
904 If FILE, find the \".../etc/PACKAGE\" file instead.
905 If FIRST is non-nil, return the directory or the file found at the
906 first. Otherwise, find the newest one, though it may take a time."
907 (let ((path load-path)
909 ;; We try to find the dir by looking at the load path,
910 ;; stripping away the last component and adding "etc/".
916 (directory-file-name (car path)))
919 (or file (file-directory-p dir)))
921 (or (member dir results)
923 (setq path (if first nil (cdr path))))
924 (setq path (cdr path))))
925 (if (or first (not (cdr results)))
927 (car (sort results 'file-newer-than-file-p)))))
930 (defvar ange-ftp-path-format)
931 (defvar efs-path-regexp))
932 (defun nnheader-re-read-dir (path)
933 "Re-read directory PATH if PATH is on a remote system."
934 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
935 (when (string-match efs-path-regexp path)
936 (efs-re-read-dir path))
937 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
938 (when (string-match (car ange-ftp-path-format) path)
939 (ange-ftp-re-read-dir path)))))
941 (defvar nnheader-file-coding-system 'raw-text
942 "Coding system used in file backends of Gnus.")
944 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
945 "Like `insert-file-contents', q.v., but only reads in the file.
946 A buffer may be modified in several ways after reading into the buffer due
947 to advanced Emacs features, such as file-name-handlers, format decoding,
948 find-file-hooks, etc.
949 This function ensures that none of these modifications will take place."
950 (let ((coding-system-for-read nnheader-file-coding-system))
951 (mm-insert-file-contents filename visit beg end replace)))
953 (defun nnheader-insert-nov-file (file first)
954 (let ((size (nth 7 (file-attributes file)))
955 (cutoff (* 32 1024)))
958 ;; If the file is small, we just load it.
959 (nnheader-insert-file-contents file)
960 ;; We start on the assumption that FIRST is pretty recent. If
961 ;; not, we just insert the rest of the file as well.
963 (nnheader-insert-file-contents file nil (- size cutoff) size)
964 (goto-char (point-min))
965 (delete-region (point) (or (search-forward "\n" nil 'move) (point)))
966 (setq current (ignore-errors (read (current-buffer))))
967 (if (and (numberp current)
970 (delete-region (point-min) (point-max))
971 (nnheader-insert-file-contents file)))))))
973 (defun nnheader-find-file-noselect (&rest args)
974 "Open a file with some variables bound.
975 See `find-file-noselect' for the arguments."
976 (let* ((format-alist nil)
977 (auto-mode-alist (mm-auto-mode-alist))
978 (default-major-mode 'fundamental-mode)
979 (enable-local-variables nil)
980 (after-insert-file-functions nil)
981 (enable-local-eval nil)
982 (coding-system-for-read nnheader-file-coding-system)
983 (ffh (if (boundp 'find-file-hook)
986 (val (symbol-value ffh)))
989 (apply 'find-file-noselect args)
992 (defun nnheader-directory-regular-files (dir)
993 "Return a list of all regular files in DIR."
994 (let ((files (directory-files dir t))
997 (when (file-regular-p (car files))
998 (push (car files) out))
1002 (defun nnheader-directory-files (&rest args)
1003 "Same as `directory-files', but prune \".\" and \"..\"."
1004 (let ((files (apply 'directory-files args))
1007 (unless (member (file-name-nondirectory (car files)) '("." ".."))
1008 (push (car files) out))
1012 (defmacro nnheader-skeleton-replace (from &optional to regexp)
1013 `(let ((new (generate-new-buffer " *nnheader replace*"))
1014 (cur (current-buffer))
1015 (start (point-min)))
1017 (goto-char (point-min))
1018 (while (,(if regexp 're-search-forward 'search-forward)
1020 (insert-buffer-substring
1021 cur start (prog1 (match-beginning 0) (set-buffer new)))
1022 (goto-char (point-max))
1023 ,(when to `(insert ,to))
1025 (setq start (point)))
1026 (insert-buffer-substring
1027 cur start (prog1 (point-max) (set-buffer new)))
1028 (copy-to-buffer cur (point-min) (point-max))
1029 (kill-buffer (current-buffer))
1032 (defun nnheader-replace-string (from to)
1033 "Do a fast replacement of FROM to TO from point to `point-max'."
1034 (nnheader-skeleton-replace from to))
1036 (defun nnheader-replace-regexp (from to)
1037 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1038 (nnheader-skeleton-replace from to t))
1040 (defun nnheader-strip-cr ()
1041 "Strip all \r's from the current buffer."
1042 (nnheader-skeleton-replace "\r"))
1044 (defalias 'nnheader-cancel-timer 'cancel-timer)
1045 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1046 (defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1048 (defun nnheader-accept-process-output (process)
1049 (accept-process-output
1051 (truncate nnheader-read-timeout)
1052 (truncate (* (- nnheader-read-timeout
1053 (truncate nnheader-read-timeout))
1056 (when (featurep 'xemacs)
1057 (require 'nnheaderxm))
1059 (run-hooks 'nnheader-load-hook)
1063 ;;; arch-tag: a9c4b7d9-52ae-4ec9-b196-dfd93124d202
1064 ;;; nnheader.el ends here