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))
39 (defvar nnheader-max-head-length 4096
40 "*Max length of the head of articles.")
42 (defvar nnheader-head-chop-length 2048
43 "*Length of each read operation when trying to fetch HEAD headers.")
45 (defvar nnheader-file-name-translation-alist nil
46 "*Alist that says how to translate characters in file names.
47 For instance, if \":\" is invalid as a file character in file names
48 on your system, you could say something like:
50 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
53 (autoload 'nnmail-message-id "nnmail")
54 (autoload 'mail-position-on-field "sendmail")
55 (autoload 'message-remove-header "message")
56 (autoload 'gnus-point-at-eol "gnus-util")
57 (autoload 'gnus-delete-line "gnus-util")
58 (autoload 'gnus-buffer-live-p "gnus-util"))
60 ;;; Header access macros.
62 ;; These macros may look very much like the ones in GNUS 4.1. They
63 ;; are, in a way, but you should note that the indices they use have
64 ;; been changed from the internal GNUS format to the NOV format. The
65 ;; makes it possible to read headers from XOVER much faster.
67 ;; The format of a header is now:
68 ;; [number subject from date id references chars lines xref extra]
70 ;; (That next-to-last entry is defined as "misc" in the NOV format,
71 ;; but Gnus uses it for xrefs.)
75 (defmacro mail-header-number (header)
76 "Return article number in HEADER."
77 `(mime-entity-location-internal ,header))
79 (defmacro mail-header-set-number (header number)
80 "Set article number of HEADER to NUMBER."
81 `(mime-entity-set-location-internal ,header ,number))
83 (defalias 'mail-header-subject 'mime-gnus-entity-subject-internal)
84 (defalias 'mail-header-set-subject 'mime-gnus-entity-set-subject-internal)
86 (defalias 'mail-header-from 'mime-gnus-entity-from-internal)
87 (defalias 'mail-header-set-from 'mime-gnus-entity-set-from-internal)
89 (defalias 'mail-header-date 'mime-gnus-entity-date-internal)
90 (defalias 'mail-header-set-date 'mime-gnus-entity-set-date-internal)
92 (defalias 'mail-header-message-id 'mime-gnus-entity-id-internal)
93 (defalias 'mail-header-id 'mime-gnus-entity-id-internal)
94 (defalias 'mail-header-set-message-id 'mime-gnus-entity-set-id-internal)
95 (defalias 'mail-header-set-id 'mime-gnus-entity-set-id-internal)
97 (defalias 'mail-header-references 'mime-gnus-entity-references-internal)
98 (defalias 'mail-header-set-references
99 'mime-gnus-entity-set-references-internal)
101 (defalias 'mail-header-chars 'mime-gnus-entity-chars-internal)
102 (defalias 'mail-header-set-chars 'mime-gnus-entity-set-chars-internal)
104 (defalias 'mail-header-lines 'mime-gnus-entity-lines-internal)
105 (defalias 'mail-header-set-lines 'mime-gnus-entity-set-lines-internal)
107 (defalias 'mail-header-xref 'mime-gnus-entity-xref-internal)
108 (defalias 'mail-header-set-xref 'mime-gnus-entity-set-xref-internal)
110 (defalias 'nnheader-decode-subject
111 (mime-find-field-decoder 'Subject 'nov))
112 (defalias 'nnheader-decode-from
113 (mime-find-field-decoder 'From 'nov))
115 (defalias 'mail-header-extra 'mime-gnus-entity-extra-internal)
116 (defalias 'mail-header-set-extra 'mime-gnus-entity-set-extra-internal)
118 (defun nnheader-decode-field-body (field-body field-name
119 &optional mode max-column)
120 (mime-decode-field-body field-body
121 (if (stringp field-name)
122 (intern (capitalize field-name))
126 (defsubst make-full-mail-header (&optional number subject from date id
127 references chars lines xref
129 "Create a new mail header structure initialized with the parameters given."
130 (luna-make-entity (mm-expand-class-name 'gnus)
133 (nnheader-decode-subject subject))
135 (nnheader-decode-from from))
138 :references references
142 :original-header (list (cons 'Subject subject)
146 (defsubst make-full-mail-header-from-decoded-header
147 (&optional number subject from date id references chars lines xref extra)
148 "Create a new mail header structure initialized with the parameters given."
149 (luna-make-entity (mm-expand-class-name 'gnus)
155 :references references
161 (defsubst make-mail-header (&optional init)
162 "Create a new mail header structure initialized with INIT."
163 (make-full-mail-header init init init init init
164 init init init init init))
166 ;; fake message-ids: generation and detection
168 (defvar nnheader-fake-message-id 1)
170 (defsubst nnheader-generate-fake-message-id ()
171 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
173 (defsubst nnheader-fake-message-id-p (id)
174 (save-match-data ; regular message-id's are <.*>
175 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
177 ;; Parsing headers and NOV lines.
179 (defsubst nnheader-header-value ()
180 (buffer-substring (match-end 0) (gnus-point-at-eol)))
182 (defun nnheader-parse-head (&optional naked)
183 (let ((case-fold-search t)
184 (cur (current-buffer))
185 (buffer-read-only nil)
186 in-reply-to lines p ref)
187 (goto-char (point-min))
190 ;; Search to the beginning of the next header. Error messages
191 ;; do not begin with 2 or 3.
193 (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
194 ;; This implementation of this function, with nine
195 ;; search-forwards instead of the one re-search-forward and
196 ;; a case (which basically was the old function) is actually
197 ;; about twice as fast, even though it looks messier. You
198 ;; can't have everything, I guess. Speed and elegance
199 ;; don't always go hand in hand.
200 (make-full-mail-header
210 (narrow-to-region (point)
211 (or (and (search-forward "\n.\n" nil t)
217 (if (search-forward "\nsubject: " nil t)
218 (nnheader-header-value) "(none)"))
222 (if (search-forward "\nfrom: " nil t)
223 (nnheader-header-value) "(nobody)"))
227 (if (search-forward "\ndate: " nil t)
228 (nnheader-header-value) ""))
232 (if (search-forward "\nmessage-id:" nil t)
234 (1- (or (search-forward "<" (gnus-point-at-eol) t)
236 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
237 ;; If there was no message-id, we just fake one to make
238 ;; subsequent routines simpler.
239 (nnheader-generate-fake-message-id)))
243 (if (search-forward "\nreferences: " nil t)
244 (nnheader-header-value)
245 ;; Get the references from the in-reply-to header if there
246 ;; were no references and the in-reply-to header looks
248 (if (and (search-forward "\nin-reply-to: " nil t)
249 (setq in-reply-to (nnheader-header-value))
250 (string-match "<[^\n>]+>" in-reply-to))
252 (setq ref (substring in-reply-to (match-beginning 0)
254 (while (string-match "<[^\n>]+>"
255 in-reply-to (match-end 0))
256 (setq ref2 (substring in-reply-to (match-beginning 0)
258 (when (> (length ref2) (length ref))
267 (if (search-forward "\nlines: " nil t)
268 (if (numberp (setq lines (read cur)))
274 (and (search-forward "\nxref: " nil t)
275 (nnheader-header-value)))
278 (when nnmail-extra-headers
279 (let ((extra nnmail-extra-headers)
283 (when (search-forward
284 (concat "\n" (symbol-name (car extra)) ": ") nil t)
285 (push (cons (car extra) (nnheader-header-value))
290 (goto-char (point-min))
293 (defmacro nnheader-nov-skip-field ()
294 '(search-forward "\t" eol 'move))
296 (defmacro nnheader-nov-field ()
297 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
299 (defmacro nnheader-nov-read-integer ()
301 (if (eq (char-after) ?\t)
303 (let ((num (condition-case nil
304 (read (current-buffer))
306 (if (numberp num) num 0)))
308 (search-forward "\t" eol 'move))))
310 (defmacro nnheader-nov-parse-extra ()
312 (while (not (memq (char-after) '(?\n nil)))
313 (setq string (nnheader-nov-field))
314 (when (string-match "^\\([^ :]+\\): " string)
315 (push (cons (intern (match-string 1 string))
316 (substring string (match-end 0)))
320 (defmacro nnheader-nov-read-message-id ()
321 '(let ((id (nnheader-nov-field)))
322 (if (string-match "^<[^>]+>$" id)
324 (nnheader-generate-fake-message-id))))
326 (defun nnheader-parse-nov ()
327 (let ((eol (gnus-point-at-eol)))
328 (make-full-mail-header
329 (nnheader-nov-read-integer) ; number
330 (nnheader-nov-field) ; subject
331 (nnheader-nov-field) ; from
332 (nnheader-nov-field) ; date
333 (nnheader-nov-read-message-id) ; id
334 (nnheader-nov-field) ; refs
335 (nnheader-nov-read-integer) ; chars
336 (nnheader-nov-read-integer) ; lines
337 (if (eq (char-after) ?\n)
339 (nnheader-nov-field)) ; misc
340 (nnheader-nov-parse-extra)))) ; extra
342 (defun nnheader-insert-nov (header)
343 (princ (mail-header-number header) (current-buffer))
346 (or (mime-entity-fetch-field header 'Subject) "(none)") "\t"
347 (or (mime-entity-fetch-field header 'From) "(nobody)") "\t"
348 (or (mail-header-date header) "") "\t"
349 (or (mail-header-id header)
352 (or (mail-header-references header) "") "\t")
353 (princ (or (mail-header-chars header) 0) (current-buffer))
355 (princ (or (mail-header-lines header) 0) (current-buffer))
357 (when (mail-header-xref header)
358 (insert "Xref: " (mail-header-xref header)))
359 (when (or (mail-header-xref header)
360 (mail-header-extra header))
362 (when (mail-header-extra header)
363 (let ((extra (mail-header-extra header)))
365 (insert (symbol-name (caar extra))
366 ": " (cdar extra) "\t")
370 (defun nnheader-insert-header (header)
372 "Subject: " (or (mail-header-subject header) "(none)") "\n"
373 "From: " (or (mail-header-from header) "(nobody)") "\n"
374 "Date: " (or (mail-header-date header) "") "\n"
375 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
376 "References: " (or (mail-header-references header) "") "\n"
378 (princ (or (mail-header-lines header) 0) (current-buffer))
381 (defun nnheader-insert-article-line (article)
382 (goto-char (point-min))
384 (princ article (current-buffer))
385 (insert " Article retrieved.\n")
386 (search-forward "\n\n" nil 'move)
387 (delete-region (point) (point-max))
391 (defun nnheader-nov-delete-outside-range (beg end)
392 "Delete all NOV lines that lie outside the BEG to END range."
393 ;; First we find the first wanted line.
394 (nnheader-find-nov-line beg)
395 (delete-region (point-min) (point))
396 ;; Then we find the last wanted line.
397 (when (nnheader-find-nov-line end)
399 (delete-region (point) (point-max)))
401 (defun nnheader-find-nov-line (article)
402 "Put point at the NOV line that start with ARTICLE.
403 If ARTICLE doesn't exist, put point where that line
404 would have been. The function will return non-nil if
405 the line could be found."
406 ;; This function basically does a binary search.
407 (let ((max (point-max))
408 (min (goto-char (point-min)))
409 (cur (current-buffer))
413 (goto-char (/ (+ max min) 2))
415 (if (or (= (point) prev)
419 (while (and (not (numberp (setq num (read cur))))
422 (cond ((> num article)
427 (setq found 'yes)))))
428 ;; We may be at the first line.
431 (setq num (read cur)))
432 ;; Now we may have found the article we're looking for, or we
433 ;; may be somewhere near it.
434 (when (and (not (eq found 'yes))
435 (not (eq num article)))
437 (while (and (< (point) max)
438 (or (not (numberp num))
443 (= (setq num (read cur)) article)))
444 (unless (eq num article)
449 (defun nnheader-retrieve-headers-from-directory* (articles
450 directory dependencies
452 fetch-old force-new large
456 (number (length articles))
458 (pathname-coding-system 'binary)
460 (cur (current-buffer))
462 headers header id end ref in-reply-to lines chars ctype)
463 ;; We don't support fetching by Message-ID.
464 (if (stringp (car articles))
467 (when (and (file-exists-p
468 (setq file (expand-file-name
470 (setq article (pop articles)))
472 (not (file-directory-p file)))
474 (nnheader-insert-head file)
476 (std11-narrow-to-header)
479 (make-full-mail-header
483 (or (std11-fetch-field "Subject")
486 (or (std11-fetch-field "From")
489 (or (std11-fetch-field "Date")
493 (goto-char (point-min))
494 (setq id (if (re-search-forward
495 "^Message-ID: *\\(<[^\n\t> ]+>\\)" nil t)
496 ;; We do it this way to make sure the Message-ID
497 ;; is (somewhat) syntactically valid.
498 (buffer-substring (match-beginning 1)
500 ;; If there was no message-id, we just fake one
501 ;; to make subsequent routines simpler.
502 (nnheader-generate-fake-message-id))))
505 (goto-char (point-min))
506 (if (search-forward "\nReferences: " nil t)
510 (buffer-substring (match-end 0) (std11-field-end))
515 (search-backward ">" end t)
518 (search-backward "<" end t)
520 ;; Get the references from the in-reply-to header if there
521 ;; were no references and the in-reply-to header looks
523 (if (and (search-forward "\nIn-Reply-To: " nil t)
525 (buffer-substring (match-end 0)
527 (string-match "<[^>]+>" in-reply-to))
529 (setq ref (substring in-reply-to (match-beginning 0)
531 (while (string-match "<[^>]+>"
532 in-reply-to (match-end 0))
534 (substring in-reply-to (match-beginning 0)
536 (when (> (length ref2) (length ref))
542 (goto-char (point-min))
543 (if (search-forward "\nChars: " nil t)
544 (if (numberp (setq chars (ignore-errors (read cur))))
549 (goto-char (point-min))
550 (if (search-forward "\nLines: " nil t)
551 (if (numberp (setq lines (ignore-errors (read cur))))
555 (std11-fetch-field "Xref")
557 (goto-char (point-min))
558 (if (setq ctype (std11-fetch-field "Content-Type"))
559 (mime-entity-set-content-type-internal
560 header (mime-parse-Content-Type ctype)))
563 (gnus-dependencies-add-header
564 header dependencies force-new))
565 (push header headers))
567 (setq count (1+ count))
571 (nnheader-message 5 "%s: Receiving headers... %d%%"
573 (/ (* count 100) number))))
576 (nnheader-message 5 "%s: Receiving headers...done" backend))
580 (defun nnheader-retrieve-headers-from-directory (articles
581 directory dependencies
583 fetch-old force-new large
586 (nreverse (nnheader-retrieve-headers-from-directory*
587 articles directory dependencies
588 fetch-old force-new large backend))))
590 (defun nnheader-get-newsgroup-headers-xover* (sequence
592 force-new dependencies
594 "Parse the news overview data in the server buffer, and return a
595 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
596 ;; Get the Xref when the users reads the articles since most/some
597 ;; NNTP servers do not include Xrefs when using XOVER.
598 ;; (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
599 (let ((cur nntp-server-buffer)
600 number headers header)
602 (set-buffer nntp-server-buffer)
603 ;; Allow the user to mangle the headers before parsing them.
604 (gnus-run-hooks 'gnus-parse-headers-hook)
605 (goto-char (point-min))
608 (while (and sequence (not (eobp)))
609 (setq number (read cur))
611 (< (car sequence) number))
612 (setq sequence (cdr sequence)))
614 (eq number (car sequence))
616 (setq sequence (cdr sequence))
619 number dependencies force-new))))
620 (push header headers))
623 (gnus-error 4 "Strange nov line (%d)"
624 (count-lines (point-min) (point)))))
626 ;; A common bug in inn is that if you have posted an article and
627 ;; then retrieves the active file, it will answer correctly --
628 ;; the new article is included. However, a NOV entry for the
629 ;; article may not have been generated yet, so this may fail.
630 ;; We work around this problem by retrieving the last few
631 ;; headers using HEAD.
634 ;; Various cruft the backends and Gnus need to communicate.
636 (defvar nntp-server-buffer nil)
637 (defvar nntp-process-response nil)
638 (defvar gnus-verbose-backends 7
639 "*A number that says how talkative the Gnus backends should be.")
640 (defvar gnus-nov-is-evil nil
641 "If non-nil, Gnus backends will never output headers in the NOV format.")
642 (defvar news-reply-yank-from nil)
643 (defvar news-reply-yank-message-id nil)
645 (defvar nnheader-callback-function nil)
647 (defun nnheader-init-server-buffer ()
648 "Initialize the Gnus-backend communication buffer."
650 (unless (gnus-buffer-live-p nntp-server-buffer)
651 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
652 (set-buffer nntp-server-buffer)
654 (kill-all-local-variables)
655 (setq case-fold-search t) ;Should ignore case.
656 (set (make-local-variable 'nntp-process-response) nil)
659 ;;; Various functions the backends use.
661 (defun nnheader-file-error (file)
662 "Return a string that says what is wrong with FILE."
665 ((not (file-exists-p file))
667 ((file-directory-p file)
669 ((not (file-readable-p file))
670 "%s is not readable"))
673 (defun nnheader-insert-head (file)
674 "Insert the head of the article."
675 (when (file-exists-p file)
676 (if (eq nnheader-max-head-length t)
677 ;; Just read the entire file.
678 (nnheader-insert-file-contents file)
679 ;; Read 1K blocks until we find a separator.
682 (while (and (eq nnheader-head-chop-length
683 (nth 1 (nnheader-insert-file-contents
685 (incf beg nnheader-head-chop-length))))
686 (prog1 (not (search-forward "\n\n" nil t))
687 (goto-char (point-max)))
688 (or (null nnheader-max-head-length)
689 (< beg nnheader-max-head-length))))))
692 (defun nnheader-article-p ()
693 "Say whether the current buffer looks like an article."
694 (goto-char (point-min))
695 (if (not (search-forward "\n\n" nil t))
697 (narrow-to-region (point-min) (1- (point)))
698 (goto-char (point-min))
699 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
700 (goto-char (match-end 0)))
705 (defun nnheader-insert-references (references message-id)
706 "Insert a References header based on REFERENCES and MESSAGE-ID."
707 (if (and (not references) (not message-id))
708 ;; This is invalid, but not all articles have Message-IDs.
710 (mail-position-on-field "References")
711 (let ((begin (save-excursion (beginning-of-line) (point)))
716 (when (and references message-id)
720 ;; Fold long References lines to conform to RFC1036 (sort of).
721 ;; The region must end with a newline to fill the region
722 ;; without inserting extra newline.
723 (fill-region-as-paragraph begin (1+ (point))))))
725 (defun nnheader-replace-header (header new-value)
726 "Remove HEADER and insert the NEW-VALUE."
729 (nnheader-narrow-to-headers)
731 (message-remove-header header)
732 (goto-char (point-max))
733 (insert header ": " new-value "\n")))))
735 (defun nnheader-narrow-to-headers ()
736 "Narrow to the head of an article."
739 (goto-char (point-min))
740 (if (search-forward "\n\n" nil t)
743 (goto-char (point-min)))
745 (defun nnheader-set-temp-buffer (name &optional noerase)
746 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
747 (set-buffer (get-buffer-create name))
748 (buffer-disable-undo)
753 (defvar jka-compr-compression-info-list)
754 (defvar nnheader-numerical-files
755 (if (boundp 'jka-compr-compression-info-list)
756 (concat "\\([0-9]+\\)\\("
757 (mapconcat (lambda (i) (aref i 0))
758 jka-compr-compression-info-list "\\|")
761 "Regexp that match numerical files.")
763 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
764 "Regexp that matches numerical file names.")
766 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
767 "Regexp that matches numerical full file paths.")
769 (defsubst nnheader-file-to-number (file)
770 "Take a file name and return the article number."
771 (if (string= nnheader-numerical-short-files "^[0-9]+$")
773 (string-match nnheader-numerical-short-files file)
774 (string-to-int (match-string 0 file))))
776 (defun nnheader-directory-files-safe (&rest args)
777 ;; It has been reported numerous times that `directory-files'
778 ;; fails with an alarming frequency on NFS mounted file systems.
779 ;; This function executes that function twice and returns
780 ;; the longest result.
781 (let ((first (apply 'directory-files args))
782 (second (apply 'directory-files args)))
783 (if (> (length first) (length second))
787 (defun nnheader-directory-articles (dir)
788 "Return a list of all article files in a directory."
789 (mapcar 'nnheader-file-to-number
790 (nnheader-directory-files-safe
791 dir nil nnheader-numerical-short-files t)))
793 (defun nnheader-article-to-file-alist (dir)
794 "Return an alist of article/file pairs in DIR."
795 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
796 (nnheader-directory-files-safe
797 dir nil nnheader-numerical-short-files t)))
799 (defun nnheader-fold-continuation-lines ()
800 "Fold continuation lines in the current buffer."
801 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
803 (defun nnheader-translate-file-chars (file &optional full)
804 "Translate FILE into something that can be a file name.
805 If FULL, translate everything."
806 (if (null nnheader-file-name-translation-alist)
807 ;; No translation is necessary.
812 ;; Do complete translation.
813 (setq leaf (copy-sequence file)
815 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
817 ;; We translate -- but only the file name. We leave the directory
819 (if (string-match "/[^/]+\\'" file)
820 ;; This is needed on NT's and stuff.
821 (setq leaf (substring file (1+ (match-beginning 0)))
822 path (substring file 0 (1+ (match-beginning 0))))
823 ;; Fall back on this.
824 (setq leaf (file-name-nondirectory file)
825 path (file-name-directory file))))
826 (setq len (length leaf))
828 (when (setq trans (cdr (assq (aref leaf i)
829 nnheader-file-name-translation-alist)))
832 (concat path leaf))))
834 (defun nnheader-report (backend &rest args)
835 "Report an error from the BACKEND.
836 The first string in ARGS can be a format string."
837 (set (intern (format "%s-status-string" backend))
838 (if (< (length args) 2)
840 (apply 'format args)))
843 (defun nnheader-get-report (backend)
844 "Get the most recent report from BACKEND."
846 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
848 (error (nnheader-message 5 ""))))
850 (defun nnheader-insert (format &rest args)
851 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
852 If FORMAT isn't a format string, it and all ARGS will be inserted
855 (set-buffer nntp-server-buffer)
857 (if (string-match "%" format)
858 (insert (apply 'format format args))
859 (apply 'insert format args))
862 (defun nnheader-replace-chars-in-string (string from to)
863 "Replace characters in STRING from FROM to TO."
864 (let ((string (substring string 0)) ;Copy string.
865 (len (length string))
867 ;; Replace all occurrences of FROM with TO.
869 (when (= (aref string idx) from)
870 (aset string idx to))
874 (defun nnheader-replace-duplicate-chars-in-string (string from to)
875 "Replace characters in STRING from FROM to TO."
876 (let ((string (substring string 0)) ;Copy string.
877 (len (length string))
879 ;; Replace all occurrences of FROM with TO.
881 (setq i (aref string idx))
882 (when (and (eq prev from) (= i from))
883 (aset string (1- idx) to)
884 (aset string idx to))
889 (defun nnheader-file-to-group (file &optional top)
890 "Return a group name based on FILE and TOP."
891 (nnheader-replace-chars-in-string
895 (substring (expand-file-name file)
898 (file-name-as-directory top))))
902 (defun nnheader-message (level &rest args)
903 "Message if the Gnus backends are talkative."
904 (if (or (not (numberp gnus-verbose-backends))
905 (<= level gnus-verbose-backends))
906 (apply 'message args)
907 (apply 'format args)))
909 (defun nnheader-be-verbose (level)
910 "Return whether the backends should be verbose on LEVEL."
911 (or (not (numberp gnus-verbose-backends))
912 (<= level gnus-verbose-backends)))
914 (defvar nnheader-pathname-coding-system 'binary
915 "*Coding system for pathname.")
917 (defun nnheader-group-pathname (group dir &optional file)
918 "Make pathname for GROUP."
920 (let ((dir (file-name-as-directory (expand-file-name dir))))
921 ;; If this directory exists, we use it directly.
922 (file-name-as-directory
923 (if (file-directory-p (concat dir group))
924 (expand-file-name group dir)
925 ;; If not, we translate dots into slashes.
926 (expand-file-name (encode-coding-string
927 (nnheader-replace-chars-in-string group ?. ?/)
928 nnheader-pathname-coding-system)
930 (cond ((null file) "")
931 ((numberp file) (int-to-string file))
934 (defun nnheader-functionp (form)
935 "Return non-nil if FORM is funcallable."
936 (or (and (symbolp form) (fboundp form))
937 (and (listp form) (eq (car form) 'lambda))))
939 (defun nnheader-concat (dir &rest files)
940 "Concat DIR as directory to FILE."
941 (apply 'concat (file-name-as-directory dir) files))
943 (defun nnheader-ms-strip-cr ()
944 "Strip ^M from the end of all lines."
946 (goto-char (point-min))
947 (while (re-search-forward "\r$" nil t)
948 (delete-backward-char 1))))
950 (defun nnheader-file-size (file)
951 "Return the file size of FILE or 0."
952 (or (nth 7 (file-attributes file)) 0))
954 (defun nnheader-find-etc-directory (package &optional file)
955 "Go through the path and find the \".../etc/PACKAGE\" directory.
956 If FILE, find the \".../etc/PACKAGE\" file instead."
957 (let ((path load-path)
959 ;; We try to find the dir by looking at the load path,
960 ;; stripping away the last component and adding "etc/".
966 (directory-file-name (car path)))
969 (or file (file-directory-p dir)))
972 (setq path (cdr path))))
975 (defvar ange-ftp-path-format)
976 (defvar efs-path-regexp)
977 (defun nnheader-re-read-dir (path)
978 "Re-read directory PATH if PATH is on a remote system."
979 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
980 (when (string-match efs-path-regexp path)
981 (efs-re-read-dir path))
982 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
983 (when (string-match (car ange-ftp-path-format) path)
984 (ange-ftp-re-read-dir path)))))
986 (defvar nnheader-file-coding-system 'raw-text
987 "Coding system used in file backends of Gnus.")
989 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
990 "Like `insert-file-contents', q.v., but only reads in the file.
991 A buffer may be modified in several ways after reading into the buffer due
992 to advanced Emacs features, such as file-name-handlers, format decoding,
993 find-file-hooks, etc.
994 This function ensures that none of these modifications will take place."
995 (let ((format-alist nil)
996 (auto-mode-alist (nnheader-auto-mode-alist))
997 (default-major-mode 'fundamental-mode)
998 (enable-local-variables nil)
999 (after-insert-file-functions nil)
1000 (enable-local-eval nil)
1001 (find-file-hooks nil))
1002 (insert-file-contents-as-coding-system
1003 nnheader-file-coding-system filename visit beg end replace)))
1005 (defun nnheader-find-file-noselect (&rest args)
1006 (let ((format-alist nil)
1007 (auto-mode-alist (nnheader-auto-mode-alist))
1008 (default-major-mode 'fundamental-mode)
1009 (enable-local-variables nil)
1010 (after-insert-file-functions nil)
1011 (enable-local-eval nil)
1012 (find-file-hooks nil))
1013 (apply 'find-file-noselect-as-coding-system
1014 nnheader-file-coding-system args)))
1016 (defun nnheader-auto-mode-alist ()
1017 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1018 (let ((alist auto-mode-alist)
1021 (when (listp (cdar alist))
1022 (push (car alist) out))
1026 (defun nnheader-directory-regular-files (dir)
1027 "Return a list of all regular files in DIR."
1028 (let ((files (directory-files dir t))
1031 (when (file-regular-p (car files))
1032 (push (car files) out))
1036 (defun nnheader-directory-files (&rest args)
1037 "Same as `directory-files', but prune \".\" and \"..\"."
1038 (let ((files (apply 'directory-files args))
1041 (unless (member (file-name-nondirectory (car files)) '("." ".."))
1042 (push (car files) out))
1046 (defmacro nnheader-skeleton-replace (from &optional to regexp)
1047 `(let ((new (generate-new-buffer " *nnheader replace*"))
1048 (cur (current-buffer))
1049 (start (point-min)))
1051 (goto-char (point-min))
1052 (while (,(if regexp 're-search-forward 'search-forward)
1054 (insert-buffer-substring
1055 cur start (prog1 (match-beginning 0) (set-buffer new)))
1056 (goto-char (point-max))
1057 ,(when to `(insert ,to))
1059 (setq start (point)))
1060 (insert-buffer-substring
1061 cur start (prog1 (point-max) (set-buffer new)))
1062 (copy-to-buffer cur (point-min) (point-max))
1063 (kill-buffer (current-buffer))
1066 (defun nnheader-replace-string (from to)
1067 "Do a fast replacement of FROM to TO from point to point-max."
1068 (nnheader-skeleton-replace from to))
1070 (defun nnheader-replace-regexp (from to)
1071 "Do a fast regexp replacement of FROM to TO from point to point-max."
1072 (nnheader-skeleton-replace from to t))
1074 (defun nnheader-strip-cr ()
1075 "Strip all \r's from the current buffer."
1076 (nnheader-skeleton-replace "\r"))
1078 (fset 'nnheader-run-at-time 'run-at-time)
1079 (fset 'nnheader-cancel-timer 'cancel-timer)
1080 (fset 'nnheader-cancel-function-timers 'cancel-function-timers)
1082 (defun nnheader-Y-or-n-p (prompt)
1083 "Ask user a \"Y/n\" question. Return t if answer is neither \"n\", \"N\" nor \"C-g\"."
1084 (let ((cursor-in-echo-area t)
1088 (let (message-log-max)
1089 (while (not (memq ans '(?\ ?N ?Y ?\C-g ?\e ?\n ?\r ?n ?y)))
1090 (message "%s(Y/n) " prompt)
1091 (setq ans (read-char-exclusive))))
1092 (if (memq ans '(?\C-g ?N ?n))
1094 (message "%s(Y/n) No" prompt)
1096 (message "%s(Y/n) Yes" prompt)
1099 (when (string-match "XEmacs\\|Lucid" emacs-version)
1100 (require 'nnheaderxm))
1102 (run-hooks 'nnheader-load-hook)
1106 ;;; nnheader.el ends here