Mercurial > emacs
annotate lisp/play/decipher.el @ 14659:7669c19beda8
Comment change.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Sat, 24 Feb 1996 04:43:05 +0000 |
| parents | 79cf52c9ef93 |
| children | 80562f089595 |
| rev | line source |
|---|---|
| 14523 | 1 ;;; decipher.el --- Cryptanalyze monoalphabetic substitution ciphers |
| 2 ;; | |
|
14545
bd43252760b4
Load cl only when compiling.
Richard M. Stallman <rms@gnu.org>
parents:
14523
diff
changeset
|
3 ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc. |
| 14523 | 4 ;; |
| 5 ;; Author: Christopher J. Madsen <ac608@yfn.ysu.edu> | |
| 6 ;; Keywords: games | |
| 7 ;; | |
| 8 ;; This file is part of GNU Emacs. | |
| 9 ;; | |
| 10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
| 11 ;; it under the terms of the GNU General Public License as published by | |
| 12 ;; the Free Software Foundation; either version 2, or (at your option) | |
| 13 ;; any later version. | |
| 14 ;; | |
| 15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
| 16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 ;; GNU 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 | |
| 22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 23 | |
| 24 ;;; Quick Start: | |
| 25 ;; | |
| 26 ;; To decipher a message, type or load it into a buffer and type | |
| 27 ;; `M-x decipher'. This will format the buffer and place it into | |
| 28 ;; Decipher mode. You can save your work to a file with the normal | |
| 29 ;; Emacs save commands; when you reload the file it will automatically | |
| 30 ;; enter Decipher mode. | |
| 31 ;; | |
| 32 ;; I'm not going to discuss how to go about breaking a cipher; try | |
| 33 ;; your local library for a book on cryptanalysis. One book you might | |
| 34 ;; find is: | |
| 35 ;; Cryptanalysis: A study of ciphers and their solution | |
| 36 ;; Helen Fouche Gaines | |
| 37 ;; ISBN 0-486-20097-3 | |
| 38 | |
| 39 ;;; Commentary: | |
| 40 ;; | |
| 41 ;; This package is designed to help you crack simple substitution | |
| 42 ;; ciphers where one letter stands for another. It works for ciphers | |
| 43 ;; with or without word divisions. (You must set the variable | |
| 44 ;; decipher-ignore-spaces for ciphers without word divisions.) | |
| 45 ;; | |
| 46 ;; First, some quick definitions: | |
| 47 ;; ciphertext The encrypted message (what you start with) | |
| 48 ;; plaintext The decrypted message (what you are trying to get) | |
| 49 ;; | |
| 50 ;; Decipher mode displays ciphertext in uppercase and plaintext in | |
| 51 ;; lowercase. You must enter the plaintext in lowercase; uppercase | |
| 52 ;; letters are interpreted as commands. The ciphertext may be entered | |
| 53 ;; in mixed case; `M-x decipher' will convert it to uppercase. | |
| 54 ;; | |
| 55 ;; Decipher mode depends on special characters in the first column of | |
| 56 ;; each line. The command `M-x decipher' inserts these characters for | |
| 57 ;; you. The characters and their meanings are: | |
| 58 ;; ( The plaintext & ciphertext alphabets on the first line | |
| 59 ;; ) The ciphertext & plaintext alphabets on the second line | |
| 60 ;; : A line of ciphertext (with plaintext below) | |
| 61 ;; > A line of plaintext (with ciphertext above) | |
| 62 ;; % A comment | |
| 63 ;; Each line in the buffer MUST begin with one of these characters (or | |
| 64 ;; be left blank). In addition, comments beginning with `%!' are reserved | |
| 65 ;; for checkpoints; see decipher-make-checkpoint & decipher-restore-checkpoint | |
| 66 ;; for more information. | |
| 67 ;; | |
| 68 ;; While the cipher message may contain digits or punctuation, Decipher | |
| 69 ;; mode will ignore these characters. | |
| 70 ;; | |
| 71 ;; The buffer is made read-only so it can't be modified by normal | |
| 72 ;; Emacs commands. | |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
73 ;; |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
74 ;; Decipher supports Font Lock mode. To use it, you can also add |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
75 ;; (add-hook 'decipher-mode-hook 'turn-on-font-lock) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
76 ;; See the variable `decipher-font-lock-keywords' if you want to customize |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
77 ;; the faces used. I'd like to thank Simon Marshall for his help in making |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
78 ;; Decipher work well with Font Lock. |
| 14523 | 79 |
| 80 ;;; Things To Do: | |
| 81 ;; | |
| 14573 | 82 ;; Email me if you have any suggestions or would like to help. |
| 83 ;; But be aware that I work on Decipher only sporadically. | |
| 84 ;; | |
| 85 ;; 1. The consonant-line shortcut | |
| 86 ;; 2. More functions for analyzing ciphertext | |
| 14523 | 87 |
| 88 ;;;=================================================================== | |
| 89 ;;; Variables: | |
| 90 ;;;=================================================================== | |
| 91 | |
|
14545
bd43252760b4
Load cl only when compiling.
Richard M. Stallman <rms@gnu.org>
parents:
14523
diff
changeset
|
92 (eval-when-compile |
|
bd43252760b4
Load cl only when compiling.
Richard M. Stallman <rms@gnu.org>
parents:
14523
diff
changeset
|
93 (require 'cl)) |
| 14523 | 94 |
| 95 (defvar decipher-force-uppercase t | |
| 96 "*Non-nil means to convert ciphertext to uppercase. | |
| 97 Nil means the case of the ciphertext is preserved. | |
| 98 This variable must be set before typing `\\[decipher]'.") | |
| 99 | |
| 100 (defvar decipher-ignore-spaces nil | |
| 101 "*Non-nil means to ignore spaces and punctuation when counting digrams. | |
| 102 You should set this to `nil' if the cipher message is divided into words, | |
| 103 or `t' if it is not. | |
| 104 This variable is buffer-local.") | |
| 105 (make-variable-buffer-local 'decipher-ignore-spaces) | |
| 106 | |
| 107 (defvar decipher-undo-limit 5000 | |
| 108 "The maximum number of entries in the undo list. | |
| 109 When the undo list exceeds this number, 100 entries are deleted from | |
| 110 the tail of the list.") | |
| 111 | |
| 112 ;; End of user modifiable variables | |
| 113 ;;-------------------------------------------------------------------- | |
| 114 | |
|
14596
afb84c1d7750
(decipher-mode, decipher-set-map, decipher-insert,
Karl Heuer <kwzh@gnu.org>
parents:
14573
diff
changeset
|
115 (defvar decipher-font-lock-keywords |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
116 '(("^:.*" . font-lock-keyword-face) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
117 ("^>.*" . font-lock-string-face) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
118 ("^%!.*" . font-lock-reference-face) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
119 ("^%.*" . font-lock-comment-face) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
120 ("\\`(\\([a-z]+\\) +\\([A-Z]+\\)" |
|
14596
afb84c1d7750
(decipher-mode, decipher-set-map, decipher-insert,
Karl Heuer <kwzh@gnu.org>
parents:
14573
diff
changeset
|
121 (1 font-lock-string-face) |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
122 (2 font-lock-keyword-face)) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
123 ("^)\\([A-Z ]+\\)\\([a-z ]+\\)" |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
124 (1 font-lock-keyword-face) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
125 (2 font-lock-string-face))) |
|
14596
afb84c1d7750
(decipher-mode, decipher-set-map, decipher-insert,
Karl Heuer <kwzh@gnu.org>
parents:
14573
diff
changeset
|
126 "Expressions to fontify in Decipher mode. |
|
14653
79cf52c9ef93
Load cl within eval-when-compile.
Richard M. Stallman <rms@gnu.org>
parents:
14650
diff
changeset
|
127 Ciphertext uses `font-lock-keyword-face', plaintext uses |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
128 `font-lock-string-face', comments use `font-lock-comment-face', and |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
129 checkpoints use `font-lock-reference-face'. You can customize the |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
130 display by changing these variables. For best results, I recommend |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
131 that all faces use the same background color. |
|
14653
79cf52c9ef93
Load cl within eval-when-compile.
Richard M. Stallman <rms@gnu.org>
parents:
14650
diff
changeset
|
132 For example, to display ciphertext in the `bold' face, use |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
133 (add-hook 'decipher-mode-hook |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
134 (lambda () (set (make-local-variable 'font-lock-keyword-face) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
135 'bold))) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
136 in your `.emacs' file.") |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
137 |
| 14523 | 138 (defvar decipher-mode-map nil |
| 139 "Keymap for Decipher mode.") | |
| 140 (if (not decipher-mode-map) | |
| 141 (progn | |
| 142 (setq decipher-mode-map (make-keymap)) | |
| 143 (suppress-keymap decipher-mode-map) | |
| 144 (define-key decipher-mode-map "A" 'decipher-show-alphabet) | |
| 145 (define-key decipher-mode-map "C" 'decipher-complete-alphabet) | |
| 146 (define-key decipher-mode-map "D" 'decipher-digram-list) | |
| 147 (define-key decipher-mode-map "F" 'decipher-frequency-count) | |
| 148 (define-key decipher-mode-map "M" 'decipher-make-checkpoint) | |
| 149 (define-key decipher-mode-map "N" 'decipher-adjacency-list) | |
| 150 (define-key decipher-mode-map "R" 'decipher-restore-checkpoint) | |
| 151 (define-key decipher-mode-map "U" 'decipher-undo) | |
| 152 (define-key decipher-mode-map " " 'decipher-keypress) | |
| 153 (substitute-key-definition 'undo 'decipher-undo | |
| 154 decipher-mode-map global-map) | |
| 155 (substitute-key-definition 'advertised-undo 'decipher-undo | |
| 156 decipher-mode-map global-map) | |
| 157 (let ((key ?a)) | |
| 158 (while (<= key ?z) | |
| 159 (define-key decipher-mode-map (vector key) 'decipher-keypress) | |
| 160 (incf key))))) | |
| 161 | |
| 162 (defvar decipher-stats-mode-map nil | |
| 163 "Keymap for Decipher-Stats mode.") | |
| 164 (if (not decipher-stats-mode-map) | |
| 165 (progn | |
| 166 (setq decipher-stats-mode-map (make-keymap)) | |
| 167 (suppress-keymap decipher-stats-mode-map) | |
| 168 (define-key decipher-stats-mode-map "D" 'decipher-digram-list) | |
| 169 (define-key decipher-stats-mode-map "F" 'decipher-frequency-count) | |
| 170 (define-key decipher-stats-mode-map "N" 'decipher-adjacency-list) | |
| 171 )) | |
| 172 | |
| 173 (defvar decipher-mode-syntax-table nil | |
| 174 "Decipher mode syntax table") | |
| 175 | |
| 176 (if decipher-mode-syntax-table | |
| 177 () | |
| 178 (let ((table (make-syntax-table)) | |
| 179 (c ?0)) | |
| 180 (while (<= c ?9) | |
| 181 (modify-syntax-entry c "_" table) ;Digits are not part of words | |
| 182 (incf c)) | |
| 183 (setq decipher-mode-syntax-table table))) | |
| 184 | |
| 185 (defvar decipher-alphabet nil) | |
| 186 ;; This is an alist containing entries (PLAIN-CHAR . CIPHER-CHAR), | |
| 187 ;; where PLAIN-CHAR runs from ?a to ?z and CIPHER-CHAR is an uppercase | |
| 188 ;; letter or space (which means no mapping is known for that letter). | |
| 189 ;; This *must* contain entries for all lowercase characters. | |
| 190 (make-variable-buffer-local 'decipher-alphabet) | |
| 191 | |
| 192 (defvar decipher-stats-buffer nil | |
| 193 "The buffer which displays statistics for this ciphertext. | |
| 194 Do not access this variable directly, use the function | |
| 195 `decipher-stats-buffer' instead.") | |
| 196 (make-variable-buffer-local 'decipher-stats-buffer) | |
| 197 | |
| 198 (defvar decipher-undo-list-size 0 | |
| 199 "The number of entries in the undo list.") | |
| 200 (make-variable-buffer-local 'decipher-undo-list-size) | |
| 201 | |
| 202 (defvar decipher-undo-list nil | |
| 203 "The undo list for this buffer. | |
| 204 Each element is either a cons cell (PLAIN-CHAR . CIPHER-CHAR) or a | |
| 205 list of such cons cells.") | |
| 206 (make-variable-buffer-local 'decipher-undo-list) | |
| 207 | |
| 208 (defvar decipher-pending-undo-list nil) | |
| 209 | |
| 14573 | 210 ;; The following variables are used by the analysis functions |
| 211 ;; and are defined here to avoid byte-compiler warnings. | |
| 212 ;; Don't mess with them unless you know what you're doing. | |
| 213 (defvar decipher-char nil | |
| 214 "See the functions decipher-loop-with-breaks and decipher-loop-no-breaks.") | |
| 215 (defvar decipher--prev-char) | |
| 216 (defvar decipher--digram) | |
| 217 (defvar decipher--digram-list) | |
| 218 (defvar decipher--before) | |
| 219 (defvar decipher--after) | |
| 220 (defvar decipher--freqs) | |
| 221 | |
| 14523 | 222 ;;;=================================================================== |
| 223 ;;; Code: | |
| 224 ;;;=================================================================== | |
| 225 ;; Main entry points: | |
| 226 ;;-------------------------------------------------------------------- | |
| 227 | |
| 228 ;;;###autoload | |
| 229 (defun decipher () | |
| 230 "Format a buffer of ciphertext for cryptanalysis and enter Decipher mode." | |
| 231 (interactive) | |
| 232 ;; Make sure the buffer ends in a newline: | |
| 233 (goto-char (point-max)) | |
| 234 (or (bolp) | |
| 235 (insert "\n")) | |
| 236 ;; See if it's already in decipher format: | |
| 237 (goto-char (point-min)) | |
| 238 (if (looking-at "^(abcdefghijklmnopqrstuvwxyz \ | |
| 239 ABCDEFGHIJKLMNOPQRSTUVWXYZ -\\*-decipher-\\*-\n)") | |
| 240 (message "Buffer is already formatted, entering Decipher mode...") | |
| 241 ;; Add the alphabet at the beginning of the file | |
| 242 (insert "(abcdefghijklmnopqrstuvwxyz \ | |
| 243 ABCDEFGHIJKLMNOPQRSTUVWXYZ -*-decipher-*-\n)\n\n") | |
| 244 ;; Add lines for the solution: | |
| 245 (let (begin) | |
| 246 (while (not (eobp)) | |
| 247 (if (looking-at "^%") | |
| 248 (forward-line) ;Leave comments alone | |
| 249 (delete-horizontal-space) | |
| 250 (if (eolp) | |
| 251 (forward-line) ;Just leave blank lines alone | |
| 252 (insert ":") ;Mark ciphertext line | |
| 253 (setq begin (point)) | |
| 254 (forward-line) | |
| 255 (if decipher-force-uppercase | |
| 256 (upcase-region begin (point))) ;Convert ciphertext to uppercase | |
| 257 (insert ">\n"))))) ;Mark plaintext line | |
| 258 (delete-blank-lines) ;Remove any blank lines | |
| 259 (delete-blank-lines)) ; at end of buffer | |
| 260 (goto-line 4) | |
| 261 (decipher-mode)) | |
| 262 | |
| 263 ;;;###autoload | |
| 264 (defun decipher-mode () | |
| 265 "Major mode for decrypting monoalphabetic substitution ciphers. | |
| 266 Lower-case letters enter plaintext. | |
| 267 Upper-case letters are commands. | |
| 268 | |
| 269 The buffer is made read-only so that normal Emacs commands cannot | |
| 270 modify it. | |
| 271 | |
| 272 The most useful commands are: | |
| 273 \\<decipher-mode-map> | |
| 274 \\[decipher-digram-list] Display a list of all digrams & their frequency | |
| 275 \\[decipher-frequency-count] Display the frequency of each ciphertext letter | |
| 276 \\[decipher-adjacency-list]\ | |
| 277 Show adjacency list for current letter (lists letters appearing next to it) | |
| 278 \\[decipher-make-checkpoint] Save the current cipher alphabet (checkpoint) | |
| 279 \\[decipher-restore-checkpoint] Restore a saved cipher alphabet (checkpoint)" | |
| 280 (interactive) | |
| 281 (kill-all-local-variables) | |
| 282 (setq buffer-undo-list t ;Disable undo | |
| 283 indent-tabs-mode nil ;Do not use tab characters | |
| 284 major-mode 'decipher-mode | |
| 285 mode-name "Decipher") | |
| 286 (if decipher-force-uppercase | |
| 287 (setq case-fold-search nil)) ;Case is significant when searching | |
| 288 (use-local-map decipher-mode-map) | |
| 289 (set-syntax-table decipher-mode-syntax-table) | |
| 290 (decipher-read-alphabet) | |
|
14596
afb84c1d7750
(decipher-mode, decipher-set-map, decipher-insert,
Karl Heuer <kwzh@gnu.org>
parents:
14573
diff
changeset
|
291 (set (make-local-variable 'font-lock-defaults) |
|
afb84c1d7750
(decipher-mode, decipher-set-map, decipher-insert,
Karl Heuer <kwzh@gnu.org>
parents:
14573
diff
changeset
|
292 '(decipher-font-lock-keywords t)) |
| 14523 | 293 ;; Make the buffer writable when we exit Decipher mode: |
| 294 (make-local-hook 'change-major-mode-hook) | |
| 295 (add-hook 'change-major-mode-hook | |
| 296 (lambda () (setq buffer-read-only nil | |
| 297 buffer-undo-list nil)) | |
| 298 nil t) | |
| 299 (run-hooks 'decipher-mode-hook) | |
| 300 (setq buffer-read-only t)) | |
| 301 (put 'decipher-mode 'mode-class 'special) | |
| 302 | |
| 303 ;;-------------------------------------------------------------------- | |
| 304 ;; Normal key handling: | |
| 305 ;;-------------------------------------------------------------------- | |
| 306 | |
| 307 (defmacro decipher-last-command-char () | |
| 308 ;; Return the char which ran this command (for compatibility with XEmacs) | |
| 309 (if (fboundp 'event-to-character) | |
| 310 '(event-to-character last-command-event) | |
| 311 'last-command-event)) | |
| 312 | |
| 313 (defun decipher-keypress () | |
| 314 "Enter a plaintext or ciphertext character." | |
| 315 (interactive) | |
| 316 (let ((decipher-function 'decipher-set-map) | |
| 317 buffer-read-only) ;Make buffer writable | |
| 318 (save-excursion | |
| 319 (or (save-excursion | |
| 320 (beginning-of-line) | |
| 321 (let ((first-char (following-char))) | |
| 322 (cond | |
| 323 ((= ?: first-char) | |
| 324 t) | |
| 325 ((= ?> first-char) | |
| 326 nil) | |
| 327 ((= ?\( first-char) | |
| 328 (setq decipher-function 'decipher-alphabet-keypress) | |
| 329 t) | |
| 330 ((= ?\) first-char) | |
| 331 (setq decipher-function 'decipher-alphabet-keypress) | |
| 332 nil) | |
| 333 (t | |
| 334 (error "Bad location"))))) | |
| 335 (let (goal-column) | |
| 336 (previous-line 1))) | |
| 337 (let ((char-a (following-char)) | |
| 338 (char-b (decipher-last-command-char))) | |
| 339 (or (and (not (= ?w (char-syntax char-a))) | |
| 340 (= char-b ?\ )) ;Spacebar just advances on non-letters | |
| 341 (funcall decipher-function char-a char-b))))) | |
| 342 (forward-char)) | |
| 343 | |
| 344 (defun decipher-alphabet-keypress (a b) | |
| 345 ;; Handle keypresses in the alphabet lines. | |
| 346 ;; A is the character in the alphabet row (which starts with '(') | |
| 347 ;; B is the character pressed | |
| 348 (cond ((and (>= a ?A) (<= a ?Z)) | |
| 349 ;; If A is uppercase, then it is in the ciphertext alphabet: | |
| 350 (decipher-set-map a b)) | |
| 351 ((and (>= a ?a) (<= a ?z)) | |
| 352 ;; If A is lowercase, then it is in the plaintext alphabet: | |
| 353 (if (= b ?\ ) | |
| 354 ;; We are clearing the association (if any): | |
| 355 (if (/= ?\ (setq b (cdr (assoc a decipher-alphabet)))) | |
| 356 (decipher-set-map b ?\ )) | |
| 357 ;; Associate the plaintext char with the char pressed: | |
| 358 (decipher-set-map b a))) | |
| 359 (t | |
| 360 ;; If A is not a letter, that's a problem: | |
| 361 (error "Bad character")))) | |
| 362 | |
| 363 ;;-------------------------------------------------------------------- | |
| 364 ;; Undo: | |
| 365 ;;-------------------------------------------------------------------- | |
| 366 | |
| 367 (defun decipher-undo () | |
| 368 "Undo a change in Decipher mode." | |
| 369 (interactive) | |
| 370 ;; If we don't get all the way thru, make last-command indicate that | |
| 371 ;; for the following command. | |
| 372 (setq this-command t) | |
| 373 (or (eq major-mode 'decipher-mode) | |
| 374 (error "This buffer is not in Decipher mode")) | |
| 375 (or (eq last-command 'decipher-undo) | |
| 376 (setq decipher-pending-undo-list decipher-undo-list)) | |
| 377 (or decipher-pending-undo-list | |
| 378 (error "No further undo information")) | |
| 379 (let ((undo-rec (pop decipher-pending-undo-list)) | |
| 380 buffer-read-only ;Make buffer writable | |
| 381 redo-map redo-rec undo-map) | |
| 382 (or (consp (car undo-rec)) | |
| 383 (setq undo-rec (list undo-rec))) | |
| 384 (while (setq undo-map (pop undo-rec)) | |
| 385 (setq redo-map (decipher-get-undo (cdr undo-map) (car undo-map))) | |
| 386 (if redo-map | |
| 387 (setq redo-rec | |
| 388 (if (consp (car redo-map)) | |
| 389 (append redo-map redo-rec) | |
| 390 (cons redo-map redo-rec)))) | |
| 391 (decipher-set-map (cdr undo-map) (car undo-map) t)) | |
| 392 (decipher-add-undo redo-rec)) | |
| 393 (setq this-command 'decipher-undo) | |
| 394 (message "Undo!")) | |
| 395 | |
| 396 (defun decipher-add-undo (undo-rec) | |
| 397 "Add UNDO-REC to the undo list." | |
| 398 (if undo-rec | |
| 399 (progn | |
| 400 (push undo-rec decipher-undo-list) | |
| 401 (incf decipher-undo-list-size) | |
| 402 (if (> decipher-undo-list-size decipher-undo-limit) | |
| 403 (let ((new-size (- decipher-undo-limit 100))) | |
| 404 ;; Truncate undo list to NEW-SIZE elements: | |
| 405 (setcdr (nthcdr (1- new-size) decipher-undo-list) nil) | |
| 406 (setq decipher-undo-list-size new-size)))))) | |
| 407 | |
| 408 (defun decipher-get-undo (cipher-char plain-char) | |
| 409 ;; Return an undo record that will undo the result of | |
| 410 ;; (decipher-set-map CIPHER-CHAR PLAIN-CHAR) | |
| 411 ;; We must use copy-list because the original cons cells will be | |
| 412 ;; modified using setcdr. | |
| 413 (let ((cipher-map (copy-list (rassoc cipher-char decipher-alphabet))) | |
| 414 (plain-map (copy-list (assoc plain-char decipher-alphabet)))) | |
| 415 (cond ((equal ?\ plain-char) | |
| 416 cipher-map) | |
| 417 ((equal cipher-char (cdr plain-map)) | |
| 418 nil) ;We aren't changing anything | |
| 419 ((equal ?\ (cdr plain-map)) | |
| 420 (or cipher-map (cons ?\ cipher-char))) | |
| 421 (cipher-map | |
| 422 (list plain-map cipher-map)) | |
| 423 (t | |
| 424 plain-map)))) | |
| 425 | |
| 426 ;;-------------------------------------------------------------------- | |
| 427 ;; Mapping ciphertext and plaintext: | |
| 428 ;;-------------------------------------------------------------------- | |
| 429 | |
| 430 (defun decipher-set-map (cipher-char plain-char &optional no-undo) | |
| 431 ;; Associate a ciphertext letter with a plaintext letter | |
| 432 ;; CIPHER-CHAR must be an uppercase or lowercase letter | |
| 433 ;; PLAIN-CHAR must be a lowercase letter (or a space) | |
| 434 ;; NO-UNDO if non-nil means do not record undo information | |
| 435 ;; Any existing associations for CIPHER-CHAR or PLAIN-CHAR will be erased. | |
| 436 (setq cipher-char (upcase cipher-char)) | |
| 437 (or (and (>= cipher-char ?A) (<= cipher-char ?Z)) | |
| 438 (error "Bad character")) ;Cipher char must be uppercase letter | |
| 439 (or no-undo | |
| 440 (decipher-add-undo (decipher-get-undo cipher-char plain-char))) | |
| 441 (let ((cipher-string (char-to-string cipher-char)) | |
| 442 (plain-string (char-to-string plain-char)) | |
| 443 case-fold-search ;Case is significant | |
| 444 mapping bound) | |
| 445 (save-excursion | |
| 446 (goto-char (point-min)) | |
| 447 (if (setq mapping (rassoc cipher-char decipher-alphabet)) | |
| 448 (progn | |
| 449 (setcdr mapping ?\ ) | |
| 450 (search-forward-regexp (concat "^([a-z]*" | |
| 451 (char-to-string (car mapping)))) | |
| 452 (decipher-insert ?\ ) | |
| 453 (beginning-of-line))) | |
| 454 (if (setq mapping (assoc plain-char decipher-alphabet)) | |
| 455 (progn | |
| 456 (if (/= ?\ (cdr mapping)) | |
| 457 (decipher-set-map (cdr mapping) ?\ t)) | |
| 458 (setcdr mapping cipher-char) | |
| 459 (search-forward-regexp (concat "^([a-z]*" plain-string)) | |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
460 (decipher-insert cipher-char) |
| 14523 | 461 (beginning-of-line))) |
| 462 (search-forward-regexp (concat "^([a-z]+ [A-Z]*" cipher-string)) | |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
463 (decipher-insert plain-char) |
| 14523 | 464 (setq case-fold-search t ;Case is not significant |
| 465 cipher-string (downcase cipher-string)) | |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
466 (let ((font-lock-fontify-region-function 'ignore)) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
467 ;; insert-and-inherit will pick the right face automatically |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
468 (while (search-forward-regexp "^:" nil t) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
469 (setq bound (save-excursion (end-of-line) (point))) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
470 (while (search-forward cipher-string bound 'end) |
|
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
471 (decipher-insert plain-char))))))) |
| 14523 | 472 |
| 473 (defun decipher-insert (char) | |
| 474 ;; Insert CHAR in the row below point. It replaces any existing | |
| 475 ;; character in that position. | |
| 476 (let ((col (1- (current-column)))) | |
| 477 (save-excursion | |
| 478 (forward-line) | |
| 479 (or (= ?\> (following-char)) | |
| 480 (= ?\) (following-char)) | |
| 481 (error "Bad location")) | |
| 482 (move-to-column col t) | |
| 483 (or (eolp) | |
| 484 (delete-char 1)) | |
|
14596
afb84c1d7750
(decipher-mode, decipher-set-map, decipher-insert,
Karl Heuer <kwzh@gnu.org>
parents:
14573
diff
changeset
|
485 (insert-and-inherit char)))) |
| 14523 | 486 |
| 487 ;;-------------------------------------------------------------------- | |
| 488 ;; Checkpoints: | |
| 489 ;;-------------------------------------------------------------------- | |
| 490 ;; A checkpoint is a comment of the form: | |
| 491 ;; %!ABCDEFGHIJKLMNOPQRSTUVWXYZ! Description | |
| 492 ;; Such comments are usually placed at the end of the buffer following | |
| 493 ;; this header (which is inserted by decipher-make-checkpoint): | |
| 494 ;; %--------------------------- | |
| 495 ;; % Checkpoints: | |
| 496 ;; % abcdefghijklmnopqrstuvwxyz | |
| 497 ;; but this is not required; checkpoints can be placed anywhere. | |
| 498 ;; | |
| 499 ;; The description is optional; all that is required is the alphabet. | |
| 500 | |
| 501 (defun decipher-make-checkpoint (desc) | |
| 502 "Checkpoint the current cipher alphabet. | |
| 503 This records the current alphabet so you can return to it later. | |
| 504 You may have any number of checkpoints. | |
| 505 Type `\\[decipher-restore-checkpoint]' to restore a checkpoint." | |
| 506 (interactive "sCheckpoint description: ") | |
| 507 (or (stringp desc) | |
| 508 (setq desc "")) | |
| 509 (let (alphabet | |
| 510 buffer-read-only ;Make buffer writable | |
| 511 mapping) | |
| 512 (goto-char (point-min)) | |
| 513 (re-search-forward "^)") | |
| 514 (move-to-column 27 t) | |
| 515 (setq alphabet (buffer-substring-no-properties (- (point) 26) (point))) | |
| 516 (if (re-search-forward "^%![A-Z ]+!" nil 'end) | |
| 517 nil ; Add new checkpoint with others | |
| 518 (if (re-search-backward "^% *Local Variables:" nil t) | |
| 519 ;; Add checkpoints before local variables list: | |
| 520 (progn (forward-line -1) | |
| 521 (or (looking-at "^ *$") | |
| 522 (progn (forward-line) (insert ?\n) (forward-line -1))))) | |
| 523 (insert "\n%" (make-string 69 ?\-) | |
| 524 "\n% Checkpoints:\n% abcdefghijklmnopqrstuvwxyz\n")) | |
| 525 (beginning-of-line) | |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
526 (insert "%!" alphabet "! " desc ?\n))) |
| 14523 | 527 |
| 528 (defun decipher-restore-checkpoint () | |
| 529 "Restore the cipher alphabet from a checkpoint. | |
| 530 If point is not on a checkpoint line, moves to the first checkpoint line. | |
| 531 If point is on a checkpoint, restores that checkpoint. | |
| 532 | |
| 533 Type `\\[decipher-make-checkpoint]' to make a checkpoint." | |
| 534 (interactive) | |
| 535 (beginning-of-line) | |
| 536 (if (looking-at "%!\\([A-Z ]+\\)!") | |
| 537 ;; Restore this checkpoint: | |
| 538 (let ((alphabet (match-string 1)) | |
| 539 buffer-read-only) ;Make buffer writable | |
| 540 (goto-char (point-min)) | |
| 541 (re-search-forward "^)") | |
| 542 (or (eolp) | |
| 543 (delete-region (point) (progn (end-of-line) (point)))) | |
| 544 (insert alphabet) | |
| 545 (decipher-resync)) | |
| 546 ;; Move to the first checkpoint: | |
| 547 (goto-char (point-min)) | |
| 548 (if (re-search-forward "^%![A-Z ]+!" nil t) | |
| 549 (message "Select the checkpoint to restore and type `%s'" | |
| 550 (substitute-command-keys "\\[decipher-restore-checkpoint]")) | |
| 551 (error "No checkpoints in this buffer")))) | |
| 552 | |
| 553 ;;-------------------------------------------------------------------- | |
| 554 ;; Miscellaneous commands: | |
| 555 ;;-------------------------------------------------------------------- | |
| 556 | |
| 557 (defun decipher-complete-alphabet () | |
| 558 "Complete the cipher alphabet. | |
| 559 This fills any blanks in the cipher alphabet with the unused letters | |
| 560 in alphabetical order. Use this when you have a keyword cipher and | |
| 561 you have determined the keyword." | |
| 562 (interactive) | |
| 563 (let ((cipher-char ?A) | |
| 564 (ptr decipher-alphabet) | |
| 565 buffer-read-only ;Make buffer writable | |
| 566 plain-map undo-rec) | |
| 567 (while (setq plain-map (pop ptr)) | |
| 568 (if (equal ?\ (cdr plain-map)) | |
| 569 (progn | |
| 570 (while (rassoc cipher-char decipher-alphabet) | |
| 571 ;; Find the next unused letter | |
| 572 (incf cipher-char)) | |
| 573 (push (cons ?\ cipher-char) undo-rec) | |
| 574 (decipher-set-map cipher-char (car plain-map) t)))) | |
| 575 (decipher-add-undo undo-rec))) | |
| 576 | |
| 577 (defun decipher-show-alphabet () | |
| 578 "Display the current cipher alphabet in the message line." | |
| 579 (interactive) | |
| 580 (message | |
| 581 (mapconcat (lambda (a) | |
| 582 (concat | |
| 583 (char-to-string (car a)) | |
| 584 (char-to-string (cdr a)))) | |
| 585 decipher-alphabet | |
| 586 ""))) | |
| 587 | |
| 588 (defun decipher-resync () | |
| 589 "Reprocess the buffer using the alphabet from the top. | |
| 590 This regenerates all deciphered plaintext and clears the undo list. | |
| 591 You should use this if you edit the ciphertext." | |
| 592 (interactive) | |
| 593 (message "Reprocessing buffer...") | |
| 594 (let (alphabet | |
| 595 buffer-read-only ;Make buffer writable | |
| 596 mapping) | |
| 597 (save-excursion | |
| 598 (decipher-read-alphabet) | |
| 599 (setq alphabet decipher-alphabet) | |
| 600 (goto-char (point-min)) | |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
601 (and (re-search-forward "^).+" nil t) |
| 14523 | 602 (replace-match ")" nil nil)) |
|
14650
2e418434d5d5
(decipher-mode): Removed nonstandard font-lock support.
Richard M. Stallman <rms@gnu.org>
parents:
14596
diff
changeset
|
603 (while (re-search-forward "^>.+" nil t) |
| 14523 | 604 (replace-match ">" nil nil)) |
| 605 (decipher-read-alphabet) | |
| 606 (while (setq mapping (pop alphabet)) | |
| 607 (or (equal ?\ (cdr mapping)) | |
| 608 (decipher-set-map (cdr mapping) (car mapping)))))) | |
| 609 (setq decipher-undo-list nil | |
| 610 decipher-undo-list-size 0) | |
| 611 (message "Reprocessing buffer...done")) | |
| 612 | |
| 613 ;;-------------------------------------------------------------------- | |
| 614 ;; Miscellaneous functions: | |
| 615 ;;-------------------------------------------------------------------- | |
| 616 | |
| 617 (defun decipher-read-alphabet () | |
| 618 "Build the decipher-alphabet from the alphabet line in the buffer." | |
| 619 (save-excursion | |
| 620 (goto-char (point-min)) | |
| 621 (search-forward-regexp "^)") | |
| 622 (move-to-column 27 t) | |
| 623 (setq decipher-alphabet nil) | |
| 624 (let ((plain-char ?z)) | |
| 625 (while (>= plain-char ?a) | |
| 626 (backward-char) | |
| 627 (push (cons plain-char (following-char)) decipher-alphabet) | |
| 628 (decf plain-char))))) | |
| 629 | |
| 630 ;;;=================================================================== | |
| 631 ;;; Analyzing ciphertext: | |
| 632 ;;;=================================================================== | |
| 633 | |
| 634 (defun decipher-frequency-count () | |
| 635 "Display the frequency count in the statistics buffer." | |
| 636 (interactive) | |
| 637 (decipher-analyze) | |
| 638 (decipher-display-regexp "^A" "^[A-Z][A-Z]")) | |
| 639 | |
| 640 (defun decipher-digram-list () | |
| 641 "Display the list of digrams in the statistics buffer." | |
| 642 (interactive) | |
| 643 (decipher-analyze) | |
| 644 (decipher-display-regexp "[A-Z][A-Z] +[0-9]" "^$")) | |
| 645 | |
| 646 (defun decipher-adjacency-list (cipher-char) | |
| 647 "Display the adjacency list for the letter at point. | |
| 648 The adjacency list shows all letters which come next to CIPHER-CHAR. | |
| 649 | |
| 650 An adjacency list (for the letter X) looks like this: | |
| 651 1 1 1 1 1 3 2 1 3 8 | |
| 652 X: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z * 11 14 9% | |
| 653 1 1 1 2 1 1 2 5 7 | |
| 654 This says that X comes before D once, and after B once. X begins 5 | |
| 655 words, and ends 3 words (`*' represents a space). X comes before 8 | |
| 656 different letters, after 7 differerent letters, and is next to a total | |
| 657 of 11 different letters. It occurs 14 times, making up 9% of the | |
| 658 ciphertext." | |
| 659 (interactive (list (upcase (following-char)))) | |
| 660 (decipher-analyze) | |
| 661 (let (start end) | |
| 662 (save-excursion | |
| 663 (set-buffer (decipher-stats-buffer)) | |
| 664 (goto-char (point-min)) | |
| 665 (or (re-search-forward (format "^%c: " cipher-char) nil t) | |
| 666 (error "Character `%c' is not used in ciphertext." cipher-char)) | |
| 667 (forward-line -1) | |
| 668 (setq start (point)) | |
| 669 (forward-line 3) | |
| 670 (setq end (point))) | |
| 671 (decipher-display-range start end))) | |
| 672 | |
| 673 ;;-------------------------------------------------------------------- | |
| 674 (defun decipher-analyze () | |
| 675 "Perform frequency analysis on the current buffer if necessary." | |
| 676 (cond | |
| 677 ;; If this is the statistics buffer, do nothing: | |
| 678 ((eq major-mode 'decipher-stats-mode)) | |
| 679 ;; If this is the Decipher buffer, see if the stats buffer exists: | |
| 680 ((eq major-mode 'decipher-mode) | |
| 681 (or (and (bufferp decipher-stats-buffer) | |
| 682 (buffer-name decipher-stats-buffer)) | |
| 683 (decipher-analyze-buffer))) | |
| 684 ;; Otherwise: | |
| 685 (t (error "This buffer is not in Decipher mode")))) | |
| 686 | |
| 687 ;;-------------------------------------------------------------------- | |
| 688 (defun decipher-display-range (start end) | |
| 689 "Display text between START and END in the statistics buffer. | |
| 690 START and END are positions in the statistics buffer. Makes the | |
| 691 statistics buffer visible and sizes the window to just fit the | |
| 692 displayed text, but leaves the current window selected." | |
| 693 (let ((stats-buffer (decipher-stats-buffer)) | |
| 694 (current-window (selected-window)) | |
| 695 (pop-up-windows t)) | |
| 696 (or (eq (current-buffer) stats-buffer) | |
| 697 (pop-to-buffer stats-buffer)) | |
| 698 (goto-char start) | |
| 699 (or (one-window-p t) | |
| 700 (enlarge-window (- (1+ (count-lines start end)) (window-height)))) | |
| 701 (recenter 0) | |
| 702 (select-window current-window))) | |
| 703 | |
| 704 (defun decipher-display-regexp (start-regexp end-regexp) | |
| 705 "Display text between two regexps in the statistics buffer. | |
| 706 | |
| 707 START-REGEXP matches the first line to display. | |
| 708 END-REGEXP matches the line after that which ends the display. | |
| 709 The ending line is included in the display unless it is blank." | |
| 710 (let (start end) | |
| 711 (save-excursion | |
| 712 (set-buffer (decipher-stats-buffer)) | |
| 713 (goto-char (point-min)) | |
| 714 (re-search-forward start-regexp) | |
| 715 (beginning-of-line) | |
| 716 (setq start (point)) | |
| 717 (re-search-forward end-regexp) | |
| 718 (beginning-of-line) | |
| 719 (or (looking-at "^ *$") | |
| 720 (forward-line 1)) | |
| 721 (setq end (point))) | |
| 722 (decipher-display-range start end))) | |
| 723 | |
| 724 ;;-------------------------------------------------------------------- | |
| 725 (defun decipher-loop-with-breaks (func) | |
| 726 "Loop through ciphertext, calling FUNC once for each letter & word division. | |
| 727 | |
| 728 FUNC is called with no arguments, and its return value is unimportant. | |
| 729 It may examine `decipher-char' to see the current ciphertext | |
| 730 character. `decipher-char' contains either an uppercase letter or a space. | |
| 731 | |
| 732 FUNC is called exactly once between words, with `decipher-char' set to | |
| 733 a space. | |
| 734 | |
| 735 See `decipher-loop-no-breaks' if you do not care about word divisions." | |
| 736 (let ((decipher-char ?\ ) | |
| 737 (decipher--loop-prev-char ?\ )) | |
| 738 (save-excursion | |
| 739 (goto-char (point-min)) | |
| 740 (funcall func) ;Space marks beginning of first word | |
| 741 (while (search-forward-regexp "^:" nil t) | |
| 742 (while (not (eolp)) | |
| 743 (setq decipher-char (upcase (following-char))) | |
| 744 (or (and (>= decipher-char ?A) (<= decipher-char ?Z)) | |
| 745 (setq decipher-char ?\ )) | |
| 746 (or (and (equal decipher-char ?\ ) | |
| 747 (equal decipher--loop-prev-char ?\ )) | |
| 748 (funcall func)) | |
| 749 (setq decipher--loop-prev-char decipher-char) | |
| 750 (forward-char)) | |
| 751 (or (equal decipher-char ?\ ) | |
| 752 (progn | |
| 753 (setq decipher-char ?\ ; | |
| 754 decipher--loop-prev-char ?\ ) | |
| 755 (funcall func))))))) | |
| 756 | |
| 757 (defun decipher-loop-no-breaks (func) | |
| 758 "Loop through ciphertext, calling FUNC once for each letter. | |
| 759 | |
| 760 FUNC is called with no arguments, and its return value is unimportant. | |
| 761 It may examine `decipher-char' to see the current ciphertext letter. | |
| 762 `decipher-char' contains an uppercase letter. | |
| 763 | |
| 764 Punctuation and spacing in the ciphertext are ignored. | |
| 765 See `decipher-loop-with-breaks' if you care about word divisions." | |
| 766 (let (decipher-char) | |
| 767 (save-excursion | |
| 768 (goto-char (point-min)) | |
| 769 (while (search-forward-regexp "^:" nil t) | |
| 770 (while (not (eolp)) | |
| 771 (setq decipher-char (upcase (following-char))) | |
| 772 (and (>= decipher-char ?A) | |
| 773 (<= decipher-char ?Z) | |
| 774 (funcall func)) | |
| 775 (forward-char)))))) | |
| 776 | |
| 777 ;;-------------------------------------------------------------------- | |
| 778 ;; Perform the analysis: | |
| 779 ;;-------------------------------------------------------------------- | |
| 780 | |
| 781 (defun decipher-insert-frequency-counts (freq-list total) | |
| 782 "Insert frequency counts in current buffer. | |
| 783 Each element of FREQ-LIST is a list (LETTER FREQ ...). | |
| 784 TOTAL is the total number of letters in the ciphertext." | |
| 785 (let ((i 4) temp-list) | |
| 786 (while (> i 0) | |
| 787 (setq temp-list freq-list) | |
| 788 (while temp-list | |
| 789 (insert (caar temp-list) | |
| 790 (format "%4d%3d%% " | |
| 791 (cadar temp-list) | |
| 792 (/ (* 100 (cadar temp-list)) total))) | |
| 793 (setq temp-list (nthcdr 4 temp-list))) | |
| 794 (insert ?\n) | |
| 795 (setq freq-list (cdr freq-list) | |
| 796 i (1- i))))) | |
| 797 | |
| 798 (defun decipher--analyze () | |
| 799 ;; Perform frequency analysis on ciphertext. | |
| 800 ;; | |
| 801 ;; This function is called repeatedly with decipher-char set to each | |
| 14573 | 802 ;; character of ciphertext. It uses decipher--prev-char to remember |
| 14523 | 803 ;; the previous ciphertext character. |
| 804 ;; | |
| 805 ;; It builds several data structures, which must be initialized | |
| 806 ;; before the first call to decipher--analyze. The arrays are | |
| 807 ;; indexed with A = 0, B = 1, ..., Z = 25, SPC = 26 (if used). | |
| 14573 | 808 ;; decipher--after: (initialize to zeros) |
| 14523 | 809 ;; A vector of 26 vectors of 27 integers. The first vector |
| 810 ;; represents the number of times A follows each character, the | |
| 811 ;; second vector represents B, and so on. | |
| 14573 | 812 ;; decipher--before: (initialize to zeros) |
| 813 ;; The same as decipher--after, but representing the number of | |
| 814 ;; times the character precedes each other character. | |
| 815 ;; decipher--digram-list: (initialize to nil) | |
| 14523 | 816 ;; An alist with an entry for each digram (2-character sequence) |
| 817 ;; encountered. Each element is a cons cell (DIGRAM . FREQ), | |
| 818 ;; where DIGRAM is a 2 character string and FREQ is the number | |
| 819 ;; of times it occurs. | |
| 14573 | 820 ;; decipher--freqs: (initialize to zeros) |
| 14523 | 821 ;; A vector of 26 integers, counting the number of occurrences |
| 822 ;; of the corresponding characters. | |
| 14573 | 823 (setq decipher--digram (format "%c%c" decipher--prev-char decipher-char)) |
| 824 (incf (cdr (or (assoc decipher--digram decipher--digram-list) | |
| 825 (car (push (cons decipher--digram 0) | |
| 826 decipher--digram-list))))) | |
| 827 (and (>= decipher--prev-char ?A) | |
| 828 (incf (aref (aref decipher--before (- decipher--prev-char ?A)) | |
| 14523 | 829 (if (equal decipher-char ?\ ) |
| 830 26 | |
| 831 (- decipher-char ?A))))) | |
| 832 (and (>= decipher-char ?A) | |
| 14573 | 833 (incf (aref decipher--freqs (- decipher-char ?A))) |
| 834 (incf (aref (aref decipher--after (- decipher-char ?A)) | |
| 835 (if (equal decipher--prev-char ?\ ) | |
| 14523 | 836 26 |
| 14573 | 837 (- decipher--prev-char ?A))))) |
| 838 (setq decipher--prev-char decipher-char)) | |
| 14523 | 839 |
| 840 (defun decipher--digram-counts (counts) | |
| 841 "Generate the counts for an adjacency list." | |
| 842 (let ((total 0)) | |
| 843 (concat | |
| 844 (mapconcat (lambda (x) | |
| 845 (cond ((> x 99) (incf total) "XX") | |
| 846 ((> x 0) (incf total) (format "%2d" x)) | |
| 847 (t " "))) | |
| 848 counts | |
| 849 "") | |
| 850 (format "%4d" (if (> (aref counts 26) 0) | |
| 851 (1- total) ;Don't count space | |
| 852 total))))) | |
| 853 | |
| 854 (defun decipher--digram-total (before-count after-count) | |
| 855 "Count the number of different letters a letter appears next to." | |
| 856 ;; We do not include spaces (word divisions) in this count. | |
| 857 (let ((total 0) | |
| 858 (i 26)) | |
| 859 (while (>= (decf i) 0) | |
| 860 (if (or (> (aref before-count i) 0) | |
| 861 (> (aref after-count i) 0)) | |
| 862 (incf total))) | |
| 863 total)) | |
| 864 | |
| 865 (defun decipher-analyze-buffer () | |
| 866 "Perform frequency analysis and store results in statistics buffer. | |
| 867 Creates the statistics buffer if it doesn't exist." | |
| 14573 | 868 (let ((decipher--prev-char (if decipher-ignore-spaces ?\ ?\*)) |
| 869 (decipher--before (make-vector 26 nil)) | |
| 870 (decipher--after (make-vector 26 nil)) | |
| 871 (decipher--freqs (make-vector 26 0)) | |
| 14523 | 872 (total-chars 0) |
| 14573 | 873 decipher--digram decipher--digram-list freq-list) |
| 14523 | 874 (message "Scanning buffer...") |
| 875 (let ((i 26)) | |
| 876 (while (>= (decf i) 0) | |
| 14573 | 877 (aset decipher--before i (make-vector 27 0)) |
| 878 (aset decipher--after i (make-vector 27 0)))) | |
| 14523 | 879 (if decipher-ignore-spaces |
| 880 (progn | |
| 881 (decipher-loop-no-breaks 'decipher--analyze) | |
| 882 ;; The first character of ciphertext was marked as following a space: | |
| 883 (let ((i 26)) | |
| 884 (while (>= (decf i) 0) | |
| 14573 | 885 (aset (aref decipher--after i) 26 0)))) |
| 14523 | 886 (decipher-loop-with-breaks 'decipher--analyze)) |
| 887 (message "Processing results...") | |
| 14573 | 888 (setcdr (last decipher--digram-list 2) nil) ;Delete the phony "* " digram |
| 14523 | 889 ;; Sort the digram list by frequency and alphabetical order: |
| 14573 | 890 (setq decipher--digram-list (sort (sort decipher--digram-list |
| 14523 | 891 (lambda (a b) (string< (car a) (car b)))) |
| 892 (lambda (a b) (> (cdr a) (cdr b))))) | |
| 893 ;; Generate the frequency list: | |
| 894 ;; Each element is a list of 3 elements (LETTER FREQ DIFFERENT), | |
| 895 ;; where LETTER is the ciphertext character, FREQ is the number | |
| 896 ;; of times it occurs, and DIFFERENT is the number of different | |
| 897 ;; letters it appears next to. | |
| 898 (let ((i 26)) | |
| 899 (while (>= (decf i) 0) | |
| 900 (setq freq-list | |
| 901 (cons (list (+ i ?A) | |
| 14573 | 902 (aref decipher--freqs i) |
| 903 (decipher--digram-total (aref decipher--before i) | |
| 904 (aref decipher--after i))) | |
| 14523 | 905 freq-list) |
| 14573 | 906 total-chars (+ total-chars (aref decipher--freqs i))))) |
| 14523 | 907 (save-excursion |
| 908 ;; Switch to statistics buffer, creating it if necessary: | |
| 909 (set-buffer (decipher-stats-buffer t)) | |
| 910 ;; This can't happen, but it never hurts to double-check: | |
| 911 (or (eq major-mode 'decipher-stats-mode) | |
| 912 (error "Buffer %s is not in Decipher-Stats mode" (buffer-name))) | |
| 913 (setq buffer-read-only nil) | |
| 914 (erase-buffer) | |
| 915 ;; Display frequency counts for letters A-Z: | |
| 916 (decipher-insert-frequency-counts freq-list total-chars) | |
| 917 (insert ?\n) | |
| 918 ;; Display frequency counts for letters in order of frequency: | |
| 919 (setq freq-list (sort freq-list | |
| 920 (lambda (a b) (> (second a) (second b))))) | |
| 921 (decipher-insert-frequency-counts freq-list total-chars) | |
| 922 ;; Display letters in order of frequency: | |
| 923 (insert ?\n (mapconcat (lambda (a) (char-to-string (car a))) | |
| 924 freq-list nil) | |
| 925 "\n\n") | |
| 926 ;; Display list of digrams in order of frequency: | |
| 14573 | 927 (let* ((rows (floor (+ (length decipher--digram-list) 9) 10)) |
| 14523 | 928 (i rows) |
| 929 temp-list) | |
| 930 (while (> i 0) | |
| 14573 | 931 (setq temp-list decipher--digram-list) |
| 14523 | 932 (while temp-list |
| 933 (insert (caar temp-list) | |
| 934 (format "%3d " | |
| 935 (cdar temp-list))) | |
| 936 (setq temp-list (nthcdr rows temp-list))) | |
| 937 (delete-horizontal-space) | |
| 938 (insert ?\n) | |
| 14573 | 939 (setq decipher--digram-list (cdr decipher--digram-list) |
| 14523 | 940 i (1- i)))) |
| 941 ;; Display adjacency list for each letter, sorted in descending | |
| 942 ;; order of the number of adjacent letters: | |
| 943 (setq freq-list (sort freq-list | |
| 944 (lambda (a b) (> (third a) (third b))))) | |
| 945 (let ((temp-list freq-list) | |
| 946 entry i) | |
| 947 (while (setq entry (pop temp-list)) | |
| 948 (if (equal 0 (second entry)) | |
| 949 nil ;This letter was not used | |
| 950 (setq i (- (car entry) ?A)) | |
| 951 (insert ?\n " " | |
| 14573 | 952 (decipher--digram-counts (aref decipher--before i)) ?\n |
| 14523 | 953 (car entry) |
| 954 ": A B C D E F G H I J K L M N O P Q R S T U V W X Y Z *" | |
| 955 (format "%4d %4d %3d%%\n " | |
| 956 (third entry) (second entry) | |
| 957 (/ (* 100 (second entry)) total-chars)) | |
| 14573 | 958 (decipher--digram-counts (aref decipher--after i)) ?\n)))) |
| 14523 | 959 (setq buffer-read-only t) |
| 960 (set-buffer-modified-p nil) | |
| 961 )) | |
| 962 (message nil)) | |
| 963 | |
| 964 ;;==================================================================== | |
| 965 ;; Statistics Buffer: | |
| 966 ;;==================================================================== | |
| 967 | |
| 968 (defun decipher-stats-mode () | |
| 969 "Major mode for displaying ciphertext statistics." | |
| 970 (interactive) | |
| 971 (kill-all-local-variables) | |
| 972 (setq buffer-read-only t | |
| 973 buffer-undo-list t ;Disable undo | |
| 974 case-fold-search nil ;Case is significant when searching | |
| 975 indent-tabs-mode nil ;Do not use tab characters | |
| 976 major-mode 'decipher-stats-mode | |
| 977 mode-name "Decipher-Stats") | |
| 978 (use-local-map decipher-stats-mode-map) | |
| 979 (run-hooks 'decipher-stats-mode-hook)) | |
| 980 (put 'decipher-stats-mode 'mode-class 'special) | |
| 981 | |
| 982 ;;-------------------------------------------------------------------- | |
| 983 | |
| 984 (defun decipher-display-stats-buffer () | |
| 985 "Make the statistics buffer visible, but do not select it." | |
| 986 (let ((stats-buffer (decipher-stats-buffer)) | |
| 987 (current-window (selected-window))) | |
| 988 (or (eq (current-buffer) stats-buffer) | |
| 989 (progn | |
| 990 (pop-to-buffer stats-buffer) | |
| 991 (select-window current-window))))) | |
| 992 | |
| 993 (defun decipher-stats-buffer (&optional create) | |
| 994 "Return the buffer used for decipher statistics. | |
| 995 If CREATE is non-nil, create the buffer if it doesn't exist. | |
| 996 This is guaranteed to return a buffer in Decipher-Stats mode; | |
| 997 if it can't, it signals an error." | |
| 998 (cond | |
| 999 ;; We may already be in the statistics buffer: | |
| 1000 ((eq major-mode 'decipher-stats-mode) | |
| 1001 (current-buffer)) | |
| 1002 ;; See if decipher-stats-buffer exists: | |
| 1003 ((and (bufferp decipher-stats-buffer) | |
| 1004 (buffer-name decipher-stats-buffer)) | |
| 1005 (or (save-excursion | |
| 1006 (set-buffer decipher-stats-buffer) | |
| 1007 (eq major-mode 'decipher-stats-mode)) | |
| 1008 (error "Buffer %s is not in Decipher-Stats mode" | |
| 1009 (buffer-name decipher-stats-buffer))) | |
| 1010 decipher-stats-buffer) | |
| 1011 ;; Create a new buffer if requested: | |
| 1012 (create | |
| 1013 (let ((stats-name (concat "*" (buffer-name) "*"))) | |
| 1014 (setq decipher-stats-buffer | |
| 1015 (if (eq 'decipher-stats-mode | |
| 1016 (cdr-safe (assoc 'major-mode | |
| 1017 (buffer-local-variables | |
| 1018 (get-buffer stats-name))))) | |
| 1019 ;; We just lost track of the statistics buffer: | |
| 1020 (get-buffer stats-name) | |
| 1021 (generate-new-buffer stats-name)))) | |
| 1022 (save-excursion | |
| 1023 (set-buffer decipher-stats-buffer) | |
| 1024 (decipher-stats-mode)) | |
| 1025 decipher-stats-buffer) | |
| 1026 ;; Give up: | |
| 1027 (t (error "No statistics buffer")))) | |
| 1028 | |
| 1029 ;;==================================================================== | |
| 1030 | |
| 1031 (provide 'decipher) | |
| 1032 | |
| 1033 ;;;(defun decipher-show-undo-list () | |
| 1034 ;;; "Display the undo list (for debugging purposes)." | |
| 1035 ;;; (interactive) | |
| 1036 ;;; (with-output-to-temp-buffer "*Decipher Undo*" | |
| 1037 ;;; (let ((undo-list decipher-undo-list) | |
| 1038 ;;; undo-rec undo-map) | |
| 1039 ;;; (save-excursion | |
| 1040 ;;; (set-buffer "*Decipher Undo*") | |
| 1041 ;;; (while (setq undo-rec (pop undo-list)) | |
| 1042 ;;; (or (consp (car undo-rec)) | |
| 1043 ;;; (setq undo-rec (list undo-rec))) | |
| 1044 ;;; (insert ?\() | |
| 1045 ;;; (while (setq undo-map (pop undo-rec)) | |
| 1046 ;;; (insert (cdr undo-map) (car undo-map) ?\ )) | |
| 1047 ;;; (delete-backward-char 1) | |
| 1048 ;;; (insert ")\n")))))) | |
| 1049 | |
| 1050 ;;; decipher.el ends here |
