1 ;;; nnheader.el --- header access macros for Gnus and its backends
3 ;; Copyright (C) 1987-1990, 1993-1998, 2000-2017 Free Software
6 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (eval-when-compile (require 'cl
))
31 (defvar nnmail-extra-headers
)
32 (defvar gnus-newsgroup-name
)
33 (defvar nnheader-file-coding-system
)
34 (defvar jka-compr-compression-info-list
)
36 ;; Requiring `gnus-util' at compile time creates a circular
37 ;; dependency between nnheader.el and gnus-util.el.
38 ;;(eval-when-compile (require 'gnus-util))
43 (autoload 'gnus-range-add
"gnus-range")
44 (autoload 'gnus-remove-from-range
"gnus-range")
45 ;; FIXME none of these are used explicitly in this file.
46 (autoload 'gnus-sorted-intersection
"gnus-range")
47 (autoload 'gnus-intersection
"gnus-range")
48 (autoload 'gnus-sorted-complement
"gnus-range")
49 (autoload 'gnus-sorted-difference
"gnus-range")
51 (defcustom gnus-verbose-backends
7
52 "Integer that says how verbose the Gnus backends should be.
53 The higher the number, the more messages the Gnus backends will flash
54 to say what it's doing. At zero, the Gnus backends will be totally
55 mute; at five, they will display most important messages; and at ten,
56 they will keep on jabbering all the time."
60 (defcustom gnus-nov-is-evil nil
61 "If non-nil, Gnus backends will never output headers in the NOV format."
65 (defcustom nnheader-max-head-length
8192
66 "Max length of the head of articles.
68 Value is an integer, nil, or t. nil means read in chunks of a file
69 indefinitely until a complete head is found; t means always read the
70 entire file immediately, disregarding `nnheader-head-chop-length'.
72 Integer values will in effect be rounded up to the nearest multiple of
73 `nnheader-head-chop-length'."
74 :group
'gnus-article-various
; FIXME?
75 :type
'(choice integer
(const :tag
"Read chunks" nil
)
76 (const :tag
"Read entire file" t
)))
78 (defcustom nnheader-head-chop-length
2048
79 "Length of each read operation when trying to fetch HEAD headers."
80 :group
'gnus-article-various
; FIXME?
83 (defvar nnheader-read-timeout
84 (if (memq system-type
'(windows-nt cygwin
))
85 ;; http://thread.gmane.org/v9655t3pjo.fsf@marauder.physik.uni-ulm.de
87 ;; IIRC, values lower than 1.0 didn't/don't work on Windows/DOS.
89 ;; There should probably be a runtime test to determine the timing
90 ;; resolution, or a primitive to report it. I don't know off-hand
91 ;; what's possible. Perhaps better, maybe the Windows/DOS primitive
92 ;; could round up non-zero timeouts to a minimum of 1.0?
94 ;; 2008-05-19 change by Larsi:
95 ;; Change the default timeout from 0.1 seconds to 0.01 seconds. This will
96 ;; make nntp and pop3 article retrieval faster in some cases, but might
97 ;; make CPU usage larger. If this has any bad side effects, we might
98 ;; revert this change.
100 ;; When changing this variable, consider changing `pop3-read-timeout' as
102 "How long nntp should wait between checking for the end of output.
103 Shorter values mean quicker response, but are more CPU intensive.")
105 (defcustom nnheader-file-name-translation-alist
106 (if (memq system-type
'(windows-nt cygwin
))
107 (mapcar (lambda (c) (cons c ?_
)) '(?
: ?
* ?
\" ?
< ?
> ??
)))
108 "Alist that says how to translate characters in file names.
109 For instance, if \":\" is invalid as a file character in file names
110 on your system, you could say something like:
112 \(setq nnheader-file-name-translation-alist \\='((?: . ?_)))"
113 :group
'gnus-article-various
; FIXME?
114 :type
'(alist :key-type character
:value-type character
))
116 (defcustom nnheader-directory-separator-character
117 (string-to-char (substring (file-name-as-directory ".") -
1))
118 "A character used as a directory separator."
119 :group
'gnus-article-various
; FIXME?
122 (autoload 'nnmail-message-id
"nnmail")
123 (autoload 'mail-position-on-field
"sendmail")
124 (autoload 'gnus-buffer-live-p
"gnus-util")
126 ;;; Header access macros.
128 ;; These macros may look very much like the ones in GNUS 4.1. They
129 ;; are, in a way, but you should note that the indices they use have
130 ;; been changed from the internal GNUS format to the NOV format. The
131 ;; makes it possible to read headers from XOVER much faster.
133 ;; The format of a header is now:
134 ;; [number subject from date id references chars lines xref extra]
136 ;; (That next-to-last entry is defined as "misc" in the NOV format,
137 ;; but Gnus uses it for xrefs.)
139 (defmacro mail-header-number
(header)
140 "Return article number in HEADER."
143 (defmacro mail-header-set-number
(header number
)
144 "Set article number of HEADER to NUMBER."
145 `(aset ,header
0 ,number
))
147 (defmacro mail-header-subject
(header)
148 "Return subject string in HEADER."
151 (defmacro mail-header-set-subject
(header subject
)
152 "Set article subject of HEADER to SUBJECT."
153 `(aset ,header
1 ,subject
))
155 (defmacro mail-header-from
(header)
156 "Return author string in HEADER."
159 (defmacro mail-header-set-from
(header from
)
160 "Set article author of HEADER to FROM."
161 `(aset ,header
2 ,from
))
163 (defmacro mail-header-date
(header)
164 "Return date in HEADER."
167 (defmacro mail-header-set-date
(header date
)
168 "Set article date of HEADER to DATE."
169 `(aset ,header
3 ,date
))
171 (defalias 'mail-header-message-id
'mail-header-id
)
172 (defmacro mail-header-id
(header)
173 "Return Id in HEADER."
176 (defalias 'mail-header-set-message-id
'mail-header-set-id
)
177 (defmacro mail-header-set-id
(header id
)
178 "Set article Id of HEADER to ID."
179 `(aset ,header
4 ,id
))
181 (defmacro mail-header-references
(header)
182 "Return references in HEADER."
185 (defmacro mail-header-set-references
(header ref
)
186 "Set article references of HEADER to REF."
187 `(aset ,header
5 ,ref
))
189 (defmacro mail-header-chars
(header)
190 "Return number of chars of article in HEADER."
193 (defmacro mail-header-set-chars
(header chars
)
194 "Set number of chars in article of HEADER to CHARS."
195 `(aset ,header
6 ,chars
))
197 (defmacro mail-header-lines
(header)
198 "Return lines in HEADER."
201 (defmacro mail-header-set-lines
(header lines
)
202 "Set article lines of HEADER to LINES."
203 `(aset ,header
7 ,lines
))
205 (defmacro mail-header-xref
(header)
206 "Return xref string in HEADER."
209 (defmacro mail-header-set-xref
(header xref
)
210 "Set article XREF of HEADER to xref."
211 `(aset ,header
8 ,xref
))
213 (defmacro mail-header-extra
(header)
214 "Return the extra headers in HEADER."
217 (defun mail-header-set-extra (header extra
)
218 "Set the extra headers in HEADER to EXTRA."
219 (aset header
9 extra
))
221 (defsubst make-mail-header
(&optional init
)
222 "Create a new mail header structure initialized with INIT."
223 (make-vector 10 init
))
225 (defsubst make-full-mail-header
(&optional number subject from date id
226 references chars lines xref
228 "Create a new mail header structure initialized with the parameters given."
229 (vector number subject from date id references chars lines xref extra
))
231 ;; fake message-ids: generation and detection
233 (defvar nnheader-fake-message-id
1)
235 (defsubst nnheader-generate-fake-message-id
(&optional number
)
237 (format "fake+none+%s+%d" gnus-newsgroup-name number
)
238 (format "fake+none+%s+%s"
240 (int-to-string (incf nnheader-fake-message-id
)))))
242 (defsubst nnheader-fake-message-id-p
(id)
243 (save-match-data ; regular message-id's are <.*>
244 (string-match "\\`fake\\+none\\+.*\\+[0-9]+\\'" id
)))
246 ;; Parsing headers and NOV lines.
248 (defsubst nnheader-remove-cr-followed-by-lf
()
249 (goto-char (point-max))
250 (while (search-backward "\r\n" nil t
)
253 (defsubst nnheader-header-value
()
254 (skip-chars-forward " \t")
255 (buffer-substring (point) (point-at-eol)))
257 (autoload 'ietf-drums-unfold-fws
"ietf-drums")
259 (defun nnheader-parse-naked-head (&optional number
)
260 ;; This function unfolds continuation lines in this buffer
261 ;; destructively. When this side effect is unwanted, use
262 ;; `nnheader-parse-head' instead of this function.
263 (let ((case-fold-search t
)
264 (buffer-read-only nil
)
265 (cur (current-buffer))
267 in-reply-to lines ref
)
268 (nnheader-remove-cr-followed-by-lf)
269 (ietf-drums-unfold-fws)
270 (subst-char-in-region (point-min) (point-max) ?
\t ?
)
274 ;; This implementation of this function, with nine
275 ;; search-forwards instead of the one re-search-forward and a
276 ;; case (which basically was the old function) is actually
277 ;; about twice as fast, even though it looks messier. You
278 ;; can't have everything, I guess. Speed and elegance don't
279 ;; always go hand in hand.
286 (if (search-forward "\nsubject:" nil t
)
287 (nnheader-header-value) "(none)"))
291 (if (search-forward "\nfrom:" nil t
)
292 (nnheader-header-value) "(nobody)"))
296 (if (search-forward "\ndate:" nil t
)
297 (nnheader-header-value) ""))
301 (if (search-forward "\nmessage-id:" nil t
)
303 (1- (or (search-forward "<" (point-at-eol) t
)
305 (or (search-forward ">" (point-at-eol) t
) (point)))
306 ;; If there was no message-id, we just fake one to make
307 ;; subsequent routines simpler.
308 (nnheader-generate-fake-message-id number
)))
312 (if (search-forward "\nreferences:" nil t
)
313 (nnheader-header-value)
314 ;; Get the references from the in-reply-to header if
315 ;; there were no references and the in-reply-to header
317 (if (and (search-forward "\nin-reply-to:" nil t
)
318 (setq in-reply-to
(nnheader-header-value))
319 (string-match "<[^\n>]+>" in-reply-to
))
321 (setq ref
(substring in-reply-to
(match-beginning 0)
323 (while (string-match "<[^\n>]+>"
324 in-reply-to
(match-end 0))
325 (setq ref2
(substring in-reply-to
(match-beginning 0)
327 (when (> (length ref2
) (length ref
))
336 (if (search-forward "\nlines: " nil t
)
337 (if (numberp (setq lines
(read cur
)))
343 (and (search-forward "\nxref:" nil t
)
344 (nnheader-header-value)))
346 (when nnmail-extra-headers
347 (let ((extra nnmail-extra-headers
)
351 (when (search-forward
352 (concat "\n" (symbol-name (car extra
)) ":") nil t
)
353 (push (cons (car extra
) (nnheader-header-value))
360 (defun nnheader-parse-head (&optional naked
)
361 (let ((cur (current-buffer)) num beg end
)
366 ;; Search to the beginning of the next header. Error
367 ;; messages do not begin with 2 or 3.
368 (when (re-search-forward "^[23][0-9]+ " nil t
)
371 end
(if (search-forward "\n.\n" nil t
)
372 (goto-char (- (point) 2))
375 (insert-buffer-substring cur beg end
)
376 (nnheader-parse-naked-head num
)))))
378 (defmacro nnheader-nov-skip-field
()
379 '(search-forward "\t" eol
'move
))
381 (defmacro nnheader-nov-field
()
382 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol
)))
384 (defmacro nnheader-nov-read-integer
()
386 (if (eq (char-after) ?
\t)
388 (let ((num (condition-case nil
389 (read (current-buffer))
391 (if (numberp num
) num
0)))
392 (or (eobp) (forward-char 1))))
394 (defmacro nnheader-nov-parse-extra
()
396 (while (not (memq (char-after) '(?
\n nil
)))
397 (setq string
(nnheader-nov-field))
398 (when (string-match "^\\([^ :]+\\): " string
)
399 (push (cons (intern (match-string 1 string
))
400 (substring string
(match-end 0)))
405 (defvar nnheader-uniquify-message-id nil
))
407 (defmacro nnheader-nov-read-message-id
(&optional number
)
408 `(let ((id (nnheader-nov-field)))
409 (if (string-match "^<[^>]+>$" id
)
410 ,(if nnheader-uniquify-message-id
411 `(if (string-match "__[^@]+@" id
)
412 (concat (substring id
0 (match-beginning 0))
413 (substring id
(1- (match-end 0))))
416 (nnheader-generate-fake-message-id ,number
))))
418 (defun nnheader-parse-nov ()
419 (let ((eol (point-at-eol))
420 (number (nnheader-nov-read-integer)))
423 (nnheader-nov-field) ; subject
424 (nnheader-nov-field) ; from
425 (nnheader-nov-field) ; date
426 (nnheader-nov-read-message-id number
) ; id
427 (nnheader-nov-field) ; refs
428 (nnheader-nov-read-integer) ; chars
429 (nnheader-nov-read-integer) ; lines
430 (if (eq (char-after) ?
\n)
432 (if (looking-at "Xref: ")
433 (goto-char (match-end 0)))
434 (nnheader-nov-field)) ; Xref
435 (nnheader-nov-parse-extra)))) ; extra
437 (defun nnheader-insert-nov (header)
438 (princ (mail-header-number header
) (current-buffer))
442 (or (mail-header-subject header
) "(none)") "\t"
443 (or (mail-header-from header
) "(nobody)") "\t"
444 (or (mail-header-date header
) "") "\t"
445 (or (mail-header-id header
)
448 (or (mail-header-references header
) "") "\t")
449 (princ (or (mail-header-chars header
) 0) (current-buffer))
451 (princ (or (mail-header-lines header
) 0) (current-buffer))
453 (when (mail-header-xref header
)
454 (insert "Xref: " (mail-header-xref header
)))
455 (when (or (mail-header-xref header
)
456 (mail-header-extra header
))
458 (when (mail-header-extra header
)
459 (let ((extra (mail-header-extra header
)))
461 (insert (symbol-name (caar extra
))
462 ": " (if (stringp (cdar extra
)) (cdar extra
) "") "\t")
466 (while (search-backward "\n" p t
)
470 (defun nnheader-parse-overview-file (file)
471 "Parse FILE and return a list of headers."
472 (mm-with-unibyte-buffer
473 (nnheader-insert-file-contents file
)
474 (goto-char (point-min))
477 (push (nnheader-parse-nov) headers
)
479 (nreverse headers
))))
481 (defun nnheader-write-overview-file (file headers
)
482 "Write HEADERS to FILE."
484 (mapcar 'nnheader-insert-nov headers
)))
486 (defun nnheader-insert-header (header)
488 "Subject: " (or (mail-header-subject header
) "(none)") "\n"
489 "From: " (or (mail-header-from header
) "(nobody)") "\n"
490 "Date: " (or (mail-header-date header
) "") "\n"
491 "Message-ID: " (or (mail-header-id header
) (nnmail-message-id)) "\n"
492 "References: " (or (mail-header-references header
) "") "\n"
494 (princ (or (mail-header-lines header
) 0) (current-buffer))
497 (defun nnheader-insert-article-line (article)
498 (goto-char (point-min))
500 (princ article
(current-buffer))
501 (insert " Article retrieved.\n")
502 (search-forward "\n\n" nil
'move
)
503 (delete-region (point) (point-max))
507 (defun nnheader-nov-delete-outside-range (beg end
)
508 "Delete all NOV lines that lie outside the BEG to END range."
509 ;; First we find the first wanted line.
510 (nnheader-find-nov-line beg
)
511 (delete-region (point-min) (point))
512 ;; Then we find the last wanted line.
513 (when (nnheader-find-nov-line end
)
515 (delete-region (point) (point-max)))
517 (defun nnheader-find-nov-line (article)
518 "Put point at the NOV line that start with ARTICLE.
519 If ARTICLE doesn't exist, put point where that line
520 would have been. The function will return non-nil if
521 the line could be found."
522 ;; This function basically does a binary search.
523 (let ((max (point-max))
524 (min (goto-char (point-min)))
525 (cur (current-buffer))
529 (goto-char (+ min
(/ (- max min
) 2)))
531 (if (or (= (point) prev
)
535 (while (and (not (numberp (setq num
(read cur
))))
538 (cond ((> num article
)
543 (setq found
'yes
)))))
544 ;; We may be at the first line.
547 (setq num
(read cur
)))
548 ;; Now we may have found the article we're looking for, or we
549 ;; may be somewhere near it.
550 (when (and (not (eq found
'yes
))
551 (not (eq num article
)))
553 (while (and (< (point) max
)
554 (or (not (numberp num
))
559 (= (setq num
(read cur
)) article
)))
560 (unless (eq num article
)
565 ;; Various cruft the backends and Gnus need to communicate.
567 (defvar nntp-server-buffer nil
)
568 (defvar nntp-process-response nil
)
570 (defvar nnheader-callback-function nil
)
572 (defun nnheader-init-server-buffer ()
573 "Initialize the Gnus-backend communication buffer."
574 (unless (gnus-buffer-live-p nntp-server-buffer
)
575 (setq nntp-server-buffer
(get-buffer-create " *nntpd*")))
576 (with-current-buffer nntp-server-buffer
578 (mm-enable-multibyte)
579 (kill-all-local-variables)
580 (setq case-fold-search t
) ;Should ignore case.
581 (set (make-local-variable 'nntp-process-response
) nil
)
584 ;;; Various functions the backends use.
586 (defun nnheader-file-error (file)
587 "Return a string that says what is wrong with FILE."
590 ((not (file-exists-p file
))
592 ((file-directory-p file
)
594 ((not (file-readable-p file
))
595 "%s is not readable"))
598 (defun nnheader-insert-head (file)
599 "Insert the head of the article."
600 (when (file-exists-p file
)
601 (if (eq nnheader-max-head-length t
)
602 ;; Just read the entire file.
603 (nnheader-insert-file-contents file
)
604 ;; Read blocks of the size specified by `nnheader-head-chop-length'
605 ;; until we find a separator.
608 ;; Use `binary' to prevent the contents from being decoded,
609 ;; or it will change the number of characters that
610 ;; `insert-file-contents' returns.
611 (coding-system-for-read 'binary
))
612 (while (and (eq nnheader-head-chop-length
613 (nth 1 (mm-insert-file-contents
615 (incf beg nnheader-head-chop-length
))))
616 ;; CRLF or CR might be used for the line-break code.
617 (prog1 (not (re-search-forward "\n\r?\n\\|\r\r" nil t
))
618 (goto-char (point-max)))
619 (or (null nnheader-max-head-length
)
620 (< beg nnheader-max-head-length
))))
621 ;; Finally decode the contents.
622 (when (mm-coding-system-p nnheader-file-coding-system
)
623 (decode-coding-region start
(point-max)
624 nnheader-file-coding-system
))))
627 (defun nnheader-article-p ()
628 "Say whether the current buffer looks like an article."
629 (goto-char (point-min))
630 (if (not (search-forward "\n\n" nil t
))
632 (narrow-to-region (point-min) (1- (point)))
633 (goto-char (point-min))
634 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
635 (goto-char (match-end 0)))
640 (defun nnheader-insert-references (references message-id
)
641 "Insert a References header based on REFERENCES and MESSAGE-ID."
642 (if (and (not references
) (not message-id
))
643 ;; This is invalid, but not all articles have Message-IDs.
645 (mail-position-on-field "References")
646 (let ((begin (point-at-bol))
651 (when (and references message-id
)
655 ;; Fold long References lines to conform to RFC1036 (sort of).
656 ;; The region must end with a newline to fill the region
657 ;; without inserting extra newline.
658 (fill-region-as-paragraph begin
(1+ (point))))))
660 (declare-function message-remove-header
"message"
661 (header &optional is-regexp first reverse
))
663 (defun nnheader-replace-header (header new-value
)
664 "Remove HEADER and insert the NEW-VALUE."
668 (nnheader-narrow-to-headers)
670 (message-remove-header header
)
671 (goto-char (point-max))
672 (insert header
": " new-value
"\n")))))
674 (defun nnheader-narrow-to-headers ()
675 "Narrow to the head of an article."
678 (goto-char (point-min))
679 (if (search-forward "\n\n" nil t
)
682 (goto-char (point-min)))
684 (defun nnheader-get-lines-and-char ()
685 "Return the number of lines and chars in the article body."
686 (goto-char (point-min))
687 (if (not (re-search-forward "\n\r?\n" nil t
))
689 (list (count-lines (point) (point-max))
690 (- (point-max) (point)))))
692 (defun nnheader-remove-body ()
693 "Remove the body from an article in this current buffer."
694 (goto-char (point-min))
695 (when (re-search-forward "\n\r?\n" nil t
)
696 (delete-region (point) (point-max))))
698 (defun nnheader-set-temp-buffer (name &optional noerase
)
699 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
700 (set-buffer (get-buffer-create name
))
701 (buffer-disable-undo)
706 (defvar nnheader-numerical-files
707 (if (boundp 'jka-compr-compression-info-list
)
708 (concat "\\([0-9]+\\)\\("
709 (mapconcat (lambda (i) (aref i
0))
710 jka-compr-compression-info-list
"\\|")
713 "Regexp that match numerical files.")
715 (defvar nnheader-numerical-short-files
(concat "^" nnheader-numerical-files
)
716 "Regexp that matches numerical file names.")
718 (defvar nnheader-numerical-full-files
(concat "/" nnheader-numerical-files
)
719 "Regexp that matches numerical full file names.")
721 (defsubst nnheader-file-to-number
(file)
722 "Take a FILE name and return the article number."
723 (if (string= nnheader-numerical-short-files
"^[0-9]+$")
724 (string-to-number file
)
725 (string-match nnheader-numerical-short-files file
)
726 (string-to-number (match-string 0 file
))))
728 (defvar nnheader-directory-files-is-safe
(not (eq system-type
'windows-nt
))
729 "If non-nil, Gnus believes `directory-files' is safe.
730 It has been reported numerous times that `directory-files' fails with
731 an alarming frequency on NFS mounted file systems. If it is nil,
732 `nnheader-directory-files-safe' is used.")
734 (defun nnheader-directory-files-safe (&rest args
)
735 "Execute `directory-files' twice and returns the longer result."
736 (let ((first (apply 'directory-files args
))
737 (second (apply 'directory-files args
)))
738 (if (> (length first
) (length second
))
742 (defun nnheader-directory-articles (dir)
743 "Return a list of all article files in directory DIR."
744 (mapcar 'nnheader-file-to-number
745 (if nnheader-directory-files-is-safe
747 dir nil nnheader-numerical-short-files t
)
748 (nnheader-directory-files-safe
749 dir nil nnheader-numerical-short-files t
))))
751 (defun nnheader-article-to-file-alist (dir)
752 "Return an alist of article/file pairs in DIR."
753 (mapcar (lambda (file) (cons (nnheader-file-to-number file
) file
))
754 (if nnheader-directory-files-is-safe
756 dir nil nnheader-numerical-short-files t
)
757 (nnheader-directory-files-safe
758 dir nil nnheader-numerical-short-files t
))))
760 (defun nnheader-fold-continuation-lines ()
761 "Fold continuation lines in the current buffer."
762 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
764 (defun nnheader-translate-file-chars (file &optional full
)
765 "Translate FILE into something that can be a file name.
766 If FULL, translate everything."
767 (if (null nnheader-file-name-translation-alist
)
768 ;; No translation is necessary.
773 ;; Do complete translation.
774 (setq leaf
(copy-sequence file
)
776 i
(if (and (< 1 (length leaf
)) (eq ?
: (aref leaf
1)))
778 ;; We translate -- but only the file name. We leave the directory
780 (setq leaf
(file-name-nondirectory file
)
781 path
(file-name-directory file
)))
782 (setq len
(length leaf
))
784 (when (setq trans
(cdr (assq (aref leaf i
)
785 nnheader-file-name-translation-alist
)))
788 (concat path leaf
))))
790 (defun nnheader-report (backend &rest args
)
791 "Report an error from the BACKEND.
792 The first string in ARGS can be a format string."
793 (set (intern (format "%s-status-string" backend
))
794 (if (< (length args
) 2)
796 (apply 'format args
)))
799 (defun nnheader-get-report-string (backend)
800 "Get the most recent report from BACKEND."
802 (format "%s" (symbol-value (intern (format "%s-status-string"
806 (defun nnheader-get-report (backend)
807 "Get the most recent report from BACKEND."
808 (nnheader-message 5 (nnheader-get-report-string backend
)))
810 (defun nnheader-insert (format &rest args
)
811 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
812 If FORMAT isn't a format string, it and all ARGS will be inserted
814 (with-current-buffer nntp-server-buffer
816 (if (string-match "%" format
)
817 (insert (apply 'format format args
))
818 (apply 'insert format args
))
821 (defsubst nnheader-replace-chars-in-string
(string from to
)
822 (subst-char-in-string from to string
))
824 (defun nnheader-replace-duplicate-chars-in-string (string from to
)
825 "Replace characters in STRING from FROM to TO."
826 (let ((string (substring string
0)) ;Copy string.
827 (len (length string
))
829 ;; Replace all occurrences of FROM with TO.
831 (setq i
(aref string idx
))
832 (when (and (eq prev from
) (= i from
))
833 (aset string
(1- idx
) to
)
834 (aset string idx to
))
839 (defun nnheader-file-to-group (file &optional top
)
840 "Return a group name based on FILE and TOP."
841 (nnheader-replace-chars-in-string
845 (substring (expand-file-name file
)
848 (file-name-as-directory top
))))
850 nnheader-directory-separator-character ?.
))
852 (defun nnheader-message (level &rest args
)
853 "Message if the Gnus backends are talkative."
854 (if (or (not (numberp gnus-verbose-backends
))
855 (<= level gnus-verbose-backends
))
856 (if gnus-add-timestamp-to-message
857 (apply 'gnus-message-with-timestamp args
)
858 (apply 'message args
))
859 (apply 'format args
)))
861 (defun nnheader-be-verbose (level)
862 "Return whether the backends should be verbose on LEVEL."
863 (or (not (numberp gnus-verbose-backends
))
864 (<= level gnus-verbose-backends
)))
866 (defcustom nnheader-pathname-coding-system
'iso-8859-1
867 "Coding system for file name."
868 :group
'gnus-article-various
; FIXME?
869 :type
'coding-system
)
871 (defun nnheader-group-pathname (group dir
&optional file
)
872 "Make file name for GROUP."
874 (let ((dir (file-name-as-directory (expand-file-name dir
))))
875 ;; If this directory exists, we use it directly.
876 (file-name-as-directory
877 (if (file-directory-p (concat dir group
))
878 (expand-file-name group dir
)
879 ;; If not, we translate dots into slashes.
880 (expand-file-name (encode-coding-string
881 (nnheader-replace-chars-in-string group ?. ?
/)
882 nnheader-pathname-coding-system
)
884 (cond ((null file
) "")
885 ((numberp file
) (int-to-string file
))
888 (defun nnheader-concat (dir &rest files
)
889 "Concat DIR as directory to FILES."
890 (apply 'concat
(file-name-as-directory dir
) files
))
892 (defun nnheader-ms-strip-cr ()
893 "Strip ^M from the end of all lines."
895 (nnheader-remove-cr-followed-by-lf)))
897 (defun nnheader-file-size (file)
898 "Return the file size of FILE or 0."
899 (or (nth 7 (file-attributes file
)) 0))
901 (defun nnheader-find-etc-directory (package &optional file first
)
902 "Go through `load-path' and find the \"../etc/PACKAGE\" directory.
903 This function will look in the parent directory of each `load-path'
904 entry, and look for the \"etc\" directory there.
905 If FILE, find the \".../etc/PACKAGE\" file instead.
906 If FIRST is non-nil, return the directory or the file found at the
907 first. Otherwise, find the newest one, though it may take a time."
908 (let ((path load-path
)
910 ;; We try to find the dir by looking at the load path,
911 ;; stripping away the last component and adding "etc/".
917 (directory-file-name (car path
)))
920 (or file
(file-directory-p dir
)))
922 (or (member dir results
)
924 (setq path
(if first nil
(cdr path
))))
925 (setq path
(cdr path
))))
926 (if (or first
(not (cdr results
)))
928 (car (sort results
'file-newer-than-file-p
)))))
930 (defvar ange-ftp-path-format
)
931 (defvar efs-path-regexp
)
932 (defun nnheader-re-read-dir (path)
933 "Re-read directory PATH if PATH is on a remote system."
934 (if (and (fboundp 'efs-re-read-dir
) (boundp 'efs-path-regexp
))
935 (when (string-match efs-path-regexp path
)
936 (efs-re-read-dir path
))
937 (when (and (fboundp 'ange-ftp-re-read-dir
) (boundp 'ange-ftp-path-format
))
938 (when (string-match (car ange-ftp-path-format
) path
)
939 (ange-ftp-re-read-dir path
)))))
941 (defvar nnheader-file-coding-system
'raw-text
942 "Coding system used in file backends of Gnus.")
944 (defun nnheader-insert-file-contents (filename &optional visit beg end replace
)
945 "Like `insert-file-contents', q.v., but only reads in the file.
946 A buffer may be modified in several ways after reading into the buffer due
947 to advanced Emacs features, such as file-name-handlers, format decoding,
948 find-file-hooks, etc.
949 This function ensures that none of these modifications will take place."
950 (let ((coding-system-for-read nnheader-file-coding-system
))
951 (mm-insert-file-contents filename visit beg end replace
)))
953 (defun nnheader-insert-nov-file (file first
)
954 (let ((size (nth 7 (file-attributes file
)))
955 (cutoff (* 32 1024)))
958 ;; If the file is small, we just load it.
959 (nnheader-insert-file-contents file
)
960 ;; We start on the assumption that FIRST is pretty recent. If
961 ;; not, we just insert the rest of the file as well.
963 (nnheader-insert-file-contents file nil
(- size cutoff
) size
)
964 (goto-char (point-min))
965 (delete-region (point) (or (search-forward "\n" nil
'move
) (point)))
966 (setq current
(ignore-errors (read (current-buffer))))
967 (if (and (numberp current
)
970 (delete-region (point-min) (point-max))
971 (nnheader-insert-file-contents file
)))))))
973 (defun nnheader-find-file-noselect (&rest args
)
974 "Open a file with some variables bound.
975 See `find-file-noselect' for the arguments."
976 (letf* ((format-alist nil
)
977 (auto-mode-alist (mm-auto-mode-alist))
978 ((default-value 'major-mode
) 'fundamental-mode
)
979 (enable-local-variables nil
)
980 (after-insert-file-functions nil
)
981 (enable-local-eval nil
)
982 (coding-system-for-read nnheader-file-coding-system
)
983 (version-control 'never
)
984 (find-file-hook nil
))
985 (apply 'find-file-noselect args
)))
987 (defun nnheader-directory-regular-files (dir)
988 "Return a list of all regular files in DIR."
989 (let ((files (directory-files dir t
))
992 (when (file-regular-p (car files
))
993 (push (car files
) out
))
997 (defun nnheader-directory-files (&rest args
)
998 "Same as `directory-files', but prune \".\" and \"..\"."
999 (let ((files (apply 'directory-files args
))
1002 (unless (member (file-name-nondirectory (car files
)) '("." ".."))
1003 (push (car files
) out
))
1007 (defmacro nnheader-skeleton-replace
(from &optional to regexp
)
1008 `(let ((new (generate-new-buffer " *nnheader replace*"))
1009 (cur (current-buffer))
1010 (start (point-min)))
1012 (goto-char (point-min))
1013 (while (,(if regexp
're-search-forward
'search-forward
)
1015 (insert-buffer-substring
1016 cur start
(prog1 (match-beginning 0) (set-buffer new
)))
1017 (goto-char (point-max))
1018 ,(when to
`(insert ,to
))
1020 (setq start
(point)))
1021 (insert-buffer-substring
1022 cur start
(prog1 (point-max) (set-buffer new
)))
1023 (copy-to-buffer cur
(point-min) (point-max))
1024 (kill-buffer (current-buffer))
1027 (defun nnheader-replace-string (from to
)
1028 "Do a fast replacement of FROM to TO from point to `point-max'."
1029 (nnheader-skeleton-replace from to
))
1031 (defun nnheader-replace-regexp (from to
)
1032 "Do a fast regexp replacement of FROM to TO from point to `point-max'."
1033 (nnheader-skeleton-replace from to t
))
1035 (defun nnheader-strip-cr ()
1036 "Strip all \r's from the current buffer."
1037 (nnheader-skeleton-replace "\r"))
1039 (defalias 'nnheader-cancel-timer
'cancel-timer
)
1040 (defalias 'nnheader-cancel-function-timers
'cancel-function-timers
)
1042 ;; When changing this function, consider changing `pop3-accept-process-output'
1044 (defun nnheader-accept-process-output (process)
1045 (accept-process-output
1047 (truncate nnheader-read-timeout
)
1048 (truncate (* (- nnheader-read-timeout
1049 (truncate nnheader-read-timeout
))
1052 (defun nnheader-update-marks-actions (backend-marks actions
)
1053 (dolist (action actions
)
1054 (let ((range (nth 0 action
))
1055 (what (nth 1 action
))
1056 (marks (nth 2 action
)))
1057 (dolist (mark marks
)
1059 (gnus-update-alist-soft
1063 (gnus-range-add (cdr (assoc mark backend-marks
)) range
))
1065 (gnus-remove-from-range
1066 (cdr (assoc mark backend-marks
)) range
))
1072 (defmacro nnheader-insert-buffer-substring
(buffer &optional start end
)
1073 "Copy string from unibyte buffer to multibyte current buffer."
1074 `(if enable-multibyte-characters
1075 (insert (with-current-buffer ,buffer
1076 (string-to-multibyte
1078 `(buffer-substring (or ,start
(point-min))
1079 (or ,end
(point-max)))
1080 '(buffer-string)))))
1081 (insert-buffer-substring ,buffer
,start
,end
)))
1083 (defvar nnheader-last-message-time
'(0 0))
1084 (defun nnheader-message-maybe (&rest args
)
1085 (let ((now (current-time)))
1086 (when (> (float-time (time-subtract now nnheader-last-message-time
)) 1)
1087 (setq nnheader-last-message-time now
)
1088 (apply 'nnheader-message args
))))
1090 (run-hooks 'nnheader-load-hook
)
1094 ;;; nnheader.el ends here