Don't warn the user about large files if they are unreadable
[emacs.git] / lisp / gnus / mm-partial.el
blob51dc8b89e3af89e884a18611cbf5526ae5a8752d
1 ;;; mm-partial.el --- showing message/partial
3 ;; Copyright (C) 2000-2018 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 <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'gnus-sum)
28 (require 'mm-util)
29 (require 'mm-decode)
31 (defun mm-partial-find-parts (id &optional art)
32 (let ((headers (with-current-buffer gnus-summary-buffer
33 gnus-newsgroup-headers))
34 phandles header)
35 (while (setq header (pop headers))
36 (unless (eq (aref header 0) art)
37 (mm-with-unibyte-buffer
38 (gnus-request-article-this-buffer (aref header 0)
39 gnus-newsgroup-name)
40 (when (search-forward id nil t)
41 (let ((nhandles (mm-dissect-buffer
42 nil gnus-article-loose-mime)) nid)
43 (if (consp (car nhandles))
44 (mm-destroy-parts nhandles)
45 (setq nid (cdr (assq 'id
46 (cdr (mm-handle-type nhandles)))))
47 (if (not (equal id nid))
48 (mm-destroy-parts nhandles)
49 (push nhandles phandles))))))))
50 phandles))
52 ;;;###autoload
53 (defun mm-inline-partial (handle &optional no-display)
54 "Show the partial part of HANDLE.
55 This function replaces the buffer of HANDLE with a buffer contains
56 the entire message.
57 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
58 (let ((id (cdr (assq 'id (cdr (mm-handle-type handle)))))
59 phandles
60 (b (point)) (n 1) total
61 phandle nn ntotal
62 gnus-displaying-mime handles buffer)
63 (unless (mm-handle-cache handle)
64 (unless id
65 (error "Can not find message/partial id"))
66 (setq phandles
67 (sort (cons handle
68 (mm-partial-find-parts
70 (with-current-buffer gnus-summary-buffer
71 (gnus-summary-article-number))))
72 #'(lambda (a b)
73 (let ((anumber (string-to-number
74 (cdr (assq 'number
75 (cdr (mm-handle-type a))))))
76 (bnumber (string-to-number
77 (cdr (assq 'number
78 (cdr (mm-handle-type b)))))))
79 (< anumber bnumber)))))
80 (setq gnus-article-mime-handles
81 (mm-merge-handles gnus-article-mime-handles phandles))
82 (with-current-buffer (generate-new-buffer " *mm*")
83 (while (setq phandle (pop phandles))
84 (setq nn (string-to-number
85 (cdr (assq 'number
86 (cdr (mm-handle-type phandle))))))
87 (setq ntotal (string-to-number
88 (cdr (assq 'total
89 (cdr (mm-handle-type phandle))))))
90 (if ntotal
91 (if total
92 (unless (eq total ntotal)
93 (error "The numbers of total are different"))
94 (setq total ntotal)))
95 (unless (< nn n)
96 (unless (eq nn n)
97 (error "Missing part %d" n))
98 (mm-insert-part phandle)
99 (goto-char (point-max))
100 (when (not (eq 0 (skip-chars-backward "\r\n")))
101 ;; remove tail blank spaces except one
102 (if (looking-at "\r?\n")
103 (goto-char (match-end 0)))
104 (delete-region (point) (point-max)))
105 (setq n (+ n 1))))
106 (unless total
107 (error "Don't known the total number of"))
108 (if (<= n total)
109 (error "Missing part %d" n))
110 (kill-buffer (mm-handle-buffer handle))
111 (goto-char (point-min))
112 (let ((point (if (search-forward "\n\n" nil t)
113 (1- (point))
114 (point-max))))
115 (goto-char (point-min))
116 (unless (re-search-forward "^mime-version:" point t)
117 (insert "MIME-Version: 1.0\n")))
118 (setcar handle (current-buffer))
119 (mm-handle-set-cache handle t)))
120 (unless no-display
121 (save-excursion
122 (save-restriction
123 (narrow-to-region b b)
124 (mm-insert-part handle)
125 (let (gnus-article-mime-handles)
126 (run-hooks 'gnus-article-decode-hook)
127 (gnus-article-prepare-display)
128 (setq handles gnus-article-mime-handles))
129 (when handles
130 ;; It is in article buffer.
131 (setq gnus-article-mime-handles
132 (mm-merge-handles gnus-article-mime-handles handles)))
133 (mm-handle-set-undisplayer
134 handle
135 `(lambda ()
136 (let (buffer-read-only)
137 (delete-region ,(point-min-marker) ,(point-max-marker))))))))))
139 (provide 'mm-partial)
141 ;;; mm-partial.el ends here