Merge from origin/emacs-24
[emacs.git] / lisp / gnus / mm-partial.el
blobf9f217357a27f9398bc0af52a73862a3a5f1425f
1 ;;; mm-partial.el --- showing message/partial
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: message partial
8 ;; This file is part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
29 (require 'gnus-sum)
30 (require 'mm-util)
31 (require 'mm-decode)
33 (defun mm-partial-find-parts (id &optional art)
34 (let ((headers (with-current-buffer gnus-summary-buffer
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
40 (gnus-request-article-this-buffer (aref header 0)
41 gnus-newsgroup-name)
42 (when (search-forward id nil t)
43 (let ((nhandles (mm-dissect-buffer
44 nil gnus-article-loose-mime)) nid)
45 (if (consp (car nhandles))
46 (mm-destroy-parts nhandles)
47 (setq nid (cdr (assq 'id
48 (cdr (mm-handle-type nhandles)))))
49 (if (not (equal id nid))
50 (mm-destroy-parts nhandles)
51 (push nhandles phandles))))))))
52 phandles))
54 ;;;###autoload
55 (defun mm-inline-partial (handle &optional no-display)
56 "Show the partial part of HANDLE.
57 This function replaces the buffer of HANDLE with a buffer contains
58 the entire message.
59 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
60 (let ((id (cdr (assq 'id (cdr (mm-handle-type handle)))))
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
67 (error "Can not find message/partial id"))
68 (setq phandles
69 (sort (cons handle
70 (mm-partial-find-parts
72 (with-current-buffer gnus-summary-buffer
73 (gnus-summary-article-number))))
74 #'(lambda (a b)
75 (let ((anumber (string-to-number
76 (cdr (assq 'number
77 (cdr (mm-handle-type a))))))
78 (bnumber (string-to-number
79 (cdr (assq 'number
80 (cdr (mm-handle-type b)))))))
81 (< anumber bnumber)))))
82 (setq gnus-article-mime-handles
83 (mm-merge-handles gnus-article-mime-handles phandles))
84 (with-current-buffer (generate-new-buffer " *mm*")
85 (while (setq phandle (pop phandles))
86 (setq nn (string-to-number
87 (cdr (assq 'number
88 (cdr (mm-handle-type phandle))))))
89 (setq ntotal (string-to-number
90 (cdr (assq 'total
91 (cdr (mm-handle-type phandle))))))
92 (if ntotal
93 (if total
94 (unless (eq total ntotal)
95 (error "The numbers of total are different"))
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))
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")))
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 (mm-merge-handles gnus-article-mime-handles handles)))
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))))))))))
148 (provide 'mm-partial)
150 ;;; mm-partial.el ends here