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