Commit | Line | Data |
---|---|---|
52490bfc | 1 | ;;; rot13.el --- display a buffer in ROT13 |
c88ab9ce | 2 | |
acaf905b | 3 | ;; Copyright (C) 1988, 2001-2012 Free Software Foundation, Inc. |
9750e079 | 4 | |
55535639 | 5 | ;; Author: Howard Gayle |
e5167999 | 6 | ;; Maintainer: FSF |
3b4a6e27 JB |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3b4a6e27 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
3b4a6e27 JB |
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 | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
3b4a6e27 | 22 | |
07b3798c | 23 | ;;; Commentary: |
3b4a6e27 | 24 | |
280b8e59 | 25 | ;; The entry point, `rot13-other-window', performs a Caesar cipher |
f1180544 | 26 | ;; encrypt/decrypt on the current buffer and displays the result in another |
52490bfc | 27 | ;; window. ROT13 encryption is sometimes used on USENET as a read-at-your- |
d9ecc911 ER |
28 | ;; own-risk wrapper for material some might consider offensive, such as |
29 | ;; ethnic humor. | |
30 | ;; | |
716e9939 | 31 | ;; Written by Howard Gayle. |
3b4a6e27 | 32 | ;; This hack is mainly to show off the char table stuff. |
280b8e59 SJ |
33 | ;; |
34 | ;; New entry points, `rot13', `rot13-string', and `rot13-region' that | |
fe3c5669 | 35 | ;; performs Caesar cipher encrypt/decrypt on buffers and strings, was |
280b8e59 | 36 | ;; added by Simon Josefsson. |
3b4a6e27 | 37 | |
e5167999 ER |
38 | ;;; Code: |
39 | ||
3b4a6e27 JB |
40 | (defvar rot13-display-table |
41 | (let ((table (make-display-table)) | |
42 | (i 0)) | |
43 | (while (< i 26) | |
342df1e2 RS |
44 | (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a))) |
45 | (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A))) | |
3b4a6e27 JB |
46 | (setq i (1+ i))) |
47 | table) | |
52490bfc | 48 | "Char table for ROT13 display.") |
3b4a6e27 | 49 | |
280b8e59 SJ |
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) | |
52490bfc | 62 | "String table for ROT13 translation.") |
280b8e59 SJ |
63 | |
64 | ;;;###autoload | |
65 | (defun rot13 (object &optional start end) | |
52490bfc | 66 | "Return ROT13 encryption of OBJECT, a buffer or string." |
280b8e59 SJ |
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) | |
52490bfc | 74 | "Return ROT13 encryption of STRING." |
280b8e59 SJ |
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) | |
52490bfc | 82 | "ROT13 encrypt the region between START and END in current buffer." |
280b8e59 SJ |
83 | (interactive "r") |
84 | (translate-region start end rot13-translate-table)) | |
85 | ||
8931b34a | 86 | ;;;###autoload |
3b4a6e27 | 87 | (defun rot13-other-window () |
52490bfc | 88 | "Display current buffer in ROT13 in another window. |
c8c6b45a EZ |
89 | The text itself is not modified, only the way it is displayed is affected. |
90 | ||
52490bfc | 91 | To terminate the ROT13 display, delete that window. As long as that window |
c8c6b45a | 92 | is not deleted, any buffer displayed in it will become instantly encoded |
52490bfc | 93 | in ROT13. |
c8c6b45a | 94 | |
1604ce98 | 95 | See also `toggle-rot13-mode'." |
3b4a6e27 JB |
96 | (interactive) |
97 | (let ((w (display-buffer (current-buffer) t))) | |
98 | (set-window-display-table w rot13-display-table))) | |
99 | ||
dcd12c89 RS |
100 | ;;;###autoload |
101 | (defun toggle-rot13-mode () | |
52490bfc | 102 | "Toggle the use of ROT13 encoding for the current window." |
dcd12c89 RS |
103 | (interactive) |
104 | (if (eq (window-display-table (selected-window)) rot13-display-table) | |
105 | (set-window-display-table (selected-window) nil) | |
106 | (if (null (window-display-table (selected-window))) | |
107 | (set-window-display-table (selected-window) rot13-display-table)))) | |
108 | ||
3b4a6e27 | 109 | (provide 'rot13) |
c88ab9ce ER |
110 | |
111 | ;;; rot13.el ends here |