X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=mule-caesar.el;h=4bc32ea756592609138d3561053a6c341f55aa06;hb=00d61c72098692fbba3a42e510be515f88d11630;hp=d1e495505bce437928180aa22a9b9632ee8871e9;hpb=3e1bee078b981a209a4d059d686d213b7bf0ddca;p=elisp%2Fapel.git diff --git a/mule-caesar.el b/mule-caesar.el index d1e4955..4bc32ea 100644 --- a/mule-caesar.el +++ b/mule-caesar.el @@ -1,9 +1,8 @@ ;;; mule-caesar.el --- ROT 13-47 Caesar rotation utility -;; Copyright (C) 1997 Free Software Foundation, Inc. +;; Copyright (C) 1997,1998 Free Software Foundation, Inc. ;; Author: MORIOKA Tomohiko -;; Version: $Id: mule-caesar.el,v 1.1 1997-05-09 01:22:58 morioka Exp $ ;; Keywords: ROT 13-47, caesar, mail, news, text/x-rot13-47 ;; This file is part of APEL (A Portable Emacs Library). @@ -23,56 +22,62 @@ ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. -;;; Commentary: - -;; Thanks for Martin Buchholz 's suggestion - ;;; Code: -(defun mule-caesar-string (string &optional stride-ascii) - "Caesar rotation of STRING, and return the result. -Optional argument STRIDE-ASCII is rotation-size for Latin alphabet -\(A-Z and a-z). For non-ASCII text, ROT47 will be performed in any -case." - (setq stride-ascii - (if stride-ascii - (mod stride-ascii 26) - 13)) - (mapconcat (function - (lambda (chr) - (if (< chr 128) - (cond ((and (<= ?A chr) (<= chr ?Z)) - (setq chr (+ chr stride-ascii)) - (if (> chr ?Z) - (setq chr (- chr 26)) - )) - ((and (<= ?a chr) (<= chr ?z)) - (setq chr (+ chr stride-ascii)) - (if (> chr ?z) - (setq chr (- chr 26)) - ))) - (let ((octet (logand chr 127))) - (if (and (< 32 octet) (< octet 127)) - (setq chr - (if (< octet 80) - (+ chr 47) - (- chr 47))) - ))) - (char-to-string chr) - )) string "") - ) +(require 'emu) ; for backward compatibility. +(require 'poe) ; char-after. +(require 'poem) ; charset-chars, char-charset, + ; and split-char. -(defun mule-caesar-region (start end stride-ascii) +(defun mule-caesar-region (start end &optional stride-ascii) "Caesar rotation of current region. Optional argument STRIDE-ASCII is rotation-size for Latin alphabet -\(A-Z and a-z). For non-ASCII text, ROT47 will be performed in any -case." +\(A-Z and a-z). For non-ASCII text, ROT-N/2 will be performed in any +case (N=charset-chars; 94 for 94 or 94x94 graphic character set; 96 +for 96 or 96x96 graphic character set)." (interactive "r\nP") + (setq stride-ascii (if stride-ascii + (mod stride-ascii 26) + 13)) (save-excursion - (let ((str (buffer-substring start end))) - (delete-region start end) - (insert (mule-caesar-string str stride-ascii)) - ))) + (save-restriction + (narrow-to-region start end) + (goto-char start) + (while (< (point)(point-max)) + (let* ((chr (char-after (point)))) + (cond ((and (<= ?A chr) (<= chr ?Z)) + (setq chr (+ chr stride-ascii)) + (if (> chr ?Z) + (setq chr (- chr 26)) + ) + (delete-char 1) + (insert chr) + ) + ((and (<= ?a chr) (<= chr ?z)) + (setq chr (+ chr stride-ascii)) + (if (> chr ?z) + (setq chr (- chr 26)) + ) + (delete-char 1) + (insert chr) + ) + ((<= chr ?\x9f) + (forward-char) + ) + (t + (let* ((stride (lsh (charset-chars (char-charset chr)) -1)) + (ret (mapcar (function + (lambda (octet) + (if (< octet 80) + (+ octet stride) + (- octet stride) + ))) + (cdr (split-char chr))))) + (delete-char 1) + (insert (make-char (char-charset chr) + (car ret)(car (cdr ret)))) + ))) + ))))) (provide 'mule-caesar)