Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; rot13.el --- display a buffer in rot13 |
c88ab9ce | 2 | |
280b8e59 | 3 | ;; Copyright (C) 1988,2002 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 | ||
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 | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
3b4a6e27 JB |
13 | ;; 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 | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
3b4a6e27 | 24 | |
07b3798c | 25 | ;;; Commentary: |
3b4a6e27 | 26 | |
280b8e59 | 27 | ;; The entry point, `rot13-other-window', performs a Caesar cipher |
f1180544 | 28 | ;; encrypt/decrypt on the current buffer and displays the result in another |
716e9939 | 29 | ;; window. Rot13 encryption is sometimes used on USENET as a read-at-your- |
d9ecc911 ER |
30 | ;; own-risk wrapper for material some might consider offensive, such as |
31 | ;; ethnic humor. | |
32 | ;; | |
716e9939 | 33 | ;; Written by Howard Gayle. |
3b4a6e27 | 34 | ;; This hack is mainly to show off the char table stuff. |
280b8e59 SJ |
35 | ;; |
36 | ;; New entry points, `rot13', `rot13-string', and `rot13-region' that | |
37 | ;; performs Ceasar cipher encrypt/decrypt on buffers and strings, was | |
38 | ;; added by Simon Josefsson. | |
3b4a6e27 | 39 | |
e5167999 ER |
40 | ;;; Code: |
41 | ||
3b4a6e27 JB |
42 | (defvar rot13-display-table |
43 | (let ((table (make-display-table)) | |
44 | (i 0)) | |
45 | (while (< i 26) | |
342df1e2 RS |
46 | (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a))) |
47 | (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A))) | |
3b4a6e27 JB |
48 | (setq i (1+ i))) |
49 | table) | |
50 | "Char table for rot 13 display.") | |
51 | ||
280b8e59 SJ |
52 | (defvar rot13-translate-table |
53 | (let ((str (make-string 127 0)) | |
54 | (i 0)) | |
55 | (while (< i 127) | |
56 | (aset str i i) | |
57 | (setq i (1+ i))) | |
58 | (setq i 0) | |
59 | (while (< i 26) | |
60 | (aset str (+ i ?a) (+ (% (+ i 13) 26) ?a)) | |
61 | (aset str (+ i ?A) (+ (% (+ i 13) 26) ?A)) | |
62 | (setq i (1+ i))) | |
63 | str) | |
64 | "String table for rot 13 translation.") | |
65 | ||
66 | ;;;###autoload | |
67 | (defun rot13 (object &optional start end) | |
68 | "Return Rot13 encryption of OBJECT, a buffer or string." | |
69 | (if (bufferp object) | |
70 | (with-current-buffer object | |
71 | (rot13-region start end)) | |
72 | (rot13-string object))) | |
73 | ||
74 | ;;;###autoload | |
75 | (defun rot13-string (string) | |
76 | "Return Rot13 encryption of STRING." | |
77 | (with-temp-buffer | |
78 | (insert string) | |
79 | (rot13-region (point-min) (point-max)) | |
80 | (buffer-string))) | |
81 | ||
82 | ;;;###autoload | |
83 | (defun rot13-region (start end) | |
84 | "Rot13 encrypt the region between START and END in current buffer." | |
85 | (interactive "r") | |
86 | (translate-region start end rot13-translate-table)) | |
87 | ||
8931b34a | 88 | ;;;###autoload |
3b4a6e27 | 89 | (defun rot13-other-window () |
7d002ae1 | 90 | "Display current buffer in rot 13 in another window. |
c8c6b45a EZ |
91 | The text itself is not modified, only the way it is displayed is affected. |
92 | ||
93 | To terminate the rot13 display, delete that window. As long as that window | |
94 | is not deleted, any buffer displayed in it will become instantly encoded | |
95 | in rot 13. | |
96 | ||
1604ce98 | 97 | See also `toggle-rot13-mode'." |
3b4a6e27 JB |
98 | (interactive) |
99 | (let ((w (display-buffer (current-buffer) t))) | |
100 | (set-window-display-table w rot13-display-table))) | |
101 | ||
dcd12c89 RS |
102 | ;;;###autoload |
103 | (defun toggle-rot13-mode () | |
104 | "Toggle the use of rot 13 encoding for the current window." | |
105 | (interactive) | |
106 | (if (eq (window-display-table (selected-window)) rot13-display-table) | |
107 | (set-window-display-table (selected-window) nil) | |
108 | (if (null (window-display-table (selected-window))) | |
109 | (set-window-display-table (selected-window) rot13-display-table)))) | |
110 | ||
3b4a6e27 | 111 | (provide 'rot13) |
c88ab9ce ER |
112 | |
113 | ;;; rot13.el ends here |