(buffer-syntactic-context): Don't quote nil and t in docstrings.
[emacs.git] / lisp / gnus / nnheader.el
blob53ab2d88ebb3f75387c6bb0d8988b6e1a585b6eb
1 ;;; nnheader.el --- header access macros for 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 ;; Keywords: news
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;;; Code:
32 (eval-when-compile (require 'cl))
34 ;; Requiring `gnus-util' at compile time creates a circular
35 ;; dependency between nnheader.el and gnus-util.el.
36 ;(eval-when-compile (require 'gnus-util))
38 (require 'mail-utils)
39 (require 'mm-util)
40 (eval-and-compile
41 (autoload 'gnus-intersection "gnus-range")
42 (autoload 'gnus-sorted-complement "gnus-range"))
44 (defvar nnheader-max-head-length 4096
45 "*Max length of the head of articles.")
47 (defvar nnheader-head-chop-length 2048
48 "*Length of each read operation when trying to fetch HEAD headers.")
50 (defvar nnheader-file-name-translation-alist
51 (let ((case-fold-search t))
52 (cond
53 ((string-match "windows-nt\\|os/2\\|emx\\|cygwin32"
54 (symbol-name system-type))
55 (append (mapcar (lambda (c) (cons c ?_))
56 '(?: ?* ?\" ?< ?> ??))
57 (if (string-match "windows-nt\\|cygwin32"
58 (symbol-name system-type))
59 nil
60 '((?+ . ?-)))))
61 (t nil)))
62 "*Alist that says how to translate characters in file names.
63 For instance, if \":\" is invalid as a file character in file names
64 on your system, you could say something like:
66 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
68 (eval-and-compile
69 (autoload 'nnmail-message-id "nnmail")
70 (autoload 'mail-position-on-field "sendmail")
71 (autoload 'message-remove-header "message")
72 (autoload 'gnus-point-at-eol "gnus-util")
73 (autoload 'gnus-delete-line "gnus-util" nil nil 'macro)
74 (autoload 'gnus-buffer-live-p "gnus-util"))
76 ;;; Header access macros.
78 ;; These macros may look very much like the ones in GNUS 4.1. They
79 ;; are, in a way, but you should note that the indices they use have
80 ;; been changed from the internal GNUS format to the NOV format. The
81 ;; makes it possible to read headers from XOVER much faster.
83 ;; The format of a header is now:
84 ;; [number subject from date id references chars lines xref extra]
86 ;; (That next-to-last entry is defined as "misc" in the NOV format,
87 ;; but Gnus uses it for xrefs.)
89 (defmacro mail-header-number (header)
90 "Return article number in HEADER."
91 `(aref ,header 0))
93 (defmacro mail-header-set-number (header number)
94 "Set article number of HEADER to NUMBER."
95 `(aset ,header 0 ,number))
97 (defmacro mail-header-subject (header)
98 "Return subject string in HEADER."
99 `(aref ,header 1))
101 (defmacro mail-header-set-subject (header subject)
102 "Set article subject of HEADER to SUBJECT."
103 `(aset ,header 1 ,subject))
105 (defmacro mail-header-from (header)
106 "Return author string in HEADER."
107 `(aref ,header 2))
109 (defmacro mail-header-set-from (header from)
110 "Set article author of HEADER to FROM."
111 `(aset ,header 2 ,from))
113 (defmacro mail-header-date (header)
114 "Return date in HEADER."
115 `(aref ,header 3))
117 (defmacro mail-header-set-date (header date)
118 "Set article date of HEADER to DATE."
119 `(aset ,header 3 ,date))
121 (defalias 'mail-header-message-id 'mail-header-id)
122 (defmacro mail-header-id (header)
123 "Return Id in HEADER."
124 `(aref ,header 4))
126 (defalias 'mail-header-set-message-id 'mail-header-set-id)
127 (defmacro mail-header-set-id (header id)
128 "Set article Id of HEADER to ID."
129 `(aset ,header 4 ,id))
131 (defmacro mail-header-references (header)
132 "Return references in HEADER."
133 `(aref ,header 5))
135 (defmacro mail-header-set-references (header ref)
136 "Set article references of HEADER to REF."
137 `(aset ,header 5 ,ref))
139 (defmacro mail-header-chars (header)
140 "Return number of chars of article in HEADER."
141 `(aref ,header 6))
143 (defmacro mail-header-set-chars (header chars)
144 "Set number of chars in article of HEADER to CHARS."
145 `(aset ,header 6 ,chars))
147 (defmacro mail-header-lines (header)
148 "Return lines in HEADER."
149 `(aref ,header 7))
151 (defmacro mail-header-set-lines (header lines)
152 "Set article lines of HEADER to LINES."
153 `(aset ,header 7 ,lines))
155 (defmacro mail-header-xref (header)
156 "Return xref string in HEADER."
157 `(aref ,header 8))
159 (defmacro mail-header-set-xref (header xref)
160 "Set article XREF of HEADER to xref."
161 `(aset ,header 8 ,xref))
163 (defmacro mail-header-extra (header)
164 "Return the extra headers in HEADER."
165 `(aref ,header 9))
167 (defmacro mail-header-set-extra (header extra)
168 "Set the extra headers in HEADER to EXTRA."
169 `(aset ,header 9 ',extra))
171 (defsubst make-mail-header (&optional init)
172 "Create a new mail header structure initialized with INIT."
173 (make-vector 10 init))
175 (defsubst make-full-mail-header (&optional number subject from date id
176 references chars lines xref
177 extra)
178 "Create a new mail header structure initialized with the parameters given."
179 (vector number subject from date id references chars lines xref extra))
181 ;; fake message-ids: generation and detection
183 (defvar nnheader-fake-message-id 1)
185 (defsubst nnheader-generate-fake-message-id ()
186 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
188 (defsubst nnheader-fake-message-id-p (id)
189 (save-match-data ; regular message-id's are <.*>
190 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
192 ;; Parsing headers and NOV lines.
194 (defsubst nnheader-header-value ()
195 (buffer-substring (match-end 0) (gnus-point-at-eol)))
197 (defun nnheader-parse-head (&optional naked)
198 (let ((case-fold-search t)
199 (cur (current-buffer))
200 (buffer-read-only nil)
201 in-reply-to lines p ref)
202 (goto-char (point-min))
203 (when naked
204 (insert "\n"))
205 ;; Search to the beginning of the next header. Error messages
206 ;; do not begin with 2 or 3.
207 (prog1
208 (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
209 ;; This implementation of this function, with nine
210 ;; search-forwards instead of the one re-search-forward and
211 ;; a case (which basically was the old function) is actually
212 ;; about twice as fast, even though it looks messier. You
213 ;; can't have everything, I guess. Speed and elegance
214 ;; don't always go hand in hand.
215 (vector
216 ;; Number.
217 (if naked
218 (progn
219 (setq p (point-min))
221 (prog1
222 (read cur)
223 (end-of-line)
224 (setq p (point))
225 (narrow-to-region (point)
226 (or (and (search-forward "\n.\n" nil t)
227 (- (point) 2))
228 (point)))))
229 ;; Subject.
230 (progn
231 (goto-char p)
232 (if (search-forward "\nsubject: " nil t)
233 (nnheader-header-value) "(none)"))
234 ;; From.
235 (progn
236 (goto-char p)
237 (if (or (search-forward "\nfrom: " nil t)
238 (search-forward "\nfrom:" nil t))
239 (nnheader-header-value) "(nobody)"))
240 ;; Date.
241 (progn
242 (goto-char p)
243 (if (search-forward "\ndate: " nil t)
244 (nnheader-header-value) ""))
245 ;; Message-ID.
246 (progn
247 (goto-char p)
248 (if (search-forward "\nmessage-id:" nil t)
249 (buffer-substring
250 (1- (or (search-forward "<" (gnus-point-at-eol) t)
251 (point)))
252 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
253 ;; If there was no message-id, we just fake one to make
254 ;; subsequent routines simpler.
255 (nnheader-generate-fake-message-id)))
256 ;; References.
257 (progn
258 (goto-char p)
259 (if (search-forward "\nreferences: " nil t)
260 (nnheader-header-value)
261 ;; Get the references from the in-reply-to header if there
262 ;; were no references and the in-reply-to header looks
263 ;; promising.
264 (if (and (search-forward "\nin-reply-to: " nil t)
265 (setq in-reply-to (nnheader-header-value))
266 (string-match "<[^\n>]+>" in-reply-to))
267 (let (ref2)
268 (setq ref (substring in-reply-to (match-beginning 0)
269 (match-end 0)))
270 (while (string-match "<[^\n>]+>"
271 in-reply-to (match-end 0))
272 (setq ref2 (substring in-reply-to (match-beginning 0)
273 (match-end 0)))
274 (when (> (length ref2) (length ref))
275 (setq ref ref2)))
276 ref)
277 nil)))
278 ;; Chars.
280 ;; Lines.
281 (progn
282 (goto-char p)
283 (if (search-forward "\nlines: " nil t)
284 (if (numberp (setq lines (read cur)))
285 lines 0)
287 ;; Xref.
288 (progn
289 (goto-char p)
290 (and (search-forward "\nxref: " nil t)
291 (nnheader-header-value)))
293 ;; Extra.
294 (when nnmail-extra-headers
295 (let ((extra nnmail-extra-headers)
296 out)
297 (while extra
298 (goto-char p)
299 (when (search-forward
300 (concat "\n" (symbol-name (car extra)) ": ") nil t)
301 (push (cons (car extra) (nnheader-header-value))
302 out))
303 (pop extra))
304 out))))
305 (when naked
306 (goto-char (point-min))
307 (delete-char 1)))))
309 (defmacro nnheader-nov-skip-field ()
310 '(search-forward "\t" eol 'move))
312 (defmacro nnheader-nov-field ()
313 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
315 (defmacro nnheader-nov-read-integer ()
316 '(prog1
317 (if (eq (char-after) ?\t)
319 (let ((num (condition-case nil
320 (read (current-buffer))
321 (error nil))))
322 (if (numberp num) num 0)))
323 (or (eobp) (forward-char 1))))
325 (defmacro nnheader-nov-parse-extra ()
326 '(let (out string)
327 (while (not (memq (char-after) '(?\n nil)))
328 (setq string (nnheader-nov-field))
329 (when (string-match "^\\([^ :]+\\): " string)
330 (push (cons (intern (match-string 1 string))
331 (substring string (match-end 0)))
332 out)))
333 out))
335 (defmacro nnheader-nov-read-message-id ()
336 '(let ((id (nnheader-nov-field)))
337 (if (string-match "^<[^>]+>$" id)
339 (nnheader-generate-fake-message-id))))
341 (defun nnheader-parse-nov ()
342 (let ((eol (gnus-point-at-eol)))
343 (vector
344 (nnheader-nov-read-integer) ; number
345 (nnheader-nov-field) ; subject
346 (nnheader-nov-field) ; from
347 (nnheader-nov-field) ; date
348 (nnheader-nov-read-message-id) ; id
349 (nnheader-nov-field) ; refs
350 (nnheader-nov-read-integer) ; chars
351 (nnheader-nov-read-integer) ; lines
352 (if (eq (char-after) ?\n)
354 (if (looking-at "Xref: ")
355 (goto-char (match-end 0)))
356 (nnheader-nov-field)) ; Xref
357 (nnheader-nov-parse-extra)))) ; extra
359 (defun nnheader-insert-nov (header)
360 (princ (mail-header-number header) (current-buffer))
361 (let ((p (point)))
362 (insert
363 "\t"
364 (or (mail-header-subject header) "(none)") "\t"
365 (or (mail-header-from header) "(nobody)") "\t"
366 (or (mail-header-date header) "") "\t"
367 (or (mail-header-id header)
368 (nnmail-message-id))
369 "\t"
370 (or (mail-header-references header) "") "\t")
371 (princ (or (mail-header-chars header) 0) (current-buffer))
372 (insert "\t")
373 (princ (or (mail-header-lines header) 0) (current-buffer))
374 (insert "\t")
375 (when (mail-header-xref header)
376 (insert "Xref: " (mail-header-xref header)))
377 (when (or (mail-header-xref header)
378 (mail-header-extra header))
379 (insert "\t"))
380 (when (mail-header-extra header)
381 (let ((extra (mail-header-extra header)))
382 (while extra
383 (insert (symbol-name (caar extra))
384 ": " (cdar extra) "\t")
385 (pop extra))))
386 (insert "\n")
387 (backward-char 1)
388 (while (search-backward "\n" p t)
389 (delete-char 1))
390 (forward-line 1)))
392 (defun nnheader-insert-header (header)
393 (insert
394 "Subject: " (or (mail-header-subject header) "(none)") "\n"
395 "From: " (or (mail-header-from header) "(nobody)") "\n"
396 "Date: " (or (mail-header-date header) "") "\n"
397 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
398 "References: " (or (mail-header-references header) "") "\n"
399 "Lines: ")
400 (princ (or (mail-header-lines header) 0) (current-buffer))
401 (insert "\n\n"))
403 (defun nnheader-insert-article-line (article)
404 (goto-char (point-min))
405 (insert "220 ")
406 (princ article (current-buffer))
407 (insert " Article retrieved.\n")
408 (search-forward "\n\n" nil 'move)
409 (delete-region (point) (point-max))
410 (forward-char -1)
411 (insert "."))
413 (defun nnheader-nov-delete-outside-range (beg end)
414 "Delete all NOV lines that lie outside the BEG to END range."
415 ;; First we find the first wanted line.
416 (nnheader-find-nov-line beg)
417 (delete-region (point-min) (point))
418 ;; Then we find the last wanted line.
419 (when (nnheader-find-nov-line end)
420 (forward-line 1))
421 (delete-region (point) (point-max)))
423 (defun nnheader-find-nov-line (article)
424 "Put point at the NOV line that start with ARTICLE.
425 If ARTICLE doesn't exist, put point where that line
426 would have been. The function will return non-nil if
427 the line could be found."
428 ;; This function basically does a binary search.
429 (let ((max (point-max))
430 (min (goto-char (point-min)))
431 (cur (current-buffer))
432 (prev (point-min))
433 num found)
434 (while (not found)
435 (goto-char (/ (+ max min) 2))
436 (beginning-of-line)
437 (if (or (= (point) prev)
438 (eobp))
439 (setq found t)
440 (setq prev (point))
441 (while (and (not (numberp (setq num (read cur))))
442 (not (eobp)))
443 (gnus-delete-line))
444 (cond ((> num article)
445 (setq max (point)))
446 ((< num article)
447 (setq min (point)))
449 (setq found 'yes)))))
450 ;; We may be at the first line.
451 (when (and (not num)
452 (not (eobp)))
453 (setq num (read cur)))
454 ;; Now we may have found the article we're looking for, or we
455 ;; may be somewhere near it.
456 (when (and (not (eq found 'yes))
457 (not (eq num article)))
458 (setq found (point))
459 (while (and (< (point) max)
460 (or (not (numberp num))
461 (< num article)))
462 (forward-line 1)
463 (setq found (point))
464 (or (eobp)
465 (= (setq num (read cur)) article)))
466 (unless (eq num article)
467 (goto-char found)))
468 (beginning-of-line)
469 (eq num article)))
471 ;; Various cruft the backends and Gnus need to communicate.
473 (defvar nntp-server-buffer nil)
474 (defvar gnus-verbose-backends 7
475 "*A number that says how talkative the Gnus backends should be.")
476 (defvar gnus-nov-is-evil nil
477 "If non-nil, Gnus backends will never output headers in the NOV format.")
478 (defvar news-reply-yank-from nil)
479 (defvar news-reply-yank-message-id nil)
481 (defvar nnheader-callback-function nil)
483 (defun nnheader-init-server-buffer ()
484 "Initialize the Gnus-backend communication buffer."
485 (save-excursion
486 (unless (gnus-buffer-live-p nntp-server-buffer)
487 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
488 (set-buffer nntp-server-buffer)
489 (mm-enable-multibyte)
490 (erase-buffer)
491 (kill-all-local-variables)
492 (setq case-fold-search t) ;Should ignore case.
495 ;;; Various functions the backends use.
497 (defun nnheader-file-error (file)
498 "Return a string that says what is wrong with FILE."
499 (format
500 (cond
501 ((not (file-exists-p file))
502 "%s does not exist")
503 ((file-directory-p file)
504 "%s is a directory")
505 ((not (file-readable-p file))
506 "%s is not readable"))
507 file))
509 (defun nnheader-insert-head (file)
510 "Insert the head of the article."
511 (when (file-exists-p file)
512 (if (eq nnheader-max-head-length t)
513 ;; Just read the entire file.
514 (nnheader-insert-file-contents file)
515 ;; Read 1K blocks until we find a separator.
516 (let ((beg 0)
517 format-alist)
518 (while (and (eq nnheader-head-chop-length
519 (nth 1 (nnheader-insert-file-contents
520 file nil beg
521 (incf beg nnheader-head-chop-length))))
522 (prog1 (not (search-forward "\n\n" nil t))
523 (goto-char (point-max)))
524 (or (null nnheader-max-head-length)
525 (< beg nnheader-max-head-length))))))
528 (defun nnheader-article-p ()
529 "Say whether the current buffer looks like an article."
530 (goto-char (point-min))
531 (if (not (search-forward "\n\n" nil t))
533 (narrow-to-region (point-min) (1- (point)))
534 (goto-char (point-min))
535 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
536 (goto-char (match-end 0)))
537 (prog1
538 (eobp)
539 (widen))))
541 (defun nnheader-insert-references (references message-id)
542 "Insert a References header based on REFERENCES and MESSAGE-ID."
543 (if (and (not references) (not message-id))
544 ;; This is invalid, but not all articles have Message-IDs.
546 (mail-position-on-field "References")
547 (let ((begin (save-excursion (beginning-of-line) (point)))
548 (fill-column 78)
549 (fill-prefix "\t"))
550 (when references
551 (insert references))
552 (when (and references message-id)
553 (insert " "))
554 (when message-id
555 (insert message-id))
556 ;; Fold long References lines to conform to RFC1036 (sort of).
557 ;; The region must end with a newline to fill the region
558 ;; without inserting extra newline.
559 (fill-region-as-paragraph begin (1+ (point))))))
561 (defun nnheader-replace-header (header new-value)
562 "Remove HEADER and insert the NEW-VALUE."
563 (save-excursion
564 (save-restriction
565 (nnheader-narrow-to-headers)
566 (prog1
567 (message-remove-header header)
568 (goto-char (point-max))
569 (insert header ": " new-value "\n")))))
571 (defun nnheader-narrow-to-headers ()
572 "Narrow to the head of an article."
573 (widen)
574 (narrow-to-region
575 (goto-char (point-min))
576 (if (search-forward "\n\n" nil t)
577 (1- (point))
578 (point-max)))
579 (goto-char (point-min)))
581 (defun nnheader-set-temp-buffer (name &optional noerase)
582 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
583 (set-buffer (get-buffer-create name))
584 (buffer-disable-undo)
585 (unless noerase
586 (erase-buffer))
587 (current-buffer))
589 (eval-when-compile (defvar jka-compr-compression-info-list))
590 (defvar nnheader-numerical-files
591 (if (boundp 'jka-compr-compression-info-list)
592 (concat "\\([0-9]+\\)\\("
593 (mapconcat (lambda (i) (aref i 0))
594 jka-compr-compression-info-list "\\|")
595 "\\)?")
596 "[0-9]+$")
597 "Regexp that match numerical files.")
599 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
600 "Regexp that matches numerical file names.")
602 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
603 "Regexp that matches numerical full file names.")
605 (defsubst nnheader-file-to-number (file)
606 "Take a FILE name and return the article number."
607 (if (string= nnheader-numerical-short-files "^[0-9]+$")
608 (string-to-int file)
609 (string-match nnheader-numerical-short-files file)
610 (string-to-int (match-string 0 file))))
612 (defun nnheader-directory-files-safe (&rest args)
613 ;; It has been reported numerous times that `directory-files'
614 ;; fails with an alarming frequency on NFS mounted file systems.
615 ;; This function executes that function twice and returns
616 ;; the longest result.
617 (let ((first (apply 'directory-files args))
618 (second (apply 'directory-files args)))
619 (if (> (length first) (length second))
620 first
621 second)))
623 (defun nnheader-directory-articles (dir)
624 "Return a list of all article files in directory DIR."
625 (mapcar 'nnheader-file-to-number
626 (nnheader-directory-files-safe
627 dir nil nnheader-numerical-short-files t)))
629 (defun nnheader-article-to-file-alist (dir)
630 "Return an alist of article/file pairs in DIR."
631 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
632 (nnheader-directory-files-safe
633 dir nil nnheader-numerical-short-files t)))
635 (defun nnheader-fold-continuation-lines ()
636 "Fold continuation lines in the current buffer."
637 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
639 (defun nnheader-translate-file-chars (file &optional full)
640 "Translate FILE into something that can be a file name.
641 If FULL, translate everything."
642 (if (null nnheader-file-name-translation-alist)
643 ;; No translation is necessary.
644 file
645 (let* ((i 0)
646 trans leaf path len)
647 (if full
648 ;; Do complete translation.
649 (setq leaf (copy-sequence file)
650 path ""
651 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
652 2 0))
653 ;; We translate -- but only the file name. We leave the directory
654 ;; alone.
655 (if (and (featurep 'xemacs)
656 (memq system-type '(win32 w32 mswindows windows-nt cygwin)))
657 ;; This is needed on NT and stuff, because
658 ;; file-name-nondirectory is not enough to split
659 ;; file names, containing ':', e.g.
660 ;; "d:\\Work\\News\\nntp+news.fido7.ru:fido7.ru.gnu.SCORE"
662 ;; we are trying to correctly split such names:
663 ;; "d:file.name" -> "a:" "file.name"
664 ;; "aaa:bbb.ccc" -> "" "aaa:bbb.ccc"
665 ;; "d:aaa\\bbb:ccc" -> "d:aaa\\" "bbb:ccc"
666 ;; etc.
667 ;; to translate then only the file name part.
668 (progn
669 (setq leaf file
670 path "")
671 (if (string-match "\\(^\\w:\\|[/\\]\\)\\([^/\\]+\\)$" file)
672 (setq leaf (substring file (match-beginning 2))
673 path (substring file 0 (match-beginning 2)))))
674 ;; Emacs DTRT, says andrewi.
675 (setq leaf (file-name-nondirectory file)
676 path (file-name-directory file))))
677 (setq len (length leaf))
678 (while (< i len)
679 (when (setq trans (cdr (assq (aref leaf i)
680 nnheader-file-name-translation-alist)))
681 (aset leaf i trans))
682 (incf i))
683 (concat path leaf))))
685 (defun nnheader-report (backend &rest args)
686 "Report an error from the BACKEND.
687 The first string in ARGS can be a format string."
688 (set (intern (format "%s-status-string" backend))
689 (if (< (length args) 2)
690 (car args)
691 (apply 'format args)))
692 nil)
694 (defun nnheader-get-report (backend)
695 "Get the most recent report from BACKEND."
696 (condition-case ()
697 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
698 backend))))
699 (error (nnheader-message 5 ""))))
701 (defun nnheader-insert (format &rest args)
702 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
703 If FORMAT isn't a format string, it and all ARGS will be inserted
704 without formatting."
705 (save-excursion
706 (set-buffer nntp-server-buffer)
707 (erase-buffer)
708 (if (string-match "%" format)
709 (insert (apply 'format format args))
710 (apply 'insert format args))
713 (eval-and-compile
714 (if (fboundp 'subst-char-in-string)
715 (defsubst nnheader-replace-chars-in-string (string from to)
716 (subst-char-in-string from to string))
717 (defun nnheader-replace-chars-in-string (string from to)
718 "Replace characters in STRING from FROM to TO."
719 (let ((string (substring string 0)) ;Copy string.
720 (len (length string))
721 (idx 0))
722 ;; Replace all occurrences of FROM with TO.
723 (while (< idx len)
724 (when (= (aref string idx) from)
725 (aset string idx to))
726 (setq idx (1+ idx)))
727 string))))
729 (defun nnheader-replace-duplicate-chars-in-string (string from to)
730 "Replace characters in STRING from FROM to TO."
731 (let ((string (substring string 0)) ;Copy string.
732 (len (length string))
733 (idx 0) prev i)
734 ;; Replace all occurrences of FROM with TO.
735 (while (< idx len)
736 (setq i (aref string idx))
737 (when (and (eq prev from) (= i from))
738 (aset string (1- idx) to)
739 (aset string idx to))
740 (setq prev i)
741 (setq idx (1+ idx)))
742 string))
744 (defun nnheader-file-to-group (file &optional top)
745 "Return a group name based on FILE and TOP."
746 (nnheader-replace-chars-in-string
747 (if (not top)
748 file
749 (condition-case ()
750 (substring (expand-file-name file)
751 (length
752 (expand-file-name
753 (file-name-as-directory top))))
754 (error "")))
755 ?/ ?.))
757 (defun nnheader-message (level &rest args)
758 "Message if the Gnus backends are talkative."
759 (if (or (not (numberp gnus-verbose-backends))
760 (<= level gnus-verbose-backends))
761 (apply 'message args)
762 (apply 'format args)))
764 (defun nnheader-be-verbose (level)
765 "Return whether the backends should be verbose on LEVEL."
766 (or (not (numberp gnus-verbose-backends))
767 (<= level gnus-verbose-backends)))
769 (defvar nnheader-pathname-coding-system 'binary
770 "*Coding system for file names.")
772 (defun nnheader-group-pathname (group dir &optional file)
773 "Make file name for GROUP."
774 (concat
775 (let ((dir (file-name-as-directory (expand-file-name dir))))
776 ;; If this directory exists, we use it directly.
777 (file-name-as-directory
778 (if (file-directory-p (concat dir group))
779 (expand-file-name group dir)
780 ;; If not, we translate dots into slashes.
781 (expand-file-name (mm-encode-coding-string
782 (nnheader-replace-chars-in-string group ?. ?/)
783 nnheader-pathname-coding-system)
784 dir))))
785 (cond ((null file) "")
786 ((numberp file) (int-to-string file))
787 (t file))))
789 (defun nnheader-functionp (form)
790 "Return non-nil if FORM is funcallable."
791 (or (and (symbolp form) (fboundp form))
792 (and (listp form) (eq (car form) 'lambda))))
794 (defun nnheader-concat (dir &rest files)
795 "Concat DIR as directory to FILES."
796 (apply 'concat (file-name-as-directory dir) files))
798 (defun nnheader-ms-strip-cr ()
799 "Strip ^M from the end of all lines."
800 (save-excursion
801 (goto-char (point-min))
802 (while (re-search-forward "\r$" nil t)
803 (delete-backward-char 1))))
805 (defun nnheader-file-size (file)
806 "Return the file size of FILE or 0."
807 (or (nth 7 (file-attributes file)) 0))
809 (defun nnheader-find-etc-directory (package &optional file)
810 "Go through the path and find the \".../etc/PACKAGE\" directory.
811 If FILE, find the \".../etc/PACKAGE\" file instead."
812 (let ((path load-path)
813 dir result)
814 ;; We try to find the dir by looking at the load path,
815 ;; stripping away the last component and adding "etc/".
816 (while path
817 (if (and (car path)
818 (file-exists-p
819 (setq dir (concat
820 (file-name-directory
821 (directory-file-name (car path)))
822 "etc/" package
823 (if file "" "/"))))
824 (or file (file-directory-p dir)))
825 (setq result dir
826 path nil)
827 (setq path (cdr path))))
828 result))
830 (eval-when-compile
831 (defvar ange-ftp-path-format)
832 (defvar efs-path-regexp))
833 (defun nnheader-re-read-dir (path)
834 "Re-read directory PATH if PATH is on a remote system."
835 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
836 (when (string-match efs-path-regexp path)
837 (efs-re-read-dir path))
838 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
839 (when (string-match (car ange-ftp-path-format) path)
840 (ange-ftp-re-read-dir path)))))
842 (defvar nnheader-file-coding-system 'raw-text
843 "Coding system used in file backends of Gnus.")
845 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
846 "Like `insert-file-contents', q.v., but only reads in the file.
847 A buffer may be modified in several ways after reading into the buffer due
848 to advanced Emacs features, such as file-name-handlers, format decoding,
849 find-file-hooks, etc.
850 This function ensures that none of these modifications will take place."
851 (let ((coding-system-for-read nnheader-file-coding-system))
852 (mm-insert-file-contents filename visit beg end replace)))
854 (defun nnheader-find-file-noselect (&rest args)
855 (let ((format-alist nil)
856 (auto-mode-alist (mm-auto-mode-alist))
857 (default-major-mode 'fundamental-mode)
858 (enable-local-variables nil)
859 (after-insert-file-functions nil)
860 (enable-local-eval nil)
861 (find-file-hooks nil)
862 (coding-system-for-read nnheader-file-coding-system))
863 (apply 'find-file-noselect args)))
865 (defun nnheader-directory-regular-files (dir)
866 "Return a list of all regular files in DIR."
867 (let ((files (directory-files dir t))
868 out)
869 (while files
870 (when (file-regular-p (car files))
871 (push (car files) out))
872 (pop files))
873 (nreverse out)))
875 (defun nnheader-directory-files (&rest args)
876 "Same as `directory-files', but prune \".\" and \"..\"."
877 (let ((files (apply 'directory-files args))
878 out)
879 (while files
880 (unless (member (file-name-nondirectory (car files)) '("." ".."))
881 (push (car files) out))
882 (pop files))
883 (nreverse out)))
885 (defmacro nnheader-skeleton-replace (from &optional to regexp)
886 `(let ((new (generate-new-buffer " *nnheader replace*"))
887 (cur (current-buffer))
888 (start (point-min)))
889 (set-buffer cur)
890 (goto-char (point-min))
891 (while (,(if regexp 're-search-forward 'search-forward)
892 ,from nil t)
893 (insert-buffer-substring
894 cur start (prog1 (match-beginning 0) (set-buffer new)))
895 (goto-char (point-max))
896 ,(when to `(insert ,to))
897 (set-buffer cur)
898 (setq start (point)))
899 (insert-buffer-substring
900 cur start (prog1 (point-max) (set-buffer new)))
901 (copy-to-buffer cur (point-min) (point-max))
902 (kill-buffer (current-buffer))
903 (set-buffer cur)))
905 (defun nnheader-replace-string (from to)
906 "Do a fast replacement of FROM to TO from point to `point-max'."
907 (nnheader-skeleton-replace from to))
909 (defun nnheader-replace-regexp (from to)
910 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
911 (nnheader-skeleton-replace from to t))
913 (defun nnheader-strip-cr ()
914 "Strip all \r's from the current buffer."
915 (nnheader-skeleton-replace "\r"))
917 (defalias 'nnheader-run-at-time 'run-at-time)
918 (defalias 'nnheader-cancel-timer 'cancel-timer)
919 (defalias 'nnheader-cancel-function-timers 'cancel-function-timers)
921 (when (featurep 'xemacs)
922 (require 'nnheaderxm))
924 (run-hooks 'nnheader-load-hook)
926 (provide 'nnheader)
928 ;;; nnheader.el ends here