Merge `deisui-1_14_0-1'.
[elisp/flim.git] / mel-u.el
1 ;;; mel-u.el --- uuencode encoder/decoder.
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 this program; 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 'mime-def)
29 (require 'path-util)
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
55      (apply (function call-process-region)
56             start end (car uuencode-external-encoder)
57             t t nil
58             (cdr uuencode-external-encoder)))
59     ;; for OS/2
60     ;;   regularize line break code
61     (goto-char (point-min))
62     (while (re-search-forward "\r$" nil t)
63       (replace-match ""))))
64
65 (defun uuencode-external-decode-region (start end)
66   "Decode current region by unofficial uuencode format.
67 This function uses external uuencode decoder which is specified by
68 variable `uuencode-external-decoder'."
69   (interactive "*r")
70   (save-excursion
71     (let ((filename (save-excursion
72                       (save-restriction
73                         (narrow-to-region start end)
74                         (goto-char start)
75                         (if (re-search-forward "^begin [0-9]+ " nil t)
76                             (if (looking-at ".+$")
77                                 (buffer-substring (match-beginning 0)
78                                                   (match-end 0)))))))
79           (default-directory temporary-file-directory))
80       (if filename
81           (as-binary-process
82            (apply (function call-process-region)
83                   start end (car uuencode-external-decoder)
84                   t nil nil
85                   (cdr uuencode-external-decoder))
86            (as-binary-input-file (insert-file-contents filename))
87            ;; The previous line causes the buffer to be made read-only, I
88            ;; do not pretend to understand the control flow leading to this
89            ;; but suspect it has something to do with image-mode. -slb
90            ;;   Use `inhibit-read-only' to avoid to force
91            ;;   buffer-read-only nil. - tomo.
92            (let ((inhibit-read-only t))
93              (delete-file filename)))))))
94
95 (mel-define-method-function (mime-encode-region start end (nil "x-uue"))
96                             'uuencode-external-encode-region)
97 (mel-define-method-function (mime-decode-region start end (nil "x-uue"))
98                             'uuencode-external-decode-region)
99
100
101 ;;; @ encoder/decoder for string
102 ;;;
103
104 (mel-define-method mime-encode-string (string (nil "x-uue"))
105   (with-temp-buffer
106     (insert string)
107     (uuencode-external-encode-region (point-min)(point-max))
108     (buffer-string)))
109
110 (mel-define-method mime-decode-string (string (nil "x-uue"))
111   (with-temp-buffer
112     (insert string)
113     (uuencode-external-decode-region (point-min)(point-max))
114     (buffer-string)))
115
116
117 ;;; @ uuencode encoder/decoder for file
118 ;;;
119
120 (mel-define-method mime-insert-encoded-file (filename (nil "x-uue"))
121   "Insert file encoded by unofficial uuencode format.
122 This function uses external uuencode encoder which is specified by
123 variable `uuencode-external-encoder'."
124   (interactive "*fInsert encoded file: ")
125   (call-process (car uuencode-external-encoder)
126                 filename t nil
127                 (file-name-nondirectory filename)))
128
129 (mel-define-method mime-write-decoded-region (start end filename
130                                                     (nil "x-uue"))
131   "Decode and write current region encoded by uuencode into FILENAME.
132 START and END are buffer positions."
133   (interactive "*r\nFWrite decoded region to file: ")
134   (save-excursion
135     (let ((file (save-excursion
136                   (save-restriction
137                     (narrow-to-region start end)
138                     (goto-char start)
139                     (if (re-search-forward "^begin [0-9]+ " nil t)
140                         (if (looking-at ".+$")
141                             (buffer-substring (match-beginning 0)
142                                               (match-end 0)))))))
143           (default-directory temporary-file-directory))
144       (if file
145           (as-binary-process
146            (apply (function call-process-region)
147                   start end (car uuencode-external-decoder)
148                   nil nil nil
149                   (cdr uuencode-external-decoder))
150            (rename-file file filename 'overwrites))))))
151
152
153 ;;; @ end
154 ;;;
155
156 (provide 'mel-u)
157
158 (mel-define-backend "x-uuencode" ("x-uue"))
159
160 ;;; mel-u.el ends here.