Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; nnheader.el --- header access macros for Gnus and its backends |
16409b0b GM |
2 | |
3 | ;; Copyright (C) 1987, 1988, 1989, 1990, 1993, 1994, 1995, 1996, | |
6f3b74c6 | 4 | ;; 1997, 1998, 2000, 2001 |
16409b0b | 5 | ;; Free Software Foundation, Inc. |
eec82323 LMI |
6 | |
7 | ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet> | |
6748645f | 8 | ;; Lars Magne Ingebrigtsen <larsi@gnus.org> |
eec82323 LMI |
9 | ;; Keywords: news |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
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. | |
27 | ||
28 | ;;; Commentary: | |
29 | ||
eec82323 LMI |
30 | ;;; Code: |
31 | ||
7df7482d | 32 | (eval-when-compile (require 'cl)) |
6f3b74c6 GM |
33 | |
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)) | |
7df7482d | 37 | |
eec82323 | 38 | (require 'mail-utils) |
16409b0b | 39 | (require 'mm-util) |
9e153fef DL |
40 | (eval-and-compile |
41 | (autoload 'gnus-intersection "gnus-range") | |
42 | (autoload 'gnus-sorted-complement "gnus-range")) | |
eec82323 LMI |
43 | |
44 | (defvar nnheader-max-head-length 4096 | |
45 | "*Max length of the head of articles.") | |
46 | ||
47 | (defvar nnheader-head-chop-length 2048 | |
48 | "*Length of each read operation when trying to fetch HEAD headers.") | |
49 | ||
c60ee5e7 | 50 | (defvar nnheader-file-name-translation-alist |
3e7b2fa7 SZ |
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))) | |
eec82323 | 62 | "*Alist that says how to translate characters in file names. |
16409b0b | 63 | For instance, if \":\" is invalid as a file character in file names |
eec82323 LMI |
64 | on your system, you could say something like: |
65 | ||
66 | \(setq nnheader-file-name-translation-alist '((?: . ?_)))") | |
67 | ||
68 | (eval-and-compile | |
16409b0b GM |
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") | |
3a9bfee7 | 73 | (autoload 'gnus-delete-line "gnus-util" nil nil 'macro) |
16409b0b | 74 | (autoload 'gnus-buffer-live-p "gnus-util")) |
eec82323 LMI |
75 | |
76 | ;;; Header access macros. | |
77 | ||
16409b0b GM |
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. | |
82 | ;; | |
83 | ;; The format of a header is now: | |
84 | ;; [number subject from date id references chars lines xref extra] | |
85 | ;; | |
86 | ;; (That next-to-last entry is defined as "misc" in the NOV format, | |
87 | ;; but Gnus uses it for xrefs.) | |
88 | ||
eec82323 LMI |
89 | (defmacro mail-header-number (header) |
90 | "Return article number in HEADER." | |
91 | `(aref ,header 0)) | |
92 | ||
93 | (defmacro mail-header-set-number (header number) | |
94 | "Set article number of HEADER to NUMBER." | |
95 | `(aset ,header 0 ,number)) | |
96 | ||
97 | (defmacro mail-header-subject (header) | |
98 | "Return subject string in HEADER." | |
99 | `(aref ,header 1)) | |
100 | ||
101 | (defmacro mail-header-set-subject (header subject) | |
102 | "Set article subject of HEADER to SUBJECT." | |
103 | `(aset ,header 1 ,subject)) | |
104 | ||
105 | (defmacro mail-header-from (header) | |
106 | "Return author string in HEADER." | |
107 | `(aref ,header 2)) | |
108 | ||
109 | (defmacro mail-header-set-from (header from) | |
110 | "Set article author of HEADER to FROM." | |
111 | `(aset ,header 2 ,from)) | |
112 | ||
113 | (defmacro mail-header-date (header) | |
114 | "Return date in HEADER." | |
115 | `(aref ,header 3)) | |
116 | ||
117 | (defmacro mail-header-set-date (header date) | |
118 | "Set article date of HEADER to DATE." | |
119 | `(aset ,header 3 ,date)) | |
120 | ||
121 | (defalias 'mail-header-message-id 'mail-header-id) | |
122 | (defmacro mail-header-id (header) | |
123 | "Return Id in HEADER." | |
124 | `(aref ,header 4)) | |
125 | ||
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)) | |
130 | ||
131 | (defmacro mail-header-references (header) | |
132 | "Return references in HEADER." | |
133 | `(aref ,header 5)) | |
134 | ||
135 | (defmacro mail-header-set-references (header ref) | |
136 | "Set article references of HEADER to REF." | |
137 | `(aset ,header 5 ,ref)) | |
138 | ||
139 | (defmacro mail-header-chars (header) | |
140 | "Return number of chars of article in HEADER." | |
141 | `(aref ,header 6)) | |
142 | ||
143 | (defmacro mail-header-set-chars (header chars) | |
144 | "Set number of chars in article of HEADER to CHARS." | |
145 | `(aset ,header 6 ,chars)) | |
146 | ||
147 | (defmacro mail-header-lines (header) | |
148 | "Return lines in HEADER." | |
149 | `(aref ,header 7)) | |
150 | ||
151 | (defmacro mail-header-set-lines (header lines) | |
152 | "Set article lines of HEADER to LINES." | |
153 | `(aset ,header 7 ,lines)) | |
154 | ||
155 | (defmacro mail-header-xref (header) | |
156 | "Return xref string in HEADER." | |
157 | `(aref ,header 8)) | |
158 | ||
159 | (defmacro mail-header-set-xref (header xref) | |
16409b0b | 160 | "Set article XREF of HEADER to xref." |
eec82323 LMI |
161 | `(aset ,header 8 ,xref)) |
162 | ||
16409b0b GM |
163 | (defmacro mail-header-extra (header) |
164 | "Return the extra headers in HEADER." | |
165 | `(aref ,header 9)) | |
166 | ||
167 | (defmacro mail-header-set-extra (header extra) | |
168 | "Set the extra headers in HEADER to EXTRA." | |
169 | `(aset ,header 9 ',extra)) | |
170 | ||
171 | (defsubst make-mail-header (&optional init) | |
eec82323 | 172 | "Create a new mail header structure initialized with INIT." |
16409b0b | 173 | (make-vector 10 init)) |
eec82323 | 174 | |
16409b0b GM |
175 | (defsubst make-full-mail-header (&optional number subject from date id |
176 | references chars lines xref | |
177 | extra) | |
eec82323 | 178 | "Create a new mail header structure initialized with the parameters given." |
16409b0b | 179 | (vector number subject from date id references chars lines xref extra)) |
eec82323 LMI |
180 | |
181 | ;; fake message-ids: generation and detection | |
182 | ||
183 | (defvar nnheader-fake-message-id 1) | |
184 | ||
185 | (defsubst nnheader-generate-fake-message-id () | |
186 | (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id)))) | |
187 | ||
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))) | |
191 | ||
192 | ;; Parsing headers and NOV lines. | |
193 | ||
194 | (defsubst nnheader-header-value () | |
195 | (buffer-substring (match-end 0) (gnus-point-at-eol))) | |
196 | ||
197 | (defun nnheader-parse-head (&optional naked) | |
198 | (let ((case-fold-search t) | |
199 | (cur (current-buffer)) | |
200 | (buffer-read-only nil) | |
6748645f | 201 | in-reply-to lines p ref) |
eec82323 LMI |
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)) | |
220 | 0) | |
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) | |
df359f6c DL |
237 | (if (or (search-forward "\nfrom: " nil t) |
238 | (search-forward "\nfrom:" nil t)) | |
eec82323 LMI |
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 | |
6748645f LMI |
250 | (1- (or (search-forward "<" (gnus-point-at-eol) t) |
251 | (point))) | |
252 | (or (search-forward ">" (gnus-point-at-eol) t) (point))) | |
eec82323 LMI |
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)) | |
16409b0b | 266 | (string-match "<[^\n>]+>" in-reply-to)) |
6748645f LMI |
267 | (let (ref2) |
268 | (setq ref (substring in-reply-to (match-beginning 0) | |
269 | (match-end 0))) | |
16409b0b GM |
270 | (while (string-match "<[^\n>]+>" |
271 | in-reply-to (match-end 0)) | |
6748645f LMI |
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))) | |
eec82323 LMI |
278 | ;; Chars. |
279 | 0 | |
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) | |
286 | 0)) | |
287 | ;; Xref. | |
288 | (progn | |
289 | (goto-char p) | |
290 | (and (search-forward "\nxref: " nil t) | |
16409b0b GM |
291 | (nnheader-header-value))) |
292 | ||
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)))) | |
eec82323 LMI |
305 | (when naked |
306 | (goto-char (point-min)) | |
307 | (delete-char 1))))) | |
308 | ||
309 | (defmacro nnheader-nov-skip-field () | |
310 | '(search-forward "\t" eol 'move)) | |
311 | ||
312 | (defmacro nnheader-nov-field () | |
313 | '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol))) | |
314 | ||
315 | (defmacro nnheader-nov-read-integer () | |
316 | '(prog1 | |
16409b0b | 317 | (if (eq (char-after) ?\t) |
eec82323 | 318 | 0 |
16409b0b GM |
319 | (let ((num (condition-case nil |
320 | (read (current-buffer)) | |
321 | (error nil)))) | |
eec82323 LMI |
322 | (if (numberp num) num 0))) |
323 | (or (eobp) (forward-char 1)))) | |
324 | ||
16409b0b GM |
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)) | |
334 | ||
335 | (defmacro nnheader-nov-read-message-id () | |
336 | '(let ((id (nnheader-nov-field))) | |
337 | (if (string-match "^<[^>]+>$" id) | |
338 | id | |
339 | (nnheader-generate-fake-message-id)))) | |
eec82323 LMI |
340 | |
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 | |
16409b0b | 348 | (nnheader-nov-read-message-id) ; id |
eec82323 LMI |
349 | (nnheader-nov-field) ; refs |
350 | (nnheader-nov-read-integer) ; chars | |
351 | (nnheader-nov-read-integer) ; lines | |
16409b0b | 352 | (if (eq (char-after) ?\n) |
eec82323 | 353 | nil |
8b93df01 DL |
354 | (if (looking-at "Xref: ") |
355 | (goto-char (match-end 0))) | |
356 | (nnheader-nov-field)) ; Xref | |
16409b0b | 357 | (nnheader-nov-parse-extra)))) ; extra |
eec82323 LMI |
358 | |
359 | (defun nnheader-insert-nov (header) | |
360 | (princ (mail-header-number header) (current-buffer)) | |
16409b0b GM |
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))) | |
391 | ||
392 | (defun nnheader-insert-header (header) | |
eec82323 | 393 | (insert |
16409b0b GM |
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: ") | |
eec82323 | 400 | (princ (or (mail-header-lines header) 0) (current-buffer)) |
16409b0b | 401 | (insert "\n\n")) |
eec82323 LMI |
402 | |
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 ".")) | |
412 | ||
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))) | |
422 | ||
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)) | |
6748645f LMI |
441 | (while (and (not (numberp (setq num (read cur)))) |
442 | (not (eobp))) | |
443 | (gnus-delete-line)) | |
444 | (cond ((> num article) | |
eec82323 LMI |
445 | (setq max (point))) |
446 | ((< num article) | |
447 | (setq min (point))) | |
448 | (t | |
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))) | |
470 | ||
471 | ;; Various cruft the backends and Gnus need to communicate. | |
472 | ||
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) | |
480 | ||
481 | (defvar nnheader-callback-function nil) | |
482 | ||
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) | |
23c6089e | 489 | (mm-enable-multibyte) |
eec82323 LMI |
490 | (erase-buffer) |
491 | (kill-all-local-variables) | |
492 | (setq case-fold-search t) ;Should ignore case. | |
493 | t)) | |
494 | ||
495 | ;;; Various functions the backends use. | |
496 | ||
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)) | |
508 | ||
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)))))) | |
526 | t)) | |
527 | ||
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)) | |
532 | nil | |
533 | (narrow-to-region (point-min) (1- (point))) | |
534 | (goto-char (point-min)) | |
16409b0b | 535 | (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n") |
eec82323 LMI |
536 | (goto-char (match-end 0))) |
537 | (prog1 | |
538 | (eobp) | |
539 | (widen)))) | |
540 | ||
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)) | |
16409b0b GM |
544 | ;; This is invalid, but not all articles have Message-IDs. |
545 | () | |
eec82323 LMI |
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)))))) | |
560 | ||
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"))))) | |
570 | ||
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))) | |
580 | ||
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)) | |
16409b0b | 584 | (buffer-disable-undo) |
eec82323 LMI |
585 | (unless noerase |
586 | (erase-buffer)) | |
587 | (current-buffer)) | |
588 | ||
16409b0b | 589 | (eval-when-compile (defvar jka-compr-compression-info-list)) |
eec82323 LMI |
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.") | |
598 | ||
599 | (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files) | |
600 | "Regexp that matches numerical file names.") | |
601 | ||
602 | (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files) | |
35ef97a5 | 603 | "Regexp that matches numerical full file names.") |
eec82323 LMI |
604 | |
605 | (defsubst nnheader-file-to-number (file) | |
16409b0b | 606 | "Take a FILE name and return the article number." |
6748645f | 607 | (if (string= nnheader-numerical-short-files "^[0-9]+$") |
eec82323 LMI |
608 | (string-to-int file) |
609 | (string-match nnheader-numerical-short-files file) | |
610 | (string-to-int (match-string 0 file)))) | |
611 | ||
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))) | |
622 | ||
623 | (defun nnheader-directory-articles (dir) | |
16409b0b | 624 | "Return a list of all article files in directory DIR." |
eec82323 LMI |
625 | (mapcar 'nnheader-file-to-number |
626 | (nnheader-directory-files-safe | |
627 | dir nil nnheader-numerical-short-files t))) | |
628 | ||
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))) | |
634 | ||
635 | (defun nnheader-fold-continuation-lines () | |
636 | "Fold continuation lines in the current buffer." | |
637 | (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " ")) | |
638 | ||
6748645f LMI |
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." | |
eec82323 LMI |
642 | (if (null nnheader-file-name-translation-alist) |
643 | ;; No translation is necessary. | |
644 | file | |
eec82323 LMI |
645 | (let* ((i 0) |
646 | trans leaf path len) | |
6748645f LMI |
647 | (if full |
648 | ;; Do complete translation. | |
649 | (setq leaf (copy-sequence file) | |
16409b0b GM |
650 | path "" |
651 | i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1))) | |
652 | 2 0)) | |
6748645f LMI |
653 | ;; We translate -- but only the file name. We leave the directory |
654 | ;; alone. | |
df359f6c | 655 | (if (and (featurep 'xemacs) |
c60ee5e7 | 656 | (memq system-type '(win32 w32 mswindows windows-nt cygwin))) |
df359f6c DL |
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" | |
c60ee5e7 | 661 | ;; |
df359f6c DL |
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. | |
6748645f LMI |
675 | (setq leaf (file-name-nondirectory file) |
676 | path (file-name-directory file)))) | |
eec82323 LMI |
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)))) | |
684 | ||
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) | |
693 | ||
694 | (defun nnheader-get-report (backend) | |
695 | "Get the most recent report from BACKEND." | |
696 | (condition-case () | |
6748645f | 697 | (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string" |
16409b0b | 698 | backend)))) |
6748645f | 699 | (error (nnheader-message 5 "")))) |
eec82323 LMI |
700 | |
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)) | |
711 | t)) | |
712 | ||
9e153fef DL |
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)))) | |
16409b0b GM |
728 | |
729 | (defun nnheader-replace-duplicate-chars-in-string (string from to) | |
eec82323 LMI |
730 | "Replace characters in STRING from FROM to TO." |
731 | (let ((string (substring string 0)) ;Copy string. | |
732 | (len (length string)) | |
16409b0b | 733 | (idx 0) prev i) |
eec82323 LMI |
734 | ;; Replace all occurrences of FROM with TO. |
735 | (while (< idx len) | |
16409b0b GM |
736 | (setq i (aref string idx)) |
737 | (when (and (eq prev from) (= i from)) | |
738 | (aset string (1- idx) to) | |
eec82323 | 739 | (aset string idx to)) |
16409b0b | 740 | (setq prev i) |
eec82323 LMI |
741 | (setq idx (1+ idx))) |
742 | string)) | |
743 | ||
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 | ?/ ?.)) | |
756 | ||
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))) | |
763 | ||
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))) | |
768 | ||
16409b0b | 769 | (defvar nnheader-pathname-coding-system 'binary |
35ef97a5 | 770 | "*Coding system for file names.") |
e0b8fdf5 | 771 | |
eec82323 | 772 | (defun nnheader-group-pathname (group dir &optional file) |
35ef97a5 | 773 | "Make file name for GROUP." |
eec82323 LMI |
774 | (concat |
775 | (let ((dir (file-name-as-directory (expand-file-name dir)))) | |
776 | ;; If this directory exists, we use it directly. | |
16409b0b GM |
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)))) | |
eec82323 LMI |
785 | (cond ((null file) "") |
786 | ((numberp file) (int-to-string file)) | |
787 | (t file)))) | |
788 | ||
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)))) | |
793 | ||
794 | (defun nnheader-concat (dir &rest files) | |
16409b0b | 795 | "Concat DIR as directory to FILES." |
eec82323 LMI |
796 | (apply 'concat (file-name-as-directory dir) files)) |
797 | ||
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)))) | |
804 | ||
805 | (defun nnheader-file-size (file) | |
806 | "Return the file size of FILE or 0." | |
807 | (or (nth 7 (file-attributes file)) 0)) | |
808 | ||
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)) | |
829 | ||
534aa266 DL |
830 | (eval-when-compile |
831 | (defvar ange-ftp-path-format) | |
832 | (defvar efs-path-regexp)) | |
eec82323 LMI |
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))))) | |
841 | ||
6748645f LMI |
842 | (defvar nnheader-file-coding-system 'raw-text |
843 | "Coding system used in file backends of Gnus.") | |
844 | ||
eec82323 LMI |
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." | |
16409b0b GM |
851 | (let ((coding-system-for-read nnheader-file-coding-system)) |
852 | (mm-insert-file-contents filename visit beg end replace))) | |
eec82323 LMI |
853 | |
854 | (defun nnheader-find-file-noselect (&rest args) | |
855 | (let ((format-alist nil) | |
16409b0b | 856 | (auto-mode-alist (mm-auto-mode-alist)) |
eec82323 LMI |
857 | (default-major-mode 'fundamental-mode) |
858 | (enable-local-variables nil) | |
e0b8fdf5 | 859 | (after-insert-file-functions nil) |
16409b0b | 860 | (enable-local-eval nil) |
6748645f | 861 | (find-file-hooks nil) |
e0b8fdf5 | 862 | (coding-system-for-read nnheader-file-coding-system)) |
eec82323 LMI |
863 | (apply 'find-file-noselect args))) |
864 | ||
eec82323 LMI |
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))) | |
874 | ||
6748645f LMI |
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))) | |
884 | ||
eec82323 LMI |
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))) | |
eec82323 LMI |
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))) | |
904 | ||
905 | (defun nnheader-replace-string (from to) | |
16409b0b | 906 | "Do a fast replacement of FROM to TO from point to `point-max'." |
eec82323 LMI |
907 | (nnheader-skeleton-replace from to)) |
908 | ||
909 | (defun nnheader-replace-regexp (from to) | |
16409b0b | 910 | "Do a fast regexp replacement of FROM to TO from point to `point-max'." |
eec82323 LMI |
911 | (nnheader-skeleton-replace from to t)) |
912 | ||
913 | (defun nnheader-strip-cr () | |
914 | "Strip all \r's from the current buffer." | |
915 | (nnheader-skeleton-replace "\r")) | |
916 | ||
16409b0b GM |
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) | |
eec82323 | 920 | |
df359f6c | 921 | (when (featurep 'xemacs) |
eec82323 LMI |
922 | (require 'nnheaderxm)) |
923 | ||
924 | (run-hooks 'nnheader-load-hook) | |
925 | ||
926 | (provide 'nnheader) | |
927 | ||
ab5796a9 | 928 | ;;; arch-tag: a9c4b7d9-52ae-4ec9-b196-dfd93124d202 |
eec82323 | 929 | ;;; nnheader.el ends here |