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