Initial Commit
[packages] / xemacs-packages / gnus / lisp / mm-partial.el
1 ;;; mm-partial.el --- showing message/partial
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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 3, 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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
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
44           (gnus-request-article-this-buffer (aref header 0)
45                                             gnus-newsgroup-name)
46           (when (search-forward id nil t)
47             (let ((nhandles (mm-dissect-buffer
48                              nil gnus-article-loose-mime)) nid)
49               (if (consp (car nhandles))
50                   (mm-destroy-parts nhandles)
51                 (setq nid (cdr (assq 'id
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.
61 This function replaces the buffer of HANDLE with a buffer contains
62 the entire message.
63 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
64   (let ((id (cdr (assq 'id (cdr (mm-handle-type handle)))))
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
71         (error "Can not find message/partial id"))
72       (setq phandles
73             (sort (cons handle
74                         (mm-partial-find-parts
75                          id
76                          (save-excursion
77                            (set-buffer gnus-summary-buffer)
78                            (gnus-summary-article-number))))
79                   #'(lambda (a b)
80                       (let ((anumber (string-to-number
81                                       (cdr (assq 'number
82                                                  (cdr (mm-handle-type a))))))
83                             (bnumber (string-to-number
84                                       (cdr (assq 'number
85                                                  (cdr (mm-handle-type b)))))))
86                         (< anumber bnumber)))))
87       (setq gnus-article-mime-handles
88             (mm-merge-handles gnus-article-mime-handles phandles))
89       (save-excursion
90         (set-buffer (generate-new-buffer " *mm*"))
91         (while (setq phandle (pop phandles))
92           (setq nn (string-to-number
93                     (cdr (assq 'number
94                                (cdr (mm-handle-type phandle))))))
95           (setq ntotal (string-to-number
96                         (cdr (assq 'total
97                                    (cdr (mm-handle-type phandle))))))
98           (if ntotal
99               (if total
100                   (unless (eq total ntotal)
101                   (error "The numbers of total are different"))
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))
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")))
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
140                   (mm-merge-handles gnus-article-mime-handles handles)))
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
154 (provide 'mm-partial)
155
156 ;;; arch-tag: 460e7424-05f2-4a1d-a0f2-70ec081eff7d
157 ;;; mm-partial.el ends here