annotate lisp/play/decipher.el @ 112013:f11676feb984

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