rot13.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;;; rot13.el --- display a buffer in ROT13
  2. ;; Copyright (C) 1988, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Howard Gayle
  4. ;; Maintainer: FSF
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; The entry point, `rot13-other-window', performs a Caesar cipher
  18. ;; encrypt/decrypt on the current buffer and displays the result in another
  19. ;; window. ROT13 encryption is sometimes used on USENET as a read-at-your-
  20. ;; own-risk wrapper for material some might consider offensive, such as
  21. ;; ethnic humor.
  22. ;;
  23. ;; Written by Howard Gayle.
  24. ;; This hack is mainly to show off the char table stuff.
  25. ;;
  26. ;; New entry points, `rot13', `rot13-string', and `rot13-region' that
  27. ;; performs Caesar cipher encrypt/decrypt on buffers and strings, was
  28. ;; added by Simon Josefsson.
  29. ;;; Code:
  30. (defvar rot13-display-table
  31. (let ((table (make-display-table))
  32. (i 0))
  33. (while (< i 26)
  34. (aset table (+ i ?a) (vector (+ (% (+ i 13) 26) ?a)))
  35. (aset table (+ i ?A) (vector (+ (% (+ i 13) 26) ?A)))
  36. (setq i (1+ i)))
  37. table)
  38. "Char table for ROT13 display.")
  39. (defvar rot13-translate-table
  40. (let ((str (make-string 127 0))
  41. (i 0))
  42. (while (< i 127)
  43. (aset str i i)
  44. (setq i (1+ i)))
  45. (setq i 0)
  46. (while (< i 26)
  47. (aset str (+ i ?a) (+ (% (+ i 13) 26) ?a))
  48. (aset str (+ i ?A) (+ (% (+ i 13) 26) ?A))
  49. (setq i (1+ i)))
  50. str)
  51. "String table for ROT13 translation.")
  52. ;;;###autoload
  53. (defun rot13 (object &optional start end)
  54. "Return ROT13 encryption of OBJECT, a buffer or string."
  55. (if (bufferp object)
  56. (with-current-buffer object
  57. (rot13-region start end))
  58. (rot13-string object)))
  59. ;;;###autoload
  60. (defun rot13-string (string)
  61. "Return ROT13 encryption of STRING."
  62. (with-temp-buffer
  63. (insert string)
  64. (rot13-region (point-min) (point-max))
  65. (buffer-string)))
  66. ;;;###autoload
  67. (defun rot13-region (start end)
  68. "ROT13 encrypt the region between START and END in current buffer."
  69. (interactive "r")
  70. (translate-region start end rot13-translate-table))
  71. ;;;###autoload
  72. (defun rot13-other-window ()
  73. "Display current buffer in ROT13 in another window.
  74. The text itself is not modified, only the way it is displayed is affected.
  75. To terminate the ROT13 display, delete that window. As long as that window
  76. is not deleted, any buffer displayed in it will become instantly encoded
  77. in ROT13.
  78. See also `toggle-rot13-mode'."
  79. (interactive)
  80. (let ((w (display-buffer (current-buffer) t)))
  81. (set-window-display-table w rot13-display-table)))
  82. ;;;###autoload
  83. (defun toggle-rot13-mode ()
  84. "Toggle the use of ROT13 encoding for the current window."
  85. (interactive)
  86. (if (eq (window-display-table (selected-window)) rot13-display-table)
  87. (set-window-display-table (selected-window) nil)
  88. (if (null (window-display-table (selected-window)))
  89. (set-window-display-table (selected-window) rot13-display-table))))
  90. (provide 'rot13)
  91. ;;; rot13.el ends here