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