c0f35772dd07bbde297e67fc7a1376205c6840c9
[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 <morioka@jaist.ac.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
55 ;;; @ encoder/decoder for region
56 ;;;
57
58 (defun gzip64-external-encode-region (beg end)
59   (interactive "*r")
60   (save-excursion
61     (as-binary-process
62      (apply (function call-process-region)
63             beg end (car gzip64-external-encoder)
64             t t nil
65             (cdr gzip64-external-encoder)))
66     ;; for OS/2
67     ;;   regularize line break code
68     (goto-char (point-min))
69     (while (re-search-forward "\r$" nil t)
70       (replace-match ""))))
71
72 (defun gzip64-external-decode-region (beg end)
73   (interactive "*r")
74   (save-excursion
75     (as-binary-process
76      (apply (function call-process-region)
77             beg end (car gzip64-external-decoder)
78             t t nil
79             (cdr gzip64-external-decoder)))))
80
81 (mel-define-method-function (mime-encode-region start end (nil "x-gzip64"))
82                             'gzip64-external-encode-region)
83 (mel-define-method-function (mime-decode-region start end (nil "x-gzip64"))
84                             'gzip64-external-decode-region)
85
86
87 ;;; @ encoder/decoder for string
88 ;;;
89
90 (mel-define-method mime-encode-string (string (nil "x-gzip64"))
91   (with-temp-buffer
92     (insert string)
93     (gzip64-external-encode-region (point-min)(point-max))
94     (buffer-string)))
95
96 (mel-define-method mime-decode-string (string (nil "x-gzip64"))
97   (with-temp-buffer
98     (insert string)
99     (gzip64-external-decode-region (point-min)(point-max))
100     (buffer-string)))
101
102
103 ;;; @ encoder/decoder for file
104 ;;;
105
106 (mel-define-method mime-insert-encoded-file (filename (nil "x-gzip64"))
107   (interactive "*fInsert encoded file: ")
108   (apply (function call-process)
109          (car gzip64-external-encoder)
110          filename t nil
111          (cdr gzip64-external-encoder)))
112
113 (mel-define-method mime-write-decoded-region (start end filename
114                                                     (nil "x-gzip64"))
115   "Decode and write current region encoded by gzip64 into FILENAME.
116 START and END are buffer positions."
117   (interactive "*r\nFWrite decoded region to file: ")
118   (as-binary-process
119    (apply (function call-process-region)
120           start end (car gzip64-external-decoder)
121           nil nil nil
122           (let ((args (cdr gzip64-external-decoder)))
123             (append (butlast args)
124                     (list (concat (car (last args)) ">" filename)))))))
125
126
127 ;;; @ end
128 ;;;
129
130 (provide 'mel-g)
131
132 ;;; mel-g.el ends here.