Mercurial > emacs
annotate lisp/gnus/uudecode.el @ 33665:b51b000b2c50
(ad-special-forms): Correct the conditional inclusion of `track-mouse'.
author | Miles Bader <miles@gnu.org> |
---|---|
date | Tue, 21 Nov 2000 08:48:45 +0000 |
parents | cef397782e2e |
children | 03ae5e62feb4 |
rev | line source |
---|---|
31717 | 1 ;;; uudecode.el -- elisp native uudecode |
2 | |
3 ;; Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
6 ;; Keywords: uudecode news | |
7 | |
8 ;; This file is a part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; Lots of codes are stolen from mm-decode.el, gnus-uu.el and | |
28 ;; base64.el | |
29 | |
33566
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
30 ;; This looks as though it could be made rather more efficient for |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
31 ;; internal working. Encoding could use a lookup table and decoding |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
32 ;; should presumably use a vector or list buffer for partial results |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
33 ;; rather than with-current-buffer. -- fx |
33263 | 34 |
31717 | 35 ;;; Code: |
36 | |
33264 | 37 (eval-when-compile (require 'cl)) |
38 | |
33298 | 39 (eval-and-compile |
40 (defalias 'uudecode-char-int | |
41 (if (fboundp 'char-int) | |
42 'char-int | |
43 'identity)) | |
44 | |
33566
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
45 (if (featurep 'xemacs) |
33298 | 46 (defalias 'uudecode-insert-char 'insert-char) |
47 (defun uudecode-insert-char (char &optional count ignored buffer) | |
48 (if (or (null buffer) (eq buffer (current-buffer))) | |
49 (insert-char char count) | |
50 (with-current-buffer buffer | |
51 (insert-char char count)))))) | |
31717 | 52 |
53 (defcustom uudecode-decoder-program "uudecode" | |
54 "*Non-nil value should be a string that names a uu decoder. | |
55 The program should expect to read uu data on its standard | |
56 input and write the converted data to its standard output." | |
57 :type 'string | |
58 :group 'gnus-extract) | |
59 | |
60 (defcustom uudecode-decoder-switches nil | |
61 "*List of command line flags passed to `uudecode-decoder-program'." | |
62 :group 'gnus-extract | |
63 :type '(repeat string)) | |
64 | |
65 (defconst uudecode-alphabet "\040-\140") | |
66 | |
67 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$") | |
68 (defconst uudecode-end-line "^end[ \t]*$") | |
69 | |
70 (defconst uudecode-body-line | |
71 (let ((i 61) (str "^M")) | |
72 (while (> (setq i (1- i)) 0) | |
73 (setq str (concat str "[^a-z]"))) | |
74 (concat str ".?$"))) | |
75 | |
76 (defvar uudecode-temporary-file-directory | |
77 (cond ((fboundp 'temp-directory) (temp-directory)) | |
78 ((boundp 'temporary-file-directory) temporary-file-directory) | |
79 ("/tmp"))) | |
80 | |
81 ;;;###autoload | |
82 (defun uudecode-decode-region-external (start end &optional file-name) | |
33566
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
83 "Uudecode region between START and END using external program. |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
84 If FILE-NAME is non-nil, save the result to FILE-NAME. The program |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
85 used is specified by `uudecode-decoder-program'." |
31717 | 86 (interactive "r\nP") |
33566
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
87 (let ((cbuf (current-buffer)) tempfile firstline status) |
31717 | 88 (save-excursion |
89 (goto-char start) | |
90 (when (re-search-forward uudecode-begin-line nil t) | |
91 (forward-line 1) | |
92 (setq firstline (point)) | |
93 (cond ((null file-name)) | |
94 ((stringp file-name)) | |
95 (t | |
96 (setq file-name (read-file-name "File to Name:" | |
97 nil nil nil | |
98 (match-string 1))))) | |
99 (setq tempfile (if file-name | |
100 (expand-file-name file-name) | |
33566
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
101 (let ((temporary-file-directory |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
102 uudecode-temporary-file-directory)) |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
103 (make-temp-file "uu")))) |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
104 (let ((cdir default-directory) |
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
105 default-process-coding-system) |
31717 | 106 (unwind-protect |
33566
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
107 (with-temp-buffer |
31717 | 108 (insert "begin 600 " (file-name-nondirectory tempfile) "\n") |
109 (insert-buffer-substring cbuf firstline end) | |
110 (cd (file-name-directory tempfile)) | |
111 (apply 'call-process-region | |
112 (point-min) | |
113 (point-max) | |
114 uudecode-decoder-program | |
115 nil | |
116 nil | |
117 nil | |
118 uudecode-decoder-switches)) | |
119 (cd cdir) (set-buffer cbuf))) | |
120 (if (file-exists-p tempfile) | |
121 (unless file-name | |
122 (goto-char start) | |
123 (delete-region start end) | |
124 (let (format-alist) | |
125 (insert-file-contents-literally tempfile))) | |
126 (message "Can not uudecode"))) | |
127 (ignore-errors (or file-name (delete-file tempfile)))))) | |
128 | |
129 ;;;###autoload | |
130 (defun uudecode-decode-region (start end &optional file-name) | |
33566
cef397782e2e
(uudecode-insert-char): Fix bogus feature test.
Dave Love <fx@gnu.org>
parents:
33298
diff
changeset
|
131 "Uudecode region between START and END without using an external program. |
31717 | 132 If FILE-NAME is non-nil, save the result to FILE-NAME." |
133 (interactive "r\nP") | |
134 (let ((work-buffer nil) | |
135 (done nil) | |
136 (counter 0) | |
137 (remain 0) | |
138 (bits 0) | |
139 (lim 0) inputpos | |
140 (non-data-chars (concat "^" uudecode-alphabet))) | |
141 (unwind-protect | |
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 (setq work-buffer (generate-new-buffer " *uudecode-work*")) | |
153 (forward-line 1) | |
154 (skip-chars-forward non-data-chars end) | |
155 (while (not done) | |
156 (setq inputpos (point)) | |
157 (setq remain 0 bits 0 counter 0) | |
158 (cond | |
159 ((> (skip-chars-forward uudecode-alphabet end) 0) | |
160 (setq lim (point)) | |
161 (setq remain | |
33263 | 162 (logand (- (uudecode-char-int (char-after inputpos)) 32) |
163 63)) | |
31717 | 164 (setq inputpos (1+ inputpos)) |
165 (if (= remain 0) (setq done t)) | |
166 (while (and (< inputpos lim) (> remain 0)) | |
167 (setq bits (+ bits | |
168 (logand | |
169 (- | |
33263 | 170 (uudecode-char-int (char-after inputpos)) 32) |
171 63))) | |
31717 | 172 (if (/= counter 0) (setq remain (1- remain))) |
173 (setq counter (1+ counter) | |
174 inputpos (1+ inputpos)) | |
175 (cond ((= counter 4) | |
176 (uudecode-insert-char | |
177 (lsh bits -16) 1 nil work-buffer) | |
178 (uudecode-insert-char | |
179 (logand (lsh bits -8) 255) 1 nil work-buffer) | |
180 (uudecode-insert-char (logand bits 255) 1 nil | |
181 work-buffer) | |
182 (setq bits 0 counter 0)) | |
183 (t (setq bits (lsh bits 6))))))) | |
184 (cond | |
185 (done) | |
186 ((> 0 remain) | |
187 (error "uucode line ends unexpectly") | |
188 (setq done t)) | |
189 ((and (= (point) end) (not done)) | |
190 ;;(error "uucode ends unexpectly") | |
191 (setq done t)) | |
192 ((= counter 3) | |
193 (uudecode-insert-char (logand (lsh bits -16) 255) 1 nil | |
194 work-buffer) | |
195 (uudecode-insert-char (logand (lsh bits -8) 255) 1 nil | |
196 work-buffer)) | |
197 ((= counter 2) | |
198 (uudecode-insert-char (logand (lsh bits -10) 255) 1 nil | |
199 work-buffer))) | |
200 (skip-chars-forward non-data-chars end)) | |
201 (if file-name | |
202 (save-excursion | |
203 (set-buffer work-buffer) | |
204 (write-file file-name)) | |
205 (or (markerp end) (setq end (set-marker (make-marker) end))) | |
206 (goto-char start) | |
207 (insert-buffer-substring work-buffer) | |
208 (delete-region (point) end)))) | |
209 (and work-buffer (kill-buffer work-buffer))))) | |
210 | |
211 (provide 'uudecode) | |
212 | |
213 ;;; uudecode.el ends here |