Importing pgnus-0.79
[elisp/gnus.git-] / lisp / nnheader.el
1
2 ;;; nnheader.el --- header access macros for Gnus and its backends
3 ;; Copyright (C) 1987-1990,1993-1999 Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; These macros may look very much like the ones in GNUS 4.1.  They
29 ;; are, in a way, but you should note that the indices they use have
30 ;; been changed from the internal GNUS format to the NOV format.  The
31 ;; makes it possible to read headers from XOVER much faster.
32 ;;
33 ;; The format of a header is now:
34 ;; [number subject from date id references chars lines xref]
35 ;;
36 ;; (That last entry is defined as "misc" in the NOV format, but Gnus
37 ;; uses it for xrefs.)
38
39 ;;; Code:
40
41 (eval-when-compile (require 'cl))
42
43 (require 'mail-utils)
44 (require 'mm-util)
45
46 (defvar nnheader-max-head-length 4096
47   "*Max length of the head of articles.")
48
49 (defvar nnheader-head-chop-length 2048
50   "*Length of each read operation when trying to fetch HEAD headers.")
51
52 (defvar nnheader-file-name-translation-alist nil
53   "*Alist that says how to translate characters in file names.
54 For instance, if \":\" is invalid as a file character in file names
55 on your system, you could say something like:
56
57 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
58
59 (eval-and-compile
60  (autoload 'nnmail-message-id "nnmail")
61  (autoload 'mail-position-on-field "sendmail")
62  (autoload 'message-remove-header "message")
63  (autoload 'cancel-function-timers "timers")
64  (autoload 'gnus-point-at-eol "gnus-util")
65  (autoload 'gnus-delete-line "gnus-util")
66  (autoload 'gnus-buffer-live-p "gnus-util"))
67
68 ;;; Header access macros.
69
70 (defmacro mail-header-number (header)
71   "Return article number in HEADER."
72   `(aref ,header 0))
73
74 (defmacro mail-header-set-number (header number)
75   "Set article number of HEADER to NUMBER."
76   `(aset ,header 0 ,number))
77
78 (defmacro mail-header-subject (header)
79   "Return subject string in HEADER."
80   `(aref ,header 1))
81
82 (defmacro mail-header-set-subject (header subject)
83   "Set article subject of HEADER to SUBJECT."
84   `(aset ,header 1 ,subject))
85
86 (defmacro mail-header-from (header)
87   "Return author string in HEADER."
88   `(aref ,header 2))
89
90 (defmacro mail-header-set-from (header from)
91   "Set article author of HEADER to FROM."
92   `(aset ,header 2 ,from))
93
94 (defmacro mail-header-date (header)
95   "Return date in HEADER."
96   `(aref ,header 3))
97
98 (defmacro mail-header-set-date (header date)
99   "Set article date of HEADER to DATE."
100   `(aset ,header 3 ,date))
101
102 (defalias 'mail-header-message-id 'mail-header-id)
103 (defmacro mail-header-id (header)
104   "Return Id in HEADER."
105   `(aref ,header 4))
106
107 (defalias 'mail-header-set-message-id 'mail-header-set-id)
108 (defmacro mail-header-set-id (header id)
109   "Set article Id of HEADER to ID."
110   `(aset ,header 4 ,id))
111
112 (defmacro mail-header-references (header)
113   "Return references in HEADER."
114   `(aref ,header 5))
115
116 (defmacro mail-header-set-references (header ref)
117   "Set article references of HEADER to REF."
118   `(aset ,header 5 ,ref))
119
120 (defmacro mail-header-chars (header)
121   "Return number of chars of article in HEADER."
122   `(aref ,header 6))
123
124 (defmacro mail-header-set-chars (header chars)
125   "Set number of chars in article of HEADER to CHARS."
126   `(aset ,header 6 ,chars))
127
128 (defmacro mail-header-lines (header)
129   "Return lines in HEADER."
130   `(aref ,header 7))
131
132 (defmacro mail-header-set-lines (header lines)
133   "Set article lines of HEADER to LINES."
134   `(aset ,header 7 ,lines))
135
136 (defmacro mail-header-xref (header)
137   "Return xref string in HEADER."
138   `(aref ,header 8))
139
140 (defmacro mail-header-set-xref (header xref)
141   "Set article xref of HEADER to xref."
142   `(aset ,header 8 ,xref))
143
144 (defmacro mail-header-extra (header)
145   "Return the extra headers in HEADER."
146   `(aref ,header 9))
147
148 (defmacro mail-header-set-extra (header extra)
149   "Set the extra headers in HEADER to EXTRA."
150   `(aset ,header 9 ',extra))
151
152 (defun make-mail-header (&optional init)
153   "Create a new mail header structure initialized with INIT."
154   (make-vector 10 init))
155
156 (defun make-full-mail-header (&optional number subject from date id
157                                         references chars lines xref
158                                         extra)
159   "Create a new mail header structure initialized with the parameters given."
160   (vector number subject from date id references chars lines xref extra))
161
162 ;; fake message-ids: generation and detection
163
164 (defvar nnheader-fake-message-id 1)
165
166 (defsubst nnheader-generate-fake-message-id ()
167   (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
168
169 (defsubst nnheader-fake-message-id-p (id)
170   (save-match-data                      ; regular message-id's are <.*>
171     (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
172
173 ;; Parsing headers and NOV lines.
174
175 (defsubst nnheader-header-value ()
176   (buffer-substring (match-end 0) (gnus-point-at-eol)))
177
178 (defun nnheader-parse-head (&optional naked)
179   (let ((case-fold-search t)
180         (cur (current-buffer))
181         (buffer-read-only nil)
182         in-reply-to lines p ref)
183     (goto-char (point-min))
184     (when naked
185       (insert "\n"))
186     ;; Search to the beginning of the next header.  Error messages
187     ;; do not begin with 2 or 3.
188     (prog1
189         (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
190           ;; This implementation of this function, with nine
191           ;; search-forwards instead of the one re-search-forward and
192           ;; a case (which basically was the old function) is actually
193           ;; about twice as fast, even though it looks messier.  You
194           ;; can't have everything, I guess.  Speed and elegance
195           ;; don't always go hand in hand.
196           (vector
197            ;; Number.
198            (if naked
199                (progn
200                  (setq p (point-min))
201                  0)
202              (prog1
203                  (read cur)
204                (end-of-line)
205                (setq p (point))
206                (narrow-to-region (point)
207                                  (or (and (search-forward "\n.\n" nil t)
208                                           (- (point) 2))
209                                      (point)))))
210            ;; Subject.
211            (progn
212              (goto-char p)
213              (if (search-forward "\nsubject: " nil t)
214                  (nnheader-header-value) "(none)"))
215            ;; From.
216            (progn
217              (goto-char p)
218              (if (search-forward "\nfrom: " nil t)
219                  (nnheader-header-value) "(nobody)"))
220            ;; Date.
221            (progn
222              (goto-char p)
223              (if (search-forward "\ndate: " nil t)
224                  (nnheader-header-value) ""))
225            ;; Message-ID.
226            (progn
227              (goto-char p)
228              (if (search-forward "\nmessage-id:" nil t)
229                  (buffer-substring
230                   (1- (or (search-forward "<" (gnus-point-at-eol) t)
231                           (point)))
232                   (or (search-forward ">" (gnus-point-at-eol) t) (point)))
233                ;; If there was no message-id, we just fake one to make
234                ;; subsequent routines simpler.
235                (nnheader-generate-fake-message-id)))
236            ;; References.
237            (progn
238              (goto-char p)
239              (if (search-forward "\nreferences: " nil t)
240                  (nnheader-header-value)
241                ;; Get the references from the in-reply-to header if there
242                ;; were no references and the in-reply-to header looks
243                ;; promising.
244                (if (and (search-forward "\nin-reply-to: " nil t)
245                         (setq in-reply-to (nnheader-header-value))
246                         (string-match "<[^>]+>" in-reply-to))
247                    (let (ref2)
248                      (setq ref (substring in-reply-to (match-beginning 0)
249                                           (match-end 0)))
250                      (while (string-match "<[^>]+>" in-reply-to (match-end 0))
251                        (setq ref2 (substring in-reply-to (match-beginning 0)
252                                              (match-end 0)))
253                        (when (> (length ref2) (length ref))
254                          (setq ref ref2)))
255                      ref)
256                  nil)))
257            ;; Chars.
258            0
259            ;; Lines.
260            (progn
261              (goto-char p)
262              (if (search-forward "\nlines: " nil t)
263                  (if (numberp (setq lines (read cur)))
264                      lines 0)
265                0))
266            ;; Xref.
267            (progn
268              (goto-char p)
269              (and (search-forward "\nxref: " nil t)
270                   (nnheader-header-value)))
271
272            ;; Extra.
273            (when nnmail-extra-headers
274              (let ((extra nnmail-extra-headers)
275                    out)
276                (while extra
277                  (goto-char p)
278                  (when (search-forward
279                         (concat "\n" (symbol-name (car extra)) ": ") nil t)
280                    (push (cons (car extra) (nnheader-header-value))
281                          out))
282                  (pop extra))
283                out))))
284       (when naked
285         (goto-char (point-min))
286         (delete-char 1)))))
287
288 (defmacro nnheader-nov-skip-field ()
289   '(search-forward "\t" eol 'move))
290
291 (defmacro nnheader-nov-field ()
292   '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
293
294 (defmacro nnheader-nov-read-integer ()
295   '(prog1
296        (if (eq (char-after) ?\t)
297            0
298          (let ((num (ignore-errors (read (current-buffer)))))
299            (if (numberp num) num 0)))
300      (or (eobp) (forward-char 1))))
301
302 (defmacro nnheader-nov-parse-extra ()
303   '(let (out string)
304      (while (not (memq (char-after) '(?\n nil)))
305        (setq string (nnheader-nov-field))
306        (when (string-match "^\\([^ :]+\\): " string)
307          (push (cons (intern (match-string 1 string))
308                      (substring string (match-end 0)))
309                out)))
310      out))
311
312 (defmacro nnheader-nov-read-message-id ()
313   '(let ((id (nnheader-nov-field)))
314      (if (string-match "^<[^>]+>$" id)
315          id
316        (nnheader-generate-fake-message-id))))
317
318 (defun nnheader-parse-nov ()
319   (let ((eol (gnus-point-at-eol)))
320     (vector
321      (nnheader-nov-read-integer)        ; number
322      (nnheader-nov-field)               ; subject
323      (nnheader-nov-field)               ; from
324      (nnheader-nov-field)               ; date
325      (nnheader-nov-read-message-id)     ; id
326      (nnheader-nov-field)               ; refs
327      (nnheader-nov-read-integer)        ; chars
328      (nnheader-nov-read-integer)        ; lines
329      (if (eq (char-after) ?\n)
330          nil
331        (nnheader-nov-field))            ; misc
332      (nnheader-nov-parse-extra))))      ; extra
333
334 (defun nnheader-insert-nov (header)
335   (princ (mail-header-number header) (current-buffer))
336   (insert
337    "\t"
338    (or (mail-header-subject header) "(none)") "\t"
339    (or (mail-header-from header) "(nobody)") "\t"
340    (or (mail-header-date header) "") "\t"
341    (or (mail-header-id header)
342        (nnmail-message-id))
343    "\t"
344    (or (mail-header-references header) "") "\t")
345   (princ (or (mail-header-chars header) 0) (current-buffer))
346   (insert "\t")
347   (princ (or (mail-header-lines header) 0) (current-buffer))
348   (insert "\t")
349   (when (mail-header-xref header)
350     (insert "Xref: " (mail-header-xref header)))
351   (when (or (mail-header-xref header)
352             (mail-header-extra header))
353     (insert "\t"))
354   (when (mail-header-extra header)
355     (let ((extra (mail-header-extra header)))
356       (while extra
357         (insert (symbol-name (caar extra))
358                 ": " (cdar extra) "\t")
359         (pop extra))))
360   (insert "\n"))
361
362 (defun nnheader-insert-article-line (article)
363   (goto-char (point-min))
364   (insert "220 ")
365   (princ article (current-buffer))
366   (insert " Article retrieved.\n")
367   (search-forward "\n\n" nil 'move)
368   (delete-region (point) (point-max))
369   (forward-char -1)
370   (insert "."))
371
372 (defun nnheader-nov-delete-outside-range (beg end)
373   "Delete all NOV lines that lie outside the BEG to END range."
374   ;; First we find the first wanted line.
375   (nnheader-find-nov-line beg)
376   (delete-region (point-min) (point))
377   ;; Then we find the last wanted line.
378   (when (nnheader-find-nov-line end)
379     (forward-line 1))
380   (delete-region (point) (point-max)))
381
382 (defun nnheader-find-nov-line (article)
383   "Put point at the NOV line that start with ARTICLE.
384 If ARTICLE doesn't exist, put point where that line
385 would have been.  The function will return non-nil if
386 the line could be found."
387   ;; This function basically does a binary search.
388   (let ((max (point-max))
389         (min (goto-char (point-min)))
390         (cur (current-buffer))
391         (prev (point-min))
392         num found)
393     (while (not found)
394       (goto-char (/ (+ max min) 2))
395       (beginning-of-line)
396       (if (or (= (point) prev)
397               (eobp))
398           (setq found t)
399         (setq prev (point))
400         (while (and (not (numberp (setq num (read cur))))
401                     (not (eobp)))
402           (gnus-delete-line))
403         (cond ((> num article)
404                (setq max (point)))
405               ((< num article)
406                (setq min (point)))
407               (t
408                (setq found 'yes)))))
409     ;; We may be at the first line.
410     (when (and (not num)
411                (not (eobp)))
412       (setq num (read cur)))
413     ;; Now we may have found the article we're looking for, or we
414     ;; may be somewhere near it.
415     (when (and (not (eq found 'yes))
416                (not (eq num article)))
417       (setq found (point))
418       (while (and (< (point) max)
419                   (or (not (numberp num))
420                       (< num article)))
421         (forward-line 1)
422         (setq found (point))
423         (or (eobp)
424             (= (setq num (read cur)) article)))
425       (unless (eq num article)
426         (goto-char found)))
427     (beginning-of-line)
428     (eq num article)))
429
430 ;; Various cruft the backends and Gnus need to communicate.
431
432 (defvar nntp-server-buffer nil)
433 (defvar gnus-verbose-backends 7
434   "*A number that says how talkative the Gnus backends should be.")
435 (defvar gnus-nov-is-evil nil
436   "If non-nil, Gnus backends will never output headers in the NOV format.")
437 (defvar news-reply-yank-from nil)
438 (defvar news-reply-yank-message-id nil)
439
440 (defvar nnheader-callback-function nil)
441
442 (defun nnheader-init-server-buffer ()
443   "Initialize the Gnus-backend communication buffer."
444   (save-excursion
445     (unless (gnus-buffer-live-p nntp-server-buffer)
446       (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
447     (mm-enable-multibyte)
448     (set-buffer nntp-server-buffer)
449     (erase-buffer)
450     (kill-all-local-variables)
451     (setq case-fold-search t)           ;Should ignore case.
452     t))
453
454 ;;; Various functions the backends use.
455
456 (defun nnheader-file-error (file)
457   "Return a string that says what is wrong with FILE."
458   (format
459    (cond
460     ((not (file-exists-p file))
461      "%s does not exist")
462     ((file-directory-p file)
463      "%s is a directory")
464     ((not (file-readable-p file))
465      "%s is not readable"))
466    file))
467
468 (defun nnheader-insert-head (file)
469   "Insert the head of the article."
470   (when (file-exists-p file)
471     (if (eq nnheader-max-head-length t)
472         ;; Just read the entire file.
473         (nnheader-insert-file-contents file)
474       ;; Read 1K blocks until we find a separator.
475       (let ((beg 0)
476             format-alist)
477         (while (and (eq nnheader-head-chop-length
478                         (nth 1 (nnheader-insert-file-contents
479                                 file nil beg
480                                 (incf beg nnheader-head-chop-length))))
481                     (prog1 (not (search-forward "\n\n" nil t))
482                       (goto-char (point-max)))
483                     (or (null nnheader-max-head-length)
484                         (< beg nnheader-max-head-length))))))
485     t))
486
487 (defun nnheader-article-p ()
488   "Say whether the current buffer looks like an article."
489   (goto-char (point-min))
490   (if (not (search-forward "\n\n" nil t))
491       nil
492     (narrow-to-region (point-min) (1- (point)))
493     (goto-char (point-min))
494     (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
495       (goto-char (match-end 0)))
496     (prog1
497         (eobp)
498       (widen))))
499
500 (defun nnheader-insert-references (references message-id)
501   "Insert a References header based on REFERENCES and MESSAGE-ID."
502   (if (and (not references) (not message-id))
503       ;; This is invalid, but not all articles have Message-IDs.
504       ()
505     (mail-position-on-field "References")
506     (let ((begin (save-excursion (beginning-of-line) (point)))
507           (fill-column 78)
508           (fill-prefix "\t"))
509       (when references
510         (insert references))
511       (when (and references message-id)
512         (insert " "))
513       (when message-id
514         (insert message-id))
515       ;; Fold long References lines to conform to RFC1036 (sort of).
516       ;; The region must end with a newline to fill the region
517       ;; without inserting extra newline.
518       (fill-region-as-paragraph begin (1+ (point))))))
519
520 (defun nnheader-replace-header (header new-value)
521   "Remove HEADER and insert the NEW-VALUE."
522   (save-excursion
523     (save-restriction
524       (nnheader-narrow-to-headers)
525       (prog1
526           (message-remove-header header)
527         (goto-char (point-max))
528         (insert header ": " new-value "\n")))))
529
530 (defun nnheader-narrow-to-headers ()
531   "Narrow to the head of an article."
532   (widen)
533   (narrow-to-region
534    (goto-char (point-min))
535    (if (search-forward "\n\n" nil t)
536        (1- (point))
537      (point-max)))
538   (goto-char (point-min)))
539
540 (defun nnheader-set-temp-buffer (name &optional noerase)
541   "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
542   (set-buffer (get-buffer-create name))
543   (buffer-disable-undo)
544   (unless noerase
545     (erase-buffer))
546   (current-buffer))
547
548 (defvar jka-compr-compression-info-list)
549 (defvar nnheader-numerical-files
550   (if (boundp 'jka-compr-compression-info-list)
551       (concat "\\([0-9]+\\)\\("
552               (mapconcat (lambda (i) (aref i 0))
553                          jka-compr-compression-info-list "\\|")
554               "\\)?")
555     "[0-9]+$")
556   "Regexp that match numerical files.")
557
558 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
559   "Regexp that matches numerical file names.")
560
561 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
562   "Regexp that matches numerical full file paths.")
563
564 (defsubst nnheader-file-to-number (file)
565   "Take a file name and return the article number."
566   (if (string= nnheader-numerical-short-files "^[0-9]+$")
567       (string-to-int file)
568     (string-match nnheader-numerical-short-files file)
569     (string-to-int (match-string 0 file))))
570
571 (defun nnheader-directory-files-safe (&rest args)
572   ;; It has been reported numerous times that `directory-files'
573   ;; fails with an alarming frequency on NFS mounted file systems.
574   ;; This function executes that function twice and returns
575   ;; the longest result.
576   (let ((first (apply 'directory-files args))
577         (second (apply 'directory-files args)))
578     (if (> (length first) (length second))
579         first
580       second)))
581
582 (defun nnheader-directory-articles (dir)
583   "Return a list of all article files in a directory."
584   (mapcar 'nnheader-file-to-number
585           (nnheader-directory-files-safe
586            dir nil nnheader-numerical-short-files t)))
587
588 (defun nnheader-article-to-file-alist (dir)
589   "Return an alist of article/file pairs in DIR."
590   (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
591           (nnheader-directory-files-safe
592            dir nil nnheader-numerical-short-files t)))
593
594 (defun nnheader-fold-continuation-lines ()
595   "Fold continuation lines in the current buffer."
596   (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
597
598 (defun nnheader-translate-file-chars (file &optional full)
599   "Translate FILE into something that can be a file name.
600 If FULL, translate everything."
601   (if (null nnheader-file-name-translation-alist)
602       ;; No translation is necessary.
603       file
604     (let* ((i 0)
605            trans leaf path len)
606       (if full
607           ;; Do complete translation.
608           (setq leaf (copy-sequence file)
609                 path "")
610         ;; We translate -- but only the file name.  We leave the directory
611         ;; alone.
612         (if (string-match "/[^/]+\\'" file)
613             ;; This is needed on NT's and stuff.
614             (setq leaf (substring file (1+ (match-beginning 0)))
615                   path (substring file 0 (1+ (match-beginning 0))))
616           ;; Fall back on this.
617           (setq leaf (file-name-nondirectory file)
618                 path (file-name-directory file))))
619       (setq len (length leaf))
620       (while (< i len)
621         (when (setq trans (cdr (assq (aref leaf i)
622                                      nnheader-file-name-translation-alist)))
623           (aset leaf i trans))
624         (incf i))
625       (concat path leaf))))
626
627 (defun nnheader-report (backend &rest args)
628   "Report an error from the BACKEND.
629 The first string in ARGS can be a format string."
630   (set (intern (format "%s-status-string" backend))
631        (if (< (length args) 2)
632            (car args)
633          (apply 'format args)))
634   nil)
635
636 (defun nnheader-get-report (backend)
637   "Get the most recent report from BACKEND."
638   (condition-case ()
639       (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
640                                                   backend))))
641     (error (nnheader-message 5 ""))))
642
643 (defun nnheader-insert (format &rest args)
644   "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
645 If FORMAT isn't a format string, it and all ARGS will be inserted
646 without formatting."
647   (save-excursion
648     (set-buffer nntp-server-buffer)
649     (erase-buffer)
650     (if (string-match "%" format)
651         (insert (apply 'format format args))
652       (apply 'insert format args))
653     t))
654
655 (defun nnheader-replace-chars-in-string (string from to)
656   "Replace characters in STRING from FROM to TO."
657   (let ((string (substring string 0))   ;Copy string.
658         (len (length string))
659         (idx 0))
660     ;; Replace all occurrences of FROM with TO.
661     (while (< idx len)
662       (when (= (aref string idx) from)
663         (aset string idx to))
664       (setq idx (1+ idx)))
665     string))
666
667 (defun nnheader-file-to-group (file &optional top)
668   "Return a group name based on FILE and TOP."
669   (nnheader-replace-chars-in-string
670    (if (not top)
671        file
672      (condition-case ()
673          (substring (expand-file-name file)
674                     (length
675                      (expand-file-name
676                       (file-name-as-directory top))))
677        (error "")))
678    ?/ ?.))
679
680 (defun nnheader-message (level &rest args)
681   "Message if the Gnus backends are talkative."
682   (if (or (not (numberp gnus-verbose-backends))
683           (<= level gnus-verbose-backends))
684       (apply 'message args)
685     (apply 'format args)))
686
687 (defun nnheader-be-verbose (level)
688   "Return whether the backends should be verbose on LEVEL."
689   (or (not (numberp gnus-verbose-backends))
690       (<= level gnus-verbose-backends)))
691
692 (defvar nnheader-pathname-coding-system 'binary
693   "*Coding system for pathname.")
694
695 (defun nnheader-group-pathname (group dir &optional file)
696   "Make pathname for GROUP."
697   (concat
698    (let ((dir (file-name-as-directory (expand-file-name dir))))
699      ;; If this directory exists, we use it directly.
700      (if (file-directory-p (concat dir group))
701          (concat dir group "/")
702        ;; If not, we translate dots into slashes.
703        (concat dir
704                (mm-encode-coding-string
705                 (nnheader-replace-chars-in-string group ?. ?/)
706                 nnheader-pathname-coding-system)
707                "/")))
708    (cond ((null file) "")
709          ((numberp file) (int-to-string file))
710          (t file))))
711
712 (defun nnheader-functionp (form)
713   "Return non-nil if FORM is funcallable."
714   (or (and (symbolp form) (fboundp form))
715       (and (listp form) (eq (car form) 'lambda))))
716
717 (defun nnheader-concat (dir &rest files)
718   "Concat DIR as directory to FILE."
719   (apply 'concat (file-name-as-directory dir) files))
720
721 (defun nnheader-ms-strip-cr ()
722   "Strip ^M from the end of all lines."
723   (save-excursion
724     (goto-char (point-min))
725     (while (re-search-forward "\r$" nil t)
726       (delete-backward-char 1))))
727
728 (defun nnheader-file-size (file)
729   "Return the file size of FILE or 0."
730   (or (nth 7 (file-attributes file)) 0))
731
732 (defun nnheader-find-etc-directory (package &optional file)
733   "Go through the path and find the \".../etc/PACKAGE\" directory.
734 If FILE, find the \".../etc/PACKAGE\" file instead."
735   (let ((path load-path)
736         dir result)
737     ;; We try to find the dir by looking at the load path,
738     ;; stripping away the last component and adding "etc/".
739     (while path
740       (if (and (car path)
741                (file-exists-p
742                 (setq dir (concat
743                            (file-name-directory
744                             (directory-file-name (car path)))
745                            "etc/" package
746                            (if file "" "/"))))
747                (or file (file-directory-p dir)))
748           (setq result dir
749                 path nil)
750         (setq path (cdr path))))
751     result))
752
753 (defvar ange-ftp-path-format)
754 (defvar efs-path-regexp)
755 (defun nnheader-re-read-dir (path)
756   "Re-read directory PATH if PATH is on a remote system."
757   (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
758       (when (string-match efs-path-regexp path)
759         (efs-re-read-dir path))
760     (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
761       (when (string-match (car ange-ftp-path-format) path)
762         (ange-ftp-re-read-dir path)))))
763
764 (defvar nnheader-file-coding-system 'binary
765   "Coding system used in file backends of Gnus.")
766
767 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
768   "Like `insert-file-contents', q.v., but only reads in the file.
769 A buffer may be modified in several ways after reading into the buffer due
770 to advanced Emacs features, such as file-name-handlers, format decoding,
771 find-file-hooks, etc.
772   This function ensures that none of these modifications will take place."
773   (let ((format-alist nil)
774         (auto-mode-alist (nnheader-auto-mode-alist))
775         (default-major-mode 'fundamental-mode)
776         (enable-local-variables nil)
777         (after-insert-file-functions nil)
778         (find-file-hooks nil)
779         (coding-system-for-read nnheader-file-coding-system))
780     (insert-file-contents filename visit beg end replace)))
781
782 (defun nnheader-find-file-noselect (&rest args)
783   (let ((format-alist nil)
784         (auto-mode-alist (nnheader-auto-mode-alist))
785         (default-major-mode 'fundamental-mode)
786         (enable-local-variables nil)
787         (after-insert-file-functions nil)
788         (find-file-hooks nil)
789         (coding-system-for-read nnheader-file-coding-system))
790     (apply 'find-file-noselect args)))
791
792 (defun nnheader-auto-mode-alist ()
793   "Return an `auto-mode-alist' with only the .gz (etc) thingies."
794   (let ((alist auto-mode-alist)
795         out)
796     (while alist
797       (when (listp (cdar alist))
798         (push (car alist) out))
799       (pop alist))
800     (nreverse out)))
801
802 (defun nnheader-directory-regular-files (dir)
803   "Return a list of all regular files in DIR."
804   (let ((files (directory-files dir t))
805         out)
806     (while files
807       (when (file-regular-p (car files))
808         (push (car files) out))
809       (pop files))
810     (nreverse out)))
811
812 (defun nnheader-directory-files (&rest args)
813   "Same as `directory-files', but prune \".\" and \"..\"."
814   (let ((files (apply 'directory-files args))
815         out)
816     (while files
817       (unless (member (file-name-nondirectory (car files)) '("." ".."))
818         (push (car files) out))
819       (pop files))
820     (nreverse out)))
821
822 (defmacro nnheader-skeleton-replace (from &optional to regexp)
823   `(let ((new (generate-new-buffer " *nnheader replace*"))
824          (cur (current-buffer))
825          (start (point-min)))
826      (set-buffer cur)
827      (goto-char (point-min))
828      (while (,(if regexp 're-search-forward 'search-forward)
829              ,from nil t)
830        (insert-buffer-substring
831         cur start (prog1 (match-beginning 0) (set-buffer new)))
832        (goto-char (point-max))
833        ,(when to `(insert ,to))
834        (set-buffer cur)
835        (setq start (point)))
836      (insert-buffer-substring
837       cur start (prog1 (point-max) (set-buffer new)))
838      (copy-to-buffer cur (point-min) (point-max))
839      (kill-buffer (current-buffer))
840      (set-buffer cur)))
841
842 (defun nnheader-replace-string (from to)
843   "Do a fast replacement of FROM to TO from point to point-max."
844   (nnheader-skeleton-replace from to))
845
846 (defun nnheader-replace-regexp (from to)
847   "Do a fast regexp replacement of FROM to TO from point to point-max."
848   (nnheader-skeleton-replace from to t))
849
850 (defun nnheader-strip-cr ()
851   "Strip all \r's from the current buffer."
852   (nnheader-skeleton-replace "\r"))
853
854 (fset 'nnheader-run-at-time 'run-at-time)
855 (fset 'nnheader-cancel-timer 'cancel-timer)
856 (fset 'nnheader-cancel-function-timers 'cancel-function-timers)
857
858 (when (string-match "XEmacs\\|Lucid" emacs-version)
859   (require 'nnheaderxm))
860
861 (run-hooks 'nnheader-load-hook)
862
863 (provide 'nnheader)
864
865 ;;; nnheader.el ends here