(uuencode-external-decode-region): fixed.
[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 MEL (MIME Encoding Library).
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 'mel)
30
31
32 ;;; @ variables
33 ;;;
34
35 (defvar uuencode-external-encoder '("uuencode" "-")
36   "*list of uuencode encoder program name and its arguments.")
37
38 (defvar uuencode-external-decoder
39   (list "sh" "-c" (format "(cd %s; uudecode)" mime-temp-directory))
40   "*list of uuencode decoder program name and its arguments.")
41
42
43 ;;; @ uuencode encoder/decoder for region
44 ;;;
45
46 (defun uuencode-external-encode-region (start end)
47   "Encode current region by unofficial uuencode format.
48 This function uses external uuencode encoder which is specified by
49 variable `uuencode-external-encoder'."
50   (interactive "*r")
51   (save-excursion
52     (as-binary-process (apply (function call-process-region)
53                               start end (car uuencode-external-encoder)
54                               t t nil (cdr uuencode-external-encoder))
55                        )
56     ;; for OS/2
57     ;;   regularize line break code
58     (goto-char (point-min))
59     (while (re-search-forward "\r$" nil t)
60       (replace-match "")
61       )
62     ))
63
64 (defun uuencode-external-decode-region (start end)
65   "Decode current region by unofficial uuencode format.
66 This function uses external uuencode decoder which is specified by
67 variable `uuencode-external-decoder'."
68   (interactive "*r")
69   (save-excursion
70     (let ((filename (save-excursion
71                       (save-restriction
72                         (narrow-to-region start end)
73                         (goto-char start)
74                         (if (re-search-forward "^begin [0-9]+ " nil t)
75                             (if (looking-at ".+$")
76                                 (buffer-substring (match-beginning 0)
77                                                   (match-end 0))
78                               )))))
79           (default-directory (or (getenv "TMP")(getenv "TEMP") "/tmp")))
80       (if filename
81           (as-binary-process
82            (apply (function call-process-region)
83                   start end (car uuencode-external-decoder)
84                   t nil nil (cdr uuencode-external-decoder))
85            (as-binary-input-file (insert-file-contents filename))
86            ;; The previous line causes the buffer to be made read-only, I
87            ;; do not pretend to understand the control flow leading to this
88            ;; but suspect it has something to do with image-mode. -slb
89            ;;   Use `inhibit-read-only' to avoid to force
90            ;;   buffer-read-only nil. - tomo.
91            (let ((inhibit-read-only t))
92              (delete-file filename)
93              )
94            ))
95       )))
96
97 (defalias 'uuencode-encode-region 'uuencode-external-encode-region)
98 (defalias 'uuencode-decode-region 'uuencode-external-decode-region)
99
100
101 ;;; @ uuencode encoder/decoder for file
102 ;;;
103
104 (defun uuencode-insert-encoded-file (filename)
105   "Insert file encoded by unofficial uuencode format.
106 This function uses external uuencode encoder which is specified by
107 variable `uuencode-external-encoder'."
108   (interactive (list (read-file-name "Insert encoded file: ")))
109   (call-process (car uuencode-external-encoder) filename t nil
110                 (file-name-nondirectory filename))
111   )
112
113 (defun uuencode-write-decoded-region (start end filename)
114   "Decode and write current region encoded by uuencode into FILENAME.
115 START and END are buffer positions."
116   (interactive
117    (list (region-beginning) (region-end)
118          (read-file-name "Write decoded region to file: ")))
119   (save-excursion
120     (let ((file (save-excursion
121                   (save-restriction
122                     (narrow-to-region start end)
123                     (goto-char start)
124                     (if (re-search-forward "^begin [0-9]+ " nil t)
125                         (if (looking-at ".+$")
126                             (buffer-substring (match-beginning 0)
127                                               (match-end 0))
128                           )))))
129           (default-directory (or (getenv "TMP")(getenv "TEMP") "/tmp")))
130       (if file
131           (as-binary-process
132            (apply (function call-process-region)
133                   start end (car uuencode-external-decoder)
134                   nil nil nil (cdr uuencode-external-decoder))
135            (rename-file file filename 'overwrites)
136            )))))
137
138
139 ;;; @ end
140 ;;;
141
142 (provide 'mel-u)
143
144 ;;; mel-u.el ends here