1 ;;; nnheader.el --- header access macros for Semi-gnus and its backends
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1993, 1994, 1995, 1996,
5 ;; Free Software Foundation, Inc.
7 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
10 ;; Katsumi Yamaoka <yamaoka@jpl.org>
11 ;; Keywords: mail, news, MIME
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'static))
39 ;; Reduce the required value of `recursive-load-depth-limit' for Emacs 21.
46 (autoload 'gnus-sorted-intersection "gnus-range")
47 (autoload 'gnus-intersection "gnus-range")
48 (autoload 'gnus-sorted-complement "gnus-range"))
50 (defvar nnheader-max-head-length 4096
51 "*Max length of the head of articles.")
53 (defvar nnheader-head-chop-length 2048
54 "*Length of each read operation when trying to fetch HEAD headers.")
56 (defvar nnheader-file-name-translation-alist nil
57 "*Alist that says how to translate characters in file names.
58 For instance, if \":\" is invalid as a file character in file names
59 on your system, you could say something like:
61 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
63 (defvar nnheader-text-coding-system
64 (if (memq system-type '(windows-nt ms-dos ms-windows))
67 "Text-safe coding system (For removing ^M).
68 This variable is a substitute for `mm-text-coding-system'.")
70 (defvar nnheader-text-coding-system-for-write nil
71 "Text coding system for write.
72 This variable is a substitute for `mm-text-coding-system-for-write'.")
75 (autoload 'nnmail-message-id "nnmail")
76 (autoload 'mail-position-on-field "sendmail")
77 (autoload 'message-remove-header "message")
78 (autoload 'gnus-point-at-eol "gnus-util")
79 (autoload 'gnus-delete-line "gnus-util")
80 (autoload 'gnus-buffer-live-p "gnus-util"))
82 ;;; Header access macros.
84 ;; These macros may look very much like the ones in GNUS 4.1. They
85 ;; are, in a way, but you should note that the indices they use have
86 ;; been changed from the internal GNUS format to the NOV format. The
87 ;; makes it possible to read headers from XOVER much faster.
89 ;; The format of a header is now:
90 ;; [number subject from date id references chars lines xref extra]
92 ;; (That next-to-last entry is defined as "misc" in the NOV format,
93 ;; but Gnus uses it for xrefs.)
97 (defmacro mail-header-number (header)
98 "Return article number in HEADER."
99 `(mime-entity-location-internal ,header))
101 (defmacro mail-header-set-number (header number)
102 "Set article number of HEADER to NUMBER."
103 `(mime-entity-set-location-internal ,header ,number))
105 (defalias 'mail-header-subject 'mime-gnus-entity-subject-internal)
106 (defalias 'mail-header-set-subject 'mime-gnus-entity-set-subject-internal)
108 (defalias 'mail-header-from 'mime-gnus-entity-from-internal)
109 (defalias 'mail-header-set-from 'mime-gnus-entity-set-from-internal)
111 (defalias 'mail-header-date 'mime-gnus-entity-date-internal)
112 (defalias 'mail-header-set-date 'mime-gnus-entity-set-date-internal)
114 (defalias 'mail-header-message-id 'mime-gnus-entity-id-internal)
115 (defalias 'mail-header-id 'mime-gnus-entity-id-internal)
116 (defalias 'mail-header-set-message-id 'mime-gnus-entity-set-id-internal)
117 (defalias 'mail-header-set-id 'mime-gnus-entity-set-id-internal)
119 (defalias 'mail-header-references 'mime-gnus-entity-references-internal)
120 (defalias 'mail-header-set-references
121 'mime-gnus-entity-set-references-internal)
123 (defalias 'mail-header-chars 'mime-gnus-entity-chars-internal)
124 (defalias 'mail-header-set-chars 'mime-gnus-entity-set-chars-internal)
126 (defalias 'mail-header-lines 'mime-gnus-entity-lines-internal)
127 (defalias 'mail-header-set-lines 'mime-gnus-entity-set-lines-internal)
129 (defalias 'mail-header-xref 'mime-gnus-entity-xref-internal)
130 (defalias 'mail-header-set-xref 'mime-gnus-entity-set-xref-internal)
132 (defalias 'nnheader-decode-subject
133 (mime-find-field-decoder 'Subject 'nov))
134 (defalias 'nnheader-decode-from
135 (mime-find-field-decoder 'From 'nov))
137 (defalias 'mail-header-extra 'mime-gnus-entity-extra-internal)
138 (defalias 'mail-header-set-extra 'mime-gnus-entity-set-extra-internal)
140 (defun nnheader-decode-field-body (field-body field-name
141 &optional mode max-column)
142 (mime-decode-field-body field-body
143 (if (stringp field-name)
144 (intern (capitalize field-name))
148 (defsubst make-full-mail-header (&optional number subject from date id
149 references chars lines xref
151 "Create a new mail header structure initialized with the parameters given."
152 (luna-make-entity (mm-expand-class-name 'gnus)
155 (nnheader-decode-subject subject))
157 (nnheader-decode-from from))
160 :references references
164 :original-header (list (cons 'Subject subject)
168 (defsubst make-full-mail-header-from-decoded-header
169 (&optional number subject from date id references chars lines xref extra)
170 "Create a new mail header structure initialized with the parameters given."
171 (luna-make-entity (mm-expand-class-name 'gnus)
177 :references references
183 (defsubst make-mail-header (&optional init)
184 "Create a new mail header structure initialized with INIT."
185 (make-full-mail-header init init init init init
186 init init init init init))
188 ;; fake message-ids: generation and detection
190 (defvar nnheader-fake-message-id 1)
192 (defsubst nnheader-generate-fake-message-id ()
193 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
195 (defsubst nnheader-fake-message-id-p (id)
196 (save-match-data ; regular message-id's are <.*>
197 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
199 ;; Parsing headers and NOV lines.
201 (defsubst nnheader-header-value ()
204 (buffer-substring (match-end 0) (std11-field-end))
207 (defun nnheader-parse-head (&optional naked)
208 (let ((case-fold-search t)
209 (cur (current-buffer))
210 (buffer-read-only nil)
211 in-reply-to lines p ref)
212 (goto-char (point-min))
215 ;; Search to the beginning of the next header. Error messages
216 ;; do not begin with 2 or 3.
218 (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
219 ;; This implementation of this function, with nine
220 ;; search-forwards instead of the one re-search-forward and
221 ;; a case (which basically was the old function) is actually
222 ;; about twice as fast, even though it looks messier. You
223 ;; can't have everything, I guess. Speed and elegance
224 ;; don't always go hand in hand.
225 (make-full-mail-header
235 (narrow-to-region (point)
236 (or (and (search-forward "\n.\n" nil t)
242 (if (search-forward "\nsubject: " nil t)
243 (nnheader-header-value) "(none)"))
247 (if (or (search-forward "\nfrom: " nil t)
248 (search-forward "\nfrom:" nil t))
249 (nnheader-header-value) "(nobody)"))
253 (if (search-forward "\ndate: " nil t)
254 (nnheader-header-value) ""))
258 (if (search-forward "\nmessage-id:" nil t)
260 (1- (or (search-forward "<" (gnus-point-at-eol) t)
262 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
263 ;; If there was no message-id, we just fake one to make
264 ;; subsequent routines simpler.
265 (nnheader-generate-fake-message-id)))
269 (if (search-forward "\nreferences: " nil t)
270 (nnheader-header-value)
271 ;; Get the references from the in-reply-to header if there
272 ;; were no references and the in-reply-to header looks
274 (if (and (search-forward "\nin-reply-to: " nil t)
275 (setq in-reply-to (nnheader-header-value))
276 (string-match "<[^\n>]+>" in-reply-to))
278 (setq ref (substring in-reply-to (match-beginning 0)
280 (while (string-match "<[^\n>]+>"
281 in-reply-to (match-end 0))
282 (setq ref2 (substring in-reply-to (match-beginning 0)
284 (when (> (length ref2) (length ref))
293 (if (search-forward "\nlines: " nil t)
294 (if (numberp (setq lines (read cur)))
300 (and (search-forward "\nxref: " nil t)
301 (nnheader-header-value)))
304 (when nnmail-extra-headers
305 (let ((extra nnmail-extra-headers)
309 (when (search-forward
310 (concat "\n" (symbol-name (car extra)) ": ") nil t)
311 (push (cons (car extra) (nnheader-header-value))
316 (goto-char (point-min))
319 (defmacro nnheader-nov-skip-field ()
320 '(search-forward "\t" eol 'move))
322 (defmacro nnheader-nov-field ()
323 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
325 (defmacro nnheader-nov-read-integer ()
327 (if (eq (char-after) ?\t)
329 (let ((num (condition-case nil
330 (read (current-buffer))
332 (if (numberp num) num 0)))
334 (search-forward "\t" eol 'move))))
336 (defmacro nnheader-nov-parse-extra ()
338 (while (not (memq (char-after) '(?\n nil)))
339 (setq string (nnheader-nov-field))
340 (when (string-match "^\\([^ :]+\\): " string)
341 (push (cons (intern (match-string 1 string))
342 (substring string (match-end 0)))
346 (defmacro nnheader-nov-read-message-id ()
347 '(let ((id (nnheader-nov-field)))
348 (if (string-match "^<[^>]+>$" id)
350 (nnheader-generate-fake-message-id))))
352 (defun nnheader-parse-nov ()
353 (let ((eol (gnus-point-at-eol)))
354 (make-full-mail-header
355 (nnheader-nov-read-integer) ; number
356 (nnheader-nov-field) ; subject
357 (nnheader-nov-field) ; from
358 (nnheader-nov-field) ; date
359 (nnheader-nov-read-message-id) ; id
360 (nnheader-nov-field) ; refs
361 (nnheader-nov-read-integer) ; chars
362 (nnheader-nov-read-integer) ; lines
363 (if (eq (char-after) ?\n)
365 (if (looking-at "Xref: ")
366 (goto-char (match-end 0)))
367 (nnheader-nov-field)) ; Xref
368 (nnheader-nov-parse-extra)))) ; extra
370 (defun nnheader-insert-nov (header)
371 (princ (mail-header-number header) (current-buffer))
375 (or (mime-entity-fetch-field header 'Subject) "(none)") "\t"
376 (or (mime-entity-fetch-field header 'From) "(nobody)") "\t"
377 (or (mail-header-date header) "") "\t"
378 (or (mail-header-id header)
381 (or (mail-header-references header) "") "\t")
382 (princ (or (mail-header-chars header) 0) (current-buffer))
384 (princ (or (mail-header-lines header) 0) (current-buffer))
386 (when (mail-header-xref header)
387 (insert "Xref: " (mail-header-xref header)))
388 (when (or (mail-header-xref header)
389 (mail-header-extra header))
391 (when (mail-header-extra header)
392 (let ((extra (mail-header-extra header)))
394 (insert (symbol-name (caar extra))
395 ": " (cdar extra) "\t")
399 (while (search-backward "\n" p t)
403 (defun nnheader-insert-header (header)
405 "Subject: " (or (mail-header-subject header) "(none)") "\n"
406 "From: " (or (mail-header-from header) "(nobody)") "\n"
407 "Date: " (or (mail-header-date header) "") "\n"
408 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
409 "References: " (or (mail-header-references header) "") "\n"
411 (princ (or (mail-header-lines header) 0) (current-buffer))
414 (defun nnheader-insert-article-line (article)
415 (goto-char (point-min))
417 (princ article (current-buffer))
418 (insert " Article retrieved.\n")
419 (search-forward "\n\n" nil 'move)
420 (delete-region (point) (point-max))
424 (defun nnheader-nov-delete-outside-range (beg end)
425 "Delete all NOV lines that lie outside the BEG to END range."
426 ;; First we find the first wanted line.
427 (nnheader-find-nov-line beg)
428 (delete-region (point-min) (point))
429 ;; Then we find the last wanted line.
430 (when (nnheader-find-nov-line end)
432 (delete-region (point) (point-max)))
434 (defun nnheader-find-nov-line (article)
435 "Put point at the NOV line that start with ARTICLE.
436 If ARTICLE doesn't exist, put point where that line
437 would have been. The function will return non-nil if
438 the line could be found."
439 ;; This function basically does a binary search.
440 (let ((max (point-max))
441 (min (goto-char (point-min)))
442 (cur (current-buffer))
446 (goto-char (/ (+ max min) 2))
448 (if (or (= (point) prev)
452 (while (and (not (numberp (setq num (read cur))))
455 (cond ((> num article)
460 (setq found 'yes)))))
461 ;; We may be at the first line.
464 (setq num (read cur)))
465 ;; Now we may have found the article we're looking for, or we
466 ;; may be somewhere near it.
467 (when (and (not (eq found 'yes))
468 (not (eq num article)))
470 (while (and (< (point) max)
471 (or (not (numberp num))
476 (= (setq num (read cur)) article)))
477 (unless (eq num article)
482 (defun nnheader-retrieve-headers-from-directory* (articles
483 directory dependencies
485 fetch-old force-new large
489 (number (length articles))
491 (file-name-coding-system 'binary)
492 (pathname-coding-system 'binary)
494 (cur (current-buffer))
496 headers header id end ref in-reply-to lines chars ctype)
497 ;; We don't support fetching by Message-ID.
498 (if (stringp (car articles))
501 (when (and (file-exists-p
502 (setq file (expand-file-name
504 (setq article (pop articles)))
506 (not (file-directory-p file)))
508 (nnheader-insert-head file)
510 (std11-narrow-to-header)
513 (make-full-mail-header
517 (or (std11-fetch-field "Subject")
520 (or (std11-fetch-field "From")
523 (or (std11-fetch-field "Date")
527 (goto-char (point-min))
528 (setq id (if (re-search-forward
529 "^Message-ID: *\\(<[^\n\t> ]+>\\)" nil t)
530 ;; We do it this way to make sure the Message-ID
531 ;; is (somewhat) syntactically valid.
532 (buffer-substring (match-beginning 1)
534 ;; If there was no message-id, we just fake one
535 ;; to make subsequent routines simpler.
536 (nnheader-generate-fake-message-id))))
539 (goto-char (point-min))
540 (if (search-forward "\nReferences: " nil t)
544 (buffer-substring (match-end 0) (std11-field-end))
549 (search-backward ">" end t)
552 (search-backward "<" end t)
554 ;; Get the references from the in-reply-to header if there
555 ;; were no references and the in-reply-to header looks
557 (if (and (search-forward "\nIn-Reply-To: " nil t)
559 (buffer-substring (match-end 0)
561 (string-match "<[^>]+>" in-reply-to))
563 (setq ref (substring in-reply-to (match-beginning 0)
565 (while (string-match "<[^>]+>"
566 in-reply-to (match-end 0))
568 (substring in-reply-to (match-beginning 0)
570 (when (> (length ref2) (length ref))
576 (goto-char (point-min))
577 (if (search-forward "\nChars: " nil t)
578 (if (numberp (setq chars (ignore-errors (read cur))))
583 (goto-char (point-min))
584 (if (search-forward "\nLines: " nil t)
585 (if (numberp (setq lines (ignore-errors (read cur))))
589 (std11-fetch-field "Xref")
591 (goto-char (point-min))
592 (if (setq ctype (std11-fetch-field "Content-Type"))
593 (mime-entity-set-content-type-internal
594 header (mime-parse-Content-Type ctype)))
597 (gnus-dependencies-add-header
598 header dependencies force-new))
599 (push header headers))
601 (setq count (1+ count))
605 (nnheader-message 5 "%s: Receiving headers... %d%%"
607 (/ (* count 100) number))))
610 (nnheader-message 5 "%s: Receiving headers...done" backend))
614 (defun nnheader-retrieve-headers-from-directory (articles
615 directory dependencies
617 fetch-old force-new large
620 (nreverse (nnheader-retrieve-headers-from-directory*
621 articles directory dependencies
622 fetch-old force-new large backend))))
624 (defun nnheader-get-newsgroup-headers-xover* (sequence
626 force-new dependencies
628 "Parse the news overview data in the server buffer, and return a
629 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
630 ;; Get the Xref when the users reads the articles since most/some
631 ;; NNTP servers do not include Xrefs when using XOVER.
632 ;; (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
633 (let ((cur nntp-server-buffer)
634 number headers header)
636 (set-buffer nntp-server-buffer)
637 ;; Allow the user to mangle the headers before parsing them.
638 (gnus-run-hooks 'gnus-parse-headers-hook)
639 (goto-char (point-min))
642 (while (and sequence (not (eobp)))
643 (setq number (read cur))
645 (< (car sequence) number))
646 (setq sequence (cdr sequence)))
648 (eq number (car sequence))
650 (setq sequence (cdr sequence))
653 number dependencies force-new))))
654 (push header headers))
657 (gnus-error 4 "Strange nov line (%d)"
658 (count-lines (point-min) (point)))))
660 ;; A common bug in inn is that if you have posted an article and
661 ;; then retrieves the active file, it will answer correctly --
662 ;; the new article is included. However, a NOV entry for the
663 ;; article may not have been generated yet, so this may fail.
664 ;; We work around this problem by retrieving the last few
665 ;; headers using HEAD.
668 ;; Various cruft the backends and Gnus need to communicate.
670 (defvar nntp-server-buffer nil)
671 (defvar nntp-process-response nil)
672 (defvar gnus-verbose-backends 7
673 "*A number that says how talkative the Gnus backends should be.")
674 (defvar gnus-nov-is-evil nil
675 "If non-nil, Gnus backends will never output headers in the NOV format.")
676 (defvar news-reply-yank-from nil)
677 (defvar news-reply-yank-message-id nil)
679 (defvar nnheader-callback-function nil)
681 (defun nnheader-init-server-buffer ()
682 "Initialize the Gnus-backend communication buffer."
684 (unless (gnus-buffer-live-p nntp-server-buffer)
685 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
686 (set-buffer nntp-server-buffer)
688 (kill-all-local-variables)
689 (setq case-fold-search t) ;Should ignore case.
690 (set (make-local-variable 'nntp-process-response) nil)
693 ;;; Various functions the backends use.
695 (defun nnheader-file-error (file)
696 "Return a string that says what is wrong with FILE."
699 ((not (file-exists-p file))
701 ((file-directory-p file)
703 ((not (file-readable-p file))
704 "%s is not readable"))
707 (defun nnheader-insert-head (file)
708 "Insert the head of the article."
709 (when (file-exists-p file)
710 (if (eq nnheader-max-head-length t)
711 ;; Just read the entire file.
712 (nnheader-insert-file-contents file)
713 ;; Read 1K blocks until we find a separator.
716 (while (and (eq nnheader-head-chop-length
717 (nth 1 (nnheader-insert-file-contents
719 (incf beg nnheader-head-chop-length))))
720 (prog1 (not (search-forward "\n\n" nil t))
721 (goto-char (point-max)))
722 (or (null nnheader-max-head-length)
723 (< beg nnheader-max-head-length))))))
726 (defun nnheader-article-p ()
727 "Say whether the current buffer looks like an article."
728 (goto-char (point-min))
729 (if (not (search-forward "\n\n" nil t))
731 (narrow-to-region (point-min) (1- (point)))
732 (goto-char (point-min))
733 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
734 (goto-char (match-end 0)))
739 (defun nnheader-insert-references (references message-id)
740 "Insert a References header based on REFERENCES and MESSAGE-ID."
741 (if (and (not references) (not message-id))
742 ;; This is invalid, but not all articles have Message-IDs.
744 (mail-position-on-field "References")
745 (let ((begin (save-excursion (beginning-of-line) (point)))
750 (when (and references message-id)
754 ;; Fold long References lines to conform to RFC1036 (sort of).
755 ;; The region must end with a newline to fill the region
756 ;; without inserting extra newline.
757 (fill-region-as-paragraph begin (1+ (point))))))
759 (defun nnheader-replace-header (header new-value)
760 "Remove HEADER and insert the NEW-VALUE."
763 (nnheader-narrow-to-headers)
765 (message-remove-header header)
766 (goto-char (point-max))
767 (insert header ": " new-value "\n")))))
769 (defun nnheader-narrow-to-headers ()
770 "Narrow to the head of an article."
773 (goto-char (point-min))
774 (if (search-forward "\n\n" nil t)
777 (goto-char (point-min)))
779 (defun nnheader-set-temp-buffer (name &optional noerase)
780 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
781 (set-buffer (get-buffer-create name))
782 (buffer-disable-undo)
787 (eval-when-compile (defvar jka-compr-compression-info-list))
788 (defvar nnheader-numerical-files
789 (if (boundp 'jka-compr-compression-info-list)
790 (concat "\\([0-9]+\\)\\("
791 (mapconcat (lambda (i) (aref i 0))
792 jka-compr-compression-info-list "\\|")
795 "Regexp that match numerical files.")
797 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
798 "Regexp that matches numerical file names.")
800 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
801 "Regexp that matches numerical full file paths.")
803 (defsubst nnheader-file-to-number (file)
804 "Take a FILE name and return the article number."
805 (if (string= nnheader-numerical-short-files "^[0-9]+$")
807 (string-match nnheader-numerical-short-files file)
808 (string-to-int (match-string 0 file))))
810 (defvar nnheader-directory-files-is-safe nil
811 "If non-nil, Gnus believes `directory-files' is safe.
812 It has been reported numerous times that `directory-files' fails with
813 an alarming frequency on NFS mounted file systems. If it is nil,
814 `nnheader-directory-files-safe' is used.")
816 (defun nnheader-directory-files-safe (&rest args)
817 "Execute `directory-files' twice and returns the longer result."
818 (let ((first (apply 'directory-files args))
819 (second (apply 'directory-files args)))
820 (if (> (length first) (length second))
824 (defun nnheader-directory-articles (dir)
825 "Return a list of all article files in directory DIR."
826 (mapcar 'nnheader-file-to-number
827 (if nnheader-directory-files-is-safe
829 dir nil nnheader-numerical-short-files t)
830 (nnheader-directory-files-safe
831 dir nil nnheader-numerical-short-files t))))
833 (defun nnheader-article-to-file-alist (dir)
834 "Return an alist of article/file pairs in DIR."
835 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
836 (if nnheader-directory-files-is-safe
838 dir nil nnheader-numerical-short-files t)
839 (nnheader-directory-files-safe
840 dir nil nnheader-numerical-short-files t))))
842 (defun nnheader-fold-continuation-lines ()
843 "Fold continuation lines in the current buffer."
844 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
846 (defun nnheader-translate-file-chars (file &optional full)
847 "Translate FILE into something that can be a file name.
848 If FULL, translate everything."
849 (if (null nnheader-file-name-translation-alist)
850 ;; No translation is necessary.
855 ;; Do complete translation.
856 (setq leaf (copy-sequence file)
858 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
860 ;; We translate -- but only the file name. We leave the directory
862 (if (and (featurep 'xemacs)
863 (memq system-type '(win32 w32 mswindows windows-nt)))
864 ;; This is needed on NT and stuff, because
865 ;; file-name-nondirectory is not enough to split
866 ;; file names, containing ':', e.g.
867 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
869 ;; we are trying to correctly split such names:
870 ;; "d:file.name" -> "a:" "file.name"
871 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
872 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
874 ;; to translate then only the file name part.
878 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
879 (setq leaf (substring file (match-beginning 2))
880 path (substring file 0 (match-beginning 2)))))
881 ;; Emacs DTRT, says andrewi.
882 (setq leaf (file-name-nondirectory file)
883 path (file-name-directory file))))
884 (setq len (length leaf))
886 (when (setq trans (cdr (assq (aref leaf i)
887 nnheader-file-name-translation-alist)))
890 (concat path leaf))))
892 (defun nnheader-report (backend &rest args)
893 "Report an error from the BACKEND.
894 The first string in ARGS can be a format string."
895 (set (intern (format "%s-status-string" backend))
896 (if (< (length args) 2)
898 (apply 'format args)))
901 (defun nnheader-get-report (backend)
902 "Get the most recent report from BACKEND."
904 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
906 (error (nnheader-message 5 ""))))
908 (defun nnheader-insert (format &rest args)
909 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
910 If FORMAT isn't a format string, it and all ARGS will be inserted
913 (set-buffer nntp-server-buffer)
915 (if (string-match "%" format)
916 (insert (apply 'format format args))
917 (apply 'insert format args))
920 (static-if (fboundp 'subst-char-in-string)
921 (defsubst nnheader-replace-chars-in-string (string from to)
922 (subst-char-in-string from to string))
923 (defun nnheader-replace-chars-in-string (string from to)
924 "Replace characters in STRING from FROM to TO."
925 (let ((string (substring string 0)) ;Copy string.
926 (len (length string))
928 ;; Replace all occurrences of FROM with TO.
930 (when (= (aref string idx) from)
931 (aset string idx to))
935 (defun nnheader-replace-duplicate-chars-in-string (string from to)
936 "Replace characters in STRING from FROM to TO."
937 (let ((string (substring string 0)) ;Copy string.
938 (len (length string))
940 ;; Replace all occurrences of FROM with TO.
942 (setq i (aref string idx))
943 (when (and (eq prev from) (= i from))
944 (aset string (1- idx) to)
945 (aset string idx to))
950 (defun nnheader-file-to-group (file &optional top)
951 "Return a group name based on FILE and TOP."
952 (nnheader-replace-chars-in-string
956 (substring (expand-file-name file)
959 (file-name-as-directory top))))
963 (defun nnheader-message (level &rest args)
964 "Message if the Gnus backends are talkative."
965 (if (or (not (numberp gnus-verbose-backends))
966 (<= level gnus-verbose-backends))
967 (apply 'message args)
968 (apply 'format args)))
970 (defun nnheader-be-verbose (level)
971 "Return whether the backends should be verbose on LEVEL."
972 (or (not (numberp gnus-verbose-backends))
973 (<= level gnus-verbose-backends)))
975 (defvar nnheader-pathname-coding-system 'binary
976 "*Coding system for pathname.")
978 (defun nnheader-group-pathname (group dir &optional file)
979 "Make pathname for GROUP."
981 (let ((dir (file-name-as-directory (expand-file-name dir))))
982 ;; If this directory exists, we use it directly.
983 (file-name-as-directory
984 (if (file-directory-p (concat dir group))
985 (expand-file-name group dir)
986 ;; If not, we translate dots into slashes.
987 (expand-file-name (encode-coding-string
988 (nnheader-replace-chars-in-string group ?. ?/)
989 nnheader-pathname-coding-system)
991 (cond ((null file) "")
992 ((numberp file) (int-to-string file))
995 (defun nnheader-functionp (form)
996 "Return non-nil if FORM is funcallable."
997 (or (and (symbolp form) (fboundp form))
998 (and (listp form) (eq (car form) 'lambda))))
1000 (defun nnheader-concat (dir &rest files)
1001 "Concat DIR as directory to FILES."
1002 (apply 'concat (file-name-as-directory dir) files))
1004 (defun nnheader-ms-strip-cr ()
1005 "Strip ^M from the end of all lines."
1007 (goto-char (point-min))
1008 (while (re-search-forward "\r$" nil t)
1009 (delete-backward-char 1))))
1011 (defun nnheader-file-size (file)
1012 "Return the file size of FILE or 0."
1013 (or (nth 7 (file-attributes file)) 0))
1015 (defun nnheader-find-etc-directory (package &optional file)
1016 "Go through the path and find the \".../etc/PACKAGE\" directory.
1017 If FILE, find the \".../etc/PACKAGE\" file instead."
1018 (let ((path load-path)
1020 ;; We try to find the dir by looking at the load path,
1021 ;; stripping away the last component and adding "etc/".
1026 (file-name-directory
1027 (directory-file-name (car path)))
1030 (or file (file-directory-p dir)))
1033 (setq path (cdr path))))
1037 (defvar ange-ftp-path-format)
1038 (defvar efs-path-regexp))
1039 (defun nnheader-re-read-dir (path)
1040 "Re-read directory PATH if PATH is on a remote system."
1041 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
1042 (when (string-match efs-path-regexp path)
1043 (efs-re-read-dir path))
1044 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
1045 (when (string-match (car ange-ftp-path-format) path)
1046 (ange-ftp-re-read-dir path)))))
1048 (defvar nnheader-file-coding-system 'raw-text
1049 "Coding system used in file backends of Gnus.")
1051 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
1052 "Like `insert-file-contents', q.v., but only reads in the file.
1053 A buffer may be modified in several ways after reading into the buffer due
1054 to advanced Emacs features, such as file-name-handlers, format decoding,
1055 find-file-hooks, etc.
1056 This function ensures that none of these modifications will take place."
1057 (let ((format-alist nil)
1058 (auto-mode-alist (nnheader-auto-mode-alist))
1059 (default-major-mode 'fundamental-mode)
1060 (enable-local-variables nil)
1061 (after-insert-file-functions nil)
1062 (enable-local-eval nil)
1063 (find-file-hooks nil))
1064 (insert-file-contents-as-coding-system
1065 nnheader-file-coding-system filename visit beg end replace)))
1067 (defun nnheader-find-file-noselect (&rest args)
1068 (let ((format-alist nil)
1069 (auto-mode-alist (nnheader-auto-mode-alist))
1070 (default-major-mode 'fundamental-mode)
1071 (enable-local-variables nil)
1072 (after-insert-file-functions nil)
1073 (enable-local-eval nil)
1074 (find-file-hooks nil))
1075 (apply 'find-file-noselect-as-coding-system
1076 nnheader-file-coding-system args)))
1078 (defun nnheader-auto-mode-alist ()
1079 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1080 (let ((alist auto-mode-alist)
1083 (when (listp (cdar alist))
1084 (push (car alist) out))
1088 (defun nnheader-directory-regular-files (dir)
1089 "Return a list of all regular files in DIR."
1090 (let ((files (directory-files dir t))
1093 (when (file-regular-p (car files))
1094 (push (car files) out))
1098 (defun nnheader-directory-files (&rest args)
1099 "Same as `directory-files', but prune \".\" and \"..\"."
1100 (let ((files (apply 'directory-files args))
1103 (unless (member (file-name-nondirectory (car files)) '("." ".."))
1104 (push (car files) out))
1108 (defmacro nnheader-skeleton-replace (from &optional to regexp)
1109 `(let ((new (generate-new-buffer " *nnheader replace*"))
1110 (cur (current-buffer))
1111 (start (point-min)))
1113 (goto-char (point-min))
1114 (while (,(if regexp 're-search-forward 'search-forward)
1116 (insert-buffer-substring
1117 cur start (prog1 (match-beginning 0) (set-buffer new)))
1118 (goto-char (point-max))
1119 ,(when to `(insert ,to))
1121 (setq start (point)))
1122 (insert-buffer-substring
1123 cur start (prog1 (point-max) (set-buffer new)))
1124 (copy-to-buffer cur (point-min) (point-max))
1125 (kill-buffer (current-buffer))
1128 (defun nnheader-replace-string (from to)
1129 "Do a fast replacement of FROM to TO from point to `point-max'."
1130 (nnheader-skeleton-replace from to))
1132 (defun nnheader-replace-regexp (from to)
1133 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1134 (nnheader-skeleton-replace from to t))
1136 (defun nnheader-strip-cr ()
1137 "Strip all \r's from the current buffer."
1138 (nnheader-skeleton-replace "\r"))
1140 (defalias 'nnheader-run-at-time 'run-at-time)
1141 (defalias 'nnheader-cancel-timer 'cancel-timer)
1142 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1143 (defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1145 (defun nnheader-Y-or-n-p (prompt)
1146 "Ask user a \"Y/n\" question. Return t if answer is neither \"n\", \"N\" nor \"C-g\"."
1147 (let ((cursor-in-echo-area t)
1151 (let (message-log-max)
1152 (while (not (memq ans '(?\ ?N ?Y ?\C-g ?\e ?\n ?\r ?n ?y)))
1153 (message "%s(Y/n) " prompt)
1154 (setq ans (read-char-exclusive))))
1155 (if (memq ans '(?\C-g ?N ?n))
1157 (message "%s(Y/n) No" prompt)
1159 (message "%s(Y/n) Yes" prompt)
1162 (defun nnheader-image-load-path (&optional package)
1164 (dolist (path load-path (nreverse result))
1165 (if (file-directory-p
1166 (setq dir (concat (file-name-directory
1167 (directory-file-name path))
1168 "etc/" (or package "gnus/"))))
1170 (push path result))))
1171 (defalias 'mm-image-load-path 'nnheader-image-load-path)
1173 (when (featurep 'xemacs)
1174 (require 'nnheaderxm))
1176 (run-hooks 'nnheader-load-hook)
1180 ;;; nnheader.el ends here