1 ;;; nnheader.el --- header access macros for Semi-gnus and its backends
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1993, 1994, 1995, 1996,
4 ;; 1997, 1998, 2000, 2001
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))
37 ;; Requiring `gnus-util' at compile time creates a circular
38 ;; dependency between nnheader.el and gnus-util.el.
39 ;(eval-when-compile (require 'gnus-util))
43 ;; Reduce the required value of `recursive-load-depth-limit' for Emacs 21.
50 (autoload 'gnus-sorted-intersection "gnus-range")
51 (autoload 'gnus-intersection "gnus-range")
52 (autoload 'gnus-sorted-complement "gnus-range"))
54 (defvar nnheader-max-head-length 4096
55 "*Max length of the head of articles.
57 Value is an integer, nil, or t. Nil means read in chunks of a file
58 indefinitely until a complete head is found\; t means always read the
59 entire file immediately, disregarding `nnheader-head-chop-length'.
61 Integer values will in effect be rounded up to the nearest multiple of
62 `nnheader-head-chop-length'.")
64 (defvar nnheader-head-chop-length 2048
65 "*Length of each read operation when trying to fetch HEAD headers.")
67 (defvar nnheader-file-name-translation-alist nil
68 "*Alist that says how to translate characters in file names.
69 For instance, if \":\" is invalid as a file character in file names
70 on your system, you could say something like:
72 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
74 (defvar nnheader-text-coding-system
75 (if (memq system-type '(windows-nt ms-dos ms-windows))
78 "Text-safe coding system (For removing ^M).
79 This variable is a substitute for `mm-text-coding-system'.")
81 (defvar nnheader-text-coding-system-for-write nil
82 "Text coding system for write.
83 This variable is a substitute for `mm-text-coding-system-for-write'.")
86 (autoload 'nnmail-message-id "nnmail")
87 (autoload 'mail-position-on-field "sendmail")
88 (autoload 'message-remove-header "message")
89 (autoload 'gnus-point-at-eol "gnus-util")
90 (autoload 'gnus-delete-line "gnus-util" nil nil 'macro)
91 (autoload 'gnus-buffer-live-p "gnus-util"))
93 ;;; Header access macros.
95 ;; These macros may look very much like the ones in GNUS 4.1. They
96 ;; are, in a way, but you should note that the indices they use have
97 ;; been changed from the internal GNUS format to the NOV format. The
98 ;; makes it possible to read headers from XOVER much faster.
100 ;; The format of a header is now:
101 ;; [number subject from date id references chars lines xref extra]
103 ;; (That next-to-last entry is defined as "misc" in the NOV format,
104 ;; but Gnus uses it for xrefs.)
108 (defmacro mail-header-number (header)
109 "Return article number in HEADER."
110 `(mime-entity-location-internal ,header))
112 (defmacro mail-header-set-number (header number)
113 "Set article number of HEADER to NUMBER."
114 `(mime-entity-set-location-internal ,header ,number))
116 (defalias 'mail-header-subject 'mime-gnus-entity-subject-internal)
117 (defalias 'mail-header-set-subject 'mime-gnus-entity-set-subject-internal)
119 (defalias 'mail-header-from 'mime-gnus-entity-from-internal)
120 (defalias 'mail-header-set-from 'mime-gnus-entity-set-from-internal)
122 (defalias 'mail-header-date 'mime-gnus-entity-date-internal)
123 (defalias 'mail-header-set-date 'mime-gnus-entity-set-date-internal)
125 (defalias 'mail-header-message-id 'mime-gnus-entity-id-internal)
126 (defalias 'mail-header-id 'mime-gnus-entity-id-internal)
127 (defalias 'mail-header-set-message-id 'mime-gnus-entity-set-id-internal)
128 (defalias 'mail-header-set-id 'mime-gnus-entity-set-id-internal)
130 (defalias 'mail-header-references 'mime-gnus-entity-references-internal)
131 (defalias 'mail-header-set-references
132 'mime-gnus-entity-set-references-internal)
134 (defalias 'mail-header-chars 'mime-gnus-entity-chars-internal)
135 (defalias 'mail-header-set-chars 'mime-gnus-entity-set-chars-internal)
137 (defalias 'mail-header-lines 'mime-gnus-entity-lines-internal)
138 (defalias 'mail-header-set-lines 'mime-gnus-entity-set-lines-internal)
140 (defalias 'mail-header-xref 'mime-gnus-entity-xref-internal)
141 (defalias 'mail-header-set-xref 'mime-gnus-entity-set-xref-internal)
143 (defalias 'nnheader-decode-subject
144 (mime-find-field-decoder 'Subject 'nov))
145 (defalias 'nnheader-decode-from
146 (mime-find-field-decoder 'From 'nov))
148 (defalias 'mail-header-extra 'mime-gnus-entity-extra-internal)
149 (defalias 'mail-header-set-extra 'mime-gnus-entity-set-extra-internal)
151 (defun nnheader-decode-field-body (field-body field-name
152 &optional mode max-column)
153 (mime-decode-field-body field-body
154 (if (stringp field-name)
155 (intern (capitalize field-name))
159 (defsubst make-full-mail-header (&optional number subject from date id
160 references chars lines xref
162 "Create a new mail header structure initialized with the parameters given."
163 (luna-make-entity (mm-expand-class-name 'gnus)
166 (nnheader-decode-subject subject))
168 (nnheader-decode-from from))
171 :references references
175 :original-header (list (cons 'Subject subject)
179 (defsubst make-full-mail-header-from-decoded-header
180 (&optional number subject from date id references chars lines xref extra)
181 "Create a new mail header structure initialized with the parameters given."
182 (luna-make-entity (mm-expand-class-name 'gnus)
188 :references references
194 (defsubst make-mail-header (&optional init)
195 "Create a new mail header structure initialized with INIT."
196 (make-full-mail-header init init init init init
197 init init init init init))
199 ;; fake message-ids: generation and detection
201 (defvar nnheader-fake-message-id 1)
203 (defsubst nnheader-generate-fake-message-id ()
204 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
206 (defsubst nnheader-fake-message-id-p (id)
207 (save-match-data ; regular message-id's are <.*>
208 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
210 ;; Parsing headers and NOV lines.
212 (defsubst nnheader-header-value ()
215 (skip-chars-forward " \t")
216 (buffer-substring (point) (std11-field-end))
219 (defun nnheader-parse-head (&optional naked)
220 (let ((case-fold-search t)
221 (cur (current-buffer))
222 (buffer-read-only nil)
223 in-reply-to lines p ref)
224 (goto-char (point-min))
227 ;; Search to the beginning of the next header. Error messages
228 ;; do not begin with 2 or 3.
230 (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
231 ;; This implementation of this function, with nine
232 ;; search-forwards instead of the one re-search-forward and
233 ;; a case (which basically was the old function) is actually
234 ;; about twice as fast, even though it looks messier. You
235 ;; can't have everything, I guess. Speed and elegance
236 ;; don't always go hand in hand.
237 (make-full-mail-header
247 (narrow-to-region (point)
248 (or (and (search-forward "\n.\n" nil t)
254 (if (search-forward "\nsubject:" nil t)
255 (nnheader-header-value) "(none)"))
259 (if (search-forward "\nfrom:" nil t)
260 (nnheader-header-value) "(nobody)"))
264 (if (search-forward "\ndate:" nil t)
265 (nnheader-header-value) ""))
269 (if (search-forward "\nmessage-id:" nil t)
271 (1- (or (search-forward "<" (gnus-point-at-eol) t)
273 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
274 ;; If there was no message-id, we just fake one to make
275 ;; subsequent routines simpler.
276 (nnheader-generate-fake-message-id)))
280 (if (search-forward "\nreferences:" nil t)
281 (nnheader-header-value)
282 ;; Get the references from the in-reply-to header if there
283 ;; were no references and the in-reply-to header looks
285 (if (and (search-forward "\nin-reply-to:" nil t)
286 (setq in-reply-to (nnheader-header-value))
287 (string-match "<[^\n>]+>" in-reply-to))
289 (setq ref (substring in-reply-to (match-beginning 0)
291 (while (string-match "<[^\n>]+>"
292 in-reply-to (match-end 0))
293 (setq ref2 (substring in-reply-to (match-beginning 0)
295 (when (> (length ref2) (length ref))
304 (if (search-forward "\nlines: " nil t)
305 (if (numberp (setq lines (read cur)))
311 (and (search-forward "\nxref:" nil t)
312 (nnheader-header-value)))
315 (when nnmail-extra-headers
316 (let ((extra nnmail-extra-headers)
320 (when (search-forward
321 (concat "\n" (symbol-name (car extra)) ":") nil t)
322 (push (cons (car extra) (nnheader-header-value))
327 (goto-char (point-min))
330 (defmacro nnheader-nov-skip-field ()
331 '(search-forward "\t" eol 'move))
333 (defmacro nnheader-nov-field ()
334 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
336 (defmacro nnheader-nov-read-integer ()
338 (if (eq (char-after) ?\t)
340 (let ((num (condition-case nil
341 (read (current-buffer))
343 (if (numberp num) num 0)))
345 (search-forward "\t" eol 'move))))
347 (defmacro nnheader-nov-parse-extra ()
349 (while (not (memq (char-after) '(?\n nil)))
350 (setq string (nnheader-nov-field))
351 (when (string-match "^\\([^ :]+\\): " string)
352 (push (cons (intern (match-string 1 string))
353 (substring string (match-end 0)))
357 (defmacro nnheader-nov-read-message-id ()
358 '(let ((id (nnheader-nov-field)))
359 (if (string-match "^<[^>]+>$" id)
361 (nnheader-generate-fake-message-id))))
363 (defun nnheader-parse-nov ()
364 (let ((eol (gnus-point-at-eol)))
365 (make-full-mail-header
366 (nnheader-nov-read-integer) ; number
367 (nnheader-nov-field) ; subject
368 (nnheader-nov-field) ; from
369 (nnheader-nov-field) ; date
370 (nnheader-nov-read-message-id) ; id
371 (nnheader-nov-field) ; refs
372 (nnheader-nov-read-integer) ; chars
373 (nnheader-nov-read-integer) ; lines
374 (if (eq (char-after) ?\n)
376 (if (looking-at "Xref: ")
377 (goto-char (match-end 0)))
378 (nnheader-nov-field)) ; Xref
379 (nnheader-nov-parse-extra)))) ; extra
381 (defun nnheader-insert-nov (header)
382 (princ (mail-header-number header) (current-buffer))
386 (or (mime-entity-fetch-field header 'Subject) "(none)") "\t"
387 (or (mime-entity-fetch-field header 'From) "(nobody)") "\t"
388 (or (mail-header-date header) "") "\t"
389 (or (mail-header-id header)
392 (or (mail-header-references header) "") "\t")
393 (princ (or (mail-header-chars header) 0) (current-buffer))
395 (princ (or (mail-header-lines header) 0) (current-buffer))
397 (when (mail-header-xref header)
398 (insert "Xref: " (mail-header-xref header)))
399 (when (or (mail-header-xref header)
400 (mail-header-extra header))
402 (when (mail-header-extra header)
403 (let ((extra (mail-header-extra header)))
405 (insert (symbol-name (caar extra))
406 ": " (cdar extra) "\t")
410 (while (search-backward "\n" p t)
414 (defun nnheader-insert-header (header)
416 "Subject: " (or (mail-header-subject header) "(none)") "\n"
417 "From: " (or (mail-header-from header) "(nobody)") "\n"
418 "Date: " (or (mail-header-date header) "") "\n"
419 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
420 "References: " (or (mail-header-references header) "") "\n"
422 (princ (or (mail-header-lines header) 0) (current-buffer))
425 (defun nnheader-insert-article-line (article)
426 (goto-char (point-min))
428 (princ article (current-buffer))
429 (insert " Article retrieved.\n")
430 (search-forward "\n\n" nil 'move)
431 (delete-region (point) (point-max))
435 (defun nnheader-nov-delete-outside-range (beg end)
436 "Delete all NOV lines that lie outside the BEG to END range."
437 ;; First we find the first wanted line.
438 (nnheader-find-nov-line beg)
439 (delete-region (point-min) (point))
440 ;; Then we find the last wanted line.
441 (when (nnheader-find-nov-line end)
443 (delete-region (point) (point-max)))
445 (defun nnheader-find-nov-line (article)
446 "Put point at the NOV line that start with ARTICLE.
447 If ARTICLE doesn't exist, put point where that line
448 would have been. The function will return non-nil if
449 the line could be found."
450 ;; This function basically does a binary search.
451 (let ((max (point-max))
452 (min (goto-char (point-min)))
453 (cur (current-buffer))
457 (goto-char (/ (+ max min) 2))
459 (if (or (= (point) prev)
463 (while (and (not (numberp (setq num (read cur))))
466 (cond ((> num article)
471 (setq found 'yes)))))
472 ;; We may be at the first line.
475 (setq num (read cur)))
476 ;; Now we may have found the article we're looking for, or we
477 ;; may be somewhere near it.
478 (when (and (not (eq found 'yes))
479 (not (eq num article)))
481 (while (and (< (point) max)
482 (or (not (numberp num))
487 (= (setq num (read cur)) article)))
488 (unless (eq num article)
493 (defun nnheader-retrieve-headers-from-directory* (articles
494 directory dependencies
496 fetch-old force-new large
500 (number (length articles))
502 (file-name-coding-system 'binary)
503 (pathname-coding-system 'binary)
505 (cur (current-buffer))
507 headers header id end ref in-reply-to lines chars ctype)
508 ;; We don't support fetching by Message-ID.
509 (if (stringp (car articles))
512 (when (and (file-exists-p
513 (setq file (expand-file-name
515 (setq article (pop articles)))
517 (not (file-directory-p file)))
519 (nnheader-insert-head file)
521 (std11-narrow-to-header)
524 (make-full-mail-header
528 (or (std11-fetch-field "Subject")
531 (or (std11-fetch-field "From")
534 (or (std11-fetch-field "Date")
538 (goto-char (point-min))
539 (setq id (if (re-search-forward
540 "^Message-ID: *\\(<[^\n\t> ]+>\\)" nil t)
541 ;; We do it this way to make sure the Message-ID
542 ;; is (somewhat) syntactically valid.
543 (buffer-substring (match-beginning 1)
545 ;; If there was no message-id, we just fake one
546 ;; to make subsequent routines simpler.
547 (nnheader-generate-fake-message-id))))
550 (goto-char (point-min))
551 (if (search-forward "\nReferences: " nil t)
555 (buffer-substring (match-end 0) (std11-field-end))
560 (search-backward ">" end t)
563 (search-backward "<" end t)
565 ;; Get the references from the in-reply-to header if there
566 ;; were no references and the in-reply-to header looks
568 (if (and (search-forward "\nIn-Reply-To: " nil t)
570 (buffer-substring (match-end 0)
572 (string-match "<[^>]+>" in-reply-to))
574 (setq ref (substring in-reply-to (match-beginning 0)
576 (while (string-match "<[^>]+>"
577 in-reply-to (match-end 0))
579 (substring in-reply-to (match-beginning 0)
581 (when (> (length ref2) (length ref))
587 (goto-char (point-min))
588 (if (search-forward "\nChars: " nil t)
589 (if (numberp (setq chars (ignore-errors (read cur))))
594 (goto-char (point-min))
595 (if (search-forward "\nLines: " nil t)
596 (if (numberp (setq lines (ignore-errors (read cur))))
600 (std11-fetch-field "Xref")
602 (goto-char (point-min))
603 (if (setq ctype (std11-fetch-field "Content-Type"))
604 (mime-entity-set-content-type-internal
605 header (mime-parse-Content-Type ctype)))
608 (gnus-dependencies-add-header
609 header dependencies force-new))
610 (push header headers))
612 (setq count (1+ count))
616 (nnheader-message 5 "%s: Receiving headers... %d%%"
618 (/ (* count 100) number))))
621 (nnheader-message 5 "%s: Receiving headers...done" backend))
625 (defun nnheader-retrieve-headers-from-directory (articles
626 directory dependencies
628 fetch-old force-new large
631 (nreverse (nnheader-retrieve-headers-from-directory*
632 articles directory dependencies
633 fetch-old force-new large backend))))
635 (defun nnheader-get-newsgroup-headers-xover* (sequence
637 force-new dependencies
639 "Parse the news overview data in the server buffer, and return a
640 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
641 ;; Get the Xref when the users reads the articles since most/some
642 ;; NNTP servers do not include Xrefs when using XOVER.
643 ;; (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
644 (let ((cur nntp-server-buffer)
645 number headers header)
647 (set-buffer nntp-server-buffer)
648 ;; Allow the user to mangle the headers before parsing them.
649 (gnus-run-hooks 'gnus-parse-headers-hook)
650 (goto-char (point-min))
653 (while (and sequence (not (eobp)))
654 (setq number (read cur))
656 (< (car sequence) number))
657 (setq sequence (cdr sequence)))
659 (eq number (car sequence))
661 (setq sequence (cdr sequence))
664 number dependencies force-new))))
665 (push header headers))
668 (gnus-error 4 "Strange nov line (%d)"
669 (count-lines (point-min) (point)))))
671 ;; A common bug in inn is that if you have posted an article and
672 ;; then retrieves the active file, it will answer correctly --
673 ;; the new article is included. However, a NOV entry for the
674 ;; article may not have been generated yet, so this may fail.
675 ;; We work around this problem by retrieving the last few
676 ;; headers using HEAD.
679 ;; Various cruft the backends and Gnus need to communicate.
681 (defvar nntp-server-buffer nil)
682 (defvar nntp-process-response nil)
683 (defvar gnus-verbose-backends 7
684 "*A number that says how talkative the Gnus backends should be.")
685 (defvar gnus-nov-is-evil nil
686 "If non-nil, Gnus backends will never output headers in the NOV format.")
687 (defvar news-reply-yank-from nil)
688 (defvar news-reply-yank-message-id nil)
690 (defvar nnheader-callback-function nil)
692 (defun nnheader-init-server-buffer ()
693 "Initialize the Gnus-backend communication buffer."
695 (unless (gnus-buffer-live-p nntp-server-buffer)
696 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
697 (set-buffer nntp-server-buffer)
699 (kill-all-local-variables)
700 (setq case-fold-search t) ;Should ignore case.
701 (set (make-local-variable 'nntp-process-response) nil)
704 ;;; Various functions the backends use.
706 (defun nnheader-file-error (file)
707 "Return a string that says what is wrong with FILE."
710 ((not (file-exists-p file))
712 ((file-directory-p file)
714 ((not (file-readable-p file))
715 "%s is not readable"))
718 (defun nnheader-insert-head (file)
719 "Insert the head of the article."
720 (when (file-exists-p file)
721 (if (eq nnheader-max-head-length t)
722 ;; Just read the entire file.
723 (nnheader-insert-file-contents file)
724 ;; Read 1K blocks until we find a separator.
727 (while (and (eq nnheader-head-chop-length
728 (nth 1 (nnheader-insert-file-contents
730 (incf beg nnheader-head-chop-length))))
731 (prog1 (not (search-forward "\n\n" nil t))
732 (goto-char (point-max)))
733 (or (null nnheader-max-head-length)
734 (< beg nnheader-max-head-length))))))
737 (defun nnheader-article-p ()
738 "Say whether the current buffer looks like an article."
739 (goto-char (point-min))
740 (if (not (search-forward "\n\n" nil t))
742 (narrow-to-region (point-min) (1- (point)))
743 (goto-char (point-min))
744 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
745 (goto-char (match-end 0)))
750 (defun nnheader-insert-references (references message-id)
751 "Insert a References header based on REFERENCES and MESSAGE-ID."
752 (if (and (not references) (not message-id))
753 ;; This is invalid, but not all articles have Message-IDs.
755 (mail-position-on-field "References")
756 (let ((begin (save-excursion (beginning-of-line) (point)))
761 (when (and references message-id)
765 ;; Fold long References lines to conform to RFC1036 (sort of).
766 ;; The region must end with a newline to fill the region
767 ;; without inserting extra newline.
768 (fill-region-as-paragraph begin (1+ (point))))))
770 (defun nnheader-replace-header (header new-value)
771 "Remove HEADER and insert the NEW-VALUE."
774 (nnheader-narrow-to-headers)
776 (message-remove-header header)
777 (goto-char (point-max))
778 (insert header ": " new-value "\n")))))
780 (defun nnheader-narrow-to-headers ()
781 "Narrow to the head of an article."
784 (goto-char (point-min))
785 (if (search-forward "\n\n" nil t)
788 (goto-char (point-min)))
790 (defun nnheader-set-temp-buffer (name &optional noerase)
791 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
792 (set-buffer (get-buffer-create name))
793 (buffer-disable-undo)
798 (eval-when-compile (defvar jka-compr-compression-info-list))
799 (defvar nnheader-numerical-files
800 (if (boundp 'jka-compr-compression-info-list)
801 (concat "\\([0-9]+\\)\\("
802 (mapconcat (lambda (i) (aref i 0))
803 jka-compr-compression-info-list "\\|")
806 "Regexp that match numerical files.")
808 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
809 "Regexp that matches numerical file names.")
811 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
812 "Regexp that matches numerical full file paths.")
814 (defsubst nnheader-file-to-number (file)
815 "Take a FILE name and return the article number."
816 (if (string= nnheader-numerical-short-files "^[0-9]+$")
818 (string-match nnheader-numerical-short-files file)
819 (string-to-int (match-string 0 file))))
821 (defvar nnheader-directory-files-is-safe nil
822 "If non-nil, Gnus believes `directory-files' is safe.
823 It has been reported numerous times that `directory-files' fails with
824 an alarming frequency on NFS mounted file systems. If it is nil,
825 `nnheader-directory-files-safe' is used.")
827 (defun nnheader-directory-files-safe (&rest args)
828 "Execute `directory-files' twice and returns the longer result."
829 (let ((first (apply 'directory-files args))
830 (second (apply 'directory-files args)))
831 (if (> (length first) (length second))
835 (defun nnheader-directory-articles (dir)
836 "Return a list of all article files in directory DIR."
837 (mapcar 'nnheader-file-to-number
838 (if nnheader-directory-files-is-safe
840 dir nil nnheader-numerical-short-files t)
841 (nnheader-directory-files-safe
842 dir nil nnheader-numerical-short-files t))))
844 (defun nnheader-article-to-file-alist (dir)
845 "Return an alist of article/file pairs in DIR."
846 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
847 (if nnheader-directory-files-is-safe
849 dir nil nnheader-numerical-short-files t)
850 (nnheader-directory-files-safe
851 dir nil nnheader-numerical-short-files t))))
853 (defun nnheader-fold-continuation-lines ()
854 "Fold continuation lines in the current buffer."
855 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
857 (defun nnheader-translate-file-chars (file &optional full)
858 "Translate FILE into something that can be a file name.
859 If FULL, translate everything."
860 (if (null nnheader-file-name-translation-alist)
861 ;; No translation is necessary.
866 ;; Do complete translation.
867 (setq leaf (copy-sequence file)
869 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
871 ;; We translate -- but only the file name. We leave the directory
873 (if (and (featurep 'xemacs)
874 (memq system-type '(cygwin32 win32 w32 mswindows windows-nt)))
875 ;; This is needed on NT and stuff, because
876 ;; file-name-nondirectory is not enough to split
877 ;; file names, containing ':', e.g.
878 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
880 ;; we are trying to correctly split such names:
881 ;; "d:file.name" -> "a:" "file.name"
882 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
883 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
885 ;; to translate then only the file name part.
889 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
890 (setq leaf (substring file (match-beginning 2))
891 path (substring file 0 (match-beginning 2)))))
892 ;; Emacs DTRT, says andrewi.
893 (setq leaf (file-name-nondirectory file)
894 path (file-name-directory file))))
895 (setq len (length leaf))
897 (when (setq trans (cdr (assq (aref leaf i)
898 nnheader-file-name-translation-alist)))
901 (concat path leaf))))
903 (defun nnheader-report (backend &rest args)
904 "Report an error from the BACKEND.
905 The first string in ARGS can be a format string."
906 (set (intern (format "%s-status-string" backend))
907 (if (< (length args) 2)
909 (apply 'format args)))
912 (defun nnheader-get-report (backend)
913 "Get the most recent report from BACKEND."
915 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
917 (error (nnheader-message 5 ""))))
919 (defun nnheader-insert (format &rest args)
920 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
921 If FORMAT isn't a format string, it and all ARGS will be inserted
924 (set-buffer nntp-server-buffer)
926 (if (string-match "%" format)
927 (insert (apply 'format format args))
928 (apply 'insert format args))
931 (static-if (fboundp 'subst-char-in-string)
932 (defsubst nnheader-replace-chars-in-string (string from to)
933 (subst-char-in-string from to string))
934 (defun nnheader-replace-chars-in-string (string from to)
935 "Replace characters in STRING from FROM to TO."
936 (let ((string (substring string 0)) ;Copy string.
937 (len (length string))
939 ;; Replace all occurrences of FROM with TO.
941 (when (= (aref string idx) from)
942 (aset string idx to))
946 (defun nnheader-replace-duplicate-chars-in-string (string from to)
947 "Replace characters in STRING from FROM to TO."
948 (let ((string (substring string 0)) ;Copy string.
949 (len (length string))
951 ;; Replace all occurrences of FROM with TO.
953 (setq i (aref string idx))
954 (when (and (eq prev from) (= i from))
955 (aset string (1- idx) to)
956 (aset string idx to))
961 (defun nnheader-file-to-group (file &optional top)
962 "Return a group name based on FILE and TOP."
963 (nnheader-replace-chars-in-string
967 (substring (expand-file-name file)
970 (file-name-as-directory top))))
974 (defun nnheader-message (level &rest args)
975 "Message if the Gnus backends are talkative."
976 (if (or (not (numberp gnus-verbose-backends))
977 (<= level gnus-verbose-backends))
978 (apply 'message args)
979 (apply 'format args)))
981 (defun nnheader-be-verbose (level)
982 "Return whether the backends should be verbose on LEVEL."
983 (or (not (numberp gnus-verbose-backends))
984 (<= level gnus-verbose-backends)))
986 (defvar nnheader-pathname-coding-system 'binary
987 "*Coding system for pathname.")
989 (defun nnheader-group-pathname (group dir &optional file)
990 "Make pathname for GROUP."
992 (let ((dir (file-name-as-directory (expand-file-name dir))))
993 ;; If this directory exists, we use it directly.
994 (file-name-as-directory
995 (if (file-directory-p (concat dir group))
996 (expand-file-name group dir)
997 ;; If not, we translate dots into slashes.
998 (expand-file-name (encode-coding-string
999 (nnheader-replace-chars-in-string group ?. ?/)
1000 nnheader-pathname-coding-system)
1002 (cond ((null file) "")
1003 ((numberp file) (int-to-string file))
1006 (defun nnheader-functionp (form)
1007 "Return non-nil if FORM is funcallable."
1008 (or (and (symbolp form) (fboundp form))
1009 (and (listp form) (eq (car form) 'lambda))))
1011 (defun nnheader-concat (dir &rest files)
1012 "Concat DIR as directory to FILES."
1013 (apply 'concat (file-name-as-directory dir) files))
1015 (defun nnheader-ms-strip-cr ()
1016 "Strip ^M from the end of all lines."
1018 (goto-char (point-min))
1019 (while (re-search-forward "\r$" nil t)
1020 (delete-backward-char 1))))
1022 (defun nnheader-file-size (file)
1023 "Return the file size of FILE or 0."
1024 (or (nth 7 (file-attributes file)) 0))
1026 (defun nnheader-find-etc-directory (package &optional file)
1027 "Go through the path and find the \".../etc/PACKAGE\" directory.
1028 If FILE, find the \".../etc/PACKAGE\" file instead."
1029 (let ((path load-path)
1031 ;; We try to find the dir by looking at the load path,
1032 ;; stripping away the last component and adding "etc/".
1037 (file-name-directory
1038 (directory-file-name (car path)))
1041 (or file (file-directory-p dir)))
1044 (setq path (cdr path))))
1048 (defvar ange-ftp-path-format)
1049 (defvar efs-path-regexp))
1050 (defun nnheader-re-read-dir (path)
1051 "Re-read directory PATH if PATH is on a remote system."
1052 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
1053 (when (string-match efs-path-regexp path)
1054 (efs-re-read-dir path))
1055 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
1056 (when (string-match (car ange-ftp-path-format) path)
1057 (ange-ftp-re-read-dir path)))))
1059 (defvar nnheader-file-coding-system 'raw-text
1060 "Coding system used in file backends of Gnus.")
1062 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
1063 "Like `insert-file-contents', q.v., but only reads in the file.
1064 A buffer may be modified in several ways after reading into the buffer due
1065 to advanced Emacs features, such as file-name-handlers, format decoding,
1066 find-file-hooks, etc.
1067 This function ensures that none of these modifications will take place."
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 (insert-file-contents-as-coding-system
1076 nnheader-file-coding-system filename visit beg end replace)))
1078 (defun nnheader-find-file-noselect (&rest args)
1079 (let ((format-alist nil)
1080 (auto-mode-alist (nnheader-auto-mode-alist))
1081 (default-major-mode 'fundamental-mode)
1082 (enable-local-variables nil)
1083 (after-insert-file-functions nil)
1084 (enable-local-eval nil)
1085 (find-file-hooks nil))
1086 (apply 'find-file-noselect-as-coding-system
1087 nnheader-file-coding-system args)))
1089 (defun nnheader-auto-mode-alist ()
1090 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1091 (let ((alist auto-mode-alist)
1094 (when (listp (cdar alist))
1095 (push (car alist) out))
1099 (defun nnheader-directory-regular-files (dir)
1100 "Return a list of all regular files in DIR."
1101 (let ((files (directory-files dir t))
1104 (when (file-regular-p (car files))
1105 (push (car files) out))
1109 (defun nnheader-directory-files (&rest args)
1110 "Same as `directory-files', but prune \".\" and \"..\"."
1111 (let ((files (apply 'directory-files args))
1114 (unless (member (file-name-nondirectory (car files)) '("." ".."))
1115 (push (car files) out))
1119 (defmacro nnheader-skeleton-replace (from &optional to regexp)
1120 `(let ((new (generate-new-buffer " *nnheader replace*"))
1121 (cur (current-buffer))
1122 (start (point-min)))
1124 (goto-char (point-min))
1125 (while (,(if regexp 're-search-forward 'search-forward)
1127 (insert-buffer-substring
1128 cur start (prog1 (match-beginning 0) (set-buffer new)))
1129 (goto-char (point-max))
1130 ,(when to `(insert ,to))
1132 (setq start (point)))
1133 (insert-buffer-substring
1134 cur start (prog1 (point-max) (set-buffer new)))
1135 (copy-to-buffer cur (point-min) (point-max))
1136 (kill-buffer (current-buffer))
1139 (defun nnheader-replace-string (from to)
1140 "Do a fast replacement of FROM to TO from point to `point-max'."
1141 (nnheader-skeleton-replace from to))
1143 (defun nnheader-replace-regexp (from to)
1144 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1145 (nnheader-skeleton-replace from to t))
1147 (defun nnheader-strip-cr ()
1148 "Strip all \r's from the current buffer."
1149 (nnheader-skeleton-replace "\r"))
1151 (defalias 'nnheader-run-at-time 'run-at-time)
1152 (defalias 'nnheader-cancel-timer 'cancel-timer)
1153 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1154 (defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1156 (defun nnheader-Y-or-n-p (prompt)
1157 "Ask user a \"Y/n\" question. Return t if answer is neither \"n\", \"N\" nor \"C-g\"."
1158 (let ((cursor-in-echo-area t)
1162 (let (message-log-max)
1163 (while (not (memq ans '(?\ ?N ?Y ?\C-g ?\e ?\n ?\r ?n ?y)))
1164 (message "%s(Y/n) " prompt)
1165 (setq ans (read-char-exclusive))))
1166 (if (memq ans '(?\C-g ?N ?n))
1168 (message "%s(Y/n) No" prompt)
1170 (message "%s(Y/n) Yes" prompt)
1173 (defun nnheader-image-load-path (&optional package)
1175 (dolist (path load-path (nreverse result))
1176 (if (file-directory-p
1177 (setq dir (concat (file-name-directory
1178 (directory-file-name path))
1179 "etc/" (or package "gnus/"))))
1181 (push path result))))
1182 (defalias 'mm-image-load-path 'nnheader-image-load-path)
1184 (defalias 'mm-read-coding-system
1185 (if (or (and (featurep 'xemacs)
1186 (<= (string-to-number emacs-version) 21.1))
1188 (lambda (prompt &optional default-coding-system)
1189 (read-coding-system prompt))
1190 'read-coding-system))
1192 (when (featurep 'xemacs)
1193 (require 'nnheaderxm))
1195 (run-hooks 'nnheader-load-hook)
1199 ;;; nnheader.el ends here