`mime-article/show-output-buffer' -> `mime-show-echo-buffer'.
[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.33 1997-03-27 20:56:38 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 (defun mime-show-echo-buffer (&rest forms)
173   "Show mime-echo buffer to display MIME-playing information."
174   (get-buffer-create mime-echo-buffer-name)
175   (let ((the-win (selected-window))
176         (win (get-buffer-window mime-echo-buffer-name))
177         )
178     (or win
179         (if (and mime-echo-window-is-shared-with-bbdb
180                  (boundp 'bbdb-buffer-name)
181                  (setq win (get-buffer-window bbdb-buffer-name))
182                  )
183             (set-window-buffer win mime-echo-buffer-name)
184           (select-window (get-buffer-window mime-view-buffer))
185           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
186           (set-window-buffer win mime-echo-buffer-name)
187           ))
188     (select-window win)
189     (goto-char (point-max))
190     (if forms
191         (insert (apply (function format) forms))
192       )
193     (select-window the-win)
194     ))
195
196
197 ;;; @ file name
198 ;;;
199
200 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
201
202 (defvar mime-view-file-name-regexp-1
203   (concat mime-view-file-name-char-regexp "+\\."
204           mime-view-file-name-char-regexp "+"))
205
206 (defvar mime-view-file-name-regexp-2
207   (concat (regexp-* mime-view-file-name-char-regexp)
208           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
209
210 (defun mime-article/get-original-filename (param &optional encoding)
211   (or (mime-article/get-uu-filename param encoding)
212       (let (ret)
213         (or (if (or (and (setq ret (mime/Content-Disposition))
214                          (setq ret (assoc "filename" (cdr ret)))
215                          )
216                     (setq ret (assoc "name" param))
217                     (setq ret (assoc "x-name" param))
218                     )
219                 (std11-strip-quoted-string (cdr ret))
220               )
221             (if (setq ret
222                       (std11-find-field-body '("Content-Description"
223                                                "Subject")))
224                 (if (or (string-match mime-view-file-name-regexp-1 ret)
225                         (string-match mime-view-file-name-regexp-2 ret))
226                     (substring ret (match-beginning 0)(match-end 0))
227                   ))
228             ))
229       ))
230
231 (defun mime-article/get-filename (param)
232   (replace-as-filename (mime-article/get-original-filename param))
233   )
234
235
236 ;;; @ mail/news message
237 ;;;
238
239 (defun mime-view-quitting-method-for-mime-show-message-mode ()
240   "Quitting method for mime-view.
241 It is registered to variable `mime-view-quitting-method-alist'."
242   (let ((mother mime-mother-buffer)
243         (win-conf mime::preview/original-window-configuration)
244         )
245     (kill-buffer mime-raw-buffer)
246     (mime-view-kill-buffer)
247     (set-window-configuration win-conf)
248     (pop-to-buffer mother)
249     ))
250
251 (defun mime-article/view-message/rfc822 (beg end cal)
252   (let* ((cnum (mime-article/point-content-number beg))
253          (new-name (format "%s-%s" (buffer-name) cnum))
254          (mother mime-view-buffer)
255          (text-decoder
256           (cdr (or (assq major-mode mime-text-decoder-alist)
257                    (assq t mime-text-decoder-alist))))
258          str)
259     (setq str (buffer-substring beg end))
260     (switch-to-buffer new-name)
261     (erase-buffer)
262     (insert str)
263     (goto-char (point-min))
264     (if (re-search-forward "^\n" nil t)
265         (delete-region (point-min) (match-end 0))
266       )
267     (setq major-mode 'mime-show-message-mode)
268     (setq mime-text-decoder text-decoder)
269     (mime-view-mode mother)
270     ))
271
272
273 ;;; @ message/partial
274 ;;;
275
276 (defvar mime-article/coding-system-alist
277   (list '(mh-show-mode . no-conversion)
278         (cons t (mime-charset-to-coding-system default-mime-charset))
279         ))
280
281 (defun mime-article::write-region (start end file)
282   (let ((coding-system-for-write
283          (cdr
284           (or (assq major-mode mime-article/coding-system-alist)
285               (assq t mime-article/coding-system-alist)
286               ))))
287     (write-region start end file)
288     ))
289
290 (defun mime-article/decode-message/partial (beg end cal)
291   (goto-char beg)
292   (let* ((root-dir
293           (expand-file-name
294            (concat "m-prts-" (user-login-name)) mime-temp-directory))
295          (id (cdr (assoc "id" cal)))
296          (number (cdr (assoc "number" cal)))
297          (total (cdr (assoc "total" cal)))
298          file
299          (mother mime-view-buffer)
300          )
301     (or (file-exists-p root-dir)
302         (make-directory root-dir)
303         )
304     (setq id (replace-as-filename id))
305     (setq root-dir (concat root-dir "/" id))
306     (or (file-exists-p root-dir)
307         (make-directory root-dir)
308         )
309     (setq file (concat root-dir "/FULL"))
310     (if (file-exists-p file)
311         (let ((full-buf (get-buffer-create "FULL"))
312               (pwin (or (get-buffer-window mother)
313                         (get-largest-window)))
314               )
315           (save-window-excursion
316             (set-buffer full-buf)
317             (erase-buffer)
318             (as-binary-input-file (insert-file-contents file))
319             (setq major-mode 'mime-show-message-mode)
320             (mime-view-mode mother)
321             )
322           (set-window-buffer pwin
323                              (save-excursion
324                                (set-buffer full-buf)
325                                mime-view-buffer))
326           (select-window pwin)
327           )
328       (re-search-forward "^$")
329       (goto-char (1+ (match-end 0)))
330       (setq file (concat root-dir "/" number))
331       (mime-article::write-region (point) (point-max) file)
332       (let ((total-file (concat root-dir "/CT")))
333         (setq total
334               (if total
335                   (progn
336                     (or (file-exists-p total-file)
337                         (save-excursion
338                           (set-buffer
339                            (get-buffer-create mime/temp-buffer-name))
340                           (erase-buffer)
341                           (insert total)
342                           (write-file total-file)
343                           (kill-buffer (current-buffer))
344                           ))
345                     (string-to-number total)
346                     )
347                 (and (file-exists-p total-file)
348                      (save-excursion
349                        (set-buffer (find-file-noselect total-file))
350                        (prog1
351                            (and (re-search-forward "[0-9]+" nil t)
352                                 (string-to-number
353                                  (buffer-substring (match-beginning 0)
354                                                    (match-end 0)))
355                                 )
356                          (kill-buffer (current-buffer))
357                          )))
358                 )))
359       (if (and total (> total 0))
360           (catch 'tag
361             (save-excursion
362               (set-buffer (get-buffer-create mime/temp-buffer-name))
363               (let ((full-buf (current-buffer)))
364                 (erase-buffer)
365                 (let ((i 1))
366                   (while (<= i total)
367                     (setq file (concat root-dir "/" (int-to-string i)))
368                     (or (file-exists-p file)
369                         (throw 'tag nil)
370                         )
371                     (as-binary-input-file (insert-file-contents file))
372                     (goto-char (point-max))
373                     (setq i (1+ i))
374                     ))
375                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
376                 (let ((i 1))
377                   (while (<= i total)
378                     (let ((file (format "%s/%d" root-dir i)))
379                       (and (file-exists-p file)
380                            (delete-file file)
381                            ))
382                     (setq i (1+ i))
383                     ))
384                 (let ((file (expand-file-name "CT" root-dir)))
385                   (and (file-exists-p file)
386                        (delete-file file)
387                        ))
388                 (save-window-excursion
389                   (setq major-mode 'mime-show-message-mode)
390                   (mime-view-mode mother)
391                   )
392                 (let ((pwin (or (get-buffer-window mother)
393                                 (get-largest-window)
394                                 ))
395                       (pbuf (save-excursion
396                               (set-buffer full-buf)
397                               mime-view-buffer)))
398                   (set-window-buffer pwin pbuf)
399                   (select-window pwin)
400                   )))))
401       )))
402
403
404 ;;; @ message/external-body
405 ;;;
406
407 (defvar mime-article/dired-function
408   (if mime/use-multi-frame
409       (function dired-other-frame)
410     (function mime-article/dired-function-for-one-frame)
411     ))
412
413 (defun mime-article/dired-function-for-one-frame (dir)
414   (let ((win (or (get-buffer-window mime-view-buffer)
415                  (get-largest-window))))
416     (select-window win)
417     (dired dir)
418     ))
419
420 (defun mime-article/decode-message/external-ftp (beg end cal)
421   (let* ((site (cdr (assoc "site" cal)))
422          (directory (cdr (assoc "directory" cal)))
423          (name (cdr (assoc "name" cal)))
424          ;;(mode (cdr (assoc "mode" cal)))
425          (pathname (concat "/anonymous@" site ":" directory))
426          )
427     (message (concat "Accessing " (expand-file-name name pathname) "..."))
428     (funcall mime-article/dired-function pathname)
429     (goto-char (point-min))
430     (search-forward name)
431     ))
432
433
434 ;;; @ rot13-47
435 ;;;
436
437 (require 'view)
438
439 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
440 (define-key mime-view-text/plain-mode-map
441   "q" (function mime-view-text/plain-exit))
442
443 (defun mime-view-text/plain-mode ()
444   "\\{mime-view-text/plain-mode-map}"
445   (setq buffer-read-only t)
446   (setq major-mode 'mime-view-text/plain-mode)
447   (setq mode-name "MIME-View text/plain")
448   (use-local-map mime-view-text/plain-mode-map)
449   )
450
451 (defun mime-view-text/plain-exit ()
452   (interactive)
453   (kill-buffer (current-buffer))
454   )
455
456 (defun mime-article/decode-caesar (beg end cal)
457   (let* ((cnum (mime-article/point-content-number beg))
458          (new-name (format "%s-%s" (buffer-name) cnum))
459          (mother mime-view-buffer)
460          (charset (cdr (assoc "charset" cal)))
461          (encoding (cdr (assq 'encoding cal)))
462          (mode major-mode)
463          str)
464     (setq str (buffer-substring beg end))
465     (let ((pwin (or (get-buffer-window mother)
466                     (get-largest-window)))
467           (buf (get-buffer-create new-name))
468           )
469       (set-window-buffer pwin buf)
470       (set-buffer buf)
471       (select-window pwin)
472       )
473     (setq buffer-read-only nil)
474     (erase-buffer)
475     (insert str)
476     (goto-char (point-min))
477     (if (re-search-forward "^\n" nil t)
478         (delete-region (point-min) (match-end 0))
479       )
480     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
481                       (assq t mime-text-decoder-alist)))))
482       (and (functionp m)
483            (funcall m charset encoding)
484            ))
485     (save-excursion
486       (set-mark (point-min))
487       (goto-char (point-max))
488       (tm:caesar-region)
489       )
490     (set-buffer-modified-p nil)
491     (mime-view-text/plain-mode)
492     ))
493
494
495 ;;; @ end
496 ;;;
497
498 (provide 'mime-play)
499
500 ;;; mime-play.el ends here