(split-char): Redefine if it has bug.
[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.4 1998-06-05 00:26:48 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 ;;; Code:
27
28 ;; avoid bug of XEmacs
29 (or (integerp (cdr (split-char ?a)))
30     (defun split-char (char)
31       "Return list of charset and one or two position-codes of CHAR."
32       (let ((charset (char-charset char)))
33         (if (eq charset 'ascii)
34             (list charset (char-int char))
35           (let ((i 0)
36                 (len (charset-dimension charset))
37                 (code (if (integerp char)
38                           char
39                         (char-int char)))
40                 dest)
41             (while (< i len)
42               (setq dest (cons (logand code 127) dest)
43                     code (lsh code -7)
44                     i (1+ i)))
45             (cons charset dest)
46             ))))
47     )
48
49 (defun char-to-octet-list (character)
50   "Return list of octets in code table of graphic character set."
51   (cdr (split-char character)))
52
53 (defun mule-caesar-region (start end &optional stride-ascii)
54   "Caesar rotation of current region.
55 Optional argument STRIDE-ASCII is rotation-size for Latin alphabet
56 \(A-Z and a-z).  For non-ASCII text, ROT-N/2 will be performed in any
57 case (N=charset-chars; 94 for 94 or 94x94 graphic character set; 96
58 for 96 or 96x96 graphic character set)."
59   (interactive "r\nP")
60   (setq stride-ascii (if stride-ascii
61                          (mod stride-ascii 26)
62                        13))
63   (save-excursion
64     (save-restriction
65       (narrow-to-region start end)
66       (goto-char start)
67       (while (< (point)(point-max))
68         (let* ((chr (char-after (point)))
69                (charset (char-charset chr))
70                )
71           (if (eq charset 'ascii)
72               (cond ((and (<= ?A chr) (<= chr ?Z))
73                      (setq chr (+ chr stride-ascii))
74                      (if (> chr ?Z)
75                          (setq chr (- chr 26))
76                        )
77                      (delete-char 1)
78                      (insert chr)
79                      )
80                     ((and (<= ?a chr) (<= chr ?z))
81                      (setq chr (+ chr stride-ascii))
82                      (if (> chr ?z)
83                          (setq chr (- chr 26))
84                        )
85                      (delete-char 1)
86                      (insert chr)
87                      )
88                     (t
89                      (forward-char)
90                      ))
91             (let* ((stride (lsh (charset-chars charset) -1))
92                    (ret (mapcar (function
93                                  (lambda (octet)
94                                    (if (< octet 80)
95                                        (+ octet stride)
96                                      (- octet stride)
97                                      )))
98                                 (char-to-octet-list chr))))
99               (delete-char 1)
100               (insert (make-char (char-charset chr)
101                                  (car ret)(car (cdr ret))))
102               )))))))
103   
104
105 (provide 'mule-caesar)
106
107 ;;; mule-caesar.el ends here