mm-partial.el 4.7 KB

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