rot13.el 3.64 KB
Newer Older
1
;;; rot13.el --- display a buffer in ROT13  -*- lexical-binding: t -*-
Eric S. Raymond's avatar
Eric S. Raymond committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1988, 2001-2020 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

5
;; Author: Howard Gayle
6
;; Maintainer: emacs-devel@gnu.org
Jim Blandy's avatar
Jim Blandy committed
7 8 9

;; This file is part of GNU Emacs.

10
;; GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
14 15 16 17 18 19 20

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Jim Blandy's avatar
Jim Blandy committed
22

Richard M. Stallman's avatar
Richard M. Stallman committed
23
;;; Commentary:
Jim Blandy's avatar
Jim Blandy committed
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 29 30
;; own-risk wrapper for material some might consider offensive, such as
;; ethnic humor.
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
31
;; Written by Howard Gayle.
Jim Blandy's avatar
Jim Blandy committed
32
;; This hack is mainly to show off the char table stuff.
33 34
;;
;; New entry points, `rot13', `rot13-string', and `rot13-region' that
Paul Eggert's avatar
Paul Eggert committed
35
;; performs Caesar cipher encrypt/decrypt on buffers and strings, was
36
;; added by Simon Josefsson.
Jim Blandy's avatar
Jim Blandy committed
37

Eric S. Raymond's avatar
Eric S. Raymond committed
38 39
;;; Code:

Jim Blandy's avatar
Jim Blandy committed
40 41 42 43
(defvar rot13-display-table
  (let ((table (make-display-table))
	(i 0))
    (while (< i 26)
44 45
      (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a)))
      (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A)))
Jim Blandy's avatar
Jim Blandy committed
46 47
      (setq i (1+ i)))
    table)
48
  "Char table for ROT13 display.")
Jim Blandy's avatar
Jim Blandy committed
49

50 51 52 53 54 55 56 57 58 59 60 61
(defvar rot13-translate-table
  (let ((str (make-string 127 0))
	(i 0))
    (while (< i 127)
      (aset str i i)
      (setq i (1+ i)))
    (setq i 0)
    (while (< i 26)
      (aset str (+ i ?a) (+ (% (+ i 13) 26) ?a))
      (aset str (+ i ?A) (+ (% (+ i 13) 26) ?A))
      (setq i (1+ i)))
    str)
62
  "String table for ROT13 translation.")
63 64 65

;;;###autoload
(defun rot13 (object &optional start end)
Simen Heggestøyl's avatar
Simen Heggestøyl committed
66 67 68 69
  "ROT13 encrypt OBJECT, a buffer or string.
If OBJECT is a buffer, encrypt the region between START and END.
If OBJECT is a string, encrypt it in its entirety, ignoring START
and END, and return the encrypted string."
70 71 72 73 74 75 76
  (if (bufferp object)
      (with-current-buffer object
	(rot13-region start end))
    (rot13-string object)))

;;;###autoload
(defun rot13-string (string)
77
  "Return ROT13 encryption of STRING."
78 79 80 81 82 83 84
  (with-temp-buffer
    (insert string)
    (rot13-region (point-min) (point-max))
    (buffer-string)))

;;;###autoload
(defun rot13-region (start end)
85
  "ROT13 encrypt the region between START and END in current buffer."
86 87 88
  (interactive "r")
  (translate-region start end rot13-translate-table))

89
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
90
(defun rot13-other-window ()
91
  "Display current buffer in ROT13 in another window.
92 93
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.
97

98
See also `toggle-rot13-mode'."
Jim Blandy's avatar
Jim Blandy committed
99 100 101 102
  (interactive)
  (let ((w (display-buffer (current-buffer) t)))
    (set-window-display-table w rot13-display-table)))

103 104
;;;###autoload
(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 111
	(set-window-display-table (selected-window) rot13-display-table))))

Jim Blandy's avatar
Jim Blandy committed
112
(provide 'rot13)
Eric S. Raymond's avatar
Eric S. Raymond committed
113 114

;;; rot13.el ends here