Merge flim-1_10_4.
[elisp/flim.git] / mel-g.el
1 ;;; mel-g.el: Gzip64 encoder/decoder for GNU Emacs
2
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
4 ;; Copyright (C) 1996,1997 Shuhei KOBAYASHI
5
6 ;; Author: Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
7 ;;      modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; Maintainer: Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;; Created: 1995/10/25
10 ;; Keywords: Gzip64, base64, gzip, MIME
11
12 ;; This file is part of MEL (MIME Encoding Library).
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 GNU Emacs; 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 'emu)
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          ))
48   "*list of gzip64 encoder program name and its arguments.")
49
50 (defvar gzip64-external-decoder
51   (let ((file (exec-installed-p "mmencode")))
52     (and file
53          (` ("sh" "-c" (, (concat file " -u | gzip -dc"))))
54          ))
55   "*list of gzip64 decoder program name and its arguments.")
56
57
58 ;;; @ encoder/decoder for region
59 ;;;
60
61 (defun gzip64-external-encode-region (beg end)
62   (interactive "*r")
63   (save-excursion
64     (as-binary-process (apply (function call-process-region)
65                               beg end (car gzip64-external-encoder)
66                               t t nil (cdr gzip64-external-encoder))
67                        )
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
76 (defun gzip64-external-decode-region (beg end)
77   (interactive "*r")
78   (save-excursion
79     (as-binary-process (apply (function call-process-region)
80                               beg end (car gzip64-external-decoder)
81                               t t nil (cdr gzip64-external-decoder))
82                        )
83     ))
84
85 (mel-define-method-function (mime-encode-region start end (nil "x-gzip64"))
86                             'gzip64-external-encode-region)
87 (mel-define-method-function (mime-decode-region start end (nil "x-gzip64"))
88                             'gzip64-external-decode-region)
89
90
91 ;;; @ encoder/decoder for string
92 ;;;
93
94 (mel-define-method mime-encode-string (string (nil "x-gzip64"))
95   (with-temp-buffer
96     (insert string)
97     (gzip64-external-encode-region (point-min)(point-max))
98     (buffer-string)))
99
100 (mel-define-method mime-decode-string (string (nil "x-gzip64"))
101   (with-temp-buffer
102     (insert string)
103     (gzip64-external-decode-region (point-min)(point-max))
104     (buffer-string)))
105
106
107 ;;; @ encoder/decoder for file
108 ;;;
109
110 (mel-define-method mime-insert-encoded-file (filename (nil "x-gzip64"))
111   (interactive (list (read-file-name "Insert encoded file: ")))
112   (apply (function call-process) (car gzip64-external-encoder)
113          filename t nil
114          (cdr gzip64-external-encoder))
115   )
116
117 (mel-define-method mime-write-decoded-region (start end filename
118                                                     (nil "x-gzip64"))
119   "Decode and write current region encoded by gzip64 into FILENAME.
120 START and END are buffer positions."
121   (interactive
122    (list (region-beginning) (region-end)
123          (read-file-name "Write decoded region to file: ")))
124   (as-binary-process
125    (apply (function call-process-region)
126           start end (car gzip64-external-decoder)
127           nil nil nil
128           (let ((args (cdr gzip64-external-decoder)))
129             (append (butlast args)
130                     (list (concat (car (last args)) ">" filename))))
131           )))
132
133
134 ;;; @ end
135 ;;;
136
137 (provide 'mel-g)
138
139 ;;; mel-g.el ends here.