Importing Gnus v5.8.6.
[elisp/gnus.git-] / lisp / mm-partial.el
1 ;;; mm-partial.el --- showing message/partial
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: message partial
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile 
29   (require 'cl))
30
31 (require 'gnus-sum)
32 (require 'mm-util)
33 (require 'mm-decode)
34
35 (defun mm-partial-find-parts (id &optional art)
36   (let ((headers (save-excursion
37                    (set-buffer gnus-summary-buffer)
38                    gnus-newsgroup-headers))
39         phandles handles  header)
40     (while (setq header (pop headers))
41       (unless (eq (aref header 0) art)
42         (mm-with-unibyte-buffer
43           (gnus-request-article-this-buffer (aref header 0) 
44                                             gnus-newsgroup-name)
45           (when (search-forward id nil t)
46             (let ((nhandles (mm-dissect-buffer)) nid)
47               (setq handles gnus-article-mime-handles)
48               (if (consp (car nhandles))
49                   (mm-destroy-parts nhandles)
50                 (setq nid (cdr (assq 'id 
51                                      (cdr (mm-handle-type nhandles)))))
52                 (if (not (equal id nid))
53                     (mm-destroy-parts nhandles)
54                   (push nhandles phandles))))))))
55     phandles))
56
57 ;;;###autoload
58 (defun mm-inline-partial (handle &optional no-display)
59   "Show the partial part of HANDLE.
60 This function replaces the buffer of HANDLE with a buffer contains 
61 the entire message.
62 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
63   (let ((id (cdr (assq 'id (cdr (mm-handle-type handle))))) 
64         phandles
65         (b (point)) (n 1) total
66         phandle nn ntotal
67         gnus-displaying-mime handles buffer)
68     (unless (mm-handle-cache handle)
69       (unless id
70         (error "Can not find message/partial id."))
71       (setq phandles
72             (sort (cons handle 
73                         (mm-partial-find-parts
74                          id 
75                          (save-excursion
76                            (set-buffer gnus-summary-buffer)
77                            (gnus-summary-article-number))))
78                   #'(lambda (a b)
79                       (let ((anumber (string-to-number 
80                                       (cdr (assq 'number 
81                                                  (cdr (mm-handle-type a))))))
82                             (bnumber (string-to-number 
83                                       (cdr (assq 'number 
84                                                  (cdr (mm-handle-type b)))))))
85                         (< anumber bnumber)))))
86       (setq gnus-article-mime-handles
87             (append (if (listp (car gnus-article-mime-handles))
88                         gnus-article-mime-handles
89                       (list gnus-article-mime-handles))
90                     phandles))
91       (save-excursion
92         (set-buffer (generate-new-buffer "*mm*"))
93         (while (setq phandle (pop phandles))
94           (setq nn (string-to-number 
95                     (cdr (assq 'number 
96                                (cdr (mm-handle-type phandle))))))
97           (setq ntotal (string-to-number 
98                         (cdr (assq 'total 
99                                    (cdr (mm-handle-type phandle))))))
100           (if ntotal
101               (if total
102                   (unless (eq total ntotal) 
103                   (error "The numbers of total are different."))
104                 (setq total ntotal)))
105           (unless (< nn n)
106             (unless (eq nn n)
107               (error "Missing part %d" n))
108             (mm-insert-part phandle)
109             (goto-char (point-max))
110             (when (not (eq 0 (skip-chars-backward "\r\n")))
111               ;; remove tail blank spaces except one
112               (if (looking-at "\r?\n")
113                   (goto-char (match-end 0)))
114               (delete-region (point) (point-max)))
115             (setq n (+ n 1))))
116         (unless total
117           (error "Don't known the total number of"))
118         (if (<= n total)
119             (error "Missing part %d" n))
120         (kill-buffer (mm-handle-buffer handle))
121         (setcar handle (current-buffer))
122         (mm-handle-set-cache handle t)))
123     (unless no-display
124       (save-excursion
125         (save-restriction
126           (narrow-to-region b b)
127           (mm-insert-part handle)
128           (let (gnus-article-mime-handles)
129             (run-hooks 'gnus-article-decode-hook)
130             (gnus-article-prepare-display)
131             (setq handles gnus-article-mime-handles))
132           (when handles
133             ;; It is in article buffer.
134             (setq gnus-article-mime-handles
135                   (nconc (if (listp (car gnus-article-mime-handles))
136                            gnus-article-mime-handles
137                            (list gnus-article-mime-handles))
138                          (if (listp (car handles)) 
139                              handles (list handles)))))
140           (mm-handle-set-undisplayer
141            handle
142            `(lambda ()
143               (let (buffer-read-only)
144                 (condition-case nil
145                     ;; This is only valid on XEmacs.
146                     (mapcar (lambda (prop)
147                             (remove-specifier
148                              (face-property 'default prop) (current-buffer)))
149                             '(background background-pixmap foreground))
150                   (error nil))
151                 (delete-region ,(point-min-marker) ,(point-max-marker))))))))))
152
153 ;; mm-partial.el ends here