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