; ; rot13-47.l ; ; Sep 1, 2011 by FUJIWARA Teruyoshi ; ; Copyright: Copyright (c) 2011 FUJIWARA Teruyoshi ; License: Distributes under the same terms as xyzzy ; ; Installation: ; 1. Copy this file into /path/to/xyzzy/site-lisp. ; 2. Add "(load-library "rot13-47") in your .xyzzy. ; 3. (optional) Byte compile makes these functions faster. ; ; Usage: ; M-x rot13/47-region: replace a text in region into rot13/47-ed string. ; (defun rot13/47-region (from to) "Replace a text in a region into rot13/47-ed string." (interactive "r") (if (> from to) (rotatef from to)) (save-excursion (save-restriction (narrow-to-region from to) (let ((result (rot13/47 (buffer-substring (point-min) (point-max))))) (delete-region (point-min) (point-max)) (insert result))))) (defun rot13 (str) (coerce (mapcar (lambda (c) (cond ((and (char>= c #\a) (char<= c #\m)) (code-char (+ (char-code c) 13))) ((and (char>= c #\n) (char<= c #\z)) (code-char (- (char-code c) 13))) ((and (char>= c #\A) (char<= c #\M)) (code-char (+ (char-code c) 13))) ((and (char>= c #\N) (char<= c #\Z)) (code-char (- (char-code c) 13))) (t c))) (coerce str 'list)) 'string)) (defun rot47 (str) (coerce (mapcar (lambda (c) (cond ((char= c #\LFD) c) ((char= c #\RET) c) ((and (char>= c #\!) (char<= c #\O)) (code-char (+ (char-code c) 47))) ((and (char>= c #\P) (char<= c #\~)) (code-char (- (char-code c) 47))))) (remove #\ESC (coerce str 'list))) 'string)) (defun rot13/47 (str) (apply 'concat (mapcar (lambda (s) (cond ((string-match "^\\$B\\(\\(.\\|\n\\)*\\)" s) (map-jis-to-internal (concat (string #\ESC) "$B" (rot47 (match-string 1))))) ((string-match "^(B\\(\\(.\\|\n\\)*\\)" s) (rot13 (match-string 1))) (t (rot13 s)))) (split-string (map-internal-to-jis str) (string #\ESC)))))