Trailing whitepace deleted.
[bpt/emacs.git] / lisp / gnus / mm-partial.el
CommitLineData
c113de23
GM
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
a1506d29 28(eval-when-compile
c113de23
GM
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 header)
40 (while (setq header (pop headers))
41 (unless (eq (aref header 0) art)
42 (mm-with-unibyte-buffer
a1506d29 43 (gnus-request-article-this-buffer (aref header 0)
c113de23
GM
44 gnus-newsgroup-name)
45 (when (search-forward id nil t)
46 (let ((nhandles (mm-dissect-buffer)) nid)
47 (if (consp (car nhandles))
48 (mm-destroy-parts nhandles)
a1506d29 49 (setq nid (cdr (assq 'id
c113de23
GM
50 (cdr (mm-handle-type nhandles)))))
51 (if (not (equal id nid))
52 (mm-destroy-parts nhandles)
53 (push nhandles phandles))))))))
54 phandles))
55
56;;;###autoload
57(defun mm-inline-partial (handle &optional no-display)
58 "Show the partial part of HANDLE.
a1506d29 59This function replaces the buffer of HANDLE with a buffer contains
c113de23
GM
60the entire message.
61If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
a1506d29 62 (let ((id (cdr (assq 'id (cdr (mm-handle-type handle)))))
c113de23
GM
63 phandles
64 (b (point)) (n 1) total
65 phandle nn ntotal
66 gnus-displaying-mime handles buffer)
67 (unless (mm-handle-cache handle)
68 (unless id
715a2ca2 69 (error "Can not find message/partial id"))
c113de23 70 (setq phandles
a1506d29 71 (sort (cons handle
c113de23 72 (mm-partial-find-parts
a1506d29 73 id
c113de23
GM
74 (save-excursion
75 (set-buffer gnus-summary-buffer)
76 (gnus-summary-article-number))))
77 #'(lambda (a b)
a1506d29
JB
78 (let ((anumber (string-to-number
79 (cdr (assq 'number
c113de23 80 (cdr (mm-handle-type a))))))
a1506d29
JB
81 (bnumber (string-to-number
82 (cdr (assq 'number
c113de23
GM
83 (cdr (mm-handle-type b)))))))
84 (< anumber bnumber)))))
85 (setq gnus-article-mime-handles
86 (append (if (listp (car gnus-article-mime-handles))
87 gnus-article-mime-handles
88 (list gnus-article-mime-handles))
89 phandles))
90 (save-excursion
5859a75c 91 (set-buffer (generate-new-buffer " *mm*"))
c113de23 92 (while (setq phandle (pop phandles))
a1506d29
JB
93 (setq nn (string-to-number
94 (cdr (assq 'number
c113de23 95 (cdr (mm-handle-type phandle))))))
a1506d29
JB
96 (setq ntotal (string-to-number
97 (cdr (assq 'total
c113de23
GM
98 (cdr (mm-handle-type phandle))))))
99 (if ntotal
100 (if total
a1506d29 101 (unless (eq total ntotal)
715a2ca2 102 (error "The numbers of total are different"))
c113de23
GM
103 (setq total ntotal)))
104 (unless (< nn n)
105 (unless (eq nn n)
106 (error "Missing part %d" n))
107 (mm-insert-part phandle)
108 (goto-char (point-max))
109 (when (not (eq 0 (skip-chars-backward "\r\n")))
110 ;; remove tail blank spaces except one
111 (if (looking-at "\r?\n")
112 (goto-char (match-end 0)))
113 (delete-region (point) (point-max)))
114 (setq n (+ n 1))))
115 (unless total
116 (error "Don't known the total number of"))
117 (if (<= n total)
118 (error "Missing part %d" n))
119 (kill-buffer (mm-handle-buffer handle))
120 (setcar handle (current-buffer))
121 (mm-handle-set-cache handle t)))
122 (unless no-display
123 (save-excursion
124 (save-restriction
125 (narrow-to-region b b)
126 (mm-insert-part handle)
127 (let (gnus-article-mime-handles)
128 (run-hooks 'gnus-article-decode-hook)
129 (gnus-article-prepare-display)
130 (setq handles gnus-article-mime-handles))
131 (when handles
132 ;; It is in article buffer.
133 (setq gnus-article-mime-handles
134 (nconc (if (listp (car gnus-article-mime-handles))
135 gnus-article-mime-handles
136 (list gnus-article-mime-handles))
a1506d29 137 (if (listp (car handles))
c113de23
GM
138 handles (list handles)))))
139 (mm-handle-set-undisplayer
140 handle
141 `(lambda ()
142 (let (buffer-read-only)
143 (condition-case nil
144 ;; This is only valid on XEmacs.
145 (mapcar (lambda (prop)
146 (remove-specifier
147 (face-property 'default prop) (current-buffer)))
148 '(background background-pixmap foreground))
149 (error nil))
150 (delete-region ,(point-min-marker) ,(point-max-marker))))))))))
151
715a2ca2 152;;; mm-partial.el ends here