(mule-caesar-region): `stride-ascii' must be optional argument.
[elisp/apel.git] / mule-caesar.el
1 ;;; mule-caesar.el --- ROT 13-47 Caesar rotation utility
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mule-caesar.el,v 1.2 1997-05-09 01:36:41 morioka Exp $
7 ;; Keywords: ROT 13-47, caesar, mail, news, text/x-rot13-47
8
9 ;; This file is part of APEL (A Portable Emacs Library).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Thanks for Martin Buchholz <mrb@eng.sun.com>'s suggestion
29
30 ;;; Code:
31
32 (defun mule-caesar-string (string &optional stride-ascii)
33   "Caesar rotation of STRING, and return the result.
34 Optional argument STRIDE-ASCII is rotation-size for Latin alphabet
35 \(A-Z and a-z).  For non-ASCII text, ROT47 will be performed in any
36 case."
37   (setq stride-ascii
38         (if stride-ascii
39             (mod stride-ascii 26)
40           13))
41   (mapconcat (function
42               (lambda (chr)
43                 (if (< chr 128)
44                     (cond ((and (<= ?A chr) (<= chr ?Z))
45                            (setq chr (+ chr stride-ascii))
46                            (if (> chr ?Z)
47                                (setq chr (- chr 26))
48                              ))
49                           ((and (<= ?a chr) (<= chr ?z))
50                            (setq chr (+ chr stride-ascii))
51                            (if (> chr ?z)
52                                (setq chr (- chr 26))
53                              )))
54                   (let ((octet (logand chr 127)))
55                     (if (and (< 32 octet) (< octet 127))
56                         (setq chr
57                               (if (< octet 80)
58                                   (+ chr 47)
59                                 (- chr 47)))
60                       )))
61                 (char-to-string chr)
62                 )) string "")
63   )
64
65 (defun mule-caesar-region (start end &optional stride-ascii)
66   "Caesar rotation of current region.
67 Optional argument STRIDE-ASCII is rotation-size for Latin alphabet
68 \(A-Z and a-z).  For non-ASCII text, ROT47 will be performed in any
69 case."
70   (interactive "r\nP")
71   (save-excursion
72     (let ((str (buffer-substring start end)))
73       (delete-region start end)
74       (insert (mule-caesar-string str stride-ascii))
75       )))
76
77
78 (provide 'mule-caesar)
79
80 ;;; mule-caesar.el ends here