1 ;;; mime-play.el --- decoder for mime-view.el
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/9/26 (separated from tm-view.el)
7 ;; Renamed: 1997/2/21 from tm-play.el
8 ;; Version: $Id: mime-play.el,v 0.9 1997-02-28 04:56:22 tmorioka Exp $
9 ;; Keywords: MIME, multimedia, mail, news
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
37 (defvar mime-preview/after-decoded-position nil)
39 (defun mime-preview/decode-content ()
41 (let ((pc (mime-preview/point-pcinfo (point))))
43 (let ((the-buf (current-buffer)))
44 (setq mime-preview/after-decoded-position (point))
45 (set-buffer (mime::preview-content-info/buffer pc))
46 (mime-article/decode-content
47 (mime::preview-content-info/content-info pc))
48 (if (eq (current-buffer)
49 (mime::preview-content-info/buffer pc))
52 (goto-char mime-preview/after-decoded-position)
56 (defun mime-article/decode-content (cinfo)
57 (let ((beg (mime::content-info/point-min cinfo))
58 (end (mime::content-info/point-max cinfo))
59 (ctype (or (mime::content-info/type cinfo) "text/plain"))
60 (params (mime::content-info/parameters cinfo))
61 (encoding (mime::content-info/encoding cinfo))
64 (if (< beg (point-min))
65 (setq beg (point-min))
67 (if (< (point-max) end)
68 (setq end (point-max))
71 (setq cal (list* (cons 'type ctype)
72 (cons 'encoding encoding)
73 (cons 'major-mode major-mode)
75 (if mime-view-decoding-mode
77 (cons 'mode mime-view-decoding-mode)
80 (setq ret (mime/get-content-decoding-alist cal))
81 (setq method (cdr (assq 'method ret)))
82 (cond ((and (symbolp method)
84 (funcall method beg end ret)
86 ((and (listp method)(stringp (car method)))
87 (mime-article/start-external-method-region beg end ret)
90 (mime-article/show-output-buffer
91 "No method are specified for %s\n" ctype)
100 (defun mime/get-content-decoding-alist (al)
101 (get-unified-alist mime/content-decoding-condition al)
105 ;;; @ external decoder
108 (defun mime-article/start-external-method-region (beg end cal)
111 (narrow-to-region beg end)
113 (let ((method (cdr (assoc 'method cal)))
114 (name (mime-article/get-filename cal))
117 (let ((file (make-temp-name
118 (expand-file-name "TM" mime/tmp-dir)))
123 (if (re-search-forward "^$" nil t)
129 (write-region b end file)
130 (message "External method is starting...")
132 'name (replace-as-filename name) cal))
133 (setq cal (put-alist 'file file cal))
136 mime/output-buffer-name (car method)
138 (mime-article/make-method-args cal
141 (apply (function start-process) args)
142 (mime-article/show-output-buffer)
146 (defun mime-article/make-method-args (cal format)
151 (let* ((item (eval arg))
152 (ret (cdr (assoc item cal)))
156 (if (eq item 'encoding)
163 (defun mime-article/show-output-buffer (&rest forms)
164 (get-buffer-create mime/output-buffer-name)
165 (let ((the-win (selected-window))
166 (win (get-buffer-window mime/output-buffer-name))
169 (if (and mime/output-buffer-window-is-shared-with-bbdb
170 (boundp 'bbdb-buffer-name)
171 (setq win (get-buffer-window bbdb-buffer-name))
173 (set-window-buffer win mime/output-buffer-name)
174 (select-window (get-buffer-window mime::article/preview-buffer))
175 (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
176 (set-window-buffer win mime/output-buffer-name)
179 (goto-char (point-max))
181 (insert (apply (function format) forms))
183 (select-window the-win)
190 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
192 (defvar mime-view-file-name-regexp-1
193 (concat mime-view-file-name-char-regexp "+\\."
194 mime-view-file-name-char-regexp "+"))
196 (defvar mime-view-file-name-regexp-2
197 (concat (regexp-* mime-view-file-name-char-regexp)
198 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
200 (defun mime-article/get-original-filename (param &optional encoding)
201 (or (mime-article/get-uu-filename param encoding)
203 (or (if (or (and (setq ret (mime/Content-Disposition))
204 (setq ret (assoc "filename" (cdr ret)))
206 (setq ret (assoc "name" param))
207 (setq ret (assoc "x-name" param))
209 (std11-strip-quoted-string (cdr ret))
212 (std11-find-field-body '("Content-Description"
214 (if (or (string-match mime-view-file-name-regexp-1 ret)
215 (string-match mime-view-file-name-regexp-2 ret))
216 (substring ret (match-beginning 0)(match-end 0))
221 (defun mime-article/get-filename (param)
222 (replace-as-filename (mime-article/get-original-filename param))
226 ;;; @ mail/news message
229 (defun mime-view-quitting-method-for-mime/show-message-mode ()
230 (let ((mother mime::preview/mother-buffer)
231 (win-conf mime::preview/original-window-configuration)
234 (mime::preview-content-info/buffer (car mime::preview/content-list)))
235 (mime-view-kill-buffer)
236 (set-window-configuration win-conf)
237 (pop-to-buffer mother)
238 ;;(goto-char (point-min))
239 ;;(mime-view-up-content)
242 (defun mime-article/view-message/rfc822 (beg end cal)
243 (let* ((cnum (mime-article/point-content-number beg))
244 (cur-buf (current-buffer))
245 (new-name (format "%s-%s" (buffer-name) cnum))
246 (mother mime::article/preview-buffer)
248 (or (cdr (assq major-mode mime-text-decoder-alist))
249 'mime-view-default-code-convert-region))
251 (setq str (buffer-substring beg end))
252 (switch-to-buffer new-name)
255 (goto-char (point-min))
256 (if (re-search-forward "^\n" nil t)
257 (delete-region (point-min) (match-end 0))
259 (setq major-mode 'mime/show-message-mode)
260 (setq mime::article/code-converter code-converter)
261 (mime-view-mode mother)
265 ;;; @ message/partial
268 (defvar mime-article/coding-system-alist
269 (list '(mh-show-mode . no-conversion)
270 (cons t (mime-charset-to-coding-system default-mime-charset))
273 (cond (running-mule-merged-emacs
274 (defun mime-article::write-region (start end file)
275 (let ((coding-system-for-write
277 (or (assq major-mode mime-article/coding-system-alist)
278 (assq t mime-article/coding-system-alist)
280 (write-region start end file)
284 running-xemacs-with-mule)
285 (defun mime-article::write-region (start end file)
286 (let ((file-coding-system
288 (or (assq major-mode mime-article/coding-system-alist)
289 (assq t mime-article/coding-system-alist)
291 (write-region start end file)
295 (defun mime-article::write-region (start end file)
296 (let ((kanji-fileio-code
298 (or (assq major-mode mime-article/kanji-code-alist)
299 (assq t mime-article/kanji-code-alist)
301 (write-region start end file)
305 (defalias 'mime-article::write-region 'write-region)
308 (defun mime-article/decode-message/partial (beg end cal)
310 (let* ((root-dir (expand-file-name
311 (concat "m-prts-" (user-login-name)) mime/tmp-dir))
312 (id (cdr (assoc "id" cal)))
313 (number (cdr (assoc "number" cal)))
314 (total (cdr (assoc "total" cal)))
316 (mother mime::article/preview-buffer)
318 (or (file-exists-p root-dir)
319 (make-directory root-dir)
321 (setq id (replace-as-filename id))
322 (setq root-dir (concat root-dir "/" id))
323 (or (file-exists-p root-dir)
324 (make-directory root-dir)
326 (setq file (concat root-dir "/FULL"))
327 (if (file-exists-p file)
328 (let ((full-buf (get-buffer-create "FULL"))
329 (pwin (or (get-buffer-window mother)
330 (get-largest-window)))
332 (save-window-excursion
333 (set-buffer full-buf)
335 (as-binary-input-file (insert-file-contents file))
336 (setq major-mode 'mime/show-message-mode)
337 (mime-view-mode mother)
339 (set-window-buffer pwin
341 (set-buffer full-buf)
342 mime::article/preview-buffer))
345 (re-search-forward "^$")
346 (goto-char (1+ (match-end 0)))
347 (setq file (concat root-dir "/" number))
348 (mime-article::write-region (point) (point-max) file)
349 (let ((total-file (concat root-dir "/CT")))
353 (or (file-exists-p total-file)
356 (get-buffer-create mime/temp-buffer-name))
359 (write-file total-file)
360 (kill-buffer (current-buffer))
362 (string-to-number total)
364 (and (file-exists-p total-file)
366 (set-buffer (find-file-noselect total-file))
368 (and (re-search-forward "[0-9]+" nil t)
370 (buffer-substring (match-beginning 0)
373 (kill-buffer (current-buffer))
376 (if (and total (> total 0))
379 (set-buffer (get-buffer-create mime/temp-buffer-name))
380 (let ((full-buf (current-buffer)))
384 (setq file (concat root-dir "/" (int-to-string i)))
385 (or (file-exists-p file)
388 (as-binary-input-file (insert-file-contents file))
389 (goto-char (point-max))
392 (as-binary-output-file (write-file (concat root-dir "/FULL")))
395 (let ((file (format "%s/%d" root-dir i)))
396 (and (file-exists-p file)
401 (let ((file (expand-file-name "CT" root-dir)))
402 (and (file-exists-p file)
405 (save-window-excursion
406 (setq major-mode 'mime/show-message-mode)
407 (mime-view-mode mother)
409 (let ((pwin (or (get-buffer-window mother)
412 (pbuf (save-excursion
413 (set-buffer full-buf)
414 mime::article/preview-buffer)))
415 (set-window-buffer pwin pbuf)
426 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
427 (define-key mime-view-text/plain-mode-map
428 "q" (function mime-view-text/plain-exit))
430 (defun mime-view-text/plain-mode ()
431 "\\{mime-view-text/plain-mode-map}"
432 (setq buffer-read-only t)
433 (setq major-mode 'mime-view-text/plain-mode)
434 (setq mode-name "MIME-View text/plain")
435 (use-local-map mime-view-text/plain-mode-map)
438 (defun mime-view-text/plain-exit ()
440 (kill-buffer (current-buffer))
443 (defun mime-article/decode-caesar (beg end cal)
444 (let* ((cnum (mime-article/point-content-number beg))
445 (cur-buf (current-buffer))
446 (new-name (format "%s-%s" (buffer-name) cnum))
447 (mother mime::article/preview-buffer)
448 (charset (cdr (assoc "charset" cal)))
449 (encoding (cdr (assq 'encoding cal)))
452 (setq str (buffer-substring beg end))
453 (let ((pwin (or (get-buffer-window mother)
454 (get-largest-window)))
455 (buf (get-buffer-create new-name))
457 (set-window-buffer pwin buf)
461 (setq buffer-read-only nil)
464 (goto-char (point-min))
465 (if (re-search-forward "^\n" nil t)
466 (delete-region (point-min) (match-end 0))
468 (let ((m (cdr (or (assq mode mime-text-decoder-alist)
469 (assq t mime-text-decoder-alist)))))
471 (funcall m charset encoding)
474 (set-mark (point-min))
475 (goto-char (point-max))
478 (set-buffer-modified-p nil)
479 (mime-view-text/plain-mode)
488 ;;; mime-play.el ends here