Merge branch 'master' into comment-cache
[emacs.git] / lisp / rot13.el
blob20a0dbed4622b0c1484801fb5b2ce12342362e72
1 ;;; rot13.el --- display a buffer in ROT13 -*- lexical-binding: t -*-
3 ;; Copyright (C) 1988, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Howard Gayle
6 ;; Maintainer: emacs-devel@gnu.org
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
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.
31 ;; Written by Howard Gayle.
32 ;; This hack is mainly to show off the char table stuff.
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.
38 ;;; Code:
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.")
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.")
64 ;;;###autoload
65 (defun rot13 (object &optional start end)
66 "ROT13 encrypt OBJECT, a buffer or string.
67 If OBJECT is a buffer, encrypt the region between START and END.
68 If OBJECT is a string, encrypt it in its entirety, ignoring START
69 and END, and return the encrypted string."
70 (if (bufferp object)
71 (with-current-buffer object
72 (rot13-region start end))
73 (rot13-string object)))
75 ;;;###autoload
76 (defun rot13-string (string)
77 "Return ROT13 encryption of STRING."
78 (with-temp-buffer
79 (insert string)
80 (rot13-region (point-min) (point-max))
81 (buffer-string)))
83 ;;;###autoload
84 (defun rot13-region (start end)
85 "ROT13 encrypt the region between START and END in current buffer."
86 (interactive "r")
87 (translate-region start end rot13-translate-table))
89 ;;;###autoload
90 (defun rot13-other-window ()
91 "Display current buffer in ROT13 in another window.
92 The text itself is not modified, only the way it is displayed is affected.
94 To terminate the ROT13 display, delete that window. As long as that window
95 is not deleted, any buffer displayed in it will become instantly encoded
96 in ROT13.
98 See also `toggle-rot13-mode'."
99 (interactive)
100 (let ((w (display-buffer (current-buffer) t)))
101 (set-window-display-table w rot13-display-table)))
103 ;;;###autoload
104 (defun toggle-rot13-mode ()
105 "Toggle the use of ROT13 encoding for the current window."
106 (interactive)
107 (if (eq (window-display-table) rot13-display-table)
108 (set-window-display-table (selected-window) nil)
109 (if (null (window-display-table))
110 (set-window-display-table (selected-window) rot13-display-table))))
112 (provide 'rot13)
114 ;;; rot13.el ends here