Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / rot13.el
CommitLineData
52490bfc 1;;; rot13.el --- display a buffer in ROT13
c88ab9ce 2
c90f2757 3;; Copyright (C) 1988, 2001, 2002, 2003, 2004, 2005,
409cc4a3 4;; 2006, 2007, 2008 Free Software Foundation, Inc.
9750e079 5
55535639 6;; Author: Howard Gayle
e5167999 7;; Maintainer: FSF
3b4a6e27
JB
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
3b4a6e27
JB
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
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
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
3b4a6e27 25
07b3798c 26;;; Commentary:
3b4a6e27 27
280b8e59 28;; The entry point, `rot13-other-window', performs a Caesar cipher
f1180544 29;; encrypt/decrypt on the current buffer and displays the result in another
52490bfc 30;; window. ROT13 encryption is sometimes used on USENET as a read-at-your-
d9ecc911
ER
31;; own-risk wrapper for material some might consider offensive, such as
32;; ethnic humor.
33;;
716e9939 34;; Written by Howard Gayle.
3b4a6e27 35;; This hack is mainly to show off the char table stuff.
280b8e59
SJ
36;;
37;; New entry points, `rot13', `rot13-string', and `rot13-region' that
38;; performs Ceasar cipher encrypt/decrypt on buffers and strings, was
39;; added by Simon Josefsson.
3b4a6e27 40
e5167999
ER
41;;; Code:
42
3b4a6e27
JB
43(defvar rot13-display-table
44 (let ((table (make-display-table))
45 (i 0))
46 (while (< i 26)
342df1e2
RS
47 (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a)))
48 (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A)))
3b4a6e27
JB
49 (setq i (1+ i)))
50 table)
52490bfc 51 "Char table for ROT13 display.")
3b4a6e27 52
280b8e59
SJ
53(defvar rot13-translate-table
54 (let ((str (make-string 127 0))
55 (i 0))
56 (while (< i 127)
57 (aset str i i)
58 (setq i (1+ i)))
59 (setq i 0)
60 (while (< i 26)
61 (aset str (+ i ?a) (+ (% (+ i 13) 26) ?a))
62 (aset str (+ i ?A) (+ (% (+ i 13) 26) ?A))
63 (setq i (1+ i)))
64 str)
52490bfc 65 "String table for ROT13 translation.")
280b8e59
SJ
66
67;;;###autoload
68(defun rot13 (object &optional start end)
52490bfc 69 "Return ROT13 encryption of OBJECT, a buffer or string."
280b8e59
SJ
70 (if (bufferp object)
71 (with-current-buffer object
72 (rot13-region start end))
73 (rot13-string object)))
74
75;;;###autoload
76(defun rot13-string (string)
52490bfc 77 "Return ROT13 encryption of STRING."
280b8e59
SJ
78 (with-temp-buffer
79 (insert string)
80 (rot13-region (point-min) (point-max))
81 (buffer-string)))
82
83;;;###autoload
84(defun rot13-region (start end)
52490bfc 85 "ROT13 encrypt the region between START and END in current buffer."
280b8e59
SJ
86 (interactive "r")
87 (translate-region start end rot13-translate-table))
88
8931b34a 89;;;###autoload
3b4a6e27 90(defun rot13-other-window ()
52490bfc 91 "Display current buffer in ROT13 in another window.
c8c6b45a
EZ
92The text itself is not modified, only the way it is displayed is affected.
93
52490bfc 94To terminate the ROT13 display, delete that window. As long as that window
c8c6b45a 95is not deleted, any buffer displayed in it will become instantly encoded
52490bfc 96in ROT13.
c8c6b45a 97
1604ce98 98See also `toggle-rot13-mode'."
3b4a6e27
JB
99 (interactive)
100 (let ((w (display-buffer (current-buffer) t)))
101 (set-window-display-table w rot13-display-table)))
102
dcd12c89
RS
103;;;###autoload
104(defun toggle-rot13-mode ()
52490bfc 105 "Toggle the use of ROT13 encoding for the current window."
dcd12c89
RS
106 (interactive)
107 (if (eq (window-display-table (selected-window)) rot13-display-table)
108 (set-window-display-table (selected-window) nil)
109 (if (null (window-display-table (selected-window)))
110 (set-window-display-table (selected-window) rot13-display-table))))
111
3b4a6e27 112(provide 'rot13)
c88ab9ce 113
cbee283d 114;; arch-tag: ad5b9ca8-946c-4414-996f-e9b1bf9ec79f
c88ab9ce 115;;; rot13.el ends here