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