Merge flim-1_11_0.
[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 'poem)
37 (require 'path-util)
38 (require 'mel)
39
40
41 ;;; @ variables
42 ;;;
43
44 (defvar gzip64-external-encoder
45   (let ((file (exec-installed-p "mmencode")))
46     (and file
47          (` ("sh" "-c" (, (concat "gzip -c | " file))))
48          ))
49   "*list of gzip64 encoder program name and its arguments.")
50
51 (defvar gzip64-external-decoder
52   (let ((file (exec-installed-p "mmencode")))
53     (and file
54          (` ("sh" "-c" (, (concat file " -u | gzip -dc"))))
55          ))
56   "*list of gzip64 decoder program name and its arguments.")
57
58
59 ;;; @ encoder/decoder for region
60 ;;;
61
62 (defun gzip64-external-encode-region (beg end)
63   (interactive "*r")
64   (save-excursion
65     (as-binary-process (apply (function call-process-region)
66                               beg end (car gzip64-external-encoder)
67                               t t nil (cdr gzip64-external-encoder))
68                        )
69     ;; for OS/2
70     ;;   regularize line break code
71     (goto-char (point-min))
72     (while (re-search-forward "\r$" nil t)
73       (replace-match "")
74       )
75     ))
76
77 (defun gzip64-external-decode-region (beg end)
78   (interactive "*r")
79   (save-excursion
80     (as-binary-process (apply (function call-process-region)
81                               beg end (car gzip64-external-decoder)
82                               t t nil (cdr gzip64-external-decoder))
83                        )
84     ))
85
86 (mel-define-method-function (mime-encode-region start end (nil "x-gzip64"))
87                             'gzip64-external-encode-region)
88 (mel-define-method-function (mime-decode-region start end (nil "x-gzip64"))
89                             'gzip64-external-decode-region)
90
91
92 ;;; @ encoder/decoder for string
93 ;;;
94
95 (mel-define-method mime-encode-string (string (nil "x-gzip64"))
96   (with-temp-buffer
97     (insert string)
98     (gzip64-external-encode-region (point-min)(point-max))
99     (buffer-string)))
100
101 (mel-define-method mime-decode-string (string (nil "x-gzip64"))
102   (with-temp-buffer
103     (insert string)
104     (gzip64-external-decode-region (point-min)(point-max))
105     (buffer-string)))
106
107
108 ;;; @ encoder/decoder for file
109 ;;;
110
111 (mel-define-method mime-insert-encoded-file (filename (nil "x-gzip64"))
112   (interactive (list (read-file-name "Insert encoded file: ")))
113   (apply (function call-process) (car gzip64-external-encoder)
114          filename t nil
115          (cdr gzip64-external-encoder))
116   )
117
118 (mel-define-method mime-write-decoded-region (start end filename
119                                                     (nil "x-gzip64"))
120   "Decode and write current region encoded by gzip64 into FILENAME.
121 START and END are buffer positions."
122   (interactive
123    (list (region-beginning) (region-end)
124          (read-file-name "Write decoded region to file: ")))
125   (as-binary-process
126    (apply (function call-process-region)
127           start end (car gzip64-external-decoder)
128           nil nil nil
129           (let ((args (cdr gzip64-external-decoder)))
130             (append (butlast args)
131                     (list (concat (car (last args)) ">" filename))))
132           )))
133
134
135 ;;; @ end
136 ;;;
137
138 (provide 'mel-g)
139
140 ;;; mel-g.el ends here.