1 ;;; nnheader.el --- header access macros for Gnus and its backends
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1993, 1994, 1995, 1996,
4 ;; 1997, 1998, 2000, 2001, 2002, 2003
5 ;; 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., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
34 ;; Requiring `gnus-util' at compile time creates a circular
35 ;; dependency between nnheader.el and gnus-util.el.
36 ;;(eval-when-compile (require 'gnus-util))
42 (autoload 'gnus-sorted-intersection "gnus-range")
43 (autoload 'gnus-intersection "gnus-range")
44 (autoload 'gnus-sorted-complement "gnus-range")
45 (autoload 'gnus-sorted-difference "gnus-range"))
47 (defcustom gnus-verbose-backends 7
48 "Integer that says how verbose the Gnus backends should be.
49 The higher the number, the more messages the Gnus backends will flash
50 to say what it's doing. At zero, the Gnus backends will be totally
51 mute; at five, they will display most important messages; and at ten,
52 they will keep on jabbering all the time."
56 (defcustom gnus-nov-is-evil nil
57 "If non-nil, Gnus backends will never output headers in the NOV format."
61 (defvar nnheader-max-head-length 4096
62 "*Max length of the head of articles.
64 Value is an integer, nil, or t. nil means read in chunks of a file
65 indefinitely until a complete head is found\; t means always read the
66 entire file immediately, disregarding `nnheader-head-chop-length'.
68 Integer values will in effect be rounded up to the nearest multiple of
69 `nnheader-head-chop-length'.")
71 (defvar nnheader-head-chop-length 2048
72 "*Length of each read operation when trying to fetch HEAD headers.")
74 (defvar nnheader-read-timeout
75 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
76 (symbol-name system-type))
79 "How long nntp should wait between checking for the end of output.
80 Shorter values mean quicker response, but are more CPU intensive.")
82 (defvar nnheader-file-name-translation-alist
83 (let ((case-fold-search t))
85 ((string-match "windows-nt\\|os/2\\|emx\\|cygwin"
86 (symbol-name system-type))
87 (append (mapcar (lambda (c) (cons c ?_))
88 '(?: ?* ?\" ?< ?> ??))
89 (if (string-match "windows-nt\\|cygwin"
90 (symbol-name system-type))
94 "*Alist that says how to translate characters in file names.
95 For instance, if \":\" is invalid as a file character in file names
96 on your system, you could say something like:
98 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
100 (defvar nnheader-directory-separator-character
101 (string-to-char (substring (file-name-as-directory ".") -1))
102 "*A character used to a directory separator.")
105 (autoload 'nnmail-message-id "nnmail")
106 (autoload 'mail-position-on-field "sendmail")
107 (autoload 'message-remove-header "message")
108 (autoload 'gnus-buffer-live-p "gnus-util"))
110 ;;; Header access macros.
112 ;; These macros may look very much like the ones in GNUS 4.1. They
113 ;; are, in a way, but you should note that the indices they use have
114 ;; been changed from the internal GNUS format to the NOV format. The
115 ;; makes it possible to read headers from XOVER much faster.
117 ;; The format of a header is now:
118 ;; [number subject from date id references chars lines xref extra]
120 ;; (That next-to-last entry is defined as "misc" in the NOV format,
121 ;; but Gnus uses it for xrefs.)
123 (defmacro mail-header-number (header)
124 "Return article number in HEADER."
127 (defmacro mail-header-set-number (header number)
128 "Set article number of HEADER to NUMBER."
129 `(aset ,header 0 ,number))
131 (defmacro mail-header-subject (header)
132 "Return subject string in HEADER."
135 (defmacro mail-header-set-subject (header subject)
136 "Set article subject of HEADER to SUBJECT."
137 `(aset ,header 1 ,subject))
139 (defmacro mail-header-from (header)
140 "Return author string in HEADER."
143 (defmacro mail-header-set-from (header from)
144 "Set article author of HEADER to FROM."
145 `(aset ,header 2 ,from))
147 (defmacro mail-header-date (header)
148 "Return date in HEADER."
151 (defmacro mail-header-set-date (header date)
152 "Set article date of HEADER to DATE."
153 `(aset ,header 3 ,date))
155 (defalias 'mail-header-message-id 'mail-header-id)
156 (defmacro mail-header-id (header)
157 "Return Id in HEADER."
160 (defalias 'mail-header-set-message-id 'mail-header-set-id)
161 (defmacro mail-header-set-id (header id)
162 "Set article Id of HEADER to ID."
163 `(aset ,header 4 ,id))
165 (defmacro mail-header-references (header)
166 "Return references in HEADER."
169 (defmacro mail-header-set-references (header ref)
170 "Set article references of HEADER to REF."
171 `(aset ,header 5 ,ref))
173 (defmacro mail-header-chars (header)
174 "Return number of chars of article in HEADER."
177 (defmacro mail-header-set-chars (header chars)
178 "Set number of chars in article of HEADER to CHARS."
179 `(aset ,header 6 ,chars))
181 (defmacro mail-header-lines (header)
182 "Return lines in HEADER."
185 (defmacro mail-header-set-lines (header lines)
186 "Set article lines of HEADER to LINES."
187 `(aset ,header 7 ,lines))
189 (defmacro mail-header-xref (header)
190 "Return xref string in HEADER."
193 (defmacro mail-header-set-xref (header xref)
194 "Set article XREF of HEADER to xref."
195 `(aset ,header 8 ,xref))
197 (defmacro mail-header-extra (header)
198 "Return the extra headers in HEADER."
201 (defmacro mail-header-set-extra (header extra)
202 "Set the extra headers in HEADER to EXTRA."
203 `(aset ,header 9 ',extra))
205 (defsubst make-mail-header (&optional init)
206 "Create a new mail header structure initialized with INIT."
207 (make-vector 10 init))
209 (defsubst make-full-mail-header (&optional number subject from date id
210 references chars lines xref
212 "Create a new mail header structure initialized with the parameters given."
213 (vector number subject from date id references chars lines xref extra))
215 ;; fake message-ids: generation and detection
217 (defvar nnheader-fake-message-id 1)
219 (defsubst nnheader-generate-fake-message-id ()
220 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
222 (defsubst nnheader-fake-message-id-p (id)
223 (save-match-data ; regular message-id's are <.*>
224 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
226 ;; Parsing headers and NOV lines.
228 (defsubst nnheader-remove-cr-followed-by-lf ()
229 (goto-char (point-max))
230 (while (search-backward "\r\n" nil t)
233 (defsubst nnheader-header-value ()
234 (skip-chars-forward " \t")
235 (buffer-substring (point) (point-at-eol)))
237 (defun nnheader-parse-naked-head (&optional number)
238 ;; This function unfolds continuation lines in this buffer
239 ;; destructively. When this side effect is unwanted, use
240 ;; `nnheader-parse-head' instead of this function.
241 (let ((case-fold-search t)
242 (buffer-read-only nil)
243 (cur (current-buffer))
245 in-reply-to lines ref)
246 (nnheader-remove-cr-followed-by-lf)
247 (ietf-drums-unfold-fws)
248 (subst-char-in-region (point-min) (point-max) ?\t ? )
252 ;; This implementation of this function, with nine
253 ;; search-forwards instead of the one re-search-forward and a
254 ;; case (which basically was the old function) is actually
255 ;; about twice as fast, even though it looks messier. You
256 ;; can't have everything, I guess. Speed and elegance don't
257 ;; always go hand in hand.
264 (if (search-forward "\nsubject:" nil t)
265 (nnheader-header-value) "(none)"))
269 (if (search-forward "\nfrom:" nil t)
270 (nnheader-header-value) "(nobody)"))
274 (if (search-forward "\ndate:" nil t)
275 (nnheader-header-value) ""))
279 (if (search-forward "\nmessage-id:" nil t)
281 (1- (or (search-forward "<" (point-at-eol) t)
283 (or (search-forward ">" (point-at-eol) t) (point)))
284 ;; If there was no message-id, we just fake one to make
285 ;; subsequent routines simpler.
286 (nnheader-generate-fake-message-id)))
290 (if (search-forward "\nreferences:" nil t)
291 (nnheader-header-value)
292 ;; Get the references from the in-reply-to header if
293 ;; there were no references and the in-reply-to header
295 (if (and (search-forward "\nin-reply-to:" nil t)
296 (setq in-reply-to (nnheader-header-value))
297 (string-match "<[^\n>]+>" in-reply-to))
299 (setq ref (substring in-reply-to (match-beginning 0)
301 (while (string-match "<[^\n>]+>"
302 in-reply-to (match-end 0))
303 (setq ref2 (substring in-reply-to (match-beginning 0)
305 (when (> (length ref2) (length ref))
314 (if (search-forward "\nlines: " nil t)
315 (if (numberp (setq lines (read cur)))
321 (and (search-forward "\nxref:" nil t)
322 (nnheader-header-value)))
324 (when nnmail-extra-headers
325 (let ((extra nnmail-extra-headers)
329 (when (search-forward
330 (concat "\n" (symbol-name (car extra)) ":") nil t)
331 (push (cons (car extra) (nnheader-header-value))
338 (defun nnheader-parse-head (&optional naked)
339 (let ((cur (current-buffer)) num beg end)
344 (goto-char (point-min))
345 ;; Search to the beginning of the next header. Error
346 ;; messages do not begin with 2 or 3.
347 (when (re-search-forward "^[23][0-9]+ " nil t)
351 end (if (search-forward "\n.\n" nil t)
355 (insert-buffer-substring cur beg end)
356 (nnheader-parse-naked-head num)))))
358 (defmacro nnheader-nov-skip-field ()
359 '(search-forward "\t" eol 'move))
361 (defmacro nnheader-nov-field ()
362 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
364 (defmacro nnheader-nov-read-integer ()
366 (if (eq (char-after) ?\t)
368 (let ((num (condition-case nil
369 (read (current-buffer))
371 (if (numberp num) num 0)))
372 (or (eobp) (forward-char 1))))
374 (defmacro nnheader-nov-parse-extra ()
376 (while (not (memq (char-after) '(?\n nil)))
377 (setq string (nnheader-nov-field))
378 (when (string-match "^\\([^ :]+\\): " string)
379 (push (cons (intern (match-string 1 string))
380 (substring string (match-end 0)))
384 (defmacro nnheader-nov-read-message-id ()
385 '(let ((id (nnheader-nov-field)))
386 (if (string-match "^<[^>]+>$" id)
388 (nnheader-generate-fake-message-id))))
390 (defun nnheader-parse-nov ()
391 (let ((eol (point-at-eol)))
393 (nnheader-nov-read-integer) ; number
394 (nnheader-nov-field) ; subject
395 (nnheader-nov-field) ; from
396 (nnheader-nov-field) ; date
397 (nnheader-nov-read-message-id) ; id
398 (nnheader-nov-field) ; refs
399 (nnheader-nov-read-integer) ; chars
400 (nnheader-nov-read-integer) ; lines
401 (if (eq (char-after) ?\n)
403 (if (looking-at "Xref: ")
404 (goto-char (match-end 0)))
405 (nnheader-nov-field)) ; Xref
406 (nnheader-nov-parse-extra)))) ; extra
408 (defun nnheader-insert-nov (header)
409 (princ (mail-header-number header) (current-buffer))
413 (or (mail-header-subject header) "(none)") "\t"
414 (or (mail-header-from header) "(nobody)") "\t"
415 (or (mail-header-date header) "") "\t"
416 (or (mail-header-id header)
419 (or (mail-header-references header) "") "\t")
420 (princ (or (mail-header-chars header) 0) (current-buffer))
422 (princ (or (mail-header-lines header) 0) (current-buffer))
424 (when (mail-header-xref header)
425 (insert "Xref: " (mail-header-xref header)))
426 (when (or (mail-header-xref header)
427 (mail-header-extra header))
429 (when (mail-header-extra header)
430 (let ((extra (mail-header-extra header)))
432 (insert (symbol-name (caar extra))
433 ": " (cdar extra) "\t")
437 (while (search-backward "\n" p t)
441 (defun nnheader-parse-overview-file (file)
442 "Parse FILE and return a list of headers."
443 (mm-with-unibyte-buffer
444 (nnheader-insert-file-contents file)
445 (goto-char (point-min))
448 (push (nnheader-parse-nov) headers)
450 (nreverse headers))))
452 (defun nnheader-write-overview-file (file headers)
453 "Write HEADERS to FILE."
455 (mapcar 'nnheader-insert-nov headers)))
457 (defun nnheader-insert-header (header)
459 "Subject: " (or (mail-header-subject header) "(none)") "\n"
460 "From: " (or (mail-header-from header) "(nobody)") "\n"
461 "Date: " (or (mail-header-date header) "") "\n"
462 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
463 "References: " (or (mail-header-references header) "") "\n"
465 (princ (or (mail-header-lines header) 0) (current-buffer))
468 (defun nnheader-insert-article-line (article)
469 (goto-char (point-min))
471 (princ article (current-buffer))
472 (insert " Article retrieved.\n")
473 (search-forward "\n\n" nil 'move)
474 (delete-region (point) (point-max))
478 (defun nnheader-nov-delete-outside-range (beg end)
479 "Delete all NOV lines that lie outside the BEG to END range."
480 ;; First we find the first wanted line.
481 (nnheader-find-nov-line beg)
482 (delete-region (point-min) (point))
483 ;; Then we find the last wanted line.
484 (when (nnheader-find-nov-line end)
486 (delete-region (point) (point-max)))
488 (defun nnheader-find-nov-line (article)
489 "Put point at the NOV line that start with ARTICLE.
490 If ARTICLE doesn't exist, put point where that line
491 would have been. The function will return non-nil if
492 the line could be found."
493 ;; This function basically does a binary search.
494 (let ((max (point-max))
495 (min (goto-char (point-min)))
496 (cur (current-buffer))
500 (goto-char (+ min (/ (- max min) 2)))
502 (if (or (= (point) prev)
506 (while (and (not (numberp (setq num (read cur))))
509 (cond ((> num article)
514 (setq found 'yes)))))
515 ;; We may be at the first line.
518 (setq num (read cur)))
519 ;; Now we may have found the article we're looking for, or we
520 ;; may be somewhere near it.
521 (when (and (not (eq found 'yes))
522 (not (eq num article)))
524 (while (and (< (point) max)
525 (or (not (numberp num))
530 (= (setq num (read cur)) article)))
531 (unless (eq num article)
536 ;; Various cruft the backends and Gnus need to communicate.
538 (defvar nntp-server-buffer nil)
539 (defvar nntp-process-response nil)
540 (defvar news-reply-yank-from nil)
541 (defvar news-reply-yank-message-id nil)
543 (defvar nnheader-callback-function nil)
545 (defun nnheader-init-server-buffer ()
546 "Initialize the Gnus-backend communication buffer."
548 (unless (gnus-buffer-live-p nntp-server-buffer)
549 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
550 (set-buffer nntp-server-buffer)
551 (mm-enable-multibyte)
553 (kill-all-local-variables)
554 (setq case-fold-search t) ;Should ignore case.
555 (set (make-local-variable 'nntp-process-response) nil)
558 ;;; Various functions the backends use.
560 (defun nnheader-file-error (file)
561 "Return a string that says what is wrong with FILE."
564 ((not (file-exists-p file))
566 ((file-directory-p file)
568 ((not (file-readable-p file))
569 "%s is not readable"))
572 (defun nnheader-insert-head (file)
573 "Insert the head of the article."
574 (when (file-exists-p file)
575 (if (eq nnheader-max-head-length t)
576 ;; Just read the entire file.
577 (nnheader-insert-file-contents file)
578 ;; Read 1K blocks until we find a separator.
581 (while (and (eq nnheader-head-chop-length
582 (nth 1 (nnheader-insert-file-contents
584 (incf beg nnheader-head-chop-length))))
585 (prog1 (not (search-forward "\n\n" nil t))
586 (goto-char (point-max)))
587 (or (null nnheader-max-head-length)
588 (< beg nnheader-max-head-length))))))
591 (defun nnheader-article-p ()
592 "Say whether the current buffer looks like an article."
593 (goto-char (point-min))
594 (if (not (search-forward "\n\n" nil t))
596 (narrow-to-region (point-min) (1- (point)))
597 (goto-char (point-min))
598 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
599 (goto-char (match-end 0)))
604 (defun nnheader-insert-references (references message-id)
605 "Insert a References header based on REFERENCES and MESSAGE-ID."
606 (if (and (not references) (not message-id))
607 ;; This is invalid, but not all articles have Message-IDs.
609 (mail-position-on-field "References")
610 (let ((begin (point-at-bol))
615 (when (and references message-id)
619 ;; Fold long References lines to conform to RFC1036 (sort of).
620 ;; The region must end with a newline to fill the region
621 ;; without inserting extra newline.
622 (fill-region-as-paragraph begin (1+ (point))))))
624 (defun nnheader-replace-header (header new-value)
625 "Remove HEADER and insert the NEW-VALUE."
628 (nnheader-narrow-to-headers)
630 (message-remove-header header)
631 (goto-char (point-max))
632 (insert header ": " new-value "\n")))))
634 (defun nnheader-narrow-to-headers ()
635 "Narrow to the head of an article."
638 (goto-char (point-min))
639 (if (search-forward "\n\n" nil t)
642 (goto-char (point-min)))
644 (defun nnheader-remove-body ()
645 "Remove the body from an article in this current buffer."
646 (goto-char (point-min))
647 (when (re-search-forward "\n\r?\n" nil t)
648 (delete-region (point) (point-max))))
650 (defun nnheader-set-temp-buffer (name &optional noerase)
651 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
652 (set-buffer (get-buffer-create name))
653 (buffer-disable-undo)
658 (eval-when-compile (defvar jka-compr-compression-info-list))
659 (defvar nnheader-numerical-files
660 (if (boundp 'jka-compr-compression-info-list)
661 (concat "\\([0-9]+\\)\\("
662 (mapconcat (lambda (i) (aref i 0))
663 jka-compr-compression-info-list "\\|")
666 "Regexp that match numerical files.")
668 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
669 "Regexp that matches numerical file names.")
671 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
672 "Regexp that matches numerical full file names.")
674 (defsubst nnheader-file-to-number (file)
675 "Take a FILE name and return the article number."
676 (if (string= nnheader-numerical-short-files "^[0-9]+$")
678 (string-match nnheader-numerical-short-files file)
679 (string-to-int (match-string 0 file))))
681 (defvar nnheader-directory-files-is-safe
682 (or (eq system-type 'windows-nt)
683 (not (featurep 'xemacs)))
684 "If non-nil, Gnus believes `directory-files' is safe.
685 It has been reported numerous times that `directory-files' fails with
686 an alarming frequency on NFS mounted file systems. If it is nil,
687 `nnheader-directory-files-safe' is used.")
689 (defun nnheader-directory-files-safe (&rest args)
690 "Execute `directory-files' twice and returns the longer result."
691 (let ((first (apply 'directory-files args))
692 (second (apply 'directory-files args)))
693 (if (> (length first) (length second))
697 (defun nnheader-directory-articles (dir)
698 "Return a list of all article files in directory DIR."
699 (mapcar 'nnheader-file-to-number
700 (if nnheader-directory-files-is-safe
702 dir nil nnheader-numerical-short-files t)
703 (nnheader-directory-files-safe
704 dir nil nnheader-numerical-short-files t))))
706 (defun nnheader-article-to-file-alist (dir)
707 "Return an alist of article/file pairs in DIR."
708 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
709 (if nnheader-directory-files-is-safe
711 dir nil nnheader-numerical-short-files t)
712 (nnheader-directory-files-safe
713 dir nil nnheader-numerical-short-files t))))
715 (defun nnheader-fold-continuation-lines ()
716 "Fold continuation lines in the current buffer."
717 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
719 (defun nnheader-translate-file-chars (file &optional full)
720 "Translate FILE into something that can be a file name.
721 If FULL, translate everything."
722 (if (null nnheader-file-name-translation-alist)
723 ;; No translation is necessary.
728 ;; Do complete translation.
729 (setq leaf (copy-sequence file)
731 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
733 ;; We translate -- but only the file name. We leave the directory
735 (if (and (featurep 'xemacs)
736 (memq system-type '(cygwin32 win32 w32 mswindows windows-nt
738 ;; This is needed on NT and stuff, because
739 ;; file-name-nondirectory is not enough to split
740 ;; file names, containing ':', e.g.
741 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
743 ;; we are trying to correctly split such names:
744 ;; "d:file.name" -> "a:" "file.name"
745 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
746 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
748 ;; to translate then only the file name part.
752 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
753 (setq leaf (substring file (match-beginning 2))
754 path (substring file 0 (match-beginning 2)))))
755 ;; Emacs DTRT, says andrewi.
756 (setq leaf (file-name-nondirectory file)
757 path (file-name-directory file))))
758 (setq len (length leaf))
760 (when (setq trans (cdr (assq (aref leaf i)
761 nnheader-file-name-translation-alist)))
764 (concat path leaf))))
766 (defun nnheader-report (backend &rest args)
767 "Report an error from the BACKEND.
768 The first string in ARGS can be a format string."
769 (set (intern (format "%s-status-string" backend))
770 (if (< (length args) 2)
772 (apply 'format args)))
775 (defun nnheader-get-report (backend)
776 "Get the most recent report from BACKEND."
778 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
780 (error (nnheader-message 5 ""))))
782 (defun nnheader-insert (format &rest args)
783 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
784 If FORMAT isn't a format string, it and all ARGS will be inserted
787 (set-buffer nntp-server-buffer)
789 (if (string-match "%" format)
790 (insert (apply 'format format args))
791 (apply 'insert format args))
794 (defsubst nnheader-replace-chars-in-string (string from to)
795 (mm-subst-char-in-string from to string))
797 (defun nnheader-replace-duplicate-chars-in-string (string from to)
798 "Replace characters in STRING from FROM to TO."
799 (let ((string (substring string 0)) ;Copy string.
800 (len (length string))
802 ;; Replace all occurrences of FROM with TO.
804 (setq i (aref string idx))
805 (when (and (eq prev from) (= i from))
806 (aset string (1- idx) to)
807 (aset string idx to))
812 (defun nnheader-file-to-group (file &optional top)
813 "Return a group name based on FILE and TOP."
814 (nnheader-replace-chars-in-string
818 (substring (expand-file-name file)
821 (file-name-as-directory top))))
823 nnheader-directory-separator-character ?.))
825 (defun nnheader-message (level &rest args)
826 "Message if the Gnus backends are talkative."
827 (if (or (not (numberp gnus-verbose-backends))
828 (<= level gnus-verbose-backends))
829 (apply 'message args)
830 (apply 'format args)))
832 (defun nnheader-be-verbose (level)
833 "Return whether the backends should be verbose on LEVEL."
834 (or (not (numberp gnus-verbose-backends))
835 (<= level gnus-verbose-backends)))
837 (defvar nnheader-pathname-coding-system 'iso-8859-1
838 "*Coding system for file name.")
840 (defun nnheader-group-pathname (group dir &optional file)
841 "Make file name for GROUP."
843 (let ((dir (file-name-as-directory (expand-file-name dir))))
844 ;; If this directory exists, we use it directly.
845 (file-name-as-directory
846 (if (file-directory-p (concat dir group))
847 (expand-file-name group dir)
848 ;; If not, we translate dots into slashes.
849 (expand-file-name (mm-encode-coding-string
850 (nnheader-replace-chars-in-string group ?. ?/)
851 nnheader-pathname-coding-system)
853 (cond ((null file) "")
854 ((numberp file) (int-to-string file))
857 (defun nnheader-concat (dir &rest files)
858 "Concat DIR as directory to FILES."
859 (apply 'concat (file-name-as-directory dir) files))
861 (defun nnheader-ms-strip-cr ()
862 "Strip ^M from the end of all lines."
864 (nnheader-remove-cr-followed-by-lf)))
866 (defun nnheader-file-size (file)
867 "Return the file size of FILE or 0."
868 (or (nth 7 (file-attributes file)) 0))
870 (defun nnheader-find-etc-directory (package &optional file first)
871 "Go through `load-path' and find the \"../etc/PACKAGE\" directory.
872 This function will look in the parent directory of each `load-path'
873 entry, and look for the \"etc\" directory there.
874 If FILE, find the \".../etc/PACKAGE\" file instead.
875 If FIRST is non-nil, return the directory or the file found at the
876 first. Otherwise, find the newest one, though it may take a time."
877 (let ((path load-path)
879 ;; We try to find the dir by looking at the load path,
880 ;; stripping away the last component and adding "etc/".
886 (directory-file-name (car path)))
889 (or file (file-directory-p dir)))
891 (or (member dir results)
893 (setq path (if first nil (cdr path))))
894 (setq path (cdr path))))
895 (if (or first (not (cdr results)))
897 (car (sort results 'file-newer-than-file-p)))))
900 (defvar ange-ftp-path-format)
901 (defvar efs-path-regexp))
902 (defun nnheader-re-read-dir (path)
903 "Re-read directory PATH if PATH is on a remote system."
904 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
905 (when (string-match efs-path-regexp path)
906 (efs-re-read-dir path))
907 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
908 (when (string-match (car ange-ftp-path-format) path)
909 (ange-ftp-re-read-dir path)))))
911 (defvar nnheader-file-coding-system 'raw-text
912 "Coding system used in file backends of Gnus.")
914 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
915 "Like `insert-file-contents', q.v., but only reads in the file.
916 A buffer may be modified in several ways after reading into the buffer due
917 to advanced Emacs features, such as file-name-handlers, format decoding,
918 find-file-hooks, etc.
919 This function ensures that none of these modifications will take place."
920 (let ((coding-system-for-read nnheader-file-coding-system))
921 (mm-insert-file-contents filename visit beg end replace)))
923 (defun nnheader-insert-nov-file (file first)
924 (let ((size (nth 7 (file-attributes file)))
925 (cutoff (* 32 1024)))
928 ;; If the file is small, we just load it.
929 (nnheader-insert-file-contents file)
930 ;; We start on the assumption that FIRST is pretty recent. If
931 ;; not, we just insert the rest of the file as well.
933 (nnheader-insert-file-contents file nil (- size cutoff) size)
934 (goto-char (point-min))
935 (delete-region (point) (or (search-forward "\n" nil 'move) (point)))
936 (setq current (ignore-errors (read (current-buffer))))
937 (if (and (numberp current)
940 (delete-region (point-min) (point-max))
941 (nnheader-insert-file-contents file)))))))
943 (defun nnheader-find-file-noselect (&rest args)
944 (let ((format-alist nil)
945 (auto-mode-alist (mm-auto-mode-alist))
946 (default-major-mode 'fundamental-mode)
947 (enable-local-variables nil)
948 (after-insert-file-functions nil)
949 (enable-local-eval nil)
950 (find-file-hooks nil)
951 (coding-system-for-read nnheader-file-coding-system))
952 (apply 'find-file-noselect args)))
954 (defun nnheader-directory-regular-files (dir)
955 "Return a list of all regular files in DIR."
956 (let ((files (directory-files dir t))
959 (when (file-regular-p (car files))
960 (push (car files) out))
964 (defun nnheader-directory-files (&rest args)
965 "Same as `directory-files', but prune \".\" and \"..\"."
966 (let ((files (apply 'directory-files args))
969 (unless (member (file-name-nondirectory (car files)) '("." ".."))
970 (push (car files) out))
974 (defmacro nnheader-skeleton-replace (from &optional to regexp)
975 `(let ((new (generate-new-buffer " *nnheader replace*"))
976 (cur (current-buffer))
979 (goto-char (point-min))
980 (while (,(if regexp 're-search-forward 'search-forward)
982 (insert-buffer-substring
983 cur start (prog1 (match-beginning 0) (set-buffer new)))
984 (goto-char (point-max))
985 ,(when to `(insert ,to))
987 (setq start (point)))
988 (insert-buffer-substring
989 cur start (prog1 (point-max) (set-buffer new)))
990 (copy-to-buffer cur (point-min) (point-max))
991 (kill-buffer (current-buffer))
994 (defun nnheader-replace-string (from to)
995 "Do a fast replacement of FROM to TO from point to `point-max'."
996 (nnheader-skeleton-replace from to))
998 (defun nnheader-replace-regexp (from to)
999 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1000 (nnheader-skeleton-replace from to t))
1002 (defun nnheader-strip-cr ()
1003 "Strip all \r's from the current buffer."
1004 (nnheader-skeleton-replace "\r"))
1006 (defalias 'nnheader-cancel-timer 'cancel-timer)
1007 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1008 (defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1010 (defun nnheader-accept-process-output (process)
1011 (accept-process-output
1013 (truncate nnheader-read-timeout)
1014 (truncate (* (- nnheader-read-timeout
1015 (truncate nnheader-read-timeout))
1018 (when (featurep 'xemacs)
1019 (require 'nnheaderxm))
1021 (run-hooks 'nnheader-load-hook)
1025 ;;; nnheader.el ends here