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 (defcustom gnus-verbose-backends 7
55 "Integer that says how verbose the Gnus backends should be.
56 The higher the number, the more messages the Gnus backends will flash
57 to say what it's doing. At zero, the Gnus backends will be totally
58 mute; at five, they will display most important messages; and at ten,
59 they will keep on jabbering all the time."
63 (defcustom gnus-nov-is-evil nil
64 "If non-nil, Gnus backends will never output headers in the NOV format."
68 (defvar nnheader-max-head-length 4096
69 "*Max length of the head of articles.
71 Value is an integer, nil, or t. Nil means read in chunks of a file
72 indefinitely until a complete head is found\; t means always read the
73 entire file immediately, disregarding `nnheader-head-chop-length'.
75 Integer values will in effect be rounded up to the nearest multiple of
76 `nnheader-head-chop-length'.")
78 (defvar nnheader-head-chop-length 2048
79 "*Length of each read operation when trying to fetch HEAD headers.")
81 (defvar nnheader-file-name-translation-alist nil
82 "*Alist that says how to translate characters in file names.
83 For instance, if \":\" is invalid as a file character in file names
84 on your system, you could say something like:
86 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
88 (defvar nnheader-text-coding-system
89 (if (memq system-type '(windows-nt ms-dos ms-windows))
92 "Text-safe coding system (For removing ^M).
93 This variable is a substitute for `mm-text-coding-system'.")
95 (defvar nnheader-text-coding-system-for-write nil
96 "Text coding system for write.
97 This variable is a substitute for `mm-text-coding-system-for-write'.")
100 (autoload 'nnmail-message-id "nnmail")
101 (autoload 'mail-position-on-field "sendmail")
102 (autoload 'message-remove-header "message")
103 (autoload 'gnus-point-at-eol "gnus-util")
104 (autoload 'gnus-delete-line "gnus-util" nil nil 'macro)
105 (autoload 'gnus-buffer-live-p "gnus-util"))
107 ;;; Header access macros.
109 ;; These macros may look very much like the ones in GNUS 4.1. They
110 ;; are, in a way, but you should note that the indices they use have
111 ;; been changed from the internal GNUS format to the NOV format. The
112 ;; makes it possible to read headers from XOVER much faster.
114 ;; The format of a header is now:
115 ;; [number subject from date id references chars lines xref extra]
117 ;; (That next-to-last entry is defined as "misc" in the NOV format,
118 ;; but Gnus uses it for xrefs.)
122 (defmacro mail-header-number (header)
123 "Return article number in HEADER."
124 `(mime-entity-location-internal ,header))
126 (defmacro mail-header-set-number (header number)
127 "Set article number of HEADER to NUMBER."
128 `(mime-entity-set-location-internal ,header ,number))
130 (defalias 'mail-header-subject 'mime-gnus-entity-subject-internal)
131 (defalias 'mail-header-set-subject 'mime-gnus-entity-set-subject-internal)
133 (defalias 'mail-header-from 'mime-gnus-entity-from-internal)
134 (defalias 'mail-header-set-from 'mime-gnus-entity-set-from-internal)
136 (defalias 'mail-header-date 'mime-gnus-entity-date-internal)
137 (defalias 'mail-header-set-date 'mime-gnus-entity-set-date-internal)
139 (defalias 'mail-header-message-id 'mime-gnus-entity-id-internal)
140 (defalias 'mail-header-id 'mime-gnus-entity-id-internal)
141 (defalias 'mail-header-set-message-id 'mime-gnus-entity-set-id-internal)
142 (defalias 'mail-header-set-id 'mime-gnus-entity-set-id-internal)
144 (defalias 'mail-header-references 'mime-gnus-entity-references-internal)
145 (defalias 'mail-header-set-references
146 'mime-gnus-entity-set-references-internal)
148 (defalias 'mail-header-chars 'mime-gnus-entity-chars-internal)
149 (defalias 'mail-header-set-chars 'mime-gnus-entity-set-chars-internal)
151 (defalias 'mail-header-lines 'mime-gnus-entity-lines-internal)
152 (defalias 'mail-header-set-lines 'mime-gnus-entity-set-lines-internal)
154 (defalias 'mail-header-xref 'mime-gnus-entity-xref-internal)
155 (defalias 'mail-header-set-xref 'mime-gnus-entity-set-xref-internal)
157 (defalias 'nnheader-decode-subject
158 (mime-find-field-decoder 'Subject 'nov))
159 (defalias 'nnheader-decode-from
160 (mime-find-field-decoder 'From 'nov))
162 (defalias 'mail-header-extra 'mime-gnus-entity-extra-internal)
163 (defalias 'mail-header-set-extra 'mime-gnus-entity-set-extra-internal)
165 (defun nnheader-decode-field-body (field-body field-name
166 &optional mode max-column)
167 (mime-decode-field-body field-body
168 (if (stringp field-name)
169 (intern (capitalize field-name))
173 (defsubst make-full-mail-header (&optional number subject from date id
174 references chars lines xref
176 "Create a new mail header structure initialized with the parameters given."
177 (luna-make-entity (mm-expand-class-name 'gnus)
180 (nnheader-decode-subject subject))
182 (nnheader-decode-from from))
185 :references references
189 :original-header (list (cons 'Subject subject)
193 (defsubst make-full-mail-header-from-decoded-header
194 (&optional number subject from date id references chars lines xref extra)
195 "Create a new mail header structure initialized with the parameters given."
196 (luna-make-entity (mm-expand-class-name 'gnus)
202 :references references
208 (defsubst make-mail-header (&optional init)
209 "Create a new mail header structure initialized with INIT."
210 (make-full-mail-header init init init init init
211 init init init init init))
213 ;; fake message-ids: generation and detection
215 (defvar nnheader-fake-message-id 1)
217 (defsubst nnheader-generate-fake-message-id ()
218 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
220 (defsubst nnheader-fake-message-id-p (id)
221 (save-match-data ; regular message-id's are <.*>
222 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
224 ;; Parsing headers and NOV lines.
226 (defsubst nnheader-header-value ()
229 (skip-chars-forward " \t")
230 (buffer-substring (point) (std11-field-end))
233 (defun nnheader-parse-head (&optional naked)
234 (let ((case-fold-search t)
235 (cur (current-buffer))
236 (buffer-read-only nil)
237 in-reply-to lines p ref)
238 (goto-char (point-min))
241 ;; Search to the beginning of the next header. Error messages
242 ;; do not begin with 2 or 3.
244 (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
245 ;; This implementation of this function, with nine
246 ;; search-forwards instead of the one re-search-forward and
247 ;; a case (which basically was the old function) is actually
248 ;; about twice as fast, even though it looks messier. You
249 ;; can't have everything, I guess. Speed and elegance
250 ;; don't always go hand in hand.
251 (make-full-mail-header
261 (narrow-to-region (point)
262 (or (and (search-forward "\n.\n" nil t)
268 (if (search-forward "\nsubject:" nil t)
269 (nnheader-header-value) "(none)"))
273 (if (search-forward "\nfrom:" nil t)
274 (nnheader-header-value) "(nobody)"))
278 (if (search-forward "\ndate:" nil t)
279 (nnheader-header-value) ""))
283 (if (search-forward "\nmessage-id:" nil t)
285 (1- (or (search-forward "<" (gnus-point-at-eol) t)
287 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
288 ;; If there was no message-id, we just fake one to make
289 ;; subsequent routines simpler.
290 (nnheader-generate-fake-message-id)))
294 (if (search-forward "\nreferences:" nil t)
295 (nnheader-header-value)
296 ;; Get the references from the in-reply-to header if there
297 ;; were no references and the in-reply-to header looks
299 (if (and (search-forward "\nin-reply-to:" nil t)
300 (setq in-reply-to (nnheader-header-value))
301 (string-match "<[^\n>]+>" in-reply-to))
303 (setq ref (substring in-reply-to (match-beginning 0)
305 (while (string-match "<[^\n>]+>"
306 in-reply-to (match-end 0))
307 (setq ref2 (substring in-reply-to (match-beginning 0)
309 (when (> (length ref2) (length ref))
318 (if (search-forward "\nlines: " nil t)
319 (if (numberp (setq lines (read cur)))
325 (and (search-forward "\nxref:" nil t)
326 (nnheader-header-value)))
329 (when nnmail-extra-headers
330 (let ((extra nnmail-extra-headers)
334 (when (search-forward
335 (concat "\n" (symbol-name (car extra)) ":") nil t)
336 (push (cons (car extra) (nnheader-header-value))
341 (goto-char (point-min))
344 (defmacro nnheader-nov-skip-field ()
345 '(search-forward "\t" eol 'move))
347 (defmacro nnheader-nov-field ()
348 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
350 (defmacro nnheader-nov-read-integer ()
352 (if (eq (char-after) ?\t)
354 (let ((num (condition-case nil
355 (read (current-buffer))
357 (if (numberp num) num 0)))
359 (search-forward "\t" eol 'move))))
361 (defmacro nnheader-nov-parse-extra ()
363 (while (not (memq (char-after) '(?\n nil)))
364 (setq string (nnheader-nov-field))
365 (when (string-match "^\\([^ :]+\\): " string)
366 (push (cons (intern (match-string 1 string))
367 (substring string (match-end 0)))
371 (defmacro nnheader-nov-read-message-id ()
372 '(let ((id (nnheader-nov-field)))
373 (if (string-match "^<[^>]+>$" id)
375 (nnheader-generate-fake-message-id))))
377 (defun nnheader-parse-nov ()
378 (let ((eol (gnus-point-at-eol)))
379 (make-full-mail-header
380 (nnheader-nov-read-integer) ; number
381 (nnheader-nov-field) ; subject
382 (nnheader-nov-field) ; from
383 (nnheader-nov-field) ; date
384 (nnheader-nov-read-message-id) ; id
385 (nnheader-nov-field) ; refs
386 (nnheader-nov-read-integer) ; chars
387 (nnheader-nov-read-integer) ; lines
388 (if (eq (char-after) ?\n)
390 (if (looking-at "Xref: ")
391 (goto-char (match-end 0)))
392 (nnheader-nov-field)) ; Xref
393 (nnheader-nov-parse-extra)))) ; extra
395 (defun nnheader-insert-nov (header)
396 (princ (mail-header-number header) (current-buffer))
400 (or (mime-entity-fetch-field header 'Subject) "(none)") "\t"
401 (or (mime-entity-fetch-field header 'From) "(nobody)") "\t"
402 (or (mail-header-date header) "") "\t"
403 (or (mail-header-id header)
406 (or (mail-header-references header) "") "\t")
407 (princ (or (mail-header-chars header) 0) (current-buffer))
409 (princ (or (mail-header-lines header) 0) (current-buffer))
411 (when (mail-header-xref header)
412 (insert "Xref: " (mail-header-xref header)))
413 (when (or (mail-header-xref header)
414 (mail-header-extra header))
416 (when (mail-header-extra header)
417 (let ((extra (mail-header-extra header)))
419 (insert (symbol-name (caar extra))
420 ": " (cdar extra) "\t")
424 (while (search-backward "\n" p t)
428 (defun nnheader-insert-header (header)
430 "Subject: " (or (mail-header-subject header) "(none)") "\n"
431 "From: " (or (mail-header-from header) "(nobody)") "\n"
432 "Date: " (or (mail-header-date header) "") "\n"
433 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
434 "References: " (or (mail-header-references header) "") "\n"
436 (princ (or (mail-header-lines header) 0) (current-buffer))
439 (defun nnheader-insert-article-line (article)
440 (goto-char (point-min))
442 (princ article (current-buffer))
443 (insert " Article retrieved.\n")
444 (search-forward "\n\n" nil 'move)
445 (delete-region (point) (point-max))
449 (defun nnheader-nov-delete-outside-range (beg end)
450 "Delete all NOV lines that lie outside the BEG to END range."
451 ;; First we find the first wanted line.
452 (nnheader-find-nov-line beg)
453 (delete-region (point-min) (point))
454 ;; Then we find the last wanted line.
455 (when (nnheader-find-nov-line end)
457 (delete-region (point) (point-max)))
459 (defun nnheader-find-nov-line (article)
460 "Put point at the NOV line that start with ARTICLE.
461 If ARTICLE doesn't exist, put point where that line
462 would have been. The function will return non-nil if
463 the line could be found."
464 ;; This function basically does a binary search.
465 (let ((max (point-max))
466 (min (goto-char (point-min)))
467 (cur (current-buffer))
471 (goto-char (/ (+ max min) 2))
473 (if (or (= (point) prev)
477 (while (and (not (numberp (setq num (read cur))))
480 (cond ((> num article)
485 (setq found 'yes)))))
486 ;; We may be at the first line.
489 (setq num (read cur)))
490 ;; Now we may have found the article we're looking for, or we
491 ;; may be somewhere near it.
492 (when (and (not (eq found 'yes))
493 (not (eq num article)))
495 (while (and (< (point) max)
496 (or (not (numberp num))
501 (= (setq num (read cur)) article)))
502 (unless (eq num article)
507 (defun nnheader-retrieve-headers-from-directory* (articles
508 directory dependencies
510 fetch-old force-new large
514 (number (length articles))
516 (file-name-coding-system 'binary)
517 (pathname-coding-system 'binary)
519 (cur (current-buffer))
521 headers header id end ref in-reply-to lines chars ctype)
522 ;; We don't support fetching by Message-ID.
523 (if (stringp (car articles))
526 (when (and (file-exists-p
527 (setq file (expand-file-name
529 (setq article (pop articles)))
531 (not (file-directory-p file)))
533 (nnheader-insert-head file)
535 (std11-narrow-to-header)
538 (make-full-mail-header
542 (or (std11-fetch-field "Subject")
545 (or (std11-fetch-field "From")
548 (or (std11-fetch-field "Date")
552 (goto-char (point-min))
553 (setq id (if (re-search-forward
554 "^Message-ID: *\\(<[^\n\t> ]+>\\)" nil t)
555 ;; We do it this way to make sure the Message-ID
556 ;; is (somewhat) syntactically valid.
557 (buffer-substring (match-beginning 1)
559 ;; If there was no message-id, we just fake one
560 ;; to make subsequent routines simpler.
561 (nnheader-generate-fake-message-id))))
564 (goto-char (point-min))
565 (if (search-forward "\nReferences: " nil t)
569 (buffer-substring (match-end 0) (std11-field-end))
574 (search-backward ">" end t)
577 (search-backward "<" end t)
579 ;; Get the references from the in-reply-to header if there
580 ;; were no references and the in-reply-to header looks
582 (if (and (search-forward "\nIn-Reply-To: " nil t)
584 (buffer-substring (match-end 0)
586 (string-match "<[^>]+>" in-reply-to))
588 (setq ref (substring in-reply-to (match-beginning 0)
590 (while (string-match "<[^>]+>"
591 in-reply-to (match-end 0))
593 (substring in-reply-to (match-beginning 0)
595 (when (> (length ref2) (length ref))
601 (goto-char (point-min))
602 (if (search-forward "\nChars: " nil t)
603 (if (numberp (setq chars (ignore-errors (read cur))))
608 (goto-char (point-min))
609 (if (search-forward "\nLines: " nil t)
610 (if (numberp (setq lines (ignore-errors (read cur))))
614 (std11-fetch-field "Xref")
616 (goto-char (point-min))
617 (if (setq ctype (std11-fetch-field "Content-Type"))
618 (mime-entity-set-content-type-internal
619 header (mime-parse-Content-Type ctype)))
622 (gnus-dependencies-add-header
623 header dependencies force-new))
624 (push header headers))
626 (setq count (1+ count))
630 (nnheader-message 5 "%s: Receiving headers... %d%%"
632 (/ (* count 100) number))))
635 (nnheader-message 5 "%s: Receiving headers...done" backend))
639 (defun nnheader-retrieve-headers-from-directory (articles
640 directory dependencies
642 fetch-old force-new large
645 (nreverse (nnheader-retrieve-headers-from-directory*
646 articles directory dependencies
647 fetch-old force-new large backend))))
649 (defun nnheader-get-newsgroup-headers-xover* (sequence
651 force-new dependencies
653 "Parse the news overview data in the server buffer, and return a
654 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
655 ;; Get the Xref when the users reads the articles since most/some
656 ;; NNTP servers do not include Xrefs when using XOVER.
657 ;; (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
658 (let ((cur nntp-server-buffer)
659 number headers header)
661 (set-buffer nntp-server-buffer)
662 ;; Allow the user to mangle the headers before parsing them.
663 (gnus-run-hooks 'gnus-parse-headers-hook)
664 (goto-char (point-min))
667 (while (and sequence (not (eobp)))
668 (setq number (read cur))
670 (< (car sequence) number))
671 (setq sequence (cdr sequence)))
673 (eq number (car sequence))
675 (setq sequence (cdr sequence))
678 number dependencies force-new))))
679 (push header headers))
682 (gnus-error 4 "Strange nov line (%d)"
683 (count-lines (point-min) (point)))))
685 ;; A common bug in inn is that if you have posted an article and
686 ;; then retrieves the active file, it will answer correctly --
687 ;; the new article is included. However, a NOV entry for the
688 ;; article may not have been generated yet, so this may fail.
689 ;; We work around this problem by retrieving the last few
690 ;; headers using HEAD.
693 ;; Various cruft the backends and Gnus need to communicate.
695 (defvar nntp-server-buffer nil)
696 (defvar nntp-process-response nil)
697 (defvar news-reply-yank-from nil)
698 (defvar news-reply-yank-message-id nil)
700 (defvar nnheader-callback-function nil)
702 (defun nnheader-init-server-buffer ()
703 "Initialize the Gnus-backend communication buffer."
705 (unless (gnus-buffer-live-p nntp-server-buffer)
706 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
707 (set-buffer nntp-server-buffer)
709 (kill-all-local-variables)
710 (setq case-fold-search t) ;Should ignore case.
711 (set (make-local-variable 'nntp-process-response) nil)
714 ;;; Various functions the backends use.
716 (defun nnheader-file-error (file)
717 "Return a string that says what is wrong with FILE."
720 ((not (file-exists-p file))
722 ((file-directory-p file)
724 ((not (file-readable-p file))
725 "%s is not readable"))
728 (defun nnheader-insert-head (file)
729 "Insert the head of the article."
730 (when (file-exists-p file)
731 (if (eq nnheader-max-head-length t)
732 ;; Just read the entire file.
733 (nnheader-insert-file-contents file)
734 ;; Read 1K blocks until we find a separator.
737 (while (and (eq nnheader-head-chop-length
738 (nth 1 (nnheader-insert-file-contents
740 (incf beg nnheader-head-chop-length))))
741 (prog1 (not (search-forward "\n\n" nil t))
742 (goto-char (point-max)))
743 (or (null nnheader-max-head-length)
744 (< beg nnheader-max-head-length))))))
747 (defun nnheader-article-p ()
748 "Say whether the current buffer looks like an article."
749 (goto-char (point-min))
750 (if (not (search-forward "\n\n" nil t))
752 (narrow-to-region (point-min) (1- (point)))
753 (goto-char (point-min))
754 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
755 (goto-char (match-end 0)))
760 (defun nnheader-insert-references (references message-id)
761 "Insert a References header based on REFERENCES and MESSAGE-ID."
762 (if (and (not references) (not message-id))
763 ;; This is invalid, but not all articles have Message-IDs.
765 (mail-position-on-field "References")
766 (let ((begin (save-excursion (beginning-of-line) (point)))
771 (when (and references message-id)
775 ;; Fold long References lines to conform to RFC1036 (sort of).
776 ;; The region must end with a newline to fill the region
777 ;; without inserting extra newline.
778 (fill-region-as-paragraph begin (1+ (point))))))
780 (defun nnheader-replace-header (header new-value)
781 "Remove HEADER and insert the NEW-VALUE."
784 (nnheader-narrow-to-headers)
786 (message-remove-header header)
787 (goto-char (point-max))
788 (insert header ": " new-value "\n")))))
790 (defun nnheader-narrow-to-headers ()
791 "Narrow to the head of an article."
794 (goto-char (point-min))
795 (if (search-forward "\n\n" nil t)
798 (goto-char (point-min)))
800 (defun nnheader-set-temp-buffer (name &optional noerase)
801 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
802 (set-buffer (get-buffer-create name))
803 (buffer-disable-undo)
808 (eval-when-compile (defvar jka-compr-compression-info-list))
809 (defvar nnheader-numerical-files
810 (if (boundp 'jka-compr-compression-info-list)
811 (concat "\\([0-9]+\\)\\("
812 (mapconcat (lambda (i) (aref i 0))
813 jka-compr-compression-info-list "\\|")
816 "Regexp that match numerical files.")
818 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
819 "Regexp that matches numerical file names.")
821 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
822 "Regexp that matches numerical full file paths.")
824 (defsubst nnheader-file-to-number (file)
825 "Take a FILE name and return the article number."
826 (if (string= nnheader-numerical-short-files "^[0-9]+$")
828 (string-match nnheader-numerical-short-files file)
829 (string-to-int (match-string 0 file))))
831 (defvar nnheader-directory-files-is-safe
832 (or (eq system-type 'windows-nt)
833 (and (not (featurep 'xemacs))
834 (> emacs-major-version 20)))
835 "If non-nil, Gnus believes `directory-files' is safe.
836 It has been reported numerous times that `directory-files' fails with
837 an alarming frequency on NFS mounted file systems. If it is nil,
838 `nnheader-directory-files-safe' is used.")
840 (defun nnheader-directory-files-safe (&rest args)
841 "Execute `directory-files' twice and returns the longer result."
842 (let ((first (apply 'directory-files args))
843 (second (apply 'directory-files args)))
844 (if (> (length first) (length second))
848 (defun nnheader-directory-articles (dir)
849 "Return a list of all article files in directory DIR."
850 (mapcar 'nnheader-file-to-number
851 (if nnheader-directory-files-is-safe
853 dir nil nnheader-numerical-short-files t)
854 (nnheader-directory-files-safe
855 dir nil nnheader-numerical-short-files t))))
857 (defun nnheader-article-to-file-alist (dir)
858 "Return an alist of article/file pairs in DIR."
859 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
860 (if nnheader-directory-files-is-safe
862 dir nil nnheader-numerical-short-files t)
863 (nnheader-directory-files-safe
864 dir nil nnheader-numerical-short-files t))))
866 (defun nnheader-fold-continuation-lines ()
867 "Fold continuation lines in the current buffer."
868 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
870 (defun nnheader-translate-file-chars (file &optional full)
871 "Translate FILE into something that can be a file name.
872 If FULL, translate everything."
873 (if (null nnheader-file-name-translation-alist)
874 ;; No translation is necessary.
879 ;; Do complete translation.
880 (setq leaf (copy-sequence file)
882 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
884 ;; We translate -- but only the file name. We leave the directory
886 (if (and (featurep 'xemacs)
887 (memq system-type '(cygwin32 win32 w32 mswindows windows-nt)))
888 ;; This is needed on NT and stuff, because
889 ;; file-name-nondirectory is not enough to split
890 ;; file names, containing ':', e.g.
891 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
893 ;; we are trying to correctly split such names:
894 ;; "d:file.name" -> "a:" "file.name"
895 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
896 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
898 ;; to translate then only the file name part.
902 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
903 (setq leaf (substring file (match-beginning 2))
904 path (substring file 0 (match-beginning 2)))))
905 ;; Emacs DTRT, says andrewi.
906 (setq leaf (file-name-nondirectory file)
907 path (file-name-directory file))))
908 (setq len (length leaf))
910 (when (setq trans (cdr (assq (aref leaf i)
911 nnheader-file-name-translation-alist)))
914 (concat path leaf))))
916 (defun nnheader-report (backend &rest args)
917 "Report an error from the BACKEND.
918 The first string in ARGS can be a format string."
919 (set (intern (format "%s-status-string" backend))
920 (if (< (length args) 2)
922 (apply 'format args)))
925 (defun nnheader-get-report (backend)
926 "Get the most recent report from BACKEND."
928 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
930 (error (nnheader-message 5 ""))))
932 (defun nnheader-insert (format &rest args)
933 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
934 If FORMAT isn't a format string, it and all ARGS will be inserted
937 (set-buffer nntp-server-buffer)
939 (if (string-match "%" format)
940 (insert (apply 'format format args))
941 (apply 'insert format args))
944 (static-if (fboundp 'subst-char-in-string)
945 (defsubst nnheader-replace-chars-in-string (string from to)
946 (subst-char-in-string from to string))
947 (defun nnheader-replace-chars-in-string (string from to)
948 "Replace characters in STRING from FROM to TO."
949 (let ((string (substring string 0)) ;Copy string.
950 (len (length string))
952 ;; Replace all occurrences of FROM with TO.
954 (when (= (aref string idx) from)
955 (aset string idx to))
959 (defun nnheader-replace-duplicate-chars-in-string (string from to)
960 "Replace characters in STRING from FROM to TO."
961 (let ((string (substring string 0)) ;Copy string.
962 (len (length string))
964 ;; Replace all occurrences of FROM with TO.
966 (setq i (aref string idx))
967 (when (and (eq prev from) (= i from))
968 (aset string (1- idx) to)
969 (aset string idx to))
974 (defun nnheader-file-to-group (file &optional top)
975 "Return a group name based on FILE and TOP."
976 (nnheader-replace-chars-in-string
980 (substring (expand-file-name file)
983 (file-name-as-directory top))))
987 (defun nnheader-message (level &rest args)
988 "Message if the Gnus backends are talkative."
989 (if (or (not (numberp gnus-verbose-backends))
990 (<= level gnus-verbose-backends))
991 (apply 'message args)
992 (apply 'format args)))
994 (defun nnheader-be-verbose (level)
995 "Return whether the backends should be verbose on LEVEL."
996 (or (not (numberp gnus-verbose-backends))
997 (<= level gnus-verbose-backends)))
999 (defvar nnheader-pathname-coding-system 'binary
1000 "*Coding system for pathname.")
1002 (defun nnheader-group-pathname (group dir &optional file)
1003 "Make pathname for GROUP."
1005 (let ((dir (file-name-as-directory (expand-file-name dir))))
1006 ;; If this directory exists, we use it directly.
1007 (file-name-as-directory
1008 (if (file-directory-p (concat dir group))
1009 (expand-file-name group dir)
1010 ;; If not, we translate dots into slashes.
1011 (expand-file-name (encode-coding-string
1012 (nnheader-replace-chars-in-string group ?. ?/)
1013 nnheader-pathname-coding-system)
1015 (cond ((null file) "")
1016 ((numberp file) (int-to-string file))
1019 (defun nnheader-functionp (form)
1020 "Return non-nil if FORM is funcallable."
1021 (or (and (symbolp form) (fboundp form))
1022 (and (listp form) (eq (car form) 'lambda))))
1024 (defun nnheader-concat (dir &rest files)
1025 "Concat DIR as directory to FILES."
1026 (apply 'concat (file-name-as-directory dir) files))
1028 (defun nnheader-ms-strip-cr ()
1029 "Strip ^M from the end of all lines."
1031 (goto-char (point-min))
1032 (while (re-search-forward "\r$" nil t)
1033 (delete-backward-char 1))))
1035 (defun nnheader-file-size (file)
1036 "Return the file size of FILE or 0."
1037 (or (nth 7 (file-attributes file)) 0))
1039 (defun nnheader-find-etc-directory (package &optional file)
1040 "Go through the path and find the \".../etc/PACKAGE\" directory.
1041 If FILE, find the \".../etc/PACKAGE\" file instead."
1042 (let ((path load-path)
1044 ;; We try to find the dir by looking at the load path,
1045 ;; stripping away the last component and adding "etc/".
1050 (file-name-directory
1051 (directory-file-name (car path)))
1054 (or file (file-directory-p dir)))
1057 (setq path (cdr path))))
1061 (defvar ange-ftp-path-format)
1062 (defvar efs-path-regexp))
1063 (defun nnheader-re-read-dir (path)
1064 "Re-read directory PATH if PATH is on a remote system."
1065 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
1066 (when (string-match efs-path-regexp path)
1067 (efs-re-read-dir path))
1068 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
1069 (when (string-match (car ange-ftp-path-format) path)
1070 (ange-ftp-re-read-dir path)))))
1072 (defvar nnheader-file-coding-system 'raw-text
1073 "Coding system used in file backends of Gnus.")
1075 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
1076 "Like `insert-file-contents', q.v., but only reads in the file.
1077 A buffer may be modified in several ways after reading into the buffer due
1078 to advanced Emacs features, such as file-name-handlers, format decoding,
1079 find-file-hooks, etc.
1080 This function ensures that none of these modifications will take place."
1081 (let ((format-alist nil)
1082 (auto-mode-alist (nnheader-auto-mode-alist))
1083 (default-major-mode 'fundamental-mode)
1084 (enable-local-variables nil)
1085 (after-insert-file-functions nil)
1086 (enable-local-eval nil)
1087 (find-file-hooks nil))
1088 (insert-file-contents-as-coding-system
1089 nnheader-file-coding-system filename visit beg end replace)))
1091 (defun nnheader-find-file-noselect (&rest args)
1092 (let ((format-alist nil)
1093 (auto-mode-alist (nnheader-auto-mode-alist))
1094 (default-major-mode 'fundamental-mode)
1095 (enable-local-variables nil)
1096 (after-insert-file-functions nil)
1097 (enable-local-eval nil)
1098 (find-file-hooks nil))
1099 (apply 'find-file-noselect-as-coding-system
1100 nnheader-file-coding-system args)))
1102 (defun nnheader-auto-mode-alist ()
1103 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1104 (let ((alist auto-mode-alist)
1107 (when (listp (cdar alist))
1108 (push (car alist) out))
1112 (defun nnheader-directory-regular-files (dir)
1113 "Return a list of all regular files in DIR."
1114 (let ((files (directory-files dir t))
1117 (when (file-regular-p (car files))
1118 (push (car files) out))
1122 (defun nnheader-directory-files (&rest args)
1123 "Same as `directory-files', but prune \".\" and \"..\"."
1124 (let ((files (apply 'directory-files args))
1127 (unless (member (file-name-nondirectory (car files)) '("." ".."))
1128 (push (car files) out))
1132 (defmacro nnheader-skeleton-replace (from &optional to regexp)
1133 `(let ((new (generate-new-buffer " *nnheader replace*"))
1134 (cur (current-buffer))
1135 (start (point-min)))
1137 (goto-char (point-min))
1138 (while (,(if regexp 're-search-forward 'search-forward)
1140 (insert-buffer-substring
1141 cur start (prog1 (match-beginning 0) (set-buffer new)))
1142 (goto-char (point-max))
1143 ,(when to `(insert ,to))
1145 (setq start (point)))
1146 (insert-buffer-substring
1147 cur start (prog1 (point-max) (set-buffer new)))
1148 (copy-to-buffer cur (point-min) (point-max))
1149 (kill-buffer (current-buffer))
1152 (defun nnheader-replace-string (from to)
1153 "Do a fast replacement of FROM to TO from point to `point-max'."
1154 (nnheader-skeleton-replace from to))
1156 (defun nnheader-replace-regexp (from to)
1157 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1158 (nnheader-skeleton-replace from to t))
1160 (defun nnheader-strip-cr ()
1161 "Strip all \r's from the current buffer."
1162 (nnheader-skeleton-replace "\r"))
1164 (defalias 'nnheader-run-at-time 'run-at-time)
1165 (defalias 'nnheader-cancel-timer 'cancel-timer)
1166 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1167 (defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1169 (defun nnheader-Y-or-n-p (prompt)
1170 "Ask user a \"Y/n\" question. Return t if answer is neither \"n\", \"N\" nor \"C-g\"."
1171 (let ((cursor-in-echo-area t)
1175 (let (message-log-max)
1176 (while (not (memq ans '(?\ ?N ?Y ?\C-g ?\e ?\n ?\r ?n ?y)))
1177 (message "%s(Y/n) " prompt)
1178 (setq ans (read-char-exclusive))))
1179 (if (memq ans '(?\C-g ?N ?n))
1181 (message "%s(Y/n) No" prompt)
1183 (message "%s(Y/n) Yes" prompt)
1186 (defun nnheader-image-load-path (&optional package)
1188 (dolist (path load-path (nreverse result))
1189 (if (file-directory-p
1190 (setq dir (concat (file-name-directory
1191 (directory-file-name path))
1192 "etc/" (or package "gnus/"))))
1194 (push path result))))
1195 (defalias 'mm-image-load-path 'nnheader-image-load-path)
1197 (defalias 'mm-read-coding-system
1198 (if (or (and (featurep 'xemacs)
1199 (<= (string-to-number emacs-version) 21.1))
1201 (lambda (prompt &optional default-coding-system)
1202 (read-coding-system prompt))
1203 'read-coding-system))
1205 (when (featurep 'xemacs)
1206 (require 'nnheaderxm))
1208 (run-hooks 'nnheader-load-hook)
1212 ;;; nnheader.el ends here