9f791972e5f3858c2a8e7c9b7416a3e570c2c7c0
[elisp/flim.git] / mel-g.el
1 ;;; mel-g.el --- Gzip64 encoder/decoder.
2
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
4 ;; Copyright (C) 1996,1997,1999 Shuhei KOBAYASHI
5
6 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
7 ;;         MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Maintainer: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
9 ;; Created: 1995/10/25
10 ;; Keywords: Gzip64, base64, gzip, MIME
11
12 ;; This file is part of FLIM (Faithful Library about Internet Message).
13
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;;; NOTE: Gzip64 is an experimental Content-Transfer-Encoding and its
32 ;;; use is STRONGLY DISCOURAGED except for private communication.
33
34 ;;; Code:
35
36 (require 'mime-def)
37 (require 'path-util)
38
39
40 ;;; @ variables
41 ;;;
42
43 (defvar gzip64-external-encoder
44   (let ((file (exec-installed-p "mmencode")))
45     (and file
46          (` ("sh" "-c" (, (concat "gzip -c | " file))))))
47   "*list of gzip64 encoder program name and its arguments.")
48
49 (defvar gzip64-external-decoder
50   (let ((file (exec-installed-p "mmencode")))
51     (and file
52          (` ("sh" "-c" (, (concat file " -u | gzip -dc"))))))
53   "*list of gzip64 decoder program name and its arguments.")
54
55
56 ;;; @ encoder/decoder for region
57 ;;;
58
59 (defun gzip64-external-encode-region (beg end)
60   (interactive "*r")
61   (save-excursion
62     (let ((coding-system-for-write 'binary))
63       (apply (function call-process-region)
64              beg end (car gzip64-external-encoder)
65              t t nil
66              (cdr gzip64-external-encoder)))
67     ;; for OS/2
68     ;;   regularize line break code
69     ;;(goto-char (point-min))
70     ;;(while (re-search-forward "\r$" nil t)
71     ;;  (replace-match ""))
72     ))
73
74 (defun gzip64-external-decode-region (beg end)
75   (interactive "*r")
76   (save-excursion
77     (let ((coding-system-for-read 'binary))
78       (apply (function call-process-region)
79              beg end (car gzip64-external-decoder)
80              t t nil
81              (cdr gzip64-external-decoder)))))
82
83 (mel-define-method-function (mime-encode-region start end (nil "x-gzip64"))
84                             'gzip64-external-encode-region)
85 (mel-define-method-function (mime-decode-region start end (nil "x-gzip64"))
86                             'gzip64-external-decode-region)
87
88
89 ;;; @ encoder/decoder for string
90 ;;;
91
92 (mel-define-method mime-encode-string (string (nil "x-gzip64"))
93   (with-temp-buffer
94     (insert string)
95     (gzip64-external-encode-region (point-min)(point-max))
96     (buffer-string)))
97
98 (mel-define-method mime-decode-string (string (nil "x-gzip64"))
99   (with-temp-buffer
100     (insert string)
101     (gzip64-external-decode-region (point-min)(point-max))
102     (buffer-string)))
103
104
105 ;;; @ encoder/decoder for file
106 ;;;
107
108 (mel-define-method mime-insert-encoded-file (filename (nil "x-gzip64"))
109   (interactive "*fInsert encoded file: ")
110   (apply (function call-process)
111          (car gzip64-external-encoder)
112          filename t nil
113          (cdr gzip64-external-encoder)))
114
115 (mel-define-method mime-write-decoded-region (start end filename
116                                                     (nil "x-gzip64"))
117   "Decode and write current region encoded by gzip64 into FILENAME.
118 START and END are buffer positions."
119   (interactive "*r\nFWrite decoded region to file: ")
120   (let ((coding-system-for-read 'binary)
121         (coding-system-for-write 'binary))
122     (apply (function call-process-region)
123            start end (car gzip64-external-decoder)
124            nil nil nil
125            (let ((args (cdr gzip64-external-decoder)))
126              (append (butlast args)
127                      (list (concat (car (last args)) ">" filename)))))))
128
129
130 ;;; @ end
131 ;;;
132
133 (provide 'mel-g)
134
135 ;;; mel-g.el ends here.