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