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, 2002, 2003
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))
44 ;; Reduce the required value of `recursive-load-depth-limit' for Emacs 21.
51 (autoload 'gnus-sorted-intersection "gnus-range")
52 (autoload 'gnus-intersection "gnus-range")
53 (autoload 'gnus-sorted-complement "gnus-range")
54 (autoload 'gnus-sorted-difference "gnus-range"))
56 (defcustom gnus-verbose-backends 7
57 "Integer that says how verbose the Gnus backends should be.
58 The higher the number, the more messages the Gnus backends will flash
59 to say what it's doing. At zero, the Gnus backends will be totally
60 mute; at five, they will display most important messages; and at ten,
61 they will keep on jabbering all the time."
65 (defcustom gnus-nov-is-evil nil
66 "If non-nil, Gnus backends will never output headers in the NOV format."
70 (defvar nnheader-max-head-length 4096
71 "*Max length of the head of articles.
73 Value is an integer, nil, or t. nil means read in chunks of a file
74 indefinitely until a complete head is found\; t means always read the
75 entire file immediately, disregarding `nnheader-head-chop-length'.
77 Integer values will in effect be rounded up to the nearest multiple of
78 `nnheader-head-chop-length'.")
80 (defvar nnheader-head-chop-length 2048
81 "*Length of each read operation when trying to fetch HEAD headers.")
83 (defvar nnheader-read-timeout
84 (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
85 (symbol-name system-type))
88 "How long nntp should wait between checking for the end of output.
89 Shorter values mean quicker response, but are more CPU intensive.")
91 (defvar nnheader-file-name-translation-alist
92 (let ((case-fold-search t))
94 ((string-match "windows-nt\\|os/2\\|emx\\|cygwin"
95 (symbol-name system-type))
96 (append (mapcar (lambda (c) (cons c ?_))
97 '(?: ?* ?\" ?< ?> ??))
98 (if (string-match "windows-nt\\|cygwin"
99 (symbol-name system-type))
103 "*Alist that says how to translate characters in file names.
104 For instance, if \":\" is invalid as a file character in file names
105 on your system, you could say something like:
107 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
109 (defvar nnheader-text-coding-system
110 (if (memq system-type '(windows-nt ms-dos ms-windows))
113 "Text-safe coding system (For removing ^M).
114 This variable is a substitute for `mm-text-coding-system'.")
116 (defvar nnheader-text-coding-system-for-write nil
117 "Text coding system for write.
118 This variable is a substitute for `mm-text-coding-system-for-write'.")
120 (defvar nnheader-auto-save-coding-system
122 ((not (fboundp 'find-coding-system)) nil)
123 ((find-coding-system 'emacs-mule)
124 (if (memq system-type '(windows-nt ms-dos ms-windows))
125 'emacs-mule-dos 'emacs-mule))
126 ((find-coding-system 'escape-quoted) 'escape-quoted)
127 ((find-coding-system 'no-conversion) 'no-conversion)
129 "Coding system of auto save file.")
131 (defvar nnheader-directory-separator-character
132 (string-to-char (substring (file-name-as-directory ".") -1))
133 "*A character used to a directory separator.")
136 (autoload 'nnmail-message-id "nnmail")
137 (autoload 'mail-position-on-field "sendmail")
138 (autoload 'message-remove-header "message")
139 (autoload 'gnus-buffer-live-p "gnus-util"))
142 (unless (featurep 'mm-util)
143 ;; Should keep track of `mm-image-load-path' in mm-util.el.
144 (defun nnheader-image-load-path (&optional package)
146 (dolist (path load-path (nreverse result))
147 (if (file-directory-p
148 (setq dir (concat (file-name-directory
149 (directory-file-name path))
150 "etc/" (or package "gnus/"))))
152 (push path result))))
153 (defalias 'mm-image-load-path 'nnheader-image-load-path)
155 ;; Should keep track of `mm-read-coding-system' in mm-util.el.
156 (defalias 'mm-read-coding-system 'read-coding-system)
158 ;; Should keep track of `mm-%s' in mm-util.el.
159 (defalias 'mm-multibyte-string-p
160 (if (fboundp 'multibyte-string-p)
163 (defalias 'mm-encode-coding-string 'encode-coding-string)
164 (defalias 'mm-decode-coding-string 'decode-coding-string)
166 ;; Should keep track of `mm-detect-coding-region' in mm-util.el.
167 (defun nnheader-detect-coding-region (start end)
168 "Like 'detect-coding-region' except returning the best one."
169 (let ((coding-systems (detect-coding-region (point) (point-max))))
170 (or (car-safe coding-systems)
172 (defalias 'mm-detect-coding-region 'nnheader-detect-coding-region)
174 ;; Should keep track of `mm-detect-mime-charset-region' in mm-util.el.
175 (defun nnheader-detect-mime-charset-region (start end)
176 "Detect MIME charset of the text in the region between START and END."
177 (coding-system-to-mime-charset
178 (nnheader-detect-coding-region start end)))
179 (defalias 'mm-detect-mime-charset-region
180 'nnheader-detect-mime-charset-region)
182 ;; Should keep track of `mm-with-unibyte-buffer' in mm-util.el.
183 (defmacro nnheader-with-unibyte-buffer (&rest forms)
184 "Create a temporary buffer, and evaluate FORMS there like `progn'.
185 Use unibyte mode for this."
186 `(let (default-enable-multibyte-characters)
187 (with-temp-buffer ,@forms)))
188 (put 'nnheader-with-unibyte-buffer 'lisp-indent-function 0)
189 (put 'nnheader-with-unibyte-buffer 'edebug-form-spec '(body))
190 (put 'mm-with-unibyte-buffer 'lisp-indent-function 0)
191 (put 'mm-with-unibyte-buffer 'edebug-form-spec '(body))
192 (defalias 'mm-with-unibyte-buffer 'nnheader-with-unibyte-buffer)
194 ;; Should keep track of `mm-with-unibyte-current-buffer' in mm-util.el.
195 (defmacro nnheader-with-unibyte-current-buffer (&rest forms)
196 "Evaluate FORMS with current current buffer temporarily made unibyte.
197 Also bind `default-enable-multibyte-characters' to nil.
198 Equivalent to `progn' in XEmacs"
199 (let ((multibyte (make-symbol "multibyte"))
200 (buffer (make-symbol "buffer")))
201 (cond ((featurep 'xemacs)
202 `(let (default-enable-multibyte-characters)
205 `(let ((,multibyte enable-multibyte-characters)
206 (,buffer (current-buffer)))
208 (let (default-enable-multibyte-characters)
209 (set-buffer-multibyte nil)
212 (set-buffer-multibyte ,multibyte)))))))
213 (put 'nnheader-with-unibyte-current-buffer 'lisp-indent-function 0)
214 (put 'nnheader-with-unibyte-current-buffer 'edebug-form-spec '(body))
215 (put 'mm-with-unibyte-current-buffer 'lisp-indent-function 0)
216 (put 'mm-with-unibyte-current-buffer 'edebug-form-spec '(body))
217 (defalias 'mm-with-unibyte-current-buffer
218 'nnheader-with-unibyte-current-buffer)
220 ;; Should keep track of `mm-with-unibyte' in mm-util.el.
221 (defmacro nnheader-with-unibyte (&rest forms)
222 "Eval the FORMS with the default value of `enable-multibyte-characters'
224 `(let (default-enable-multibyte-characters)
226 (put 'nnheader-with-unibyte 'lisp-indent-function 0)
227 (put 'nnheader-with-unibyte 'edebug-form-spec '(body))
228 (put 'mm-with-unibyte 'lisp-indent-function 0)
229 (put 'mm-with-unibyte 'edebug-form-spec '(body))
230 (defalias 'mm-with-unibyte 'nnheader-with-unibyte)
232 ;; Should keep track of `mm-guess-mime-charset' in mm-util.el.
233 (defun nnheader-guess-mime-charset ()
234 "Guess the default MIME charset from the language environment."
236 (and (boundp 'current-language-environment)
237 (assoc current-language-environment
238 language-info-alist)))
241 ((null language-info)
245 (or (assq 'coding-priority language-info)
246 (assq 'coding-system language-info))))
247 (if (fboundp 'coding-system-get)
248 (or (coding-system-get item 'mime-charset)
251 ((setq item (car (last (assq 'charset language-info))))
254 (charsets-to-mime-charset (list item))))
257 (defalias 'mm-guess-mime-charset 'nnheader-guess-mime-charset)
259 (defalias 'mm-char-int 'char-int)
261 ;; Should keep track of the same alias in mm-util.el.
262 (defalias 'mm-multibyte-p
263 (static-cond ((and (featurep 'xemacs) (featurep 'mule))
268 (lambda nil enable-multibyte-characters))))
270 ;; Should keep track of the same alias in mm-util.el.
271 (defalias 'mm-make-temp-file
272 (if (fboundp 'make-temp-file)
274 (lambda (prefix &optional dir-flag)
275 (let ((file (expand-file-name
276 (make-temp-name prefix)
277 (if (fboundp 'temp-directory)
279 temporary-file-directory))))
281 (make-directory file))
284 ;; Should keep track of `mm-coding-system-p' in mm-util.el.
285 (defun nnheader-coding-system-p (sym)
286 "Return non-nil if SYM is a coding system."
287 (or (and (fboundp 'find-coding-system) (find-coding-system sym))
288 (and (fboundp 'coding-system-p) (coding-system-p sym))))
289 (defalias 'mm-coding-system-p 'nnheader-coding-system-p))
292 (unless (featurep 'mail-parse)
293 (unless (fboundp 'std11-narrow-to-field)
294 (defalias 'std11-narrow-to-field
295 ;; Should keep track of `rfc2047-narrow-to-field' in rfc2047.el.
297 "Narrow the buffer to the header on the current line."
299 (narrow-to-region (point)
302 (when (eolp) (forward-line 1))
304 (goto-char (point-min)))))
305 (defalias 'mail-header-narrow-to-field 'std11-narrow-to-field)
307 ;; Should keep track of `ietf-drums-narrow-to-header' in ietf-drums.el.
308 (defun mail-narrow-to-head ()
309 "Narrow to the header section in the current buffer."
311 (goto-char (point-min))
312 (if (re-search-forward "^\r?$" nil 1)
315 (goto-char (point-min)))
317 (unless (fboundp 'std11-fold-region)
318 (defalias 'std11-fold-region
319 ;; Should keep track of `rfc2047-fold-region' in rfc2047.el.
321 "Fold long lines in region B to E."
323 (narrow-to-region b e)
324 (goto-char (point-min))
328 (bol (save-restriction
332 (when (and (or break qword-break)
333 (> (- (point) bol) 76))
334 (goto-char (or break qword-break))
337 (if (looking-at "[ \t]")
340 (setq bol (1- (point)))
341 ;; Don't break before the first non-LWSP characters.
342 (skip-chars-forward " \t")
346 ((eq (char-after) ?\n)
351 (skip-chars-forward " \t")
352 (unless (or (eobp) (eq (char-after) ?\n))
354 ((eq (char-after) ?\r)
356 ((memq (char-after) '(? ?\t))
357 (skip-chars-forward " \t")
359 ;; Don't break just after the header name.
361 (setq break (1- (point)))))
363 (if (not (looking-at "=\\?[^=]"))
364 (if (eq (char-after) ?=)
366 (skip-chars-forward "^ \t\n\r="))
367 (setq qword-break (point))
368 (skip-chars-forward "^ \t\n\r")))
370 (skip-chars-forward "^ \t\n\r"))))
371 (when (and (or break qword-break)
372 (> (- (point) bol) 76))
373 (goto-char (or break qword-break))
376 (if (looking-at "[ \t]")
379 (setq bol (1- (point)))
380 ;; Don't break before the first non-LWSP characters.
381 (skip-chars-forward " \t")
383 (forward-char 1))))))))
385 (unless (fboundp 'std11-fold-field)
386 (defalias 'std11-fold-field
387 ;; Should keep track of `rfc2047-fold-field' in rfc2047.el.
389 "Fold the current line."
392 (std11-narrow-to-field)
393 (std11-fold-region (point-min) (point-max)))))))
394 (defalias 'mail-header-fold-field 'std11-fold-field)
396 (unless (fboundp 'std11-unfold-region)
397 (defalias 'std11-unfold-region
398 ;; Should keep track of `rfc2047-unfold-region' in rfc2047.el.
400 "Unfold lines in region B to E."
402 (narrow-to-region b e)
403 (goto-char (point-min))
404 (let ((bol (save-restriction
407 (eol (point-at-eol)))
410 (if (and (looking-at "[ \t]")
411 (< (- (point-at-eol) bol) 76))
412 (delete-region eol (progn
414 (skip-chars-forward "\r\n")
416 (setq bol (point-at-bol)))
417 (setq eol (point-at-eol))
418 (forward-line 1)))))))
420 (unless (fboundp 'std11-unfold-field)
421 (defalias 'std11-unfold-field
422 ;; Should keep track of `rfc2047-unfold-field' in rfc2047.el.
424 "Fold the current line."
427 (std11-narrow-to-field)
428 (std11-unfold-region (point-min) (point-max)))))))
429 (defalias 'mail-header-unfold-field 'std11-unfold-field)
431 (unless (fboundp 'std11-extract-addresses-components)
432 (defalias 'std11-extract-addresses-components
433 ;; This is the original function in T-gnus.
435 "Extract a list of full name and canonical address from STRING. Each
436 element looks like a list of the form (FULL-NAME CANONICAL-ADDRESS).
437 If no name can be extracted, FULL-NAME will be nil."
440 (dolist (structure (std11-parse-addresses-string
441 (std11-unfold-string string))
443 (push (list (std11-full-name-string structure)
444 (std11-address-string structure))
446 (nreverse addresses))))))
448 ;; Should keep track of `ietf-drums-parse-addresses' in ietf-drums.el.
449 (defun mail-header-parse-addresses (string)
450 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
453 (cons (nth 1 components) (car components))))
454 (std11-extract-addresses-components string)))
456 ;; Should keep track of `rfc2047-field-value' in rfc2047.el.
457 (defun std11-field-value (&optional dont-include-last-newline)
458 "Return the value of the field at point. If the optional argument is
459 given, the return value will not contain the last newline."
460 (let ((begin (point))
461 (inhibit-point-motion-hooks t)
465 (while (and (memq (char-after) '(?\t ?\ ))
466 (zerop (forward-line -1))))
467 (when (looking-at "[^\t\n ]+:[\t\n ]+")
468 (goto-char (setq start (match-end 0)))
470 (while (and (memq (char-after) '(?\t ?\ ))
471 (zerop (forward-line 1))))
472 (when dont-include-last-newline
473 (skip-chars-backward "\t\n " start))
474 (setq value (buffer-substring start (point)))))
477 (defalias 'mail-header-field-value 'std11-field-value))
480 (unless (featurep 'ietf-drums)
481 ;; Should keep track of `ietf-drums-unfold-fws' in ietf-drums.el.
482 (defun nnheader-unfold-fws ()
483 "Unfold folding white space in the current buffer."
484 (goto-char (point-min))
485 (while (re-search-forward "[ \t]*\n[ \t]+" nil t)
486 (replace-match " " t t))
487 (goto-char (point-min)))
489 (defalias 'ietf-drums-unfold-fws 'nnheader-unfold-fws))
491 ;;; Header access macros.
493 ;; These macros may look very much like the ones in GNUS 4.1. They
494 ;; are, in a way, but you should note that the indices they use have
495 ;; been changed from the internal GNUS format to the NOV format. The
496 ;; makes it possible to read headers from XOVER much faster.
498 ;; The format of a header is now:
499 ;; [number subject from date id references chars lines xref extra]
501 ;; (That next-to-last entry is defined as "misc" in the NOV format,
502 ;; but Gnus uses it for xrefs.)
506 (defmacro mail-header-number (header)
507 "Return article number in HEADER."
508 `(mime-entity-location-internal ,header))
510 (defmacro mail-header-set-number (header number)
511 "Set article number of HEADER to NUMBER."
512 `(mime-entity-set-location-internal ,header ,number))
514 (defalias 'mail-header-subject 'mime-gnus-entity-subject-internal)
515 (defalias 'mail-header-set-subject 'mime-gnus-entity-set-subject-internal)
517 (defalias 'mail-header-from 'mime-gnus-entity-from-internal)
518 (defalias 'mail-header-set-from 'mime-gnus-entity-set-from-internal)
520 (defalias 'mail-header-date 'mime-gnus-entity-date-internal)
521 (defalias 'mail-header-set-date 'mime-gnus-entity-set-date-internal)
523 (defalias 'mail-header-message-id 'mime-gnus-entity-id-internal)
524 (defalias 'mail-header-id 'mime-gnus-entity-id-internal)
525 (defalias 'mail-header-set-message-id 'mime-gnus-entity-set-id-internal)
526 (defalias 'mail-header-set-id 'mime-gnus-entity-set-id-internal)
528 (defalias 'mail-header-references 'mime-gnus-entity-references-internal)
529 (defalias 'mail-header-set-references
530 'mime-gnus-entity-set-references-internal)
532 (defalias 'mail-header-chars 'mime-gnus-entity-chars-internal)
533 (defalias 'mail-header-set-chars 'mime-gnus-entity-set-chars-internal)
535 (defalias 'mail-header-lines 'mime-gnus-entity-lines-internal)
536 (defalias 'mail-header-set-lines 'mime-gnus-entity-set-lines-internal)
538 (defalias 'mail-header-xref 'mime-gnus-entity-xref-internal)
539 (defalias 'mail-header-set-xref 'mime-gnus-entity-set-xref-internal)
541 (defalias 'nnheader-decode-subject
542 (mime-find-field-decoder 'Subject 'nov))
543 (defalias 'nnheader-decode-from
544 (mime-find-field-decoder 'From 'nov))
546 (defalias 'mail-header-extra 'mime-gnus-entity-extra-internal)
547 (defalias 'mail-header-set-extra 'mime-gnus-entity-set-extra-internal)
549 (defun nnheader-decode-field-body (field-body field-name
550 &optional mode max-column)
551 (mime-decode-field-body field-body
552 (if (stringp field-name)
553 (intern (capitalize field-name))
557 (defsubst make-full-mail-header (&optional number subject from date id
558 references chars lines xref
560 "Create a new mail header structure initialized with the parameters given."
561 (luna-make-entity (mm-expand-class-name 'gnus)
564 (nnheader-decode-subject subject))
566 (nnheader-decode-from from))
569 :references references
573 :original-header (list (cons 'Subject subject)
577 (defsubst make-full-mail-header-from-decoded-header
578 (&optional number subject from date id references chars lines xref extra)
579 "Create a new mail header structure initialized with the parameters given."
580 (luna-make-entity (mm-expand-class-name 'gnus)
586 :references references
592 (defsubst make-mail-header (&optional init)
593 "Create a new mail header structure initialized with INIT."
594 (make-full-mail-header init init init init init
595 init init init init init))
597 ;; fake message-ids: generation and detection
599 (defvar nnheader-fake-message-id 1)
601 (defsubst nnheader-generate-fake-message-id ()
602 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
604 (defsubst nnheader-fake-message-id-p (id)
605 (save-match-data ; regular message-id's are <.*>
606 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
608 ;; Parsing headers and NOV lines.
610 (defsubst nnheader-remove-cr-followed-by-lf ()
611 (goto-char (point-max))
612 (while (search-backward "\r\n" nil t)
615 (defsubst nnheader-header-value ()
618 (skip-chars-forward " \t")
619 (buffer-substring (point) (std11-field-end))
622 (defun nnheader-parse-naked-head (&optional number)
623 ;; This function unfolds continuation lines in this buffer
624 ;; destructively. When this side effect is unwanted, use
625 ;; `nnheader-parse-head' instead of this function.
626 (let ((case-fold-search t)
627 (buffer-read-only nil)
628 (cur (current-buffer))
630 in-reply-to lines ref)
631 (nnheader-remove-cr-followed-by-lf)
632 (ietf-drums-unfold-fws)
633 (subst-char-in-region (point-min) (point-max) ?\t ? )
637 ;; This implementation of this function, with nine
638 ;; search-forwards instead of the one re-search-forward and a
639 ;; case (which basically was the old function) is actually
640 ;; about twice as fast, even though it looks messier. You
641 ;; can't have everything, I guess. Speed and elegance don't
642 ;; always go hand in hand.
643 (make-full-mail-header
649 (if (search-forward "\nsubject:" nil t)
650 (nnheader-header-value) "(none)"))
654 (if (search-forward "\nfrom:" nil t)
655 (nnheader-header-value) "(nobody)"))
659 (if (search-forward "\ndate:" nil t)
660 (nnheader-header-value) ""))
664 (if (search-forward "\nmessage-id:" nil t)
666 (1- (or (search-forward "<" (point-at-eol) t)
668 (or (search-forward ">" (point-at-eol) t) (point)))
669 ;; If there was no message-id, we just fake one to make
670 ;; subsequent routines simpler.
671 (nnheader-generate-fake-message-id)))
675 (if (search-forward "\nreferences:" nil t)
676 (nnheader-header-value)
677 ;; Get the references from the in-reply-to header if
678 ;; there were no references and the in-reply-to header
680 (if (and (search-forward "\nin-reply-to:" nil t)
681 (setq in-reply-to (nnheader-header-value))
682 (string-match "<[^\n>]+>" in-reply-to))
684 (setq ref (substring in-reply-to (match-beginning 0)
686 (while (string-match "<[^\n>]+>"
687 in-reply-to (match-end 0))
688 (setq ref2 (substring in-reply-to (match-beginning 0)
690 (when (> (length ref2) (length ref))
699 (if (search-forward "\nlines: " nil t)
700 (if (numberp (setq lines (read cur)))
706 (and (search-forward "\nxref:" nil t)
707 (nnheader-header-value)))
709 (when nnmail-extra-headers
710 (let ((extra nnmail-extra-headers)
714 (when (search-forward
715 (concat "\n" (symbol-name (car extra)) ":") nil t)
716 (push (cons (car extra) (nnheader-header-value))
723 (defun nnheader-parse-head (&optional naked)
724 (let ((cur (current-buffer)) num beg end)
729 (goto-char (point-min))
730 ;; Search to the beginning of the next header. Error
731 ;; messages do not begin with 2 or 3.
732 (when (re-search-forward "^[23][0-9]+ " nil t)
736 end (if (search-forward "\n.\n" nil t)
740 (insert-buffer-substring cur beg end)
741 (nnheader-parse-naked-head num)))))
743 (defmacro nnheader-nov-skip-field ()
744 '(search-forward "\t" eol 'move))
746 (defmacro nnheader-nov-field ()
747 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
749 (defmacro nnheader-nov-read-integer ()
751 (if (eq (char-after) ?\t)
753 (let ((num (condition-case nil
754 (read (current-buffer))
756 (if (numberp num) num 0)))
758 (search-forward "\t" eol 'move))))
760 (defmacro nnheader-nov-parse-extra ()
762 (while (not (memq (char-after) '(?\n nil)))
763 (setq string (nnheader-nov-field))
764 (when (string-match "^\\([^ :]+\\): " string)
765 (push (cons (intern (match-string 1 string))
766 (substring string (match-end 0)))
770 (defmacro nnheader-nov-read-message-id ()
771 '(let ((id (nnheader-nov-field)))
772 (if (string-match "^<[^>]+>$" id)
774 (nnheader-generate-fake-message-id))))
776 (defun nnheader-parse-nov ()
777 (let ((eol (point-at-eol)))
778 (make-full-mail-header
779 (nnheader-nov-read-integer) ; number
780 (nnheader-nov-field) ; subject
781 (nnheader-nov-field) ; from
782 (nnheader-nov-field) ; date
783 (nnheader-nov-read-message-id) ; id
784 (nnheader-nov-field) ; refs
785 (nnheader-nov-read-integer) ; chars
786 (nnheader-nov-read-integer) ; lines
787 (if (eq (char-after) ?\n)
789 (if (looking-at "Xref: ")
790 (goto-char (match-end 0)))
791 (nnheader-nov-field)) ; Xref
792 (nnheader-nov-parse-extra)))) ; extra
794 (defun nnheader-insert-nov (header)
795 (princ (mail-header-number header) (current-buffer))
799 (or (mime-entity-fetch-field header 'Subject) "(none)") "\t"
800 (or (mime-entity-fetch-field header 'From) "(nobody)") "\t"
801 (or (mail-header-date header) "") "\t"
802 (or (mail-header-id header)
805 (or (mail-header-references header) "") "\t")
806 (princ (or (mail-header-chars header) 0) (current-buffer))
808 (princ (or (mail-header-lines header) 0) (current-buffer))
810 (when (mail-header-xref header)
811 (insert "Xref: " (mail-header-xref header)))
812 (when (or (mail-header-xref header)
813 (mail-header-extra header))
815 (when (mail-header-extra header)
816 (let ((extra (mail-header-extra header)))
818 (insert (symbol-name (caar extra))
819 ": " (cdar extra) "\t")
823 (while (search-backward "\n" p t)
827 (defun nnheader-parse-overview-file (file)
828 "Parse FILE and return a list of headers."
829 (mm-with-unibyte-buffer
830 (nnheader-insert-file-contents file)
831 (goto-char (point-min))
834 (push (nnheader-parse-nov) headers)
836 (nreverse headers))))
838 (defun nnheader-write-overview-file (file headers)
839 "Write HEADERS to FILE."
841 (mapcar 'nnheader-insert-nov headers)))
843 (defun nnheader-insert-header (header)
845 "Subject: " (or (mail-header-subject header) "(none)") "\n"
846 "From: " (or (mail-header-from header) "(nobody)") "\n"
847 "Date: " (or (mail-header-date header) "") "\n"
848 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
849 "References: " (or (mail-header-references header) "") "\n"
851 (princ (or (mail-header-lines header) 0) (current-buffer))
854 (defun nnheader-insert-article-line (article)
855 (goto-char (point-min))
857 (princ article (current-buffer))
858 (insert " Article retrieved.\n")
859 (search-forward "\n\n" nil 'move)
860 (delete-region (point) (point-max))
864 (defun nnheader-nov-delete-outside-range (beg end)
865 "Delete all NOV lines that lie outside the BEG to END range."
866 ;; First we find the first wanted line.
867 (nnheader-find-nov-line beg)
868 (delete-region (point-min) (point))
869 ;; Then we find the last wanted line.
870 (when (nnheader-find-nov-line end)
872 (delete-region (point) (point-max)))
874 (defun nnheader-find-nov-line (article)
875 "Put point at the NOV line that start with ARTICLE.
876 If ARTICLE doesn't exist, put point where that line
877 would have been. The function will return non-nil if
878 the line could be found."
879 ;; This function basically does a binary search.
880 (let ((max (point-max))
881 (min (goto-char (point-min)))
882 (cur (current-buffer))
886 (goto-char (+ min (/ (- max min) 2)))
888 (if (or (= (point) prev)
892 (while (and (not (numberp (setq num (read cur))))
895 (cond ((> num article)
900 (setq found 'yes)))))
901 ;; We may be at the first line.
904 (setq num (read cur)))
905 ;; Now we may have found the article we're looking for, or we
906 ;; may be somewhere near it.
907 (when (and (not (eq found 'yes))
908 (not (eq num article)))
910 (while (and (< (point) max)
911 (or (not (numberp num))
916 (= (setq num (read cur)) article)))
917 (unless (eq num article)
922 (defun nnheader-retrieve-headers-from-directory* (articles
923 directory dependencies
925 fetch-old force-new large
929 (number (length articles))
931 (file-name-coding-system 'binary)
933 (cur (current-buffer))
935 headers header id end ref in-reply-to lines chars ctype)
936 ;; We don't support fetching by Message-ID.
937 (if (stringp (car articles))
940 (when (and (file-exists-p
941 (setq file (expand-file-name
943 (setq article (pop articles)))
945 (not (file-directory-p file)))
947 (nnheader-insert-head file)
949 (std11-narrow-to-header)
952 (make-full-mail-header
956 (or (std11-fetch-field "Subject")
959 (or (std11-fetch-field "From")
962 (or (std11-fetch-field "Date")
966 (goto-char (point-min))
967 (setq id (if (re-search-forward
968 "^Message-ID: *\\(<[^\n\t> ]+>\\)" nil t)
969 ;; We do it this way to make sure the Message-ID
970 ;; is (somewhat) syntactically valid.
971 (buffer-substring (match-beginning 1)
973 ;; If there was no message-id, we just fake one
974 ;; to make subsequent routines simpler.
975 (nnheader-generate-fake-message-id))))
978 (goto-char (point-min))
979 (if (search-forward "\nReferences: " nil t)
983 (buffer-substring (match-end 0) (std11-field-end))
988 (search-backward ">" end t)
991 (search-backward "<" end t)
993 ;; Get the references from the in-reply-to header if there
994 ;; were no references and the in-reply-to header looks
996 (if (and (search-forward "\nIn-Reply-To: " nil t)
998 (buffer-substring (match-end 0)
1000 (string-match "<[^>]+>" in-reply-to))
1002 (setq ref (substring in-reply-to (match-beginning 0)
1004 (while (string-match "<[^>]+>"
1005 in-reply-to (match-end 0))
1007 (substring in-reply-to (match-beginning 0)
1009 (when (> (length ref2) (length ref))
1015 (goto-char (point-min))
1016 (if (search-forward "\nChars: " nil t)
1017 (if (numberp (setq chars (ignore-errors (read cur))))
1022 (goto-char (point-min))
1023 (if (search-forward "\nLines: " nil t)
1024 (if (numberp (setq lines (ignore-errors (read cur))))
1028 (std11-fetch-field "Xref")
1030 (goto-char (point-min))
1031 (if (setq ctype (std11-fetch-field "Content-Type"))
1032 (mime-entity-set-content-type-internal
1033 header (mime-parse-Content-Type ctype)))
1036 (gnus-dependencies-add-header
1037 header dependencies force-new))
1038 (push header headers))
1040 (setq count (1+ count))
1043 (zerop (% count 20))
1044 (nnheader-message 5 "%s: Receiving headers... %d%%"
1046 (/ (* count 100) number))))
1049 (nnheader-message 5 "%s: Receiving headers...done" backend))
1053 (defun nnheader-retrieve-headers-from-directory (articles
1054 directory dependencies
1056 fetch-old force-new large
1059 (nreverse (nnheader-retrieve-headers-from-directory*
1060 articles directory dependencies
1061 fetch-old force-new large backend))))
1063 (defun nnheader-get-newsgroup-headers-xover* (sequence
1065 force-new dependencies
1067 "Parse the news overview data in the server buffer, and return a
1068 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
1069 ;; Get the Xref when the users reads the articles since most/some
1070 ;; NNTP servers do not include Xrefs when using XOVER.
1071 ;; (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
1072 (let ((cur nntp-server-buffer)
1073 number headers header)
1075 (set-buffer nntp-server-buffer)
1076 ;; Allow the user to mangle the headers before parsing them.
1077 (gnus-run-hooks 'gnus-parse-headers-hook)
1078 (goto-char (point-min))
1081 (while (and sequence (not (eobp)))
1082 (setq number (read cur))
1083 (while (and sequence
1084 (< (car sequence) number))
1085 (setq sequence (cdr sequence)))
1087 (eq number (car sequence))
1089 (setq sequence (cdr sequence))
1090 (setq header (inline
1091 (gnus-nov-parse-line
1092 number dependencies force-new))))
1093 (push header headers))
1096 (gnus-error 4 "Strange nov line (%d)"
1097 (count-lines (point-min) (point)))))
1099 ;; A common bug in inn is that if you have posted an article and
1100 ;; then retrieves the active file, it will answer correctly --
1101 ;; the new article is included. However, a NOV entry for the
1102 ;; article may not have been generated yet, so this may fail.
1103 ;; We work around this problem by retrieving the last few
1104 ;; headers using HEAD.
1107 ;; Various cruft the backends and Gnus need to communicate.
1109 (defvar nntp-server-buffer nil)
1110 (defvar nntp-process-response nil)
1111 (defvar news-reply-yank-from nil)
1112 (defvar news-reply-yank-message-id nil)
1114 (defvar nnheader-callback-function nil)
1116 (defun nnheader-init-server-buffer ()
1117 "Initialize the Gnus-backend communication buffer."
1119 (unless (gnus-buffer-live-p nntp-server-buffer)
1120 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
1121 (set-buffer nntp-server-buffer)
1123 (kill-all-local-variables)
1124 (setq case-fold-search t) ;Should ignore case.
1125 (set (make-local-variable 'nntp-process-response) nil)
1128 ;;; Various functions the backends use.
1130 (defun nnheader-file-error (file)
1131 "Return a string that says what is wrong with FILE."
1134 ((not (file-exists-p file))
1135 "%s does not exist")
1136 ((file-directory-p file)
1137 "%s is a directory")
1138 ((not (file-readable-p file))
1139 "%s is not readable"))
1142 (defun nnheader-insert-head (file)
1143 "Insert the head of the article."
1144 (when (file-exists-p file)
1145 (if (eq nnheader-max-head-length t)
1146 ;; Just read the entire file.
1147 (nnheader-insert-file-contents file)
1148 ;; Read 1K blocks until we find a separator.
1151 (while (and (eq nnheader-head-chop-length
1152 (nth 1 (nnheader-insert-file-contents
1154 (incf beg nnheader-head-chop-length))))
1155 (prog1 (not (search-forward "\n\n" nil t))
1156 (goto-char (point-max)))
1157 (or (null nnheader-max-head-length)
1158 (< beg nnheader-max-head-length))))))
1161 (defun nnheader-article-p ()
1162 "Say whether the current buffer looks like an article."
1163 (goto-char (point-min))
1164 (if (not (search-forward "\n\n" nil t))
1166 (narrow-to-region (point-min) (1- (point)))
1167 (goto-char (point-min))
1168 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
1169 (goto-char (match-end 0)))
1174 (defun nnheader-insert-references (references message-id)
1175 "Insert a References header based on REFERENCES and MESSAGE-ID."
1176 (if (and (not references) (not message-id))
1177 ;; This is invalid, but not all articles have Message-IDs.
1179 (mail-position-on-field "References")
1180 (let ((begin (point-at-bol))
1184 (insert references))
1185 (when (and references message-id)
1188 (insert message-id))
1189 ;; Fold long References lines to conform to RFC1036 (sort of).
1190 ;; The region must end with a newline to fill the region
1191 ;; without inserting extra newline.
1192 (fill-region-as-paragraph begin (1+ (point))))))
1194 (defun nnheader-replace-header (header new-value)
1195 "Remove HEADER and insert the NEW-VALUE."
1198 (nnheader-narrow-to-headers)
1200 (message-remove-header header)
1201 (goto-char (point-max))
1202 (insert header ": " new-value "\n")))))
1204 (defun nnheader-narrow-to-headers ()
1205 "Narrow to the head of an article."
1208 (goto-char (point-min))
1209 (if (search-forward "\n\n" nil t)
1212 (goto-char (point-min)))
1214 (defun nnheader-remove-body ()
1215 "Remove the body from an article in this current buffer."
1216 (goto-char (point-min))
1217 (when (re-search-forward "\n\r?\n" nil t)
1218 (delete-region (point) (point-max))))
1220 (defun nnheader-set-temp-buffer (name &optional noerase)
1221 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
1222 (set-buffer (get-buffer-create name))
1223 (buffer-disable-undo)
1228 (eval-when-compile (defvar jka-compr-compression-info-list))
1229 (defvar nnheader-numerical-files
1230 (if (boundp 'jka-compr-compression-info-list)
1231 (concat "\\([0-9]+\\)\\("
1232 (mapconcat (lambda (i) (aref i 0))
1233 jka-compr-compression-info-list "\\|")
1236 "Regexp that match numerical files.")
1238 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
1239 "Regexp that matches numerical file names.")
1241 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
1242 "Regexp that matches numerical full file names.")
1244 (defsubst nnheader-file-to-number (file)
1245 "Take a FILE name and return the article number."
1246 (if (string= nnheader-numerical-short-files "^[0-9]+$")
1247 (string-to-int file)
1248 (string-match nnheader-numerical-short-files file)
1249 (string-to-int (match-string 0 file))))
1251 (defvar nnheader-directory-files-is-safe
1252 (or (eq system-type 'windows-nt)
1253 (not (featurep 'xemacs)))
1254 "If non-nil, Gnus believes `directory-files' is safe.
1255 It has been reported numerous times that `directory-files' fails with
1256 an alarming frequency on NFS mounted file systems. If it is nil,
1257 `nnheader-directory-files-safe' is used.")
1259 (defun nnheader-directory-files-safe (&rest args)
1260 "Execute `directory-files' twice and returns the longer result."
1261 (let ((first (apply 'directory-files args))
1262 (second (apply 'directory-files args)))
1263 (if (> (length first) (length second))
1267 (defun nnheader-directory-articles (dir)
1268 "Return a list of all article files in directory DIR."
1269 (mapcar 'nnheader-file-to-number
1270 (if nnheader-directory-files-is-safe
1272 dir nil nnheader-numerical-short-files t)
1273 (nnheader-directory-files-safe
1274 dir nil nnheader-numerical-short-files t))))
1276 (defun nnheader-article-to-file-alist (dir)
1277 "Return an alist of article/file pairs in DIR."
1278 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
1279 (if nnheader-directory-files-is-safe
1281 dir nil nnheader-numerical-short-files t)
1282 (nnheader-directory-files-safe
1283 dir nil nnheader-numerical-short-files t))))
1285 (defun nnheader-fold-continuation-lines ()
1286 "Fold continuation lines in the current buffer."
1287 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
1289 (defun nnheader-translate-file-chars (file &optional full)
1290 "Translate FILE into something that can be a file name.
1291 If FULL, translate everything."
1292 (if (null nnheader-file-name-translation-alist)
1293 ;; No translation is necessary.
1296 trans leaf path len)
1298 ;; Do complete translation.
1299 (setq leaf (copy-sequence file)
1301 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
1303 ;; We translate -- but only the file name. We leave the directory
1305 (if (and (featurep 'xemacs)
1306 (memq system-type '(cygwin32 win32 w32 mswindows windows-nt
1308 ;; This is needed on NT and stuff, because
1309 ;; file-name-nondirectory is not enough to split
1310 ;; file names, containing ':', e.g.
1311 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
1313 ;; we are trying to correctly split such names:
1314 ;; "d:file.name" -> "a:" "file.name"
1315 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
1316 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
1318 ;; to translate then only the file name part.
1322 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
1323 (setq leaf (substring file (match-beginning 2))
1324 path (substring file 0 (match-beginning 2)))))
1325 ;; Emacs DTRT, says andrewi.
1326 (setq leaf (file-name-nondirectory file)
1327 path (file-name-directory file))))
1328 (setq len (length leaf))
1330 (when (setq trans (cdr (assq (aref leaf i)
1331 nnheader-file-name-translation-alist)))
1332 (aset leaf i trans))
1334 (concat path leaf))))
1336 (defun nnheader-report (backend &rest args)
1337 "Report an error from the BACKEND.
1338 The first string in ARGS can be a format string."
1339 (set (intern (format "%s-status-string" backend))
1340 (if (< (length args) 2)
1342 (apply 'format args)))
1345 (defun nnheader-get-report (backend)
1346 "Get the most recent report from BACKEND."
1348 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
1350 (error (nnheader-message 5 ""))))
1352 (defun nnheader-insert (format &rest args)
1353 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
1354 If FORMAT isn't a format string, it and all ARGS will be inserted
1355 without formatting."
1357 (set-buffer nntp-server-buffer)
1359 (if (string-match "%" format)
1360 (insert (apply 'format format args))
1361 (apply 'insert format args))
1364 (static-if (fboundp 'subst-char-in-string)
1365 (defsubst nnheader-replace-chars-in-string (string from to)
1366 (subst-char-in-string from to string))
1367 (defun nnheader-replace-chars-in-string (string from to)
1368 "Replace characters in STRING from FROM to TO."
1369 (let ((string (substring string 0)) ;Copy string.
1370 (len (length string))
1372 ;; Replace all occurrences of FROM with TO.
1374 (when (= (aref string idx) from)
1375 (aset string idx to))
1376 (setq idx (1+ idx)))
1379 (defun nnheader-replace-duplicate-chars-in-string (string from to)
1380 "Replace characters in STRING from FROM to TO."
1381 (let ((string (substring string 0)) ;Copy string.
1382 (len (length string))
1384 ;; Replace all occurrences of FROM with TO.
1386 (setq i (aref string idx))
1387 (when (and (eq prev from) (= i from))
1388 (aset string (1- idx) to)
1389 (aset string idx to))
1391 (setq idx (1+ idx)))
1394 (defun nnheader-file-to-group (file &optional top)
1395 "Return a group name based on FILE and TOP."
1396 (nnheader-replace-chars-in-string
1400 (substring (expand-file-name file)
1403 (file-name-as-directory top))))
1405 nnheader-directory-separator-character ?.))
1407 (defun nnheader-message (level &rest args)
1408 "Message if the Gnus backends are talkative."
1409 (if (or (not (numberp gnus-verbose-backends))
1410 (<= level gnus-verbose-backends))
1411 (apply 'message args)
1412 (apply 'format args)))
1414 (defun nnheader-be-verbose (level)
1415 "Return whether the backends should be verbose on LEVEL."
1416 (or (not (numberp gnus-verbose-backends))
1417 (<= level gnus-verbose-backends)))
1419 (defvar nnheader-pathname-coding-system 'binary
1420 "*Coding system for file name.")
1422 (defun nnheader-group-pathname (group dir &optional file)
1423 "Make file name for GROUP."
1425 (let ((dir (file-name-as-directory (expand-file-name dir))))
1426 ;; If this directory exists, we use it directly.
1427 (file-name-as-directory
1428 (if (file-directory-p (concat dir group))
1429 (expand-file-name group dir)
1430 ;; If not, we translate dots into slashes.
1431 (expand-file-name (encode-coding-string
1432 (nnheader-replace-chars-in-string group ?. ?/)
1433 nnheader-pathname-coding-system)
1435 (cond ((null file) "")
1436 ((numberp file) (int-to-string file))
1439 (defun nnheader-concat (dir &rest files)
1440 "Concat DIR as directory to FILES."
1441 (apply 'concat (file-name-as-directory dir) files))
1443 (defun nnheader-ms-strip-cr ()
1444 "Strip ^M from the end of all lines."
1446 (nnheader-remove-cr-followed-by-lf)))
1448 (defun nnheader-file-size (file)
1449 "Return the file size of FILE or 0."
1450 (or (nth 7 (file-attributes file)) 0))
1452 (defun nnheader-find-etc-directory (package &optional file first)
1453 "Go through `load-path' and find the \"../etc/PACKAGE\" directory.
1454 This function will look in the parent directory of each `load-path'
1455 entry, and look for the \"etc\" directory there.
1456 If FILE, find the \".../etc/PACKAGE\" file instead.
1457 If FIRST is non-nil, return the directory or the file found at the
1458 first. Otherwise, find the newest one, though it may take a time."
1459 (let ((path load-path)
1461 ;; We try to find the dir by looking at the load path,
1462 ;; stripping away the last component and adding "etc/".
1467 (file-name-directory
1468 (directory-file-name (car path)))
1471 (or file (file-directory-p dir)))
1473 (or (member dir results)
1475 (setq path (if first nil (cdr path))))
1476 (setq path (cdr path))))
1477 (if (or first (not (cdr results)))
1479 (car (sort results 'file-newer-than-file-p)))))
1482 (defvar ange-ftp-path-format)
1483 (defvar efs-path-regexp))
1484 (defun nnheader-re-read-dir (path)
1485 "Re-read directory PATH if PATH is on a remote system."
1486 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
1487 (when (string-match efs-path-regexp path)
1488 (efs-re-read-dir path))
1489 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
1490 (when (string-match (car ange-ftp-path-format) path)
1491 (ange-ftp-re-read-dir path)))))
1493 (defvar nnheader-file-coding-system 'raw-text
1494 "Coding system used in file backends of Gnus.")
1496 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
1497 "Like `insert-file-contents', q.v., but only reads in the file.
1498 A buffer may be modified in several ways after reading into the buffer due
1499 to advanced Emacs features, such as file-name-handlers, format decoding,
1500 find-file-hooks, etc.
1501 This function ensures that none of these modifications will take place."
1502 (let ((format-alist nil)
1503 (auto-mode-alist (nnheader-auto-mode-alist))
1504 (default-major-mode 'fundamental-mode)
1505 (enable-local-variables nil)
1506 (after-insert-file-functions nil)
1507 (enable-local-eval nil)
1508 (find-file-hooks nil))
1509 (insert-file-contents-as-coding-system
1510 nnheader-file-coding-system filename visit beg end replace)))
1512 (defun nnheader-insert-nov-file (file first)
1513 (let ((size (nth 7 (file-attributes file)))
1514 (cutoff (* 32 1024)))
1517 ;; If the file is small, we just load it.
1518 (nnheader-insert-file-contents file)
1519 ;; We start on the assumption that FIRST is pretty recent. If
1520 ;; not, we just insert the rest of the file as well.
1522 (nnheader-insert-file-contents file nil (- size cutoff) size)
1523 (goto-char (point-min))
1524 (delete-region (point) (or (search-forward "\n" nil 'move) (point)))
1525 (setq current (ignore-errors (read (current-buffer))))
1526 (if (and (numberp current)
1529 (delete-region (point-min) (point-max))
1530 (nnheader-insert-file-contents file)))))))
1532 (defun nnheader-find-file-noselect (&rest args)
1533 (let ((format-alist nil)
1534 (auto-mode-alist (nnheader-auto-mode-alist))
1535 (default-major-mode 'fundamental-mode)
1536 (enable-local-variables nil)
1537 (after-insert-file-functions nil)
1538 (enable-local-eval nil)
1539 (find-file-hooks nil))
1540 (apply 'find-file-noselect-as-coding-system
1541 nnheader-file-coding-system args)))
1543 (defun nnheader-auto-mode-alist ()
1544 "Return an `auto-mode-alist' with only the .gz (etc) thingies."
1545 (let ((alist auto-mode-alist)
1548 (when (listp (cdar alist))
1549 (push (car alist) out))
1553 (defun nnheader-directory-regular-files (dir)
1554 "Return a list of all regular files in DIR."
1555 (let ((files (directory-files dir t))
1558 (when (file-regular-p (car files))
1559 (push (car files) out))
1563 (defun nnheader-directory-files (&rest args)
1564 "Same as `directory-files', but prune \".\" and \"..\"."
1565 (let ((files (apply 'directory-files args))
1568 (unless (member (file-name-nondirectory (car files)) '("." ".."))
1569 (push (car files) out))
1573 (defmacro nnheader-skeleton-replace (from &optional to regexp)
1574 `(let ((new (generate-new-buffer " *nnheader replace*"))
1575 (cur (current-buffer))
1576 (start (point-min)))
1578 (goto-char (point-min))
1579 (while (,(if regexp 're-search-forward 'search-forward)
1581 (insert-buffer-substring
1582 cur start (prog1 (match-beginning 0) (set-buffer new)))
1583 (goto-char (point-max))
1584 ,(when to `(insert ,to))
1586 (setq start (point)))
1587 (insert-buffer-substring
1588 cur start (prog1 (point-max) (set-buffer new)))
1589 (copy-to-buffer cur (point-min) (point-max))
1590 (kill-buffer (current-buffer))
1593 (defun nnheader-replace-string (from to)
1594 "Do a fast replacement of FROM to TO from point to `point-max'."
1595 (nnheader-skeleton-replace from to))
1597 (defun nnheader-replace-regexp (from to)
1598 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1599 (nnheader-skeleton-replace from to t))
1601 (defun nnheader-strip-cr ()
1602 "Strip all \r's from the current buffer."
1603 (nnheader-skeleton-replace "\r"))
1605 (defalias 'nnheader-cancel-timer 'cancel-timer)
1606 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
1607 (defalias 'nnheader-string-as-multibyte 'string-as-multibyte)
1609 (defun nnheader-Y-or-n-p (prompt)
1610 "Ask user a \"Y/n\" question. Return t if answer is neither \"n\", \"N\" nor \"C-g\"."
1611 (let ((cursor-in-echo-area t)
1615 (let (message-log-max)
1616 (while (not (memq ans '(?\ ?N ?Y ?\C-g ?\e ?\n ?\r ?n ?y)))
1617 (message "%s(Y/n) " prompt)
1618 (setq ans (read-char-exclusive))))
1619 (if (memq ans '(?\C-g ?N ?n))
1621 (message "%s(Y/n) No" prompt)
1623 (message "%s(Y/n) Yes" prompt)
1626 (defun-maybe shell-command-to-string (command)
1627 "Execute shell command COMMAND and return its output as a string."
1628 (with-output-to-string
1629 (with-current-buffer
1631 (call-process shell-file-name nil t nil shell-command-switch command))))
1633 (defun nnheader-accept-process-output (process)
1634 (accept-process-output
1636 (truncate nnheader-read-timeout)
1637 (truncate (* (- nnheader-read-timeout
1638 (truncate nnheader-read-timeout))
1641 (when (featurep 'xemacs)
1642 (require 'nnheaderxm))
1644 (run-hooks 'nnheader-load-hook)
1648 ;;; nnheader.el ends here