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