Merge from emacs-23 branch, up to 2010-05-20T22:16:19Z!juri@jurta.org.
[bpt/emacs.git] / lisp / net / tramp-uu.el
CommitLineData
16674e4f
KG
1;;; tramp-uu.el --- uuencode in Lisp
2
44ffae96 3;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5df4f04c 4;; 2009, 2010, 2011 Free Software Foundation, Inc.
16674e4f 5
e52196e0 6;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
16674e4f 7;; Keywords: comm, terminals
bd78fa1d 8;; Package: tramp
16674e4f 9
874a927a
GM
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
16674e4f 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16674e4f 16
874a927a 17;; GNU Emacs is distributed in the hope that it will be useful,
16674e4f
KG
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
16674e4f
KG
24
25;;; Commentary:
26
27;; An implementation of "uuencode" in Lisp. Uses the function
28;; base64-encode-region which is built-in to modern Emacsen.
29
30;;; Code:
31
32(defvar tramp-uu-b64-alphabet
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
34 "Mapping from base64-encoded character to the byte it represents.")
35
36(defvar tramp-uu-b64-char-to-byte
37 (let ((i 0))
38 (mapcar (lambda (c)
39 (prog1
40 (cons c i)
4007ba5b 41 (setq i (1+ i))))
16674e4f
KG
42 tramp-uu-b64-alphabet))
43 "Alist of mapping from base64 character to its byte.")
44
45(defun tramp-uu-byte-to-uu-char (byte)
46 "Return the character encoding BYTE."
47 (if (zerop byte) ?` (+ byte 32)))
48
49(defun tramp-uu-b64-char-to-byte (char)
50 "Return the byte that is encoded as CHAR."
51 (cdr (assq char tramp-uu-b64-char-to-byte)))
52
0f34aa77 53;;;###tramp-autoload
16674e4f
KG
54(defun tramp-uuencode-region (beg end)
55 "UU-encode the region between BEG and END."
56 ;; First we base64 encode the region, then we transmogrify that into
57 ;; uu encoding.
58 (let ((len (base64-encode-region beg end t))
59 (padding 0)
60 i c)
61 (save-excursion
62 (goto-char beg)
63 (setq i 0)
64 (while (< i len)
65 (setq c (char-after (point)))
66 (delete-char 1)
67 (if (equal c ?=)
19a87064
MA
68 ;; "=" means padding. Insert "`" instead. Not counted for length.
69 (progn (insert "`") (setq len (1- len)))
70 (insert (tramp-uu-byte-to-uu-char (tramp-uu-b64-char-to-byte c)))
71 (setq i (1+ i)))
16674e4f
KG
72 ;; Every 60 characters, add "M" at beginning of line (as
73 ;; length byte) and insert a newline.
74 (when (zerop (% i 60))
75 (save-excursion
76 (beginning-of-line)
77 (insert (char-to-string (+ 32 (/ (* 3 60) 4)))))
78 (insert "\n")))
79 ;; If there is something leftover, we compute the length byte
80 ;; for that stuff and insert it and a trailing newline.
81 (unless (zerop (% i 60))
82 (save-excursion
83 (beginning-of-line)
84 (insert (char-to-string (+ 32 (% (- end beg) 45)))))
85 (insert "\n"))
86 ;; Why is there always a "`" line at the end?
87 (insert "`\nend\n")
88 (goto-char beg)
89 (insert "begin 600 xxx\n"))))
90
0f34aa77
MA
91(add-hook 'tramp-unload-hook
92 (lambda ()
93 (unload-feature 'tramp-uu 'force)))
94
16674e4f 95(provide 'tramp-uu)
ab5796a9 96
16674e4f 97;;; tramp-uu.el ends here
668efb3f
MA
98
99;; Local Variables:
100;; mode: Emacs-Lisp
101;; coding: utf-8
102;; End: