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