tramp-uu.el 3.0 KB

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