comparison lisp/rot13.el @ 45514:a7fc02accfb3

(rot13-translate-table): New variable. (rot13, rot13-string, rot13-region): New functions.
author Simon Josefsson <jas@extundo.com>
date Sat, 25 May 2002 22:54:10 +0000
parents 253f761ad37b
children e88404e8f2cf
comparison
equal deleted inserted replaced
45513:878a4fae5e98 45514:a7fc02accfb3
1 ;;; rot13.el --- display a buffer in rot13 1 ;;; rot13.el --- display a buffer in rot13
2 2
3 ;; Copyright (C) 1988 Free Software Foundation, Inc. 3 ;; Copyright (C) 1988,2002 Free Software Foundation, Inc.
4 4
5 ;; Author: Howard Gayle 5 ;; Author: Howard Gayle
6 ;; Maintainer: FSF 6 ;; Maintainer: FSF
7 7
8 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA. 23 ;; Boston, MA 02111-1307, USA.
24 24
25 ;;; Commentary: 25 ;;; Commentary:
26 26
27 ;; The single entry point, `rot13-other-window', performs a Caesar cipher 27 ;; The entry point, `rot13-other-window', performs a Caesar cipher
28 ;; encrypt/decrypt on the current buffer and displays the result in another 28 ;; encrypt/decrypt on the current buffer and displays the result in another
29 ;; window. Rot13 encryption is sometimes used on USENET as a read-at-your- 29 ;; window. Rot13 encryption is sometimes used on USENET as a read-at-your-
30 ;; own-risk wrapper for material some might consider offensive, such as 30 ;; own-risk wrapper for material some might consider offensive, such as
31 ;; ethnic humor. 31 ;; ethnic humor.
32 ;; 32 ;;
33 ;; Written by Howard Gayle. 33 ;; Written by Howard Gayle.
34 ;; This hack is mainly to show off the char table stuff. 34 ;; This hack is mainly to show off the char table stuff.
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.
35 39
36 ;;; Code: 40 ;;; Code:
37 41
38 (defvar rot13-display-table 42 (defvar rot13-display-table
39 (let ((table (make-display-table)) 43 (let ((table (make-display-table))
42 (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a))) 46 (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a)))
43 (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A))) 47 (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A)))
44 (setq i (1+ i))) 48 (setq i (1+ i)))
45 table) 49 table)
46 "Char table for rot 13 display.") 50 "Char table for rot 13 display.")
51
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))
47 87
48 ;;;###autoload 88 ;;;###autoload
49 (defun rot13-other-window () 89 (defun rot13-other-window ()
50 "Display current buffer in rot 13 in another window. 90 "Display current buffer in rot 13 in another window.
51 The text itself is not modified, only the way it is displayed is affected. 91 The text itself is not modified, only the way it is displayed is affected.