aa8b9f3cf42e9663e3af2bc2dc60e17d3a8a99c4
[elisp/flim.git] / mel-g.el
1 ;;; mel-g.el --- Gzip64 encoder/decoder.
2
3 ;; Copyright (C) 1995,96,97,98,99,2001  Free Software Foundation, Inc.
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;;      MORIOKA Tomohiko <tomo@m17n.org>
7 ;; Maintainer: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
8 ;; Created: 1995/10/25
9 ;; Keywords: Gzip64, base64, gzip, MIME
10
11 ;; This file is part of FLIM (Faithful Library about Internet Message).
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 this program; 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 ;;; Commentary:
29
30 ;;; NOTE: Gzip64 is an experimental Content-Transfer-Encoding and its
31 ;;; use is STRONGLY DISCOURAGED except for private communication.
32
33 ;;; Code:
34
35 (require 'mime-def)
36 (require 'path-util)
37
38
39 ;;; @ variables
40 ;;;
41
42 (defvar gzip64-external-encoder
43   (let ((file (exec-installed-p "mmencode")))
44     (and file
45          (` ("sh" "-c" (, (concat "gzip -c | " file))))))
46   "*list of gzip64 encoder program name and its arguments.")
47
48 (defvar gzip64-external-decoder
49   (let ((file (exec-installed-p "mmencode")))
50     (and file
51          (` ("sh" "-c" (, (concat file " -u | gzip -dc"))))))
52   "*list of gzip64 decoder program name and its arguments.")
53
54 (unless (and gzip64-external-encoder gzip64-external-decoder)
55   (error "Please install mmencode command."))
56
57 ;;; @ encoder/decoder for region
58 ;;;
59
60 (defun gzip64-external-encode-region (beg end)
61   (interactive "*r")
62   (save-excursion
63     (let ((coding-system-for-write 'binary))
64       (apply (function call-process-region)
65              beg end (car gzip64-external-encoder)
66              t t nil
67              (cdr gzip64-external-encoder)))
68     ;; for OS/2
69     ;;   regularize line break code
70     ;;(goto-char (point-min))
71     ;;(while (re-search-forward "\r$" nil t)
72     ;;  (replace-match ""))
73     ))
74
75 (defun gzip64-external-decode-region (beg end)
76   (interactive "*r")
77   (save-excursion
78     (let ((coding-system-for-read 'binary))
79       (apply (function call-process-region)
80              beg end (car gzip64-external-decoder)
81              t t nil
82              (cdr gzip64-external-decoder)))))
83
84 (mel-define-method-function (mime-encode-region start end (nil "x-gzip64"))
85                             'gzip64-external-encode-region)
86 (mel-define-method-function (mime-decode-region start end (nil "x-gzip64"))
87                             'gzip64-external-decode-region)
88
89
90 ;;; @ encoder/decoder for string
91 ;;;
92
93 (mel-define-method mime-encode-string (string (nil "x-gzip64"))
94   (with-temp-buffer
95     (insert string)
96     (gzip64-external-encode-region (point-min)(point-max))
97     (buffer-string)))
98
99 (mel-define-method mime-decode-string (string (nil "x-gzip64"))
100   (with-temp-buffer
101     (insert string)
102     (gzip64-external-decode-region (point-min)(point-max))
103     (buffer-string)))
104
105
106 ;;; @ encoder/decoder for file
107 ;;;
108
109 (mel-define-method mime-insert-encoded-file (filename (nil "x-gzip64"))
110   (interactive "*fInsert encoded file: ")
111   (apply (function call-process)
112          (car gzip64-external-encoder)
113          filename t nil
114          (cdr gzip64-external-encoder)))
115
116 (mel-define-method mime-write-decoded-region (start end filename
117                                                     (nil "x-gzip64"))
118   "Decode and write current region encoded by gzip64 into FILENAME.
119 START and END are buffer positions."
120   (interactive "*r\nFWrite decoded region to file: ")
121   (let ((coding-system-for-read 'binary)
122         (coding-system-for-write 'binary))
123     (apply (function call-process-region)
124            start end (car gzip64-external-decoder)
125            nil nil nil
126            (let ((args (cdr gzip64-external-decoder)))
127              (append (butlast args)
128                      (list (concat (car (last args)) ">" filename)))))))
129
130
131 ;;; @ end
132 ;;;
133
134 (provide 'mel-g)
135
136 ;;; mel-g.el ends here