Modify header.
[elisp/apel.git] / mule-caesar.el
1 ;;; mule-caesar.el --- ROT 13-47 Caesar rotation utility
2
3 ;; Copyright (C) 1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: ROT 13-47, caesar, mail, news, text/x-rot13-47
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 ;; avoid bug of XEmacs
28 (or (integerp (cdr (split-char ?a)))
29     (defun split-char (char)
30       "Return list of charset and one or two position-codes of CHAR."
31       (let ((charset (char-charset char)))
32         (if (eq charset 'ascii)
33             (list charset (char-int char))
34           (let ((i 0)
35                 (len (charset-dimension charset))
36                 (code (if (integerp char)
37                           char
38                         (char-int char)))
39                 dest)
40             (while (< i len)
41               (setq dest (cons (logand code 127) dest)
42                     code (lsh code -7)
43                     i (1+ i)))
44             (cons charset dest)
45             ))))
46     )
47
48 (defun char-to-octet-list (character)
49   "Return list of octets in code table of graphic character set."
50   (cdr (split-char character)))
51
52 (defun mule-caesar-region (start end &optional stride-ascii)
53   "Caesar rotation of current region.
54 Optional argument STRIDE-ASCII is rotation-size for Latin alphabet
55 \(A-Z and a-z).  For non-ASCII text, ROT-N/2 will be performed in any
56 case (N=charset-chars; 94 for 94 or 94x94 graphic character set; 96
57 for 96 or 96x96 graphic character set)."
58   (interactive "r\nP")
59   (setq stride-ascii (if stride-ascii
60                          (mod stride-ascii 26)
61                        13))
62   (save-excursion
63     (save-restriction
64       (narrow-to-region start end)
65       (goto-char start)
66       (while (< (point)(point-max))
67         (let* ((chr (char-after (point)))
68                (charset (char-charset chr))
69                )
70           (if (eq charset 'ascii)
71               (cond ((and (<= ?A chr) (<= chr ?Z))
72                      (setq chr (+ chr stride-ascii))
73                      (if (> chr ?Z)
74                          (setq chr (- chr 26))
75                        )
76                      (delete-char 1)
77                      (insert chr)
78                      )
79                     ((and (<= ?a chr) (<= chr ?z))
80                      (setq chr (+ chr stride-ascii))
81                      (if (> chr ?z)
82                          (setq chr (- chr 26))
83                        )
84                      (delete-char 1)
85                      (insert chr)
86                      )
87                     (t
88                      (forward-char)
89                      ))
90             (let* ((stride (lsh (charset-chars charset) -1))
91                    (ret (mapcar (function
92                                  (lambda (octet)
93                                    (if (< octet 80)
94                                        (+ octet stride)
95                                      (- octet stride)
96                                      )))
97                                 (char-to-octet-list chr))))
98               (delete-char 1)
99               (insert (make-char (char-charset chr)
100                                  (car ret)(car (cdr ret))))
101               )))))))
102   
103
104 (provide 'mule-caesar)
105
106 ;;; mule-caesar.el ends here