(eword-decode-string, eword-decode-region): Mention language info in doc string.
[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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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     (let ((coding-system-for-read  'binary)
55           (coding-system-for-write 'binary))
56       (apply (function call-process-region)
57              start end (car uuencode-external-encoder)
58              t t nil
59              (cdr uuencode-external-encoder)))
60     ;; for OS/2
61     ;;   regularize line break code
62     (goto-char (point-min))
63     (while (re-search-forward "\r$" nil t)
64       (replace-match ""))))
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 (make-temp-file "x-uue")))
73       (save-excursion
74         (save-restriction
75           (set-mark end)
76           (narrow-to-region start end)
77           (goto-char start)
78           (when (and (re-search-forward "^begin [0-9]+ " nil t)
79                      (looking-at ".+$"))
80             (replace-match filename)
81             (let ((coding-system-for-read  'binary)
82                   (coding-system-for-write 'binary))
83               (apply (function call-process-region)
84                      start (mark) (car uuencode-external-decoder)
85                      t nil nil
86                      (cdr uuencode-external-decoder)))
87             (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 (mel-define-method-function (mime-encode-region start end (nil "x-uue"))
97                             'uuencode-external-encode-region)
98 (mel-define-method-function (mime-decode-region start end (nil "x-uue"))
99                             'uuencode-external-decode-region)
100
101
102 ;;; @ encoder/decoder for string
103 ;;;
104
105 (mel-define-method mime-encode-string (string (nil "x-uue"))
106   (with-temp-buffer
107     (insert string)
108     (uuencode-external-encode-region (point-min)(point-max))
109     (buffer-string)))
110
111 (mel-define-method mime-decode-string (string (nil "x-uue"))
112   (with-temp-buffer
113     (insert string)
114     (uuencode-external-decode-region (point-min)(point-max))
115     (buffer-string)))
116
117
118 ;;; @ uuencode encoder/decoder for file
119 ;;;
120
121 (mel-define-method mime-insert-encoded-file (filename (nil "x-uue"))
122   "Insert file encoded by unofficial uuencode format.
123 This function uses external uuencode encoder which is specified by
124 variable `uuencode-external-encoder'."
125   (interactive "*fInsert encoded file: ")
126   (call-process (car uuencode-external-encoder)
127                 filename t nil
128                 (file-name-nondirectory filename)))
129
130 (mel-define-method mime-write-decoded-region (start end filename
131                                                     (nil "x-uue"))
132   "Decode and write current region encoded by uuencode into FILENAME.
133 START and END are buffer positions."
134   (interactive "*r\nFWrite decoded region to file: ")
135   (save-excursion
136     (let ((clone-buf (clone-buffer " *x-uue*"))
137           (file (make-temp-file "x-uue")))
138       (save-excursion
139         (save-restriction
140           (set-buffer clone-buf)
141           (narrow-to-region start end)
142           (setq buffer-read-only nil)
143           (goto-char start)
144           (when (and (re-search-forward "^begin [0-9]+ " nil t)
145                    (looking-at ".+$"))
146             (replace-match file)
147             (let ((coding-system-for-read  'binary)
148                   (coding-system-for-write 'binary))
149               (apply (function call-process-region)
150                      (point-min) (point-max) (car uuencode-external-decoder)
151                      nil nil nil
152                      (cdr uuencode-external-decoder))
153               (rename-file file filename 'overwrites)
154               (message (concat "Wrote " filename))))))
155       (kill-buffer clone-buf))))
156
157 ;;; @ end
158 ;;;
159
160 (provide 'mel-u)
161
162 (mel-define-backend "x-uuencode" ("x-uue"))
163
164 ;;; mel-u.el ends here.