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