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