*** empty log message ***
[gnus] / 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 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'mail-utils)
33 (require 'mm-util)
34
35 (defvar nnheader-max-head-length 4096
36   "*Max length of the head of articles.")
37
38 (defvar nnheader-head-chop-length 2048
39   "*Length of each read operation when trying to fetch HEAD headers.")
40
41 (defvar nnheader-file-name-translation-alist nil
42   "*Alist that says how to translate characters in file names.
43 For instance, if \":\" is invalid as a file character in file names
44 on your system, you could say something like:
45
46 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
47
48 (eval-and-compile
49  (autoload 'nnmail-message-id "nnmail")
50  (autoload 'mail-position-on-field "sendmail")
51  (autoload 'message-remove-header "message")
52  (autoload 'cancel-function-timers "timers")
53  (autoload 'gnus-point-at-eol "gnus-util")
54  (autoload 'gnus-delete-line "gnus-util")
55  (autoload 'gnus-buffer-live-p "gnus-util"))
56
57 ;;; Header access macros.
58
59 ;; These macros may look very much like the ones in GNUS 4.1.  They
60 ;; are, in a way, but you should note that the indices they use have
61 ;; been changed from the internal GNUS format to the NOV format.  The
62 ;; makes it possible to read headers from XOVER much faster.
63 ;;
64 ;; The format of a header is now:
65 ;; [number subject from date id references chars lines xref extra]
66 ;;
67 ;; (That next-to-last entry is defined as "misc" in the NOV format,
68 ;; but Gnus uses it for xrefs.)
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 "<[^\n>]+>" in-reply-to))
247                    (let (ref2)
248                      (setq ref (substring in-reply-to (match-beginning 0)
249                                           (match-end 0)))
250                      (while (string-match "<[^\n>]+>"
251                                           in-reply-to (match-end 0))
252                        (setq ref2 (substring in-reply-to (match-beginning 0)
253                                              (match-end 0)))
254                        (when (> (length ref2) (length ref))
255                          (setq ref ref2)))
256                      ref)
257                  nil)))
258            ;; Chars.
259            0
260            ;; Lines.
261            (progn
262              (goto-char p)
263              (if (search-forward "\nlines: " nil t)
264                  (if (numberp (setq lines (read cur)))
265                      lines 0)
266                0))
267            ;; Xref.
268            (progn
269              (goto-char p)
270              (and (search-forward "\nxref: " nil t)
271                   (nnheader-header-value)))
272
273            ;; Extra.
274            (when nnmail-extra-headers
275              (let ((extra nnmail-extra-headers)
276                    out)
277                (while extra
278                  (goto-char p)
279                  (when (search-forward
280                         (concat "\n" (symbol-name (car extra)) ": ") nil t)
281                    (push (cons (car extra) (nnheader-header-value))
282                          out))
283                  (pop extra))
284                out))))
285       (when naked
286         (goto-char (point-min))
287         (delete-char 1)))))
288
289 (defmacro nnheader-nov-skip-field ()
290   '(search-forward "\t" eol 'move))
291
292 (defmacro nnheader-nov-field ()
293   '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
294
295 (defmacro nnheader-nov-read-integer ()
296   '(prog1
297        (if (eq (char-after) ?\t)
298            0
299          (let ((num (ignore-errors (read (current-buffer)))))
300            (if (numberp num) num 0)))
301      (or (eobp) (forward-char 1))))
302
303 (defmacro nnheader-nov-parse-extra ()
304   '(let (out string)
305      (while (not (memq (char-after) '(?\n nil)))
306        (setq string (nnheader-nov-field))
307        (when (string-match "^\\([^ :]+\\): " string)
308          (push (cons (intern (match-string 1 string))
309                      (substring string (match-end 0)))
310                out)))
311      out))
312
313 (defmacro nnheader-nov-read-message-id ()
314   '(let ((id (nnheader-nov-field)))
315      (if (string-match "^<[^>]+>$" id)
316          id
317        (nnheader-generate-fake-message-id))))
318
319 (defun nnheader-parse-nov ()
320   (let ((eol (gnus-point-at-eol)))
321     (vector
322      (nnheader-nov-read-integer)        ; number
323      (nnheader-nov-field)               ; subject
324      (nnheader-nov-field)               ; from
325      (nnheader-nov-field)               ; date
326      (nnheader-nov-read-message-id)     ; id
327      (nnheader-nov-field)               ; refs
328      (nnheader-nov-read-integer)        ; chars
329      (nnheader-nov-read-integer)        ; lines
330      (if (eq (char-after) ?\n)
331          nil
332        (nnheader-nov-field))            ; misc
333      (nnheader-nov-parse-extra))))      ; extra
334
335 (defun nnheader-insert-nov (header)
336   (princ (mail-header-number header) (current-buffer))
337   (insert
338    "\t"
339    (or (mail-header-subject header) "(none)") "\t"
340    (or (mail-header-from header) "(nobody)") "\t"
341    (or (mail-header-date header) "") "\t"
342    (or (mail-header-id header)
343        (nnmail-message-id))
344    "\t"
345    (or (mail-header-references header) "") "\t")
346   (princ (or (mail-header-chars header) 0) (current-buffer))
347   (insert "\t")
348   (princ (or (mail-header-lines header) 0) (current-buffer))
349   (insert "\t")
350   (when (mail-header-xref header)
351     (insert "Xref: " (mail-header-xref header)))
352   (when (or (mail-header-xref header)
353             (mail-header-extra header))
354     (insert "\t"))
355   (when (mail-header-extra header)
356     (let ((extra (mail-header-extra header)))
357       (while extra
358         (insert (symbol-name (caar extra))
359                 ": " (cdar extra) "\t")
360         (pop extra))))
361   (insert "\n"))
362
363 (defun nnheader-insert-article-line (article)
364   (goto-char (point-min))
365   (insert "220 ")
366   (princ article (current-buffer))
367   (insert " Article retrieved.\n")
368   (search-forward "\n\n" nil 'move)
369   (delete-region (point) (point-max))
370   (forward-char -1)
371   (insert "."))
372
373 (defun nnheader-nov-delete-outside-range (beg end)
374   "Delete all NOV lines that lie outside the BEG to END range."
375   ;; First we find the first wanted line.
376   (nnheader-find-nov-line beg)
377   (delete-region (point-min) (point))
378   ;; Then we find the last wanted line.
379   (when (nnheader-find-nov-line end)
380     (forward-line 1))
381   (delete-region (point) (point-max)))
382
383 (defun nnheader-find-nov-line (article)
384   "Put point at the NOV line that start with ARTICLE.
385 If ARTICLE doesn't exist, put point where that line
386 would have been.  The function will return non-nil if
387 the line could be found."
388   ;; This function basically does a binary search.
389   (let ((max (point-max))
390         (min (goto-char (point-min)))
391         (cur (current-buffer))
392         (prev (point-min))
393         num found)
394     (while (not found)
395       (goto-char (/ (+ max min) 2))
396       (beginning-of-line)
397       (if (or (= (point) prev)
398               (eobp))
399           (setq found t)
400         (setq prev (point))
401         (while (and (not (numberp (setq num (read cur))))
402                     (not (eobp)))
403           (gnus-delete-line))
404         (cond ((> num article)
405                (setq max (point)))
406               ((< num article)
407                (setq min (point)))
408               (t
409                (setq found 'yes)))))
410     ;; We may be at the first line.
411     (when (and (not num)
412                (not (eobp)))
413       (setq num (read cur)))
414     ;; Now we may have found the article we're looking for, or we
415     ;; may be somewhere near it.
416     (when (and (not (eq found 'yes))
417                (not (eq num article)))
418       (setq found (point))
419       (while (and (< (point) max)
420                   (or (not (numberp num))
421                       (< num article)))
422         (forward-line 1)
423         (setq found (point))
424         (or (eobp)
425             (= (setq num (read cur)) article)))
426       (unless (eq num article)
427         (goto-char found)))
428     (beginning-of-line)
429     (eq num article)))
430
431 ;; Various cruft the backends and Gnus need to communicate.
432
433 (defvar nntp-server-buffer nil)
434 (defvar gnus-verbose-backends 7
435   "*A number that says how talkative the Gnus backends should be.")
436 (defvar gnus-nov-is-evil nil
437   "If non-nil, Gnus backends will never output headers in the NOV format.")
438 (defvar news-reply-yank-from nil)
439 (defvar news-reply-yank-message-id nil)
440
441 (defvar nnheader-callback-function nil)
442
443 (defun nnheader-init-server-buffer ()
444   "Initialize the Gnus-backend communication buffer."
445   (save-excursion
446     (unless (gnus-buffer-live-p nntp-server-buffer)
447       (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
448     (mm-enable-multibyte)
449     (set-buffer nntp-server-buffer)
450     (erase-buffer)
451     (kill-all-local-variables)
452     (setq case-fold-search t)           ;Should ignore case.
453     t))
454
455 ;;; Various functions the backends use.
456
457 (defun nnheader-file-error (file)
458   "Return a string that says what is wrong with FILE."
459   (format
460    (cond
461     ((not (file-exists-p file))
462      "%s does not exist")
463     ((file-directory-p file)
464      "%s is a directory")
465     ((not (file-readable-p file))
466      "%s is not readable"))
467    file))
468
469 (defun nnheader-insert-head (file)
470   "Insert the head of the article."
471   (when (file-exists-p file)
472     (if (eq nnheader-max-head-length t)
473         ;; Just read the entire file.
474         (nnheader-insert-file-contents file)
475       ;; Read 1K blocks until we find a separator.
476       (let ((beg 0)
477             format-alist)
478         (while (and (eq nnheader-head-chop-length
479                         (nth 1 (nnheader-insert-file-contents
480                                 file nil beg
481                                 (incf beg nnheader-head-chop-length))))
482                     (prog1 (not (search-forward "\n\n" nil t))
483                       (goto-char (point-max)))
484                     (or (null nnheader-max-head-length)
485                         (< beg nnheader-max-head-length))))))
486     t))
487
488 (defun nnheader-article-p ()
489   "Say whether the current buffer looks like an article."
490   (goto-char (point-min))
491   (if (not (search-forward "\n\n" nil t))
492       nil
493     (narrow-to-region (point-min) (1- (point)))
494     (goto-char (point-min))
495     (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
496       (goto-char (match-end 0)))
497     (prog1
498         (eobp)
499       (widen))))
500
501 (defun nnheader-insert-references (references message-id)
502   "Insert a References header based on REFERENCES and MESSAGE-ID."
503   (if (and (not references) (not message-id))
504       ;; This is invalid, but not all articles have Message-IDs.
505       ()
506     (mail-position-on-field "References")
507     (let ((begin (save-excursion (beginning-of-line) (point)))
508           (fill-column 78)
509           (fill-prefix "\t"))
510       (when references
511         (insert references))
512       (when (and references message-id)
513         (insert " "))
514       (when message-id
515         (insert message-id))
516       ;; Fold long References lines to conform to RFC1036 (sort of).
517       ;; The region must end with a newline to fill the region
518       ;; without inserting extra newline.
519       (fill-region-as-paragraph begin (1+ (point))))))
520
521 (defun nnheader-replace-header (header new-value)
522   "Remove HEADER and insert the NEW-VALUE."
523   (save-excursion
524     (save-restriction
525       (nnheader-narrow-to-headers)
526       (prog1
527           (message-remove-header header)
528         (goto-char (point-max))
529         (insert header ": " new-value "\n")))))
530
531 (defun nnheader-narrow-to-headers ()
532   "Narrow to the head of an article."
533   (widen)
534   (narrow-to-region
535    (goto-char (point-min))
536    (if (search-forward "\n\n" nil t)
537        (1- (point))
538      (point-max)))
539   (goto-char (point-min)))
540
541 (defun nnheader-set-temp-buffer (name &optional noerase)
542   "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
543   (set-buffer (get-buffer-create name))
544   (buffer-disable-undo)
545   (unless noerase
546     (erase-buffer))
547   (current-buffer))
548
549 (defvar jka-compr-compression-info-list)
550 (defvar nnheader-numerical-files
551   (if (boundp 'jka-compr-compression-info-list)
552       (concat "\\([0-9]+\\)\\("
553               (mapconcat (lambda (i) (aref i 0))
554                          jka-compr-compression-info-list "\\|")
555               "\\)?")
556     "[0-9]+$")
557   "Regexp that match numerical files.")
558
559 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
560   "Regexp that matches numerical file names.")
561
562 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
563   "Regexp that matches numerical full file paths.")
564
565 (defsubst nnheader-file-to-number (file)
566   "Take a file name and return the article number."
567   (if (string= nnheader-numerical-short-files "^[0-9]+$")
568       (string-to-int file)
569     (string-match nnheader-numerical-short-files file)
570     (string-to-int (match-string 0 file))))
571
572 (defun nnheader-directory-files-safe (&rest args)
573   ;; It has been reported numerous times that `directory-files'
574   ;; fails with an alarming frequency on NFS mounted file systems.
575   ;; This function executes that function twice and returns
576   ;; the longest result.
577   (let ((first (apply 'directory-files args))
578         (second (apply 'directory-files args)))
579     (if (> (length first) (length second))
580         first
581       second)))
582
583 (defun nnheader-directory-articles (dir)
584   "Return a list of all article files in a directory."
585   (mapcar 'nnheader-file-to-number
586           (nnheader-directory-files-safe
587            dir nil nnheader-numerical-short-files t)))
588
589 (defun nnheader-article-to-file-alist (dir)
590   "Return an alist of article/file pairs in DIR."
591   (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
592           (nnheader-directory-files-safe
593            dir nil nnheader-numerical-short-files t)))
594
595 (defun nnheader-fold-continuation-lines ()
596   "Fold continuation lines in the current buffer."
597   (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
598
599 (defun nnheader-translate-file-chars (file &optional full)
600   "Translate FILE into something that can be a file name.
601 If FULL, translate everything."
602   (if (null nnheader-file-name-translation-alist)
603       ;; No translation is necessary.
604       file
605     (let* ((i 0)
606            trans leaf path len)
607       (if full
608           ;; Do complete translation.
609           (setq leaf (copy-sequence file)
610                 path "")
611         ;; We translate -- but only the file name.  We leave the directory
612         ;; alone.
613         (if (string-match "/[^/]+\\'" file)
614             ;; This is needed on NT's and stuff.
615             (setq leaf (substring file (1+ (match-beginning 0)))
616                   path (substring file 0 (1+ (match-beginning 0))))
617           ;; Fall back on this.
618           (setq leaf (file-name-nondirectory file)
619                 path (file-name-directory file))))
620       (setq len (length leaf))
621       (while (< i len)
622         (when (setq trans (cdr (assq (aref leaf i)
623                                      nnheader-file-name-translation-alist)))
624           (aset leaf i trans))
625         (incf i))
626       (concat path leaf))))
627
628 (defun nnheader-report (backend &rest args)
629   "Report an error from the BACKEND.
630 The first string in ARGS can be a format string."
631   (set (intern (format "%s-status-string" backend))
632        (if (< (length args) 2)
633            (car args)
634          (apply 'format args)))
635   nil)
636
637 (defun nnheader-get-report (backend)
638   "Get the most recent report from BACKEND."
639   (condition-case ()
640       (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
641                                                   backend))))
642     (error (nnheader-message 5 ""))))
643
644 (defun nnheader-insert (format &rest args)
645   "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
646 If FORMAT isn't a format string, it and all ARGS will be inserted
647 without formatting."
648   (save-excursion
649     (set-buffer nntp-server-buffer)
650     (erase-buffer)
651     (if (string-match "%" format)
652         (insert (apply 'format format args))
653       (apply 'insert format args))
654     t))
655
656 (defun nnheader-replace-chars-in-string (string from to)
657   "Replace characters in STRING from FROM to TO."
658   (let ((string (substring string 0))   ;Copy string.
659         (len (length string))
660         (idx 0))
661     ;; Replace all occurrences of FROM with TO.
662     (while (< idx len)
663       (when (= (aref string idx) from)
664         (aset string idx to))
665       (setq idx (1+ idx)))
666     string))
667
668 (defun nnheader-file-to-group (file &optional top)
669   "Return a group name based on FILE and TOP."
670   (nnheader-replace-chars-in-string
671    (if (not top)
672        file
673      (condition-case ()
674          (substring (expand-file-name file)
675                     (length
676                      (expand-file-name
677                       (file-name-as-directory top))))
678        (error "")))
679    ?/ ?.))
680
681 (defun nnheader-message (level &rest args)
682   "Message if the Gnus backends are talkative."
683   (if (or (not (numberp gnus-verbose-backends))
684           (<= level gnus-verbose-backends))
685       (apply 'message args)
686     (apply 'format args)))
687
688 (defun nnheader-be-verbose (level)
689   "Return whether the backends should be verbose on LEVEL."
690   (or (not (numberp gnus-verbose-backends))
691       (<= level gnus-verbose-backends)))
692
693 (defvar nnheader-pathname-coding-system 'binary
694   "*Coding system for pathname.")
695
696 (defun nnheader-group-pathname (group dir &optional file)
697   "Make pathname for GROUP."
698   (concat
699    (let ((dir (file-name-as-directory (expand-file-name dir))))
700      ;; If this directory exists, we use it directly.
701      (if (file-directory-p (concat dir group))
702          (concat dir group "/")
703        ;; If not, we translate dots into slashes.
704        (concat dir
705                (mm-encode-coding-string
706                 (nnheader-replace-chars-in-string group ?. ?/)
707                 nnheader-pathname-coding-system)
708                "/")))
709    (cond ((null file) "")
710          ((numberp file) (int-to-string file))
711          (t file))))
712
713 (defun nnheader-functionp (form)
714   "Return non-nil if FORM is funcallable."
715   (or (and (symbolp form) (fboundp form))
716       (and (listp form) (eq (car form) 'lambda))))
717
718 (defun nnheader-concat (dir &rest files)
719   "Concat DIR as directory to FILE."
720   (apply 'concat (file-name-as-directory dir) files))
721
722 (defun nnheader-ms-strip-cr ()
723   "Strip ^M from the end of all lines."
724   (save-excursion
725     (goto-char (point-min))
726     (while (re-search-forward "\r$" nil t)
727       (delete-backward-char 1))))
728
729 (defun nnheader-file-size (file)
730   "Return the file size of FILE or 0."
731   (or (nth 7 (file-attributes file)) 0))
732
733 (defun nnheader-find-etc-directory (package &optional file)
734   "Go through the path and find the \".../etc/PACKAGE\" directory.
735 If FILE, find the \".../etc/PACKAGE\" file instead."
736   (let ((path load-path)
737         dir result)
738     ;; We try to find the dir by looking at the load path,
739     ;; stripping away the last component and adding "etc/".
740     (while path
741       (if (and (car path)
742                (file-exists-p
743                 (setq dir (concat
744                            (file-name-directory
745                             (directory-file-name (car path)))
746                            "etc/" package
747                            (if file "" "/"))))
748                (or file (file-directory-p dir)))
749           (setq result dir
750                 path nil)
751         (setq path (cdr path))))
752     result))
753
754 (defvar ange-ftp-path-format)
755 (defvar efs-path-regexp)
756 (defun nnheader-re-read-dir (path)
757   "Re-read directory PATH if PATH is on a remote system."
758   (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
759       (when (string-match efs-path-regexp path)
760         (efs-re-read-dir path))
761     (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
762       (when (string-match (car ange-ftp-path-format) path)
763         (ange-ftp-re-read-dir path)))))
764
765 (defvar nnheader-file-coding-system 'raw-text
766   "Coding system used in file backends of Gnus.")
767
768 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
769   "Like `insert-file-contents', q.v., but only reads in the file.
770 A buffer may be modified in several ways after reading into the buffer due
771 to advanced Emacs features, such as file-name-handlers, format decoding,
772 find-file-hooks, etc.
773   This function ensures that none of these modifications will take place."
774   (let ((format-alist nil)
775         (auto-mode-alist (nnheader-auto-mode-alist))
776         (default-major-mode 'fundamental-mode)
777         (enable-local-variables nil)
778         (after-insert-file-functions nil)
779         (enable-local-eval nil)
780         (find-file-hooks nil)
781         (coding-system-for-read nnheader-file-coding-system))
782     (insert-file-contents filename visit beg end replace)))
783
784 (defun nnheader-find-file-noselect (&rest args)
785   (let ((format-alist nil)
786         (auto-mode-alist (nnheader-auto-mode-alist))
787         (default-major-mode 'fundamental-mode)
788         (enable-local-variables nil)
789         (after-insert-file-functions nil)
790         (enable-local-eval nil)
791         (find-file-hooks nil)
792         (coding-system-for-read nnheader-file-coding-system))
793     (apply 'find-file-noselect args)))
794
795 (defun nnheader-auto-mode-alist ()
796   "Return an `auto-mode-alist' with only the .gz (etc) thingies."
797   (let ((alist auto-mode-alist)
798         out)
799     (while alist
800       (when (listp (cdar alist))
801         (push (car alist) out))
802       (pop alist))
803     (nreverse out)))
804
805 (defun nnheader-directory-regular-files (dir)
806   "Return a list of all regular files in DIR."
807   (let ((files (directory-files dir t))
808         out)
809     (while files
810       (when (file-regular-p (car files))
811         (push (car files) out))
812       (pop files))
813     (nreverse out)))
814
815 (defun nnheader-directory-files (&rest args)
816   "Same as `directory-files', but prune \".\" and \"..\"."
817   (let ((files (apply 'directory-files args))
818         out)
819     (while files
820       (unless (member (file-name-nondirectory (car files)) '("." ".."))
821         (push (car files) out))
822       (pop files))
823     (nreverse out)))
824
825 (defmacro nnheader-skeleton-replace (from &optional to regexp)
826   `(let ((new (generate-new-buffer " *nnheader replace*"))
827          (cur (current-buffer))
828          (start (point-min)))
829      (set-buffer cur)
830      (goto-char (point-min))
831      (while (,(if regexp 're-search-forward 'search-forward)
832              ,from nil t)
833        (insert-buffer-substring
834         cur start (prog1 (match-beginning 0) (set-buffer new)))
835        (goto-char (point-max))
836        ,(when to `(insert ,to))
837        (set-buffer cur)
838        (setq start (point)))
839      (insert-buffer-substring
840       cur start (prog1 (point-max) (set-buffer new)))
841      (copy-to-buffer cur (point-min) (point-max))
842      (kill-buffer (current-buffer))
843      (set-buffer cur)))
844
845 (defun nnheader-replace-string (from to)
846   "Do a fast replacement of FROM to TO from point to point-max."
847   (nnheader-skeleton-replace from to))
848
849 (defun nnheader-replace-regexp (from to)
850   "Do a fast regexp replacement of FROM to TO from point to point-max."
851   (nnheader-skeleton-replace from to t))
852
853 (defun nnheader-strip-cr ()
854   "Strip all \r's from the current buffer."
855   (nnheader-skeleton-replace "\r"))
856
857 (fset 'nnheader-run-at-time 'run-at-time)
858 (fset 'nnheader-cancel-timer 'cancel-timer)
859 (fset 'nnheader-cancel-function-timers 'cancel-function-timers)
860
861 (when (string-match "XEmacs\\|Lucid" emacs-version)
862   (require 'nnheaderxm))
863
864 (run-hooks 'nnheader-load-hook)
865
866 (provide 'nnheader)
867
868 ;;; nnheader.el ends here