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