annotate lisp/mail/uudecode.el @ 94989:c49219863e6b

(top-level): Don't require cl when compiling.
author John Paul Wallington <jpw@pobox.com>
date Thu, 15 May 2008 02:00:45 +0000
parents ef65fa4dca3b
children a9dc0e7c3f2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
1 ;;; uudecode.el -- elisp native uudecode
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
2
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
87649
107ccd98fa12 Merge from emacs--rel--22
Miles Bader <miles@gnu.org>
parents: 86962
diff changeset
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
5
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
7 ;; Keywords: uudecode news
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
8
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
10
94674
ef65fa4dca3b Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92782
diff changeset
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
94674
ef65fa4dca3b Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92782
diff changeset
13 ;; the Free Software Foundation, either version 3 of the License, or
ef65fa4dca3b Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92782
diff changeset
14 ;; (at your option) any later version.
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
15
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
20
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
94674
ef65fa4dca3b Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92782
diff changeset
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
23
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
24 ;;; Commentary:
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
25
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
26 ;;; Code:
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
27
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
28 (eval-when-compile (require 'cl))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
29
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
30 (eval-and-compile
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
31 (defalias 'uudecode-char-int
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
32 (if (fboundp 'char-int)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
33 'char-int
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
34 'identity)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
35
86962
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
36 (defgroup uudecode nil
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
37 "Decoding of uuencoded data."
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
38 :group 'mail
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
39 :group 'news)
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
40
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
41 (defcustom uudecode-decoder-program "uudecode"
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
42 "*Non-nil value should be a string that names a uu decoder.
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
43 The program should expect to read uu data on its standard
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
44 input and write the converted data to its standard output."
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
45 :type 'string
86962
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
46 :group 'uudecode)
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
47
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
48 (defcustom uudecode-decoder-switches nil
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
49 "*List of command line flags passed to `uudecode-decoder-program'."
86962
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
50 :group 'uudecode
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
51 :type '(repeat string))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
52
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
53 (defcustom uudecode-use-external
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
54 (executable-find uudecode-decoder-program)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
55 "*Use external uudecode program."
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
56 :version "22.1"
86962
4a055549d556 (uudecode): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 86959
diff changeset
57 :group 'uudecode
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
58 :type 'boolean)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
59
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
60 (defconst uudecode-alphabet "\040-\140")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
61
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
62 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
63 (defconst uudecode-end-line "^end[ \t]*$")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
64
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
65 (defconst uudecode-body-line
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
66 (let ((i 61) (str "^M"))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
67 (while (> (setq i (1- i)) 0)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
68 (setq str (concat str "[^a-z]")))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
69 (concat str ".?$")))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
70
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
71 (defvar uudecode-temporary-file-directory
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
72 (cond ((fboundp 'temp-directory) (temp-directory))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
73 ((boundp 'temporary-file-directory) temporary-file-directory)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
74 ("/tmp")))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
75
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
76 ;;;###autoload
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
77 (defun uudecode-decode-region-external (start end &optional file-name)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
78 "Uudecode region between START and END using external program.
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
79 If FILE-NAME is non-nil, save the result to FILE-NAME. The program
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
80 used is specified by `uudecode-decoder-program'."
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
81 (interactive "r\nP")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
82 (let ((cbuf (current-buffer)) tempfile firstline status)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
83 (save-excursion
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
84 (goto-char start)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
85 (when (re-search-forward uudecode-begin-line nil t)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
86 (forward-line 1)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
87 (setq firstline (point))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
88 (cond ((null file-name))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
89 ((stringp file-name))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
90 (t
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
91 (setq file-name (read-file-name "File to Name:"
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
92 nil nil nil
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
93 (match-string 1)))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
94 (setq tempfile (if file-name
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
95 (expand-file-name file-name)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
96 (if (fboundp 'make-temp-file)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
97 (let ((temporary-file-directory
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
98 uudecode-temporary-file-directory))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
99 (make-temp-file "uu"))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
100 (expand-file-name
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
101 (make-temp-name "uu")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
102 uudecode-temporary-file-directory))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
103 (let ((cdir default-directory)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
104 (default-process-coding-system
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
105 (if (featurep 'xemacs)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
106 ;; In XEmacs, `nil' is not a valid coding system.
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
107 '(binary . binary)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
108 nil)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
109 (unwind-protect
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
110 (with-temp-buffer
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
111 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
112 (insert-buffer-substring cbuf firstline end)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
113 (cd (file-name-directory tempfile))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
114 (apply 'call-process-region
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
115 (point-min)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
116 (point-max)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
117 uudecode-decoder-program
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
118 nil
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
119 nil
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
120 nil
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
121 uudecode-decoder-switches))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
122 (cd cdir) (set-buffer cbuf)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
123 (if (file-exists-p tempfile)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
124 (unless file-name
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
125 (goto-char start)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
126 (delete-region start end)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
127 (let (format-alist)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
128 (insert-file-contents-literally tempfile)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
129 (message "Can not uudecode")))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
130 (ignore-errors (or file-name (delete-file tempfile))))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
131
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
132 (eval-and-compile
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
133 (defalias 'uudecode-string-to-multibyte
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
134 (cond
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
135 ((featurep 'xemacs)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
136 'identity)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
137 ((fboundp 'string-to-multibyte)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
138 'string-to-multibyte)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
139 (t
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
140 (lambda (string)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
141 "Return a multibyte string with the same individual chars as string."
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
142 (mapconcat
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
143 (lambda (ch) (string-as-multibyte (char-to-string ch)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
144 string ""))))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
145
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
146 ;;;###autoload
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
147 (defun uudecode-decode-region-internal (start end &optional file-name)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
148 "Uudecode region between START and END without using an external program.
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
149 If FILE-NAME is non-nil, save the result to FILE-NAME."
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
150 (interactive "r\nP")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
151 (let ((done nil)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
152 (counter 0)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
153 (remain 0)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
154 (bits 0)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
155 (lim 0) inputpos result
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
156 (non-data-chars (concat "^" uudecode-alphabet)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
157 (save-excursion
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
158 (goto-char start)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
159 (when (re-search-forward uudecode-begin-line nil t)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
160 (cond ((null file-name))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
161 ((stringp file-name))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
162 (t
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
163 (setq file-name (expand-file-name
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
164 (read-file-name "File to Name:"
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
165 nil nil nil
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
166 (match-string 1))))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
167 (forward-line 1)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
168 (skip-chars-forward non-data-chars end)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
169 (while (not done)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
170 (setq inputpos (point))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
171 (setq remain 0 bits 0 counter 0)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
172 (cond
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
173 ((> (skip-chars-forward uudecode-alphabet end) 0)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
174 (setq lim (point))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
175 (setq remain
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
176 (logand (- (uudecode-char-int (char-after inputpos)) 32)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
177 63))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
178 (setq inputpos (1+ inputpos))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
179 (if (= remain 0) (setq done t))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
180 (while (and (< inputpos lim) (> remain 0))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
181 (setq bits (+ bits
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
182 (logand
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
183 (-
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
184 (uudecode-char-int (char-after inputpos)) 32)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
185 63)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
186 (if (/= counter 0) (setq remain (1- remain)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
187 (setq counter (1+ counter)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
188 inputpos (1+ inputpos))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
189 (cond ((= counter 4)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
190 (setq result (cons
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
191 (concat
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
192 (char-to-string (lsh bits -16))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
193 (char-to-string (logand (lsh bits -8) 255))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
194 (char-to-string (logand bits 255)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
195 result))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
196 (setq bits 0 counter 0))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
197 (t (setq bits (lsh bits 6)))))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
198 (cond
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
199 (done)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
200 ((> 0 remain)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
201 (error "uucode line ends unexpectly")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
202 (setq done t))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
203 ((and (= (point) end) (not done))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
204 ;;(error "uucode ends unexpectly")
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
205 (setq done t))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
206 ((= counter 3)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
207 (setq result (cons
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
208 (concat
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
209 (char-to-string (logand (lsh bits -16) 255))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
210 (char-to-string (logand (lsh bits -8) 255)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
211 result)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
212 ((= counter 2)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
213 (setq result (cons
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
214 (char-to-string (logand (lsh bits -10) 255))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
215 result))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
216 (skip-chars-forward non-data-chars end))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
217 (if file-name
92782
b643f53a57c5 (uudecode-decode-region-internal): Use set-buffer-multibyte rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 87649
diff changeset
218 (with-temp-file file-name
b643f53a57c5 (uudecode-decode-region-internal): Use set-buffer-multibyte rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 87649
diff changeset
219 (set-buffer-multibyte nil)
b643f53a57c5 (uudecode-decode-region-internal): Use set-buffer-multibyte rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 87649
diff changeset
220 (insert (apply 'concat (nreverse result))))
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
221 (or (markerp end) (setq end (set-marker (make-marker) end)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
222 (goto-char start)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
223 (if enable-multibyte-characters
92782
b643f53a57c5 (uudecode-decode-region-internal): Use set-buffer-multibyte rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 87649
diff changeset
224 (dolist (x (nreverse result))
b643f53a57c5 (uudecode-decode-region-internal): Use set-buffer-multibyte rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 87649
diff changeset
225 (insert (uudecode-string-to-multibyte x)))
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
226 (insert (apply 'concat (nreverse result))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
227 (delete-region (point) end))))))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
228
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
229 ;;;###autoload
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
230 (defun uudecode-decode-region (start end &optional file-name)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
231 "Uudecode region between START and END.
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
232 If FILE-NAME is non-nil, save the result to FILE-NAME."
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
233 (if uudecode-use-external
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
234 (uudecode-decode-region-external start end file-name)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
235 (uudecode-decode-region-internal start end file-name)))
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
236
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
237 (provide 'uudecode)
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
238
92782
b643f53a57c5 (uudecode-decode-region-internal): Use set-buffer-multibyte rather than
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 87649
diff changeset
239 ;; arch-tag: e1f09ed5-62b4-4677-9f13-4e81c4fe8ce3
86959
2a30481eb39c Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
240 ;;; uudecode.el ends here