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