Merge flim-1_11_3.
[elisp/flim.git] / mel-u.el
1 ;;; mel-u.el: uuencode encoder/decoder for GNU Emacs
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/10/25
7 ;; Keywords: uuencode
8
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'emu)
29 (require 'mime-def)
30
31
32 (mel-define-backend "x-uue")
33
34
35 ;;; @ variables
36 ;;;
37
38 (defvar uuencode-external-encoder '("uuencode" "-")
39   "*list of uuencode encoder program name and its arguments.")
40
41 (defvar uuencode-external-decoder '("sh" "-c" "uudecode")
42   "*list of uuencode decoder program name and its arguments.")
43
44
45 ;;; @ uuencode encoder/decoder for region
46 ;;;
47
48 (defun uuencode-external-encode-region (start end)
49   "Encode current region by unofficial uuencode format.
50 This function uses external uuencode encoder which is specified by
51 variable `uuencode-external-encoder'."
52   (interactive "*r")
53   (save-excursion
54     (as-binary-process (apply (function call-process-region)
55                               start end (car uuencode-external-encoder)
56                               t t nil (cdr uuencode-external-encoder))
57                        )
58     ;; for OS/2
59     ;;   regularize line break code
60     (goto-char (point-min))
61     (while (re-search-forward "\r$" nil t)
62       (replace-match "")
63       )
64     ))
65
66 (defun uuencode-external-decode-region (start end)
67   "Decode current region by unofficial uuencode format.
68 This function uses external uuencode decoder which is specified by
69 variable `uuencode-external-decoder'."
70   (interactive "*r")
71   (save-excursion
72     (let ((filename (save-excursion
73                       (save-restriction
74                         (narrow-to-region start end)
75                         (goto-char start)
76                         (if (re-search-forward "^begin [0-9]+ " nil t)
77                             (if (looking-at ".+$")
78                                 (buffer-substring (match-beginning 0)
79                                                   (match-end 0))
80                               )))))
81           (default-directory temporary-file-directory))
82       (if filename
83           (as-binary-process
84            (apply (function call-process-region)
85                   start end (car uuencode-external-decoder)
86                   t nil nil (cdr uuencode-external-decoder))
87            (as-binary-input-file (insert-file-contents filename))
88            ;; The previous line causes the buffer to be made read-only, I
89            ;; do not pretend to understand the control flow leading to this
90            ;; but suspect it has something to do with image-mode. -slb
91            ;;   Use `inhibit-read-only' to avoid to force
92            ;;   buffer-read-only nil. - tomo.
93            (let ((inhibit-read-only t))
94              (delete-file filename)
95              )
96            ))
97       )))
98
99 (mel-define-method-function (mime-encode-region start end (nil "x-uue"))
100                             'uuencode-external-encode-region)
101 (mel-define-method-function (mime-decode-region start end (nil "x-uue"))
102                             'uuencode-external-decode-region)
103
104
105 ;;; @ encoder/decoder for string
106 ;;;
107
108 (mel-define-method mime-encode-string (string (nil "x-uue"))
109   (with-temp-buffer
110     (insert string)
111     (uuencode-external-encode-region (point-min)(point-max))
112     (buffer-string)))
113
114 (mel-define-method mime-decode-string (string (nil "x-uue"))
115   (with-temp-buffer
116     (insert string)
117     (uuencode-external-decode-region (point-min)(point-max))
118     (buffer-string)))
119
120
121 ;;; @ uuencode encoder/decoder for file
122 ;;;
123
124 (mel-define-method mime-insert-encoded-file (filename (nil "x-uue"))
125   "Insert file encoded by unofficial uuencode format.
126 This function uses external uuencode encoder which is specified by
127 variable `uuencode-external-encoder'."
128   (interactive (list (read-file-name "Insert encoded file: ")))
129   (call-process (car uuencode-external-encoder) filename t nil
130                 (file-name-nondirectory filename))
131   )
132
133 (mel-define-method mime-write-decoded-region (start end filename
134                                                     (nil "x-uue"))
135   "Decode and write current region encoded by uuencode into FILENAME.
136 START and END are buffer positions."
137   (interactive
138    (list (region-beginning) (region-end)
139          (read-file-name "Write decoded region to file: ")))
140   (save-excursion
141     (let ((file (save-excursion
142                   (save-restriction
143                     (narrow-to-region start end)
144                     (goto-char start)
145                     (if (re-search-forward "^begin [0-9]+ " nil t)
146                         (if (looking-at ".+$")
147                             (buffer-substring (match-beginning 0)
148                                               (match-end 0))
149                           )))))
150           (default-directory temporary-file-directory))
151       (if file
152           (as-binary-process
153            (apply (function call-process-region)
154                   start end (car uuencode-external-decoder)
155                   nil nil nil (cdr uuencode-external-decoder))
156            (rename-file file filename 'overwrites)
157            )))))
158
159
160 ;;; @ end
161 ;;;
162
163 (provide 'mel-u)
164
165 (mel-define-backend "x-uuencode" ("x-uue"))
166
167 ;;; mel-u.el ends here