(mime-display-caesar): Use `view-buffer' instead of `view-mode-enter'.
[elisp/semi.git] / mime-play.el
1 ;;; mime-play.el --- decoder for mime-view.el
2
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
4
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.39 1997-05-07 10:01:07 morioka Exp $
9 ;; Keywords: MIME, multimedia, mail, news
10
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
12
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.
17
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.
22
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.
27
28 ;;; Code:
29
30 (require 'mime-view)
31 (require 'alist)
32 (require 'filename)
33
34   
35 ;;; @ content decoder
36 ;;;
37
38 (defvar mime-preview/after-decoded-position nil)
39
40 (defun mime-view-play-current-entity (&optional mode)
41   "Play current entity.
42 It decodes current entity to call internal or external method.  The
43 method is selected from variable `mime/content-decoding-condition'.
44 If MODE is specified, play as it.  Default MODE is \"play\"."
45   (interactive)
46   (or mode
47       (setq mode "play")
48       )
49   (let ((cinfo (get-text-property (point) 'mime-view-cinfo)))
50     (if cinfo
51         (let ((the-buf (current-buffer))
52               (raw-buffer (get-text-property (point) 'mime-view-raw-buffer))
53               )
54           (setq mime-preview/after-decoded-position (point))
55           (set-buffer raw-buffer)
56           (mime-article/decode-content cinfo mode)
57           (if (eq (current-buffer) raw-buffer)
58               (progn
59                 (set-buffer the-buf)
60                 (goto-char mime-preview/after-decoded-position)
61                 ))
62           ))))
63
64 (defun mime-article/decode-content (cinfo &optional mode)
65   (let ((beg (mime::content-info/point-min cinfo))
66         (end (mime::content-info/point-max cinfo))
67         (ctype (or (mime::content-info/type cinfo) "text/plain"))
68         (params (mime::content-info/parameters cinfo))
69         (encoding (mime::content-info/encoding cinfo))
70         )
71     ;; Check for VM
72     (if (< beg (point-min))
73         (setq beg (point-min))
74       )
75     (if (< (point-max) end)
76         (setq end (point-max))
77       )
78     (let (method cal ret)
79       (setq cal (list* (cons 'type ctype)
80                        (cons 'encoding encoding)
81                        (cons 'major-mode major-mode)
82                        params))
83       (if mode
84           (setq cal (cons (cons 'mode mode) cal))
85         )
86       (setq ret (mime/get-content-decoding-alist cal))
87       (setq method (cdr (assq 'method ret)))
88       (cond ((and (symbolp method)
89                   (fboundp method))
90              (funcall method beg end ret)
91              )
92             ((and (listp method)(stringp (car method)))
93              (mime-article/start-external-method-region beg end ret)
94              )
95             (t
96              (mime-show-echo-buffer
97               "No method are specified for %s\n" ctype)
98              ))
99       )
100     ))
101
102
103 ;;; @ method selector
104 ;;;
105
106 (defun mime/get-content-decoding-alist (al)
107   (get-unified-alist mime/content-decoding-condition al)
108   )
109
110
111 ;;; @ external decoder
112 ;;;
113
114 (defun mime-article/start-external-method-region (beg end cal)
115   (save-excursion
116     (save-restriction
117       (narrow-to-region beg end)
118       (goto-char beg)
119       (let ((method (cdr (assoc 'method cal)))
120             (name (mime-article/get-filename cal))
121             )
122         (if method
123             (let ((file (make-temp-name
124                          (expand-file-name "TM" mime-temp-directory)))
125                   b args)
126               (if (nth 1 method)
127                   (setq b beg)
128                 (setq b
129                       (if (re-search-forward "^$" nil t)
130                           (1+ (match-end 0))
131                         (point-min)
132                         ))
133                 )
134               (goto-char b)
135               (write-region b end file)
136               (message "External method is starting...")
137               (setq cal (put-alist
138                          'name (replace-as-filename name) cal))
139               (setq cal (put-alist 'file file cal))
140               (setq args (nconc
141                           (list (car method)
142                                 mime-echo-buffer-name (car method)
143                                 )
144                           (mime-article/make-method-args cal
145                                                          (cdr (cdr method)))
146                           ))
147               (apply (function start-process) args)
148               (mime-show-echo-buffer)
149               ))
150         ))))
151
152 (defun mime-article/make-method-args (cal format)
153   (mapcar (function
154            (lambda (arg)
155              (if (stringp arg)
156                  arg
157                (let* ((item (eval arg))
158                       (ret (cdr (assoc item cal)))
159                       )
160                  (if ret
161                      ret
162                    (if (eq item 'encoding)
163                        "7bit"
164                      ""))
165                  ))
166              ))
167           format))
168
169 (defvar mime-echo-window-is-shared-with-bbdb t
170   "*If non-nil, mime-echo window is shared with BBDB window.")
171
172 (defvar mime-echo-window-height
173   (function
174    (lambda ()
175      (/ (window-height) 5)
176      ))
177   "*Size of mime-echo window.
178 It allows function or integer.  If it is function,
179 `mime-show-echo-buffer' calls it to get height of mime-echo window.
180 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
181 window.")
182
183 (defun mime-show-echo-buffer (&rest forms)
184   "Show mime-echo buffer to display MIME-playing information."
185   (get-buffer-create mime-echo-buffer-name)
186   (let ((the-win (selected-window))
187         (win (get-buffer-window mime-echo-buffer-name))
188         )
189     (or win
190         (if (and mime-echo-window-is-shared-with-bbdb
191                  (boundp 'bbdb-buffer-name)
192                  (setq win (get-buffer-window bbdb-buffer-name))
193                  )
194             (set-window-buffer win mime-echo-buffer-name)
195           (select-window (get-buffer-window mime-view-buffer))
196           (setq win (split-window-vertically
197                      (- (window-height)
198                         (if (functionp mime-echo-window-height)
199                             (funcall mime-echo-window-height)
200                           mime-echo-window-height)
201                         )))
202           (set-window-buffer win mime-echo-buffer-name)
203           ))
204     (select-window win)
205     (goto-char (point-max))
206     (if forms
207         (insert (apply (function format) forms))
208       )
209     (select-window the-win)
210     ))
211
212
213 ;;; @ file name
214 ;;;
215
216 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
217
218 (defvar mime-view-file-name-regexp-1
219   (concat mime-view-file-name-char-regexp "+\\."
220           mime-view-file-name-char-regexp "+"))
221
222 (defvar mime-view-file-name-regexp-2
223   (concat (regexp-* mime-view-file-name-char-regexp)
224           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
225
226 (defun mime-article/get-original-filename (param &optional encoding)
227   (or (mime-article/get-uu-filename param encoding)
228       (let (ret)
229         (or (if (or (and (setq ret (mime/Content-Disposition))
230                          (setq ret (assoc "filename" (cdr ret)))
231                          )
232                     (setq ret (assoc "name" param))
233                     (setq ret (assoc "x-name" param))
234                     )
235                 (std11-strip-quoted-string (cdr ret))
236               )
237             (if (setq ret
238                       (std11-find-field-body '("Content-Description"
239                                                "Subject")))
240                 (if (or (string-match mime-view-file-name-regexp-1 ret)
241                         (string-match mime-view-file-name-regexp-2 ret))
242                     (substring ret (match-beginning 0)(match-end 0))
243                   ))
244             ))
245       ))
246
247 (defun mime-article/get-filename (param)
248   (replace-as-filename (mime-article/get-original-filename param))
249   )
250
251
252 ;;; @ mail/news message
253 ;;;
254
255 (defun mime-view-quitting-method-for-mime-show-message-mode ()
256   "Quitting method for mime-view.
257 It is registered to variable `mime-view-quitting-method-alist'."
258   (let ((mother mime-mother-buffer)
259         (win-conf mime::preview/original-window-configuration)
260         )
261     (kill-buffer mime-raw-buffer)
262     (mime-view-kill-buffer)
263     (set-window-configuration win-conf)
264     (pop-to-buffer mother)
265     ))
266
267 (defun mime-article/view-message/rfc822 (beg end cal)
268   (let* ((cnum (mime-article/point-content-number beg))
269          (new-name (format "%s-%s" (buffer-name) cnum))
270          (mother mime-view-buffer)
271          (text-decoder
272           (cdr (or (assq major-mode mime-text-decoder-alist)
273                    (assq t mime-text-decoder-alist))))
274          str)
275     (setq str (buffer-substring beg end))
276     (switch-to-buffer new-name)
277     (erase-buffer)
278     (insert str)
279     (goto-char (point-min))
280     (if (re-search-forward "^\n" nil t)
281         (delete-region (point-min) (match-end 0))
282       )
283     (setq major-mode 'mime-show-message-mode)
284     (setq mime-text-decoder text-decoder)
285     (mime-view-mode mother)
286     ))
287
288
289 ;;; @ message/partial
290 ;;;
291
292 (defvar mime-article/coding-system-alist
293   (list '(mh-show-mode . no-conversion)
294         (cons t (mime-charset-to-coding-system default-mime-charset))
295         ))
296
297 (defun mime-article::write-region (start end file)
298   (let ((coding-system-for-write
299          (cdr
300           (or (assq major-mode mime-article/coding-system-alist)
301               (assq t mime-article/coding-system-alist)
302               ))))
303     (write-region start end file)
304     ))
305
306 (defun mime-article/decode-message/partial (beg end cal)
307   (goto-char beg)
308   (let* ((root-dir
309           (expand-file-name
310            (concat "m-prts-" (user-login-name)) mime-temp-directory))
311          (id (cdr (assoc "id" cal)))
312          (number (cdr (assoc "number" cal)))
313          (total (cdr (assoc "total" cal)))
314          file
315          (mother mime-view-buffer)
316          )
317     (or (file-exists-p root-dir)
318         (make-directory root-dir)
319         )
320     (setq id (replace-as-filename id))
321     (setq root-dir (concat root-dir "/" id))
322     (or (file-exists-p root-dir)
323         (make-directory root-dir)
324         )
325     (setq file (concat root-dir "/FULL"))
326     (if (file-exists-p file)
327         (let ((full-buf (get-buffer-create "FULL"))
328               (pwin (or (get-buffer-window mother)
329                         (get-largest-window)))
330               )
331           (save-window-excursion
332             (set-buffer full-buf)
333             (erase-buffer)
334             (as-binary-input-file (insert-file-contents file))
335             (setq major-mode 'mime-show-message-mode)
336             (mime-view-mode mother)
337             )
338           (set-window-buffer pwin
339                              (save-excursion
340                                (set-buffer full-buf)
341                                mime-view-buffer))
342           (select-window pwin)
343           )
344       (re-search-forward "^$")
345       (goto-char (1+ (match-end 0)))
346       (setq file (concat root-dir "/" number))
347       (mime-article::write-region (point) (point-max) file)
348       (let ((total-file (concat root-dir "/CT")))
349         (setq total
350               (if total
351                   (progn
352                     (or (file-exists-p total-file)
353                         (save-excursion
354                           (set-buffer
355                            (get-buffer-create mime/temp-buffer-name))
356                           (erase-buffer)
357                           (insert total)
358                           (write-file total-file)
359                           (kill-buffer (current-buffer))
360                           ))
361                     (string-to-number total)
362                     )
363                 (and (file-exists-p total-file)
364                      (save-excursion
365                        (set-buffer (find-file-noselect total-file))
366                        (prog1
367                            (and (re-search-forward "[0-9]+" nil t)
368                                 (string-to-number
369                                  (buffer-substring (match-beginning 0)
370                                                    (match-end 0)))
371                                 )
372                          (kill-buffer (current-buffer))
373                          )))
374                 )))
375       (if (and total (> total 0))
376           (catch 'tag
377             (save-excursion
378               (set-buffer (get-buffer-create mime/temp-buffer-name))
379               (let ((full-buf (current-buffer)))
380                 (erase-buffer)
381                 (let ((i 1))
382                   (while (<= i total)
383                     (setq file (concat root-dir "/" (int-to-string i)))
384                     (or (file-exists-p file)
385                         (throw 'tag nil)
386                         )
387                     (as-binary-input-file (insert-file-contents file))
388                     (goto-char (point-max))
389                     (setq i (1+ i))
390                     ))
391                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
392                 (let ((i 1))
393                   (while (<= i total)
394                     (let ((file (format "%s/%d" root-dir i)))
395                       (and (file-exists-p file)
396                            (delete-file file)
397                            ))
398                     (setq i (1+ i))
399                     ))
400                 (let ((file (expand-file-name "CT" root-dir)))
401                   (and (file-exists-p file)
402                        (delete-file file)
403                        ))
404                 (save-window-excursion
405                   (setq major-mode 'mime-show-message-mode)
406                   (mime-view-mode mother)
407                   )
408                 (let ((pwin (or (get-buffer-window mother)
409                                 (get-largest-window)
410                                 ))
411                       (pbuf (save-excursion
412                               (set-buffer full-buf)
413                               mime-view-buffer)))
414                   (set-window-buffer pwin pbuf)
415                   (select-window pwin)
416                   )))))
417       )))
418
419
420 ;;; @ message/external-body
421 ;;;
422
423 (defvar mime-article/dired-function
424   (if mime/use-multi-frame
425       (function dired-other-frame)
426     (function mime-article/dired-function-for-one-frame)
427     ))
428
429 (defun mime-article/dired-function-for-one-frame (dir)
430   (let ((win (or (get-buffer-window mime-view-buffer)
431                  (get-largest-window))))
432     (select-window win)
433     (dired dir)
434     ))
435
436 (defun mime-article/decode-message/external-ftp (beg end cal)
437   (let* ((site (cdr (assoc "site" cal)))
438          (directory (cdr (assoc "directory" cal)))
439          (name (cdr (assoc "name" cal)))
440          ;;(mode (cdr (assoc "mode" cal)))
441          (pathname (concat "/anonymous@" site ":" directory))
442          )
443     (message (concat "Accessing " (expand-file-name name pathname) "..."))
444     (funcall mime-article/dired-function pathname)
445     (goto-char (point-min))
446     (search-forward name)
447     ))
448
449
450 ;;; @ rot13-47
451 ;;;
452
453 (defun mime-display-caesar (beg end cal)
454   "Internal method for mime-view to display ROT13-47-48 message."
455   (let* ((cnum (mime-article/point-content-number beg))
456          (new-name (format "%s-%s" (buffer-name) cnum))
457          (the-buf (current-buffer))
458          (mother mime-view-buffer)
459          (charset (cdr (assoc "charset" cal)))
460          (encoding (cdr (assq 'encoding cal)))
461          (mode major-mode)
462          str)
463     (setq str (buffer-substring beg end))
464     (let ((pwin (or (get-buffer-window mother)
465                     (get-largest-window)))
466           (buf (get-buffer-create new-name))
467           )
468       (set-window-buffer pwin buf)
469       (set-buffer buf)
470       (select-window pwin)
471       )
472     (setq buffer-read-only nil)
473     (erase-buffer)
474     (insert str)
475     (goto-char (point-min))
476     (if (re-search-forward "^\n" nil t)
477         (delete-region (point-min) (match-end 0))
478       )
479     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
480                       (assq t mime-text-decoder-alist)))))
481       (and (functionp m)
482            (funcall m charset encoding)
483            ))
484     (save-excursion
485       (set-mark (point-min))
486       (goto-char (point-max))
487       (tm:caesar-region)
488       )
489     (set-buffer-modified-p nil)
490     (set-buffer mother)
491     (view-buffer new-name)
492     ))
493
494
495 ;;; @ end
496 ;;;
497
498 (provide 'mime-play)
499
500 ;;; mime-play.el ends here