(defgroup reftex): Update home page url-link.
[bpt/emacs.git] / lisp / gnus / uudecode.el
CommitLineData
23f87bed 1;;; uudecode.el -- elisp native uudecode
c113de23 2
e84b4b86 3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
88e6695f 4;; 2005, 2006 Free Software Foundation, Inc.
c113de23
GM
5
6;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7;; Keywords: uudecode news
8
715a2ca2 9;; This file is part of GNU Emacs.
c113de23
GM
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU 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
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
c113de23
GM
25
26;;; Commentary:
27
c113de23
GM
28;;; Code:
29
23f87bed
MB
30(autoload 'executable-find "executable")
31
117b4b78
DL
32(eval-when-compile (require 'cl))
33
05c2a83c
DL
34(eval-and-compile
35 (defalias 'uudecode-char-int
36 (if (fboundp 'char-int)
37 'char-int
23f87bed 38 'identity)))
c113de23
GM
39
40(defcustom uudecode-decoder-program "uudecode"
41 "*Non-nil value should be a string that names a uu decoder.
42The program should expect to read uu data on its standard
43input and write the converted data to its standard output."
44 :type 'string
45 :group 'gnus-extract)
46
47(defcustom uudecode-decoder-switches nil
48 "*List of command line flags passed to `uudecode-decoder-program'."
49 :group 'gnus-extract
50 :type '(repeat string))
51
23f87bed
MB
52(defcustom uudecode-use-external
53 (executable-find uudecode-decoder-program)
54 "*Use external uudecode program."
bf247b6e 55 :version "22.1"
23f87bed
MB
56 :group 'gnus-extract
57 :type 'boolean)
58
c113de23
GM
59(defconst uudecode-alphabet "\040-\140")
60
61(defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
62(defconst uudecode-end-line "^end[ \t]*$")
63
64(defconst uudecode-body-line
65 (let ((i 61) (str "^M"))
66 (while (> (setq i (1- i)) 0)
67 (setq str (concat str "[^a-z]")))
68 (concat str ".?$")))
69
70(defvar uudecode-temporary-file-directory
71 (cond ((fboundp 'temp-directory) (temp-directory))
72 ((boundp 'temporary-file-directory) temporary-file-directory)
73 ("/tmp")))
74
75;;;###autoload
76(defun uudecode-decode-region-external (start end &optional file-name)
96403ac1
DL
77 "Uudecode region between START and END using external program.
78If FILE-NAME is non-nil, save the result to FILE-NAME. The program
79used is specified by `uudecode-decoder-program'."
c113de23 80 (interactive "r\nP")
96403ac1 81 (let ((cbuf (current-buffer)) tempfile firstline status)
c113de23
GM
82 (save-excursion
83 (goto-char start)
84 (when (re-search-forward uudecode-begin-line nil t)
85 (forward-line 1)
86 (setq firstline (point))
87 (cond ((null file-name))
88 ((stringp file-name))
89 (t
90 (setq file-name (read-file-name "File to Name:"
91 nil nil nil
92 (match-string 1)))))
93 (setq tempfile (if file-name
94 (expand-file-name file-name)
23f87bed
MB
95 (if (fboundp 'make-temp-file)
96 (let ((temporary-file-directory
97 uudecode-temporary-file-directory))
98 (make-temp-file "uu"))
99 (expand-file-name
100 (make-temp-name "uu")
101 uudecode-temporary-file-directory))))
96403ac1 102 (let ((cdir default-directory)
26c9afc3
MB
103 (default-process-coding-system
104 (if (featurep 'xemacs)
105 ;; In XEmacs, `nil' is not a valid coding system.
106 '(binary . binary)
107 nil)))
c113de23 108 (unwind-protect
96403ac1 109 (with-temp-buffer
c113de23
GM
110 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
111 (insert-buffer-substring cbuf firstline end)
112 (cd (file-name-directory tempfile))
113 (apply 'call-process-region
114 (point-min)
115 (point-max)
116 uudecode-decoder-program
117 nil
118 nil
119 nil
120 uudecode-decoder-switches))
121 (cd cdir) (set-buffer cbuf)))
122 (if (file-exists-p tempfile)
123 (unless file-name
124 (goto-char start)
125 (delete-region start end)
126 (let (format-alist)
127 (insert-file-contents-literally tempfile)))
128 (message "Can not uudecode")))
c113de23
GM
129 (ignore-errors (or file-name (delete-file tempfile))))))
130
c113de23 131;;;###autoload
23f87bed 132(defun uudecode-decode-region-internal (start end &optional file-name)
96403ac1 133 "Uudecode region between START and END without using an external program.
c113de23
GM
134If FILE-NAME is non-nil, save the result to FILE-NAME."
135 (interactive "r\nP")
23f87bed 136 (let ((done nil)
c113de23
GM
137 (counter 0)
138 (remain 0)
139 (bits 0)
23f87bed 140 (lim 0) inputpos result
c113de23 141 (non-data-chars (concat "^" uudecode-alphabet)))
23f87bed
MB
142 (save-excursion
143 (goto-char start)
144 (when (re-search-forward uudecode-begin-line nil t)
145 (cond ((null file-name))
146 ((stringp file-name))
147 (t
148 (setq file-name (expand-file-name
149 (read-file-name "File to Name:"
150 nil nil nil
151 (match-string 1))))))
152 (forward-line 1)
153 (skip-chars-forward non-data-chars end)
154 (while (not done)
155 (setq inputpos (point))
156 (setq remain 0 bits 0 counter 0)
157 (cond
158 ((> (skip-chars-forward uudecode-alphabet end) 0)
159 (setq lim (point))
160 (setq remain
161 (logand (- (uudecode-char-int (char-after inputpos)) 32)
162 63))
163 (setq inputpos (1+ inputpos))
164 (if (= remain 0) (setq done t))
165 (while (and (< inputpos lim) (> remain 0))
166 (setq bits (+ bits
167 (logand
168 (-
169 (uudecode-char-int (char-after inputpos)) 32)
170 63)))
171 (if (/= counter 0) (setq remain (1- remain)))
172 (setq counter (1+ counter)
173 inputpos (1+ inputpos))
174 (cond ((= counter 4)
175 (setq result (cons
176 (concat
177 (char-to-string (lsh bits -16))
178 (char-to-string (logand (lsh bits -8) 255))
179 (char-to-string (logand bits 255)))
180 result))
181 (setq bits 0 counter 0))
182 (t (setq bits (lsh bits 6)))))))
183 (cond
184 (done)
185 ((> 0 remain)
186 (error "uucode line ends unexpectly")
187 (setq done t))
188 ((and (= (point) end) (not done))
189 ;;(error "uucode ends unexpectly")
190 (setq done t))
191 ((= counter 3)
192 (setq result (cons
193 (concat
194 (char-to-string (logand (lsh bits -16) 255))
195 (char-to-string (logand (lsh bits -8) 255)))
196 result)))
197 ((= counter 2)
198 (setq result (cons
199 (char-to-string (logand (lsh bits -10) 255))
200 result))))
201 (skip-chars-forward non-data-chars end))
202 (if file-name
203 (let (default-enable-multibyte-characters)
204 (with-temp-file file-name
205 (insert (apply 'concat (nreverse result)))))
206 (or (markerp end) (setq end (set-marker (make-marker) end)))
c113de23 207 (goto-char start)
23f87bed
MB
208 (insert (apply 'concat (nreverse result)))
209 (delete-region (point) end))))))
210
211;;;###autoload
212(defun uudecode-decode-region (start end &optional file-name)
213 "Uudecode region between START and END.
214If FILE-NAME is non-nil, save the result to FILE-NAME."
215 (if uudecode-use-external
216 (uudecode-decode-region-external start end file-name)
217 (uudecode-decode-region-internal start end file-name)))
c113de23
GM
218
219(provide 'uudecode)
220
ab5796a9 221;;; arch-tag: e1f09ed5-62b4-4677-9f13-4e81c4fe8ce3
c113de23 222;;; uudecode.el ends here