Update FSF's address in GPL notices.
[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., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, 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 '("sh" "-c" "gzip -c | mmencode")
43   "*list of gzip64 encoder program name and its arguments.")
44
45 (defvar gzip64-external-decoder '("sh" "-c" "mmencode -u | gzip -dc")
46   "*list of gzip64 decoder program name and its arguments.")
47
48
49 ;;; @ encoder/decoder for region
50 ;;;
51
52 (defun gzip64-external-encode-region (beg end)
53   (interactive "*r")
54   (save-excursion
55     (let ((coding-system-for-write 'binary))
56       (apply (function call-process-region)
57              beg end (car gzip64-external-encoder)
58              t t nil
59              (cdr gzip64-external-encoder)))
60     ;; for OS/2
61     ;;   regularize line break code
62     ;;(goto-char (point-min))
63     ;;(while (re-search-forward "\r$" nil t)
64     ;;  (replace-match ""))
65     ))
66
67 (defun gzip64-external-decode-region (beg end)
68   (interactive "*r")
69   (save-excursion
70     (let ((coding-system-for-read 'binary))
71       (apply (function call-process-region)
72              beg end (car gzip64-external-decoder)
73              t t nil
74              (cdr gzip64-external-decoder)))))
75
76 (mel-define-method-function (mime-encode-region start end (nil "x-gzip64"))
77                             'gzip64-external-encode-region)
78 (mel-define-method-function (mime-decode-region start end (nil "x-gzip64"))
79                             'gzip64-external-decode-region)
80
81
82 ;;; @ encoder/decoder for string
83 ;;;
84
85 (mel-define-method mime-encode-string (string (nil "x-gzip64"))
86   (with-temp-buffer
87     (insert string)
88     (gzip64-external-encode-region (point-min)(point-max))
89     (buffer-string)))
90
91 (mel-define-method mime-decode-string (string (nil "x-gzip64"))
92   (with-temp-buffer
93     (insert string)
94     (gzip64-external-decode-region (point-min)(point-max))
95     (buffer-string)))
96
97
98 ;;; @ encoder/decoder for file
99 ;;;
100
101 (mel-define-method mime-insert-encoded-file (filename (nil "x-gzip64"))
102   (interactive "*fInsert encoded file: ")
103   (apply (function call-process)
104          (car gzip64-external-encoder)
105          filename t nil
106          (cdr gzip64-external-encoder)))
107
108 (mel-define-method mime-write-decoded-region (start end filename
109                                                     (nil "x-gzip64"))
110   "Decode and write current region encoded by gzip64 into FILENAME.
111 START and END are buffer positions."
112   (interactive "*r\nFWrite decoded region to file: ")
113   (let ((coding-system-for-read 'binary)
114         (coding-system-for-write 'binary))
115     (apply (function call-process-region)
116            start end (car gzip64-external-decoder)
117            nil nil nil
118            (let ((args (cdr gzip64-external-decoder)))
119              (append (butlast args)
120                      (list (concat (car (last args)) ">" filename)))))))
121
122
123 ;;; @ end
124 ;;;
125
126 (provide 'mel-g)
127
128 ;;; mel-g.el ends here