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