| 1 | ;;; rot13.el --- display a buffer in ROT13 |
| 2 | |
| 3 | ;; Copyright (C) 1988, 2001-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Howard Gayle |
| 6 | ;; Maintainer: emacs-devel@gnu.org |
| 7 | |
| 8 | ;; This file is 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 3 of the License, or |
| 13 | ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | ;;; Commentary: |
| 24 | |
| 25 | ;; The entry point, `rot13-other-window', performs a Caesar cipher |
| 26 | ;; encrypt/decrypt on the current buffer and displays the result in another |
| 27 | ;; window. ROT13 encryption is sometimes used on USENET as a read-at-your- |
| 28 | ;; own-risk wrapper for material some might consider offensive, such as |
| 29 | ;; ethnic humor. |
| 30 | ;; |
| 31 | ;; Written by Howard Gayle. |
| 32 | ;; This hack is mainly to show off the char table stuff. |
| 33 | ;; |
| 34 | ;; New entry points, `rot13', `rot13-string', and `rot13-region' that |
| 35 | ;; performs Caesar cipher encrypt/decrypt on buffers and strings, was |
| 36 | ;; added by Simon Josefsson. |
| 37 | |
| 38 | ;;; Code: |
| 39 | |
| 40 | (defvar rot13-display-table |
| 41 | (let ((table (make-display-table)) |
| 42 | (i 0)) |
| 43 | (while (< i 26) |
| 44 | (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a))) |
| 45 | (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A))) |
| 46 | (setq i (1+ i))) |
| 47 | table) |
| 48 | "Char table for ROT13 display.") |
| 49 | |
| 50 | (defvar rot13-translate-table |
| 51 | (let ((str (make-string 127 0)) |
| 52 | (i 0)) |
| 53 | (while (< i 127) |
| 54 | (aset str i i) |
| 55 | (setq i (1+ i))) |
| 56 | (setq i 0) |
| 57 | (while (< i 26) |
| 58 | (aset str (+ i ?a) (+ (% (+ i 13) 26) ?a)) |
| 59 | (aset str (+ i ?A) (+ (% (+ i 13) 26) ?A)) |
| 60 | (setq i (1+ i))) |
| 61 | str) |
| 62 | "String table for ROT13 translation.") |
| 63 | |
| 64 | ;;;###autoload |
| 65 | (defun rot13 (object &optional start end) |
| 66 | "Return ROT13 encryption of OBJECT, a buffer or string." |
| 67 | (if (bufferp object) |
| 68 | (with-current-buffer object |
| 69 | (rot13-region start end)) |
| 70 | (rot13-string object))) |
| 71 | |
| 72 | ;;;###autoload |
| 73 | (defun rot13-string (string) |
| 74 | "Return ROT13 encryption of STRING." |
| 75 | (with-temp-buffer |
| 76 | (insert string) |
| 77 | (rot13-region (point-min) (point-max)) |
| 78 | (buffer-string))) |
| 79 | |
| 80 | ;;;###autoload |
| 81 | (defun rot13-region (start end) |
| 82 | "ROT13 encrypt the region between START and END in current buffer." |
| 83 | (interactive "r") |
| 84 | (translate-region start end rot13-translate-table)) |
| 85 | |
| 86 | ;;;###autoload |
| 87 | (defun rot13-other-window () |
| 88 | "Display current buffer in ROT13 in another window. |
| 89 | The text itself is not modified, only the way it is displayed is affected. |
| 90 | |
| 91 | To terminate the ROT13 display, delete that window. As long as that window |
| 92 | is not deleted, any buffer displayed in it will become instantly encoded |
| 93 | in ROT13. |
| 94 | |
| 95 | See also `toggle-rot13-mode'." |
| 96 | (interactive) |
| 97 | (let ((w (display-buffer (current-buffer) t))) |
| 98 | (set-window-display-table w rot13-display-table))) |
| 99 | |
| 100 | ;;;###autoload |
| 101 | (defun toggle-rot13-mode () |
| 102 | "Toggle the use of ROT13 encoding for the current window." |
| 103 | (interactive) |
| 104 | (if (eq (window-display-table) rot13-display-table) |
| 105 | (set-window-display-table (selected-window) nil) |
| 106 | (if (null (window-display-table)) |
| 107 | (set-window-display-table (selected-window) rot13-display-table)))) |
| 108 | |
| 109 | (provide 'rot13) |
| 110 | |
| 111 | ;;; rot13.el ends here |