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