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 '((?: . ?_)))")
52 (defvar nnheader-text-coding-system
53 (if (memq system-type '(windows-nt ms-dos ms-windows))
56 "Text-safe coding system (For removing ^M).
57 This variable is a substitute for `mm-text-coding-system'.")
59 (defvar nnheader-text-coding-system-for-write nil
60 "Text coding system for write.
61 This variable is a substitute for `mm-text-coding-system-for-write'.")
64 (autoload 'nnmail-message-id "nnmail")
65 (autoload 'mail-position-on-field "sendmail")
66 (autoload 'message-remove-header "message")
67 (autoload 'gnus-point-at-eol "gnus-util")
68 (autoload 'gnus-delete-line "gnus-util")
69 (autoload 'gnus-buffer-live-p "gnus-util"))
71 ;;; Header access macros.
73 ;; These macros may look very much like the ones in GNUS 4.1. They
74 ;; are, in a way, but you should note that the indices they use have
75 ;; been changed from the internal GNUS format to the NOV format. The
76 ;; makes it possible to read headers from XOVER much faster.
78 ;; The format of a header is now:
79 ;; [number subject from date id references chars lines xref extra]
81 ;; (That next-to-last entry is defined as "misc" in the NOV format,
82 ;; but Gnus uses it for xrefs.)
86 (defmacro mail-header-number (header)
87 "Return article number in HEADER."
88 `(mime-entity-location-internal ,header))
90 (defmacro mail-header-set-number (header number)
91 "Set article number of HEADER to NUMBER."
92 `(mime-entity-set-location-internal ,header ,number))
94 (defalias 'mail-header-subject 'mime-gnus-entity-subject-internal)
95 (defalias 'mail-header-set-subject 'mime-gnus-entity-set-subject-internal)
97 (defalias 'mail-header-from 'mime-gnus-entity-from-internal)
98 (defalias 'mail-header-set-from 'mime-gnus-entity-set-from-internal)
100 (defalias 'mail-header-date 'mime-gnus-entity-date-internal)
101 (defalias 'mail-header-set-date 'mime-gnus-entity-set-date-internal)
103 (defalias 'mail-header-message-id 'mime-gnus-entity-id-internal)
104 (defalias 'mail-header-id 'mime-gnus-entity-id-internal)
105 (defalias 'mail-header-set-message-id 'mime-gnus-entity-set-id-internal)
106 (defalias 'mail-header-set-id 'mime-gnus-entity-set-id-internal)
108 (defalias 'mail-header-references 'mime-gnus-entity-references-internal)
109 (defalias 'mail-header-set-references
110 'mime-gnus-entity-set-references-internal)
112 (defalias 'mail-header-chars 'mime-gnus-entity-chars-internal)
113 (defalias 'mail-header-set-chars 'mime-gnus-entity-set-chars-internal)
115 (defalias 'mail-header-lines 'mime-gnus-entity-lines-internal)
116 (defalias 'mail-header-set-lines 'mime-gnus-entity-set-lines-internal)
118 (defalias 'mail-header-xref 'mime-gnus-entity-xref-internal)
119 (defalias 'mail-header-set-xref 'mime-gnus-entity-set-xref-internal)
121 (defalias 'nnheader-decode-subject
122 (mime-find-field-decoder 'Subject 'nov))
123 (defalias 'nnheader-decode-from
124 (mime-find-field-decoder 'From 'nov))
126 (defalias 'mail-header-extra 'mime-gnus-entity-extra-internal)
127 (defalias 'mail-header-set-extra 'mime-gnus-entity-set-extra-internal)
129 (defun nnheader-decode-field-body (field-body field-name
130 &optional mode max-column)
131 (mime-decode-field-body field-body
132 (if (stringp field-name)
133 (intern (capitalize field-name))
137 (defsubst make-full-mail-header (&optional number subject from date id
138 references chars lines xref
140 "Create a new mail header structure initialized with the parameters given."
141 (luna-make-entity (mm-expand-class-name 'gnus)
144 (nnheader-decode-subject subject))
146 (nnheader-decode-from from))
149 :references references
153 :original-header (list (cons 'Subject subject)
157 (defsubst make-full-mail-header-from-decoded-header
158 (&optional number subject from date id references chars lines xref extra)
159 "Create a new mail header structure initialized with the parameters given."
160 (luna-make-entity (mm-expand-class-name 'gnus)
166 :references references
172 (defsubst make-mail-header (&optional init)
173 "Create a new mail header structure initialized with INIT."
174 (make-full-mail-header init init init init init
175 init init init init init))
177 ;; fake message-ids: generation and detection
179 (defvar nnheader-fake-message-id 1)
181 (defsubst nnheader-generate-fake-message-id ()
182 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
184 (defsubst nnheader-fake-message-id-p (id)
185 (save-match-data ; regular message-id's are <.*>
186 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
188 ;; Parsing headers and NOV lines.
190 (defsubst nnheader-header-value ()
191 (buffer-substring (match-end 0) (gnus-point-at-eol)))
193 (defun nnheader-parse-head (&optional naked)
194 (let ((case-fold-search t)
195 (cur (current-buffer))
196 (buffer-read-only nil)
197 in-reply-to lines p ref)
198 (goto-char (point-min))
201 ;; Search to the beginning of the next header. Error messages
202 ;; do not begin with 2 or 3.
204 (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
205 ;; This implementation of this function, with nine
206 ;; search-forwards instead of the one re-search-forward and
207 ;; a case (which basically was the old function) is actually
208 ;; about twice as fast, even though it looks messier. You
209 ;; can't have everything, I guess. Speed and elegance
210 ;; don't always go hand in hand.
211 (make-full-mail-header
221 (narrow-to-region (point)
222 (or (and (search-forward "\n.\n" nil t)
228 (if (search-forward "\nsubject: " nil t)
229 (nnheader-header-value) "(none)"))
233 (if (search-forward "\nfrom: " nil t)
234 (nnheader-header-value) "(nobody)"))
238 (if (search-forward "\ndate: " nil t)
239 (nnheader-header-value) ""))
243 (if (search-forward "\nmessage-id:" nil t)
245 (1- (or (search-forward "<" (gnus-point-at-eol) t)
247 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
248 ;; If there was no message-id, we just fake one to make
249 ;; subsequent routines simpler.
250 (nnheader-generate-fake-message-id)))
254 (if (search-forward "\nreferences: " nil t)
255 (nnheader-header-value)
256 ;; Get the references from the in-reply-to header if there
257 ;; were no references and the in-reply-to header looks
259 (if (and (search-forward "\nin-reply-to: " nil t)
260 (setq in-reply-to (nnheader-header-value))
261 (string-match "<[^\n>]+>" in-reply-to))
263 (setq ref (substring in-reply-to (match-beginning 0)
265 (while (string-match "<[^\n>]+>"
266 in-reply-to (match-end 0))
267 (setq ref2 (substring in-reply-to (match-beginning 0)
269 (when (> (length ref2) (length ref))
278 (if (search-forward "\nlines: " nil t)
279 (if (numberp (setq lines (read cur)))
285 (and (search-forward "\nxref: " nil t)
286 (nnheader-header-value)))
289 (when nnmail-extra-headers
290 (let ((extra nnmail-extra-headers)
294 (when (search-forward
295 (concat "\n" (symbol-name (car extra)) ": ") nil t)
296 (push (cons (car extra) (nnheader-header-value))
301 (goto-char (point-min))
304 (defmacro nnheader-nov-skip-field ()
305 '(search-forward "\t" eol 'move))
307 (defmacro nnheader-nov-field ()
308 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
310 (defmacro nnheader-nov-read-integer ()
312 (if (eq (char-after) ?\t)
314 (let ((num (condition-case nil
315 (read (current-buffer))
317 (if (numberp num) num 0)))
319 (search-forward "\t" eol 'move))))
321 (defmacro nnheader-nov-parse-extra ()
323 (while (not (memq (char-after) '(?\n nil)))
324 (setq string (nnheader-nov-field))
325 (when (string-match "^\\([^ :]+\\): " string)
326 (push (cons (intern (match-string 1 string))
327 (substring string (match-end 0)))
331 (defmacro nnheader-nov-read-message-id ()
332 '(let ((id (nnheader-nov-field)))
333 (if (string-match "^<[^>]+>$" id)
335 (nnheader-generate-fake-message-id))))
337 (defun nnheader-parse-nov ()
338 (let ((eol (gnus-point-at-eol)))
339 (make-full-mail-header
340 (nnheader-nov-read-integer) ; number
341 (nnheader-nov-field) ; subject
342 (nnheader-nov-field) ; from
343 (nnheader-nov-field) ; date
344 (nnheader-nov-read-message-id) ; id
345 (nnheader-nov-field) ; refs
346 (nnheader-nov-read-integer) ; chars
347 (nnheader-nov-read-integer) ; lines
348 (if (eq (char-after) ?\n)
350 (nnheader-nov-field)) ; misc
351 (nnheader-nov-parse-extra)))) ; extra
353 (defun nnheader-insert-nov (header)
354 (princ (mail-header-number header) (current-buffer))
358 (or (mime-entity-fetch-field header 'Subject) "(none)") "\t"
359 (or (mime-entity-fetch-field header 'From) "(nobody)") "\t"
360 (or (mail-header-date header) "") "\t"
361 (or (mail-header-id header)
364 (or (mail-header-references header) "") "\t")
365 (princ (or (mail-header-chars header) 0) (current-buffer))
367 (princ (or (mail-header-lines header) 0) (current-buffer))
369 (when (mail-header-xref header)
370 (insert "Xref: " (mail-header-xref header)))
371 (when (or (mail-header-xref header)
372 (mail-header-extra header))
374 (when (mail-header-extra header)
375 (let ((extra (mail-header-extra header)))
377 (insert (symbol-name (caar extra))
378 ": " (cdar extra) "\t")
382 (while (search-backward "\n" p t)
386 (defun nnheader-insert-header (header)
388 "Subject: " (or (mail-header-subject header) "(none)") "\n"
389 "From: " (or (mail-header-from header) "(nobody)") "\n"
390 "Date: " (or (mail-header-date header) "") "\n"
391 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
392 "References: " (or (mail-header-references header) "") "\n"
394 (princ (or (mail-header-lines header) 0) (current-buffer))
397 (defun nnheader-insert-article-line (article)
398 (goto-char (point-min))
400 (princ article (current-buffer))
401 (insert " Article retrieved.\n")
402 (search-forward "\n\n" nil 'move)
403 (delete-region (point) (point-max))
407 (defun nnheader-nov-delete-outside-range (beg end)
408 "Delete all NOV lines that lie outside the BEG to END range."
409 ;; First we find the first wanted line.
410 (nnheader-find-nov-line beg)
411 (delete-region (point-min) (point))
412 ;; Then we find the last wanted line.
413 (when (nnheader-find-nov-line end)
415 (delete-region (point) (point-max)))
417 (defun nnheader-find-nov-line (article)
418 "Put point at the NOV line that start with ARTICLE.
419 If ARTICLE doesn't exist, put point where that line
420 would have been. The function will return non-nil if
421 the line could be found."
422 ;; This function basically does a binary search.
423 (let ((max (point-max))
424 (min (goto-char (point-min)))
425 (cur (current-buffer))
429 (goto-char (/ (+ max min) 2))
431 (if (or (= (point) prev)
435 (while (and (not (numberp (setq num (read cur))))
438 (cond ((> num article)
443 (setq found 'yes)))))
444 ;; We may be at the first line.
447 (setq num (read cur)))
448 ;; Now we may have found the article we're looking for, or we
449 ;; may be somewhere near it.
450 (when (and (not (eq found 'yes))
451 (not (eq num article)))
453 (while (and (< (point) max)
454 (or (not (numberp num))
459 (= (setq num (read cur)) article)))
460 (unless (eq num article)
465 (defun nnheader-retrieve-headers-from-directory* (articles
466 directory dependencies
468 fetch-old force-new large
472 (number (length articles))
474 (file-name-coding-system 'binary)
475 (pathname-coding-system 'binary)
477 (cur (current-buffer))
479 headers header id end ref in-reply-to lines chars ctype)
480 ;; We don't support fetching by Message-ID.
481 (if (stringp (car articles))
484 (when (and (file-exists-p
485 (setq file (expand-file-name
487 (setq article (pop articles)))
489 (not (file-directory-p file)))
491 (nnheader-insert-head file)
493 (std11-narrow-to-header)
496 (make-full-mail-header
500 (or (std11-fetch-field "Subject")
503 (or (std11-fetch-field "From")
506 (or (std11-fetch-field "Date")
510 (goto-char (point-min))
511 (setq id (if (re-search-forward
512 "^Message-ID: *\\(<[^\n\t> ]+>\\)" nil t)
513 ;; We do it this way to make sure the Message-ID
514 ;; is (somewhat) syntactically valid.
515 (buffer-substring (match-beginning 1)
517 ;; If there was no message-id, we just fake one
518 ;; to make subsequent routines simpler.
519 (nnheader-generate-fake-message-id))))
522 (goto-char (point-min))
523 (if (search-forward "\nReferences: " nil t)
527 (buffer-substring (match-end 0) (std11-field-end))
532 (search-backward ">" end t)
535 (search-backward "<" end t)
537 ;; Get the references from the in-reply-to header if there
538 ;; were no references and the in-reply-to header looks
540 (if (and (search-forward "\nIn-Reply-To: " nil t)
542 (buffer-substring (match-end 0)
544 (string-match "<[^>]+>" in-reply-to))
546 (setq ref (substring in-reply-to (match-beginning 0)
548 (while (string-match "<[^>]+>"
549 in-reply-to (match-end 0))
551 (substring in-reply-to (match-beginning 0)
553 (when (> (length ref2) (length ref))
559 (goto-char (point-min))
560 (if (search-forward "\nChars: " nil t)
561 (if (numberp (setq chars (ignore-errors (read cur))))
566 (goto-char (point-min))
567 (if (search-forward "\nLines: " nil t)
568 (if (numberp (setq lines (ignore-errors (read cur))))
572 (std11-fetch-field "Xref")
574 (goto-char (point-min))
575 (if (setq ctype (std11-fetch-field "Content-Type"))
576 (mime-entity-set-content-type-internal
577 header (mime-parse-Content-Type ctype)))
580 (gnus-dependencies-add-header
581 header dependencies force-new))
582 (push header headers))
584 (setq count (1+ count))
588 (nnheader-message 5 "%s: Receiving headers... %d%%"
590 (/ (* count 100) number))))
593 (nnheader-message 5 "%s: Receiving headers...done" backend))
597 (defun nnheader-retrieve-headers-from-directory (articles
598 directory dependencies
600 fetch-old force-new large
603 (nreverse (nnheader-retrieve-headers-from-directory*
604 articles directory dependencies
605 fetch-old force-new large backend))))
607 (defun nnheader-get-newsgroup-headers-xover* (sequence
609 force-new dependencies
611 "Parse the news overview data in the server buffer, and return a
612 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
613 ;; Get the Xref when the users reads the articles since most/some
614 ;; NNTP servers do not include Xrefs when using XOVER.
615 ;; (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
616 (let ((cur nntp-server-buffer)
617 number headers header)
619 (set-buffer nntp-server-buffer)
620 ;; Allow the user to mangle the headers before parsing them.
621 (gnus-run-hooks 'gnus-parse-headers-hook)
622 (goto-char (point-min))
625 (while (and sequence (not (eobp)))
626 (setq number (read cur))
628 (< (car sequence) number))
629 (setq sequence (cdr sequence)))
631 (eq number (car sequence))
633 (setq sequence (cdr sequence))
636 number dependencies force-new))))
637 (push header headers))
640 (gnus-error 4 "Strange nov line (%d)"
641 (count-lines (point-min) (point)))))
643 ;; A common bug in inn is that if you have posted an article and
644 ;; then retrieves the active file, it will answer correctly --
645 ;; the new article is included. However, a NOV entry for the
646 ;; article may not have been generated yet, so this may fail.
647 ;; We work around this problem by retrieving the last few
648 ;; headers using HEAD.
651 ;; Various cruft the backends and Gnus need to communicate.
653 (defvar nntp-server-buffer nil)
654 (defvar nntp-process-response nil)
655 (defvar gnus-verbose-backends 7
656 "*A number that says how talkative the Gnus backends should be.")
657 (defvar gnus-nov-is-evil nil
658 "If non-nil, Gnus backends will never output headers in the NOV format.")
659 (defvar news-reply-yank-from nil)
660 (defvar news-reply-yank-message-id nil)
662 (defvar nnheader-callback-function nil)
664 (defun nnheader-init-server-buffer ()
665 "Initialize the Gnus-backend communication buffer."
667 (unless (gnus-buffer-live-p nntp-server-buffer)
668 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
669 (set-buffer nntp-server-buffer)
671 (kill-all-local-variables)
672 (setq case-fold-search t) ;Should ignore case.
673 (set (make-local-variable 'nntp-process-response) nil)
676 ;;; Various functions the backends use.
678 (defun nnheader-file-error (file)
679 "Return a string that says what is wrong with FILE."
682 ((not (file-exists-p file))
684 ((file-directory-p file)
686 ((not (file-readable-p file))
687 "%s is not readable"))
690 (defun nnheader-insert-head (file)
691 "Insert the head of the article."
692 (when (file-exists-p file)
693 (if (eq nnheader-max-head-length t)
694 ;; Just read the entire file.
695 (nnheader-insert-file-contents file)
696 ;; Read 1K blocks until we find a separator.
699 (while (and (eq nnheader-head-chop-length
700 (nth 1 (nnheader-insert-file-contents
702 (incf beg nnheader-head-chop-length))))
703 (prog1 (not (search-forward "\n\n" nil t))
704 (goto-char (point-max)))
705 (or (null nnheader-max-head-length)
706 (< beg nnheader-max-head-length))))))
709 (defun nnheader-article-p ()
710 "Say whether the current buffer looks like an article."
711 (goto-char (point-min))
712 (if (not (search-forward "\n\n" nil t))
714 (narrow-to-region (point-min) (1- (point)))
715 (goto-char (point-min))
716 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
717 (goto-char (match-end 0)))
722 (defun nnheader-insert-references (references message-id)
723 "Insert a References header based on REFERENCES and MESSAGE-ID."
724 (if (and (not references) (not message-id))
725 ;; This is invalid, but not all articles have Message-IDs.
727 (mail-position-on-field "References")
728 (let ((begin (save-excursion (beginning-of-line) (point)))
733 (when (and references message-id)
737 ;; Fold long References lines to conform to RFC1036 (sort of).
738 ;; The region must end with a newline to fill the region
739 ;; without inserting extra newline.
740 (fill-region-as-paragraph begin (1+ (point))))))
742 (defun nnheader-replace-header (header new-value)
743 "Remove HEADER and insert the NEW-VALUE."
746 (nnheader-narrow-to-headers)
748 (message-remove-header header)
749 (goto-char (point-max))
750 (insert header ": " new-value "\n")))))
752 (defun nnheader-narrow-to-headers ()
753 "Narrow to the head of an article."
756 (goto-char (point-min))
757 (if (search-forward "\n\n" nil t)
760 (goto-char (point-min)))
762 (defun nnheader-set-temp-buffer (name &optional noerase)
763 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
764 (set-buffer (get-buffer-create name))
765 (buffer-disable-undo)
770 (eval-when-compile (defvar jka-compr-compression-info-list))
771 (defvar nnheader-numerical-files
772 (if (boundp 'jka-compr-compression-info-list)
773 (concat "\\([0-9]+\\)\\("
774 (mapconcat (lambda (i) (aref i 0))
775 jka-compr-compression-info-list "\\|")
778 "Regexp that match numerical files.")
780 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
781 "Regexp that matches numerical file names.")
783 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
784 "Regexp that matches numerical full file paths.")
786 (defsubst nnheader-file-to-number (file)
787 "Take a file name and return the article number."
788 (if (string= nnheader-numerical-short-files "^[0-9]+$")
790 (string-match nnheader-numerical-short-files file)
791 (string-to-int (match-string 0 file))))
793 (defun nnheader-directory-files-safe (&rest args)
794 ;; It has been reported numerous times that `directory-files'
795 ;; fails with an alarming frequency on NFS mounted file systems.
796 ;; This function executes that function twice and returns
797 ;; the longest result.
798 (let ((first (apply 'directory-files args))
799 (second (apply 'directory-files args)))
800 (if (> (length first) (length second))
804 (defun nnheader-directory-articles (dir)
805 "Return a list of all article files in a directory."
806 (mapcar 'nnheader-file-to-number
807 (nnheader-directory-files-safe
808 dir nil nnheader-numerical-short-files t)))
810 (defun nnheader-article-to-file-alist (dir)
811 "Return an alist of article/file pairs in DIR."
812 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
813 (nnheader-directory-files-safe
814 dir nil nnheader-numerical-short-files t)))
816 (defun nnheader-fold-continuation-lines ()
817 "Fold continuation lines in the current buffer."
818 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
820 (defun nnheader-translate-file-chars (file &optional full)
821 "Translate FILE into something that can be a file name.
822 If FULL, translate everything."
823 (if (null nnheader-file-name-translation-alist)
824 ;; No translation is necessary.
829 ;; Do complete translation.
830 (setq leaf (copy-sequence file)
832 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
834 ;; We translate -- but only the file name. We leave the directory
836 (if (string-match "/[^/]+\\'" file)
837 ;; This is needed on NT's and stuff.
838 (setq leaf (substring file (1+ (match-beginning 0)))
839 path (substring file 0 (1+ (match-beginning 0))))
840 ;; Fall back on this.
841 (setq leaf (file-name-nondirectory file)
842 path (file-name-directory file))))
843 (setq len (length leaf))
845 (when (setq trans (cdr (assq (aref leaf i)
846 nnheader-file-name-translation-alist)))
849 (concat path leaf))))
851 (defun nnheader-report (backend &rest args)
852 "Report an error from the BACKEND.
853 The first string in ARGS can be a format string."
854 (set (intern (format "%s-status-string" backend))
855 (if (< (length args) 2)
857 (apply 'format args)))
860 (defun nnheader-get-report (backend)
861 "Get the most recent report from BACKEND."
863 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
865 (error (nnheader-message 5 ""))))
867 (defun nnheader-insert (format &rest args)
868 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
869 If FORMAT isn't a format string, it and all ARGS will be inserted
872 (set-buffer nntp-server-buffer)
874 (if (string-match "%" format)
875 (insert (apply 'format format args))
876 (apply 'insert format args))
879 (defun nnheader-replace-chars-in-string (string from to)
880 "Replace characters in STRING from FROM to TO."
881 (let ((string (substring string 0)) ;Copy string.
882 (len (length string))
884 ;; Replace all occurrences of FROM with TO.
886 (when (= (aref string idx) from)
887 (aset string idx to))
891 (defun nnheader-replace-duplicate-chars-in-string (string from to)
892 "Replace characters in STRING from FROM to TO."
893 (let ((string (substring string 0)) ;Copy string.
894 (len (length string))
896 ;; Replace all occurrences of FROM with TO.
898 (setq i (aref string idx))
899 (when (and (eq prev from) (= i from))
900 (aset string (1- idx) to)
901 (aset string idx to))
906 (defun nnheader-file-to-group (file &optional top)
907 "Return a group name based on FILE and TOP."
908 (nnheader-replace-chars-in-string
912 (substring (expand-file-name file)
915 (file-name-as-directory top))))
919 (defun nnheader-message (level &rest args)
920 "Message if the Gnus backends are talkative."
921 (if (or (not (numberp gnus-verbose-backends))
922 (<= level gnus-verbose-backends))
923 (apply 'message args)
924 (apply 'format args)))
926 (defun nnheader-be-verbose (level)
927 "Return whether the backends should be verbose on LEVEL."
928 (or (not (numberp gnus-verbose-backends))
929 (<= level gnus-verbose-backends)))
931 (defvar nnheader-pathname-coding-system 'binary
932 "*Coding system for pathname.")
934 (defun nnheader-group-pathname (group dir &optional file)
935 "Make pathname for GROUP."
937 (let ((dir (file-name-as-directory (expand-file-name dir))))
938 ;; If this directory exists, we use it directly.
939 (file-name-as-directory
940 (if (file-directory-p (concat dir group))
941 (expand-file-name group dir)
942 ;; If not, we translate dots into slashes.
943 (expand-file-name (encode-coding-string
944 (nnheader-replace-chars-in-string group ?. ?/)
945 nnheader-pathname-coding-system)
947 (cond ((null file) "")
948 ((numberp file) (int-to-string file))
951 (defun nnheader-functionp (form)
952 "Return non-nil if FORM is funcallable."
953 (or (and (symbolp form) (fboundp form))
954 (and (listp form) (eq (car form) 'lambda))))
956 (defun nnheader-concat (dir &rest files)
957 "Concat DIR as directory to FILE."
958 (apply 'concat (file-name-as-directory dir) files))
960 (defun nnheader-ms-strip-cr ()
961 "Strip ^M from the end of all lines."
963 (goto-char (point-min))
964 (while (re-search-forward "\r$" nil t)
965 (delete-backward-char 1))))
967 (defun nnheader-file-size (file)
968 "Return the file size of FILE or 0."
969 (or (nth 7 (file-attributes file)) 0))
971 (defun nnheader-find-etc-directory (package &optional file)
972 "Go through the path and find the \".../etc/PACKAGE\" directory.
973 If FILE, find the \".../etc/PACKAGE\" file instead."
974 (let ((path load-path)
976 ;; We try to find the dir by looking at the load path,
977 ;; stripping away the last component and adding "etc/".
983 (directory-file-name (car path)))
986 (or file (file-directory-p dir)))
989 (setq path (cdr path))))
992 (defvar ange-ftp-path-format)
993 (defvar efs-path-regexp)
994 (defun nnheader-re-read-dir (path)
995 "Re-read directory PATH if PATH is on a remote system."
996 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
997 (when (string-match efs-path-regexp path)
998 (efs-re-read-dir path))
999 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
1000 (when (string-match (car ange-ftp-path-format) path)
1001 (ange-ftp-re-read-dir path)))))
1003 (defvar nnheader-file-coding-system 'raw-text
1004 "Coding system used in file backends of Gnus.")
1006 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
1007 "Like `insert-file-contents', q.v., but only reads in the file.
1008 A buffer may be modified in several ways after reading into the buffer due
1009 to advanced Emacs features, such as file-name-handlers, format decoding,
1010 find-file-hooks, etc.
1011 This function ensures that none of these modifications will take place."
1012 (let ((format-alist nil)
1013 (auto-mode-alist (nnheader-auto-mode-alist))
1014 (default-major-mode 'fundamental-mode)
1015 (enable-local-variables nil)
1016 (after-insert-file-functions nil)
1017 (enable-local-eval nil)
1018 (find-file-hooks nil))
1019 (insert-file-contents-as-coding-system
1020 nnheader-file-coding-system filename visit beg end replace)))
1022 (defun nnheader-find-file-noselect (&rest args)
1023 (let ((format-alist nil)
1024 (auto-mode-alist (nnheader-auto-mode-alist))
1025 (default-major-mode 'fundamental-mode)
1026 (enable-local-variables nil)
1027 (after-insert-file-functions nil)
1028 (enable-local-eval nil)
1029 (find-file-hooks nil))
1030 (apply 'find-file-noselect-as-coding-system
1031 nnheader-file-coding-system args)))
1033 (defun nnheader-auto-mode-alist ()
1034 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1035 (let ((alist auto-mode-alist)
1038 (when (listp (cdar alist))
1039 (push (car alist) out))
1043 (defun nnheader-directory-regular-files (dir)
1044 "Return a list of all regular files in DIR."
1045 (let ((files (directory-files dir t))
1048 (when (file-regular-p (car files))
1049 (push (car files) out))
1053 (defun nnheader-directory-files (&rest args)
1054 "Same as `directory-files', but prune \".\" and \"..\"."
1055 (let ((files (apply 'directory-files args))
1058 (unless (member (file-name-nondirectory (car files)) '("." ".."))
1059 (push (car files) out))
1063 (defmacro nnheader-skeleton-replace (from &optional to regexp)
1064 `(let ((new (generate-new-buffer " *nnheader replace*"))
1065 (cur (current-buffer))
1066 (start (point-min)))
1068 (goto-char (point-min))
1069 (while (,(if regexp 're-search-forward 'search-forward)
1071 (insert-buffer-substring
1072 cur start (prog1 (match-beginning 0) (set-buffer new)))
1073 (goto-char (point-max))
1074 ,(when to `(insert ,to))
1076 (setq start (point)))
1077 (insert-buffer-substring
1078 cur start (prog1 (point-max) (set-buffer new)))
1079 (copy-to-buffer cur (point-min) (point-max))
1080 (kill-buffer (current-buffer))
1083 (defun nnheader-replace-string (from to)
1084 "Do a fast replacement of FROM to TO from point to point-max."
1085 (nnheader-skeleton-replace from to))
1087 (defun nnheader-replace-regexp (from to)
1088 "Do a fast regexp replacement of FROM to TO from point to point-max."
1089 (nnheader-skeleton-replace from to t))
1091 (defun nnheader-strip-cr ()
1092 "Strip all \r's from the current buffer."
1093 (nnheader-skeleton-replace "\r"))
1095 (defalias 'nnheader-run-at-time 'run-at-time)
1096 (defalias 'nnheader-cancel-timer 'cancel-timer)
1097 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1099 (defun nnheader-Y-or-n-p (prompt)
1100 "Ask user a \"Y/n\" question. Return t if answer is neither \"n\", \"N\" nor \"C-g\"."
1101 (let ((cursor-in-echo-area t)
1105 (let (message-log-max)
1106 (while (not (memq ans '(?\ ?N ?Y ?\C-g ?\e ?\n ?\r ?n ?y)))
1107 (message "%s(Y/n) " prompt)
1108 (setq ans (read-char-exclusive))))
1109 (if (memq ans '(?\C-g ?N ?n))
1111 (message "%s(Y/n) No" prompt)
1113 (message "%s(Y/n) Yes" prompt)
1116 (when (string-match "XEmacs" emacs-version)
1117 (require 'nnheaderxm))
1119 (run-hooks 'nnheader-load-hook)
1123 ;;; nnheader.el ends here