Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / gnus / mm-partial.el
CommitLineData
c113de23 1;;; mm-partial.el --- showing message/partial
e84b4b86 2
acaf905b 3;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
c113de23
GM
4
5;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6;; Keywords: message partial
7
8;; This file is part of GNU Emacs.
9
5e809f55
GM
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 3 of the License, or
13;; (at your option) any later version.
c113de23 14
5e809f55
GM
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.
c113de23
GM
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c113de23
GM
22
23;;; Commentary:
24
25;;; Code:
26
23f87bed 27(eval-when-compile (require 'cl))
c113de23
GM
28
29(require 'gnus-sum)
30(require 'mm-util)
31(require 'mm-decode)
32
33(defun mm-partial-find-parts (id &optional art)
01c52d31 34 (let ((headers (with-current-buffer gnus-summary-buffer
c113de23
GM
35 gnus-newsgroup-headers))
36 phandles header)
37 (while (setq header (pop headers))
38 (unless (eq (aref header 0) art)
39 (mm-with-unibyte-buffer
a1506d29 40 (gnus-request-article-this-buffer (aref header 0)
c113de23
GM
41 gnus-newsgroup-name)
42 (when (search-forward id nil t)
23f87bed
MB
43 (let ((nhandles (mm-dissect-buffer
44 nil gnus-article-loose-mime)) nid)
c113de23
GM
45 (if (consp (car nhandles))
46 (mm-destroy-parts nhandles)
a1506d29 47 (setq nid (cdr (assq 'id
c113de23
GM
48 (cdr (mm-handle-type nhandles)))))
49 (if (not (equal id nid))
50 (mm-destroy-parts nhandles)
51 (push nhandles phandles))))))))
52 phandles))
53
54;;;###autoload
55(defun mm-inline-partial (handle &optional no-display)
56 "Show the partial part of HANDLE.
a1506d29 57This function replaces the buffer of HANDLE with a buffer contains
c113de23
GM
58the entire message.
59If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
a1506d29 60 (let ((id (cdr (assq 'id (cdr (mm-handle-type handle)))))
c113de23
GM
61 phandles
62 (b (point)) (n 1) total
63 phandle nn ntotal
64 gnus-displaying-mime handles buffer)
65 (unless (mm-handle-cache handle)
66 (unless id
715a2ca2 67 (error "Can not find message/partial id"))
c113de23 68 (setq phandles
a1506d29 69 (sort (cons handle
c113de23 70 (mm-partial-find-parts
a1506d29 71 id
20a673b2 72 (with-current-buffer gnus-summary-buffer
c113de23
GM
73 (gnus-summary-article-number))))
74 #'(lambda (a b)
a1506d29
JB
75 (let ((anumber (string-to-number
76 (cdr (assq 'number
c113de23 77 (cdr (mm-handle-type a))))))
a1506d29
JB
78 (bnumber (string-to-number
79 (cdr (assq 'number
c113de23
GM
80 (cdr (mm-handle-type b)))))))
81 (< anumber bnumber)))))
82 (setq gnus-article-mime-handles
23f87bed 83 (mm-merge-handles gnus-article-mime-handles phandles))
20a673b2 84 (with-current-buffer (generate-new-buffer " *mm*")
c113de23 85 (while (setq phandle (pop phandles))
a1506d29
JB
86 (setq nn (string-to-number
87 (cdr (assq 'number
c113de23 88 (cdr (mm-handle-type phandle))))))
a1506d29
JB
89 (setq ntotal (string-to-number
90 (cdr (assq 'total
c113de23
GM
91 (cdr (mm-handle-type phandle))))))
92 (if ntotal
93 (if total
a1506d29 94 (unless (eq total ntotal)
715a2ca2 95 (error "The numbers of total are different"))
c113de23
GM
96 (setq total ntotal)))
97 (unless (< nn n)
98 (unless (eq nn n)
99 (error "Missing part %d" n))
100 (mm-insert-part phandle)
101 (goto-char (point-max))
102 (when (not (eq 0 (skip-chars-backward "\r\n")))
103 ;; remove tail blank spaces except one
104 (if (looking-at "\r?\n")
105 (goto-char (match-end 0)))
106 (delete-region (point) (point-max)))
107 (setq n (+ n 1))))
108 (unless total
109 (error "Don't known the total number of"))
110 (if (<= n total)
111 (error "Missing part %d" n))
112 (kill-buffer (mm-handle-buffer handle))
23f87bed
MB
113 (goto-char (point-min))
114 (let ((point (if (search-forward "\n\n" nil t)
115 (1- (point))
116 (point-max))))
117 (goto-char (point-min))
118 (unless (re-search-forward "^mime-version:" point t)
119 (insert "MIME-Version: 1.0\n")))
c113de23
GM
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
23f87bed 134 (mm-merge-handles gnus-article-mime-handles handles)))
c113de23
GM
135 (mm-handle-set-undisplayer
136 handle
137 `(lambda ()
138 (let (buffer-read-only)
139 (condition-case nil
140 ;; This is only valid on XEmacs.
141 (mapcar (lambda (prop)
142 (remove-specifier
143 (face-property 'default prop) (current-buffer)))
144 '(background background-pixmap foreground))
145 (error nil))
146 (delete-region ,(point-min-marker) ,(point-max-marker))))))))))
147
23f87bed
MB
148(provide 'mm-partial)
149
715a2ca2 150;;; mm-partial.el ends here