(struct window): New member height_fixed_p.
[bpt/emacs.git] / lisp / nndoc.el
1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
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 ;;; Code:
28
29 (require 'nnheader)
30 (require 'message)
31 (require 'nnmail)
32 (require 'nnoo)
33 (eval-when-compile (require 'cl))
34
35 (nnoo-declare nndoc)
36
37 (defvoo nndoc-article-type 'guess
38 "*Type of the file.
39 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
40 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
41 `guess'.")
42
43 (defvoo nndoc-post-type 'mail
44 "*Whether the nndoc group is `mail' or `post'.")
45
46 (defvar nndoc-type-alist
47 `((mmdf
48 (article-begin . "^\^A\^A\^A\^A\n")
49 (body-end . "^\^A\^A\^A\^A\n"))
50 (news
51 (article-begin . "^Path:"))
52 (rnews
53 (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
54 (body-end-function . nndoc-rnews-body-end))
55 (mbox
56 (article-begin . "^From \\([^ \n]*\\(\\|\".*\"[^ \n]*\\)\\) ?\\([^ \n]*\\) *\\([^ ]*\\) *\\([0-9]*\\) *\\([0-9:]*\\) *\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?\\|[-+]?[0-9][0-9][0-9][0-9]\\|\\) * [0-9][0-9]\\([0-9]*\\) *\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?\\|[-+]?[0-9][0-9][0-9][0-9]\\|\\) *\\(remote from .*\\)?\n")
57 (article-begin-function . nndoc-mbox-article-begin)
58 (body-end-function . nndoc-mbox-body-end))
59 (babyl
60 (article-begin . "\^_\^L *\n")
61 (body-end . "\^_")
62 (body-begin-function . nndoc-babyl-body-begin)
63 (head-begin-function . nndoc-babyl-head-begin))
64 (forward
65 (article-begin . "^-+ Start of forwarded message -+\n+")
66 (body-end . "^-+ End of forwarded message -+$")
67 (prepare-body . nndoc-unquote-dashes))
68 (clari-briefs
69 (article-begin . "^ \\*")
70 (body-end . "^\t------*[ \t]^*\n^ \\*")
71 (body-begin . "^\t")
72 (head-end . "^\t")
73 (generate-head . nndoc-generate-clari-briefs-head)
74 (article-transform . nndoc-transform-clari-briefs))
75 (slack-digest
76 (article-begin . "^------------------------------*[\n \t]+")
77 (head-end . "^ ?$")
78 (body-end-function . nndoc-digest-body-end)
79 (body-begin . "^ ?$")
80 (file-end . "^End of")
81 (prepare-body . nndoc-unquote-dashes))
82 (mime-digest
83 (article-begin . "")
84 (head-end . "^ ?$")
85 (body-end . "")
86 (file-end . ""))
87 (standard-digest
88 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
89 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n+"))
90 (prepare-body . nndoc-unquote-dashes)
91 (body-end-function . nndoc-digest-body-end)
92 (head-end . "^ ?$")
93 (body-begin . "^ ?\n")
94 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
95 (guess
96 (guess . nndoc-guess-type))
97 (digest
98 (guess . nndoc-guess-digest-type))
99 ))
100
101 \f
102
103 (defvoo nndoc-file-begin nil)
104 (defvoo nndoc-first-article nil)
105 (defvoo nndoc-article-end nil)
106 (defvoo nndoc-article-begin nil)
107 (defvoo nndoc-article-begin-function nil)
108 (defvoo nndoc-head-begin nil)
109 (defvoo nndoc-head-end nil)
110 (defvoo nndoc-file-end nil)
111 (defvoo nndoc-body-begin nil)
112 (defvoo nndoc-body-end-function nil)
113 (defvoo nndoc-body-begin-function nil)
114 (defvoo nndoc-head-begin-function nil)
115 (defvoo nndoc-body-end nil)
116 (defvoo nndoc-dissection-alist nil)
117 (defvoo nndoc-prepare-body nil)
118 (defvoo nndoc-generate-head nil)
119 (defvoo nndoc-article-transform nil)
120
121 (defvoo nndoc-status-string "")
122 (defvoo nndoc-group-alist nil)
123 (defvoo nndoc-current-buffer nil
124 "Current nndoc news buffer.")
125 (defvoo nndoc-address nil)
126
127 (defconst nndoc-version "nndoc 1.0"
128 "nndoc version.")
129
130 \f
131
132 ;;; Interface functions
133
134 (nnoo-define-basics nndoc)
135
136 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
137 (when (nndoc-possibly-change-buffer newsgroup server)
138 (save-excursion
139 (set-buffer nntp-server-buffer)
140 (erase-buffer)
141 (let (article entry)
142 (if (stringp (car articles))
143 'headers
144 (while articles
145 (when (setq entry (cdr (assq (setq article (pop articles))
146 nndoc-dissection-alist)))
147 (insert (format "221 %d Article retrieved.\n" article))
148 (if nndoc-generate-head
149 (funcall nndoc-generate-head article)
150 (insert-buffer-substring
151 nndoc-current-buffer (car entry) (nth 1 entry)))
152 (goto-char (point-max))
153 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
154 (insert (format "Lines: %d\n" (nth 4 entry)))
155 (insert ".\n")))
156
157 (nnheader-fold-continuation-lines)
158 'headers)))))
159
160 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
161 (nndoc-possibly-change-buffer newsgroup server)
162 (save-excursion
163 (let ((buffer (or buffer nntp-server-buffer))
164 (entry (cdr (assq article nndoc-dissection-alist)))
165 beg)
166 (set-buffer buffer)
167 (erase-buffer)
168 (if (stringp article)
169 nil
170 (insert-buffer-substring
171 nndoc-current-buffer (car entry) (nth 1 entry))
172 (insert "\n")
173 (setq beg (point))
174 (insert-buffer-substring
175 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
176 (goto-char beg)
177 (when nndoc-prepare-body
178 (funcall nndoc-prepare-body))
179 (when nndoc-article-transform
180 (funcall nndoc-article-transform article))
181 t))))
182
183 (deffoo nndoc-request-group (group &optional server dont-check)
184 "Select news GROUP."
185 (let (number)
186 (cond
187 ((not (nndoc-possibly-change-buffer group server))
188 (nnheader-report 'nndoc "No such file or buffer: %s"
189 nndoc-address))
190 (dont-check
191 (nnheader-report 'nndoc "Selected group %s" group)
192 t)
193 ((zerop (setq number (length nndoc-dissection-alist)))
194 (nndoc-close-group group)
195 (nnheader-report 'nndoc "No articles in group %s" group))
196 (t
197 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
198
199 (deffoo nndoc-request-type (group &optional article)
200 (cond ((not article) 'unknown)
201 (nndoc-post-type nndoc-post-type)
202 (t 'unknown)))
203
204 (deffoo nndoc-close-group (group &optional server)
205 (nndoc-possibly-change-buffer group server)
206 (and nndoc-current-buffer
207 (buffer-name nndoc-current-buffer)
208 (kill-buffer nndoc-current-buffer))
209 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
210 nndoc-group-alist))
211 (setq nndoc-current-buffer nil)
212 (nnoo-close-server 'nndoc server)
213 (setq nndoc-dissection-alist nil)
214 t)
215
216 (deffoo nndoc-request-list (&optional server)
217 nil)
218
219 (deffoo nndoc-request-newgroups (date &optional server)
220 nil)
221
222 (deffoo nndoc-request-list-newsgroups (&optional server)
223 nil)
224
225 \f
226 ;;; Internal functions.
227
228 (defun nndoc-possibly-change-buffer (group source)
229 (let (buf)
230 (cond
231 ;; The current buffer is this group's buffer.
232 ((and nndoc-current-buffer
233 (buffer-name nndoc-current-buffer)
234 (eq nndoc-current-buffer
235 (setq buf (cdr (assoc group nndoc-group-alist))))))
236 ;; We change buffers by taking an old from the group alist.
237 ;; `source' is either a string (a file name) or a buffer object.
238 (buf
239 (setq nndoc-current-buffer buf))
240 ;; It's a totally new group.
241 ((or (and (bufferp nndoc-address)
242 (buffer-name nndoc-address))
243 (and (stringp nndoc-address)
244 (file-exists-p nndoc-address)
245 (not (file-directory-p nndoc-address))))
246 (push (cons group (setq nndoc-current-buffer
247 (get-buffer-create
248 (concat " *nndoc " group "*"))))
249 nndoc-group-alist)
250 (setq nndoc-dissection-alist nil)
251 (save-excursion
252 (set-buffer nndoc-current-buffer)
253 (buffer-disable-undo (current-buffer))
254 (erase-buffer)
255 (if (stringp nndoc-address)
256 (insert-file-contents nndoc-address)
257 (insert-buffer-substring nndoc-address)))))
258 ;; Initialize the nndoc structures according to this new document.
259 (when (and nndoc-current-buffer
260 (not nndoc-dissection-alist))
261 (save-excursion
262 (set-buffer nndoc-current-buffer)
263 (nndoc-set-delims)
264 (nndoc-dissect-buffer)))
265 (unless nndoc-current-buffer
266 (nndoc-close-server))
267 ;; Return whether we managed to select a file.
268 nndoc-current-buffer))
269
270 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
271 (defun nndoc-guess-digest-type ()
272 "Guess what digest type the current document is."
273 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
274 boundary-id b-delimiter entry)
275 (goto-char (point-min))
276 (cond
277 ;; MIME digest.
278 ((and
279 (re-search-forward
280 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
281 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
282 nil t)
283 (match-beginning 1))
284 (setq boundary-id (match-string 1)
285 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
286 (setq entry (assq 'mime-digest nndoc-type-alist))
287 (setcdr entry
288 (list
289 (cons 'head-end "^ ?$")
290 (cons 'body-begin "^ ?\n")
291 (cons 'article-begin b-delimiter)
292 (cons 'body-end-function 'nndoc-digest-body-end)
293 ; (cons 'body-end
294 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
295 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
296 'mime-digest)
297 ;; Standard digest.
298 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
299 (re-search-forward
300 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
301 'standard-digest)
302 ;; Stupid digest.
303 (t
304 'slack-digest))))
305
306 (defun nndoc-guess-type ()
307 "Guess what document type is in the current buffer."
308 (goto-char (point-min))
309 (cond
310 ((looking-at message-unix-mail-delimiter)
311 'mbox)
312 ((looking-at "\^A\^A\^A\^A$")
313 'mmdf)
314 ((looking-at "^Path:.*\n")
315 'news)
316 ((looking-at "#! *rnews")
317 'rnews)
318 ((re-search-forward "\^_\^L *\n" nil t)
319 'babyl)
320 ((save-excursion
321 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
322 (not (re-search-forward "^Subject:.*digest" nil t))))
323 'forward)
324 ((let ((case-fold-search nil))
325 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
326 'clari-briefs)
327 (t
328 'digest)))
329
330 (defun nndoc-set-delims ()
331 "Set the nndoc delimiter variables according to the type of the document."
332 (let ((vars '(nndoc-file-begin
333 nndoc-first-article
334 nndoc-article-end nndoc-head-begin nndoc-head-end
335 nndoc-file-end nndoc-article-begin
336 nndoc-body-begin nndoc-body-end-function nndoc-body-end
337 nndoc-prepare-body nndoc-article-transform
338 nndoc-generate-head nndoc-body-begin-function
339 nndoc-head-begin-function nndoc-article-begin-function)))
340 (while vars
341 (set (pop vars) nil)))
342 (let* (defs guess)
343 ;; Guess away until we find the real file type.
344 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
345 guess (assq 'guess defs))
346 (setq nndoc-article-type (funcall (cdr guess))))
347 ;; Set the nndoc variables.
348 (while defs
349 (set (intern (format "nndoc-%s" (caar defs)))
350 (cdr (pop defs))))))
351
352 (defun nndoc-search (regexp)
353 (prog1
354 (re-search-forward regexp nil t)
355 (beginning-of-line)))
356
357 (defun nndoc-dissect-buffer ()
358 "Go through the document and partition it into heads/bodies/articles."
359 (let ((i 0)
360 (first t)
361 head-begin head-end body-begin body-end)
362 (setq nndoc-dissection-alist nil)
363 (save-excursion
364 (set-buffer nndoc-current-buffer)
365 (goto-char (point-min))
366 ;; Find the beginning of the file.
367 (when nndoc-file-begin
368 (nndoc-search nndoc-file-begin))
369 ;; Go through the file.
370 (while (if (and first nndoc-first-article)
371 (nndoc-search nndoc-first-article)
372 (if nndoc-article-begin-function
373 (funcall nndoc-article-begin-function)
374 (nndoc-search nndoc-article-begin)))
375 (setq first nil)
376 (cond (nndoc-head-begin-function
377 (funcall nndoc-head-begin-function))
378 (nndoc-head-begin
379 (nndoc-search nndoc-head-begin)))
380 (if (and nndoc-file-end
381 (looking-at nndoc-file-end))
382 (goto-char (point-max))
383 (setq head-begin (point))
384 (nndoc-search (or nndoc-head-end "^$"))
385 (setq head-end (point))
386 (if nndoc-body-begin-function
387 (funcall nndoc-body-begin-function)
388 (nndoc-search (or nndoc-body-begin "^\n")))
389 (setq body-begin (point))
390 (or (and nndoc-body-end-function
391 (funcall nndoc-body-end-function))
392 (and nndoc-body-end
393 (nndoc-search nndoc-body-end))
394 (if nndoc-article-begin-function
395 (funcall nndoc-article-begin-function)
396 (nndoc-search nndoc-article-begin))
397 (progn
398 (goto-char (point-max))
399 (when nndoc-file-end
400 (and (re-search-backward nndoc-file-end nil t)
401 (beginning-of-line)))))
402 (setq body-end (point))
403 (push (list (incf i) head-begin head-end body-begin body-end
404 (count-lines body-begin body-end))
405 nndoc-dissection-alist))))))
406
407 (defun nndoc-unquote-dashes ()
408 "Unquote quoted non-separators in digests."
409 (while (re-search-forward "^- -"nil t)
410 (replace-match "-" t t)))
411
412 (defun nndoc-digest-body-end ()
413 (and (re-search-forward nndoc-article-begin nil t)
414 (goto-char (match-beginning 0))))
415
416 (defun nndoc-mbox-article-begin ()
417 (when (re-search-forward nndoc-article-begin nil t)
418 (goto-char (match-beginning 0))))
419
420 (defun nndoc-mbox-body-end ()
421 (let ((beg (point))
422 len end)
423 (when
424 (save-excursion
425 (and (re-search-backward nndoc-article-begin nil t)
426 (setq end (point))
427 (search-forward "\n\n" beg t)
428 (re-search-backward
429 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
430 (setq len (string-to-int (match-string 1)))
431 (search-forward "\n\n" beg t)
432 (or (= (setq len (+ (point) len)) (point-max))
433 (and (< len (point-max))
434 (goto-char len)
435 (looking-at nndoc-article-begin)))))
436 (goto-char len))))
437
438 (defun nndoc-rnews-body-end ()
439 (and (re-search-backward nndoc-article-begin nil t)
440 (forward-line 1)
441 (goto-char (+ (point) (string-to-int (match-string 1))))))
442
443 (defun nndoc-transform-clari-briefs (article)
444 (goto-char (point-min))
445 (when (looking-at " *\\*\\(.*\\)\n")
446 (replace-match "" t t))
447 (nndoc-generate-clari-briefs-head article))
448
449 (defun nndoc-generate-clari-briefs-head (article)
450 (let ((entry (cdr (assq article nndoc-dissection-alist)))
451 subject from)
452 (save-excursion
453 (set-buffer nndoc-current-buffer)
454 (save-restriction
455 (narrow-to-region (car entry) (nth 3 entry))
456 (goto-char (point-min))
457 (when (looking-at " *\\*\\(.*\\)$")
458 (setq subject (match-string 1))
459 (when (string-match "[ \t]+$" subject)
460 (setq subject (substring subject 0 (match-beginning 0)))))
461 (when
462 (let ((case-fold-search nil))
463 (re-search-forward
464 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
465 (setq from (match-string 1)))))
466 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
467 "\nSubject: " (or subject "(no subject)") "\n")))
468
469 (defun nndoc-babyl-body-begin ()
470 (re-search-forward "^\n" nil t)
471 (when (looking-at "\*\*\* EOOH \*\*\*")
472 (re-search-forward "^\n" nil t)))
473
474 (defun nndoc-babyl-head-begin ()
475 (when (re-search-forward "^[0-9].*\n" nil t)
476 (when (looking-at "\*\*\* EOOH \*\*\*")
477 (forward-line 1))
478 t))
479
480 (provide 'nndoc)
481
482 ;;; nndoc.el ends here