summaryrefslogtreecommitdiff
path: root/lisp/play/decipher.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/play/decipher.el')
-rw-r--r--lisp/play/decipher.el104
1 files changed, 44 insertions, 60 deletions
diff --git a/lisp/play/decipher.el b/lisp/play/decipher.el
index 524ca81f30a..47ed6e28b58 100644
--- a/lisp/play/decipher.el
+++ b/lisp/play/decipher.el
@@ -1,4 +1,4 @@
-;;; decipher.el --- cryptanalyze monoalphabetic substitution ciphers
+;;; decipher.el --- cryptanalyze monoalphabetic substitution ciphers -*- lexical-binding: t; -*-
;;
;; Copyright (C) 1995-1996, 2001-2021 Free Software Foundation, Inc.
;;
@@ -71,7 +71,7 @@
;; Emacs commands.
;;
;; Decipher supports Font Lock mode. To use it, you can also add
-;; (add-hook 'decipher-mode-hook 'turn-on-font-lock)
+;; (add-hook 'decipher-mode-hook #'turn-on-font-lock)
;; See the variable `decipher-font-lock-keywords' if you want to customize
;; the faces used. I'd like to thank Simon Marshall for his help in making
;; Decipher work well with Font Lock.
@@ -84,6 +84,8 @@
;; 1. The consonant-line shortcut
;; 2. More functions for analyzing ciphertext
+;;; Code:
+
;;;===================================================================
;;; Variables:
;;;===================================================================
@@ -139,20 +141,20 @@ the tail of the list."
(defvar decipher-mode-map
(let ((map (make-keymap)))
(suppress-keymap map)
- (define-key map "A" 'decipher-show-alphabet)
- (define-key map "C" 'decipher-complete-alphabet)
- (define-key map "D" 'decipher-digram-list)
- (define-key map "F" 'decipher-frequency-count)
- (define-key map "M" 'decipher-make-checkpoint)
- (define-key map "N" 'decipher-adjacency-list)
- (define-key map "R" 'decipher-restore-checkpoint)
- (define-key map "U" 'decipher-undo)
- (define-key map " " 'decipher-keypress)
- (define-key map [remap undo] 'decipher-undo)
- (define-key map [remap advertised-undo] 'decipher-undo)
+ (define-key map "A" #'decipher-show-alphabet)
+ (define-key map "C" #'decipher-complete-alphabet)
+ (define-key map "D" #'decipher-digram-list)
+ (define-key map "F" #'decipher-frequency-count)
+ (define-key map "M" #'decipher-make-checkpoint)
+ (define-key map "N" #'decipher-adjacency-list)
+ (define-key map "R" #'decipher-restore-checkpoint)
+ (define-key map "U" #'decipher-undo)
+ (define-key map " " #'decipher-keypress)
+ (define-key map [remap undo] #'decipher-undo)
+ (define-key map [remap advertised-undo] #'decipher-undo)
(let ((key ?a))
(while (<= key ?z)
- (define-key map (vector key) 'decipher-keypress)
+ (define-key map (vector key) #'decipher-keypress)
(cl-incf key)))
map)
"Keymap for Decipher mode.")
@@ -161,24 +163,21 @@ the tail of the list."
(defvar decipher-stats-mode-map
(let ((map (make-keymap)))
(suppress-keymap map)
- (define-key map "D" 'decipher-digram-list)
- (define-key map "F" 'decipher-frequency-count)
- (define-key map "N" 'decipher-adjacency-list)
+ (define-key map "D" #'decipher-digram-list)
+ (define-key map "F" #'decipher-frequency-count)
+ (define-key map "N" #'decipher-adjacency-list)
map)
-"Keymap for Decipher-Stats mode.")
+ "Keymap for Decipher-Stats mode.")
-(defvar decipher-mode-syntax-table nil
- "Decipher mode syntax table")
-
-(if decipher-mode-syntax-table
- ()
+(defvar decipher-mode-syntax-table
(let ((table (make-syntax-table))
(c ?0))
(while (<= c ?9)
(modify-syntax-entry c "_" table) ;Digits are not part of words
(cl-incf c))
- (setq decipher-mode-syntax-table table)))
+ table)
+ "Decipher mode syntax table")
(defvar-local decipher-alphabet nil)
;; This is an alist containing entries (PLAIN-CHAR . CIPHER-CHAR),
@@ -214,7 +213,6 @@ list of such cons cells.")
(defvar decipher--freqs)
;;;===================================================================
-;;; Code:
;;;===================================================================
;; Main entry points:
;;--------------------------------------------------------------------
@@ -256,7 +254,7 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ -*-decipher-*-\n)\n\n")
(decipher-mode))
;;;###autoload
-(defun decipher-mode ()
+(define-derived-mode decipher-mode nil "Decipher"
"Major mode for decrypting monoalphabetic substitution ciphers.
Lower-case letters enter plaintext.
Upper-case letters are commands.
@@ -272,16 +270,10 @@ The most useful commands are:
Show adjacency list for current letter (lists letters appearing next to it)
\\[decipher-make-checkpoint] Save the current cipher alphabet (checkpoint)
\\[decipher-restore-checkpoint] Restore a saved cipher alphabet (checkpoint)"
- (interactive)
- (kill-all-local-variables)
(setq buffer-undo-list t ;Disable undo
- indent-tabs-mode nil ;Do not use tab characters
- major-mode 'decipher-mode
- mode-name "Decipher")
+ indent-tabs-mode nil) ;Do not use tab characters
(if decipher-force-uppercase
(setq case-fold-search nil)) ;Case is significant when searching
- (use-local-map decipher-mode-map)
- (set-syntax-table decipher-mode-syntax-table)
(unless (= (point-min) (point-max))
(decipher-read-alphabet))
(setq-local font-lock-defaults
@@ -291,7 +283,6 @@ The most useful commands are:
(lambda () (setq buffer-read-only nil
buffer-undo-list nil))
nil t)
- (run-mode-hooks 'decipher-mode-hook)
(setq buffer-read-only t))
(put 'decipher-mode 'mode-class 'special)
@@ -301,7 +292,7 @@ The most useful commands are:
(defun decipher-keypress ()
"Enter a plaintext or ciphertext character."
- (interactive)
+ (interactive nil decipher-mode)
(let ((decipher-function 'decipher-set-map)
buffer-read-only) ;Make buffer writable
(save-excursion
@@ -314,10 +305,10 @@ The most useful commands are:
((= ?> first-char)
nil)
((= ?\( first-char)
- (setq decipher-function 'decipher-alphabet-keypress)
+ (setq decipher-function #'decipher-alphabet-keypress)
t)
((= ?\) first-char)
- (setq decipher-function 'decipher-alphabet-keypress)
+ (setq decipher-function #'decipher-alphabet-keypress)
nil)
(t
(error "Bad location")))))
@@ -355,7 +346,7 @@ The most useful commands are:
(defun decipher-undo ()
"Undo a change in Decipher mode."
- (interactive)
+ (interactive nil decipher-mode)
;; If we don't get all the way thru, make last-command indicate that
;; for the following command.
(setq this-command t)
@@ -456,7 +447,7 @@ The most useful commands are:
(decipher-insert plain-char)
(setq case-fold-search t ;Case is not significant
cipher-string (downcase cipher-string))
- (let ((font-lock-fontify-region-function 'ignore))
+ (let ((font-lock-fontify-region-function #'ignore))
;; insert-and-inherit will pick the right face automatically
(while (search-forward-regexp "^:" nil t)
(setq bound (point-at-eol))
@@ -496,7 +487,7 @@ The most useful commands are:
This records the current alphabet so you can return to it later.
You may have any number of checkpoints.
Type `\\[decipher-restore-checkpoint]' to restore a checkpoint."
- (interactive "sCheckpoint description: ")
+ (interactive "sCheckpoint description: " decipher-mode)
(or (stringp desc)
(setq desc ""))
(let (alphabet
@@ -523,7 +514,7 @@ If point is not on a checkpoint line, moves to the first checkpoint line.
If point is on a checkpoint, restores that checkpoint.
Type `\\[decipher-make-checkpoint]' to make a checkpoint."
- (interactive)
+ (interactive nil decipher-mode)
(beginning-of-line)
(if (looking-at "%!\\([A-Z ]+\\)!")
;; Restore this checkpoint:
@@ -551,7 +542,7 @@ Type `\\[decipher-make-checkpoint]' to make a checkpoint."
This fills any blanks in the cipher alphabet with the unused letters
in alphabetical order. Use this when you have a keyword cipher and
you have determined the keyword."
- (interactive)
+ (interactive nil decipher-mode)
(let ((cipher-char ?A)
(ptr decipher-alphabet)
buffer-read-only ;Make buffer writable
@@ -568,7 +559,7 @@ you have determined the keyword."
(defun decipher-show-alphabet ()
"Display the current cipher alphabet in the message line."
- (interactive)
+ (interactive nil decipher-mode)
(message "%s"
(mapconcat (lambda (a)
(concat
@@ -581,7 +572,7 @@ you have determined the keyword."
"Reprocess the buffer using the alphabet from the top.
This regenerates all deciphered plaintext and clears the undo list.
You should use this if you edit the ciphertext."
- (interactive)
+ (interactive nil decipher-mode)
(message "Reprocessing buffer...")
(let (alphabet
buffer-read-only ;Make buffer writable
@@ -625,13 +616,13 @@ You should use this if you edit the ciphertext."
(defun decipher-frequency-count ()
"Display the frequency count in the statistics buffer."
- (interactive)
+ (interactive nil decipher-mode)
(decipher-analyze)
(decipher-display-regexp "^A" "^[A-Z][A-Z]"))
(defun decipher-digram-list ()
"Display the list of digrams in the statistics buffer."
- (interactive)
+ (interactive nil decipher-mode)
(decipher-analyze)
(decipher-display-regexp "[A-Z][A-Z] +[0-9]" "^$"))
@@ -648,7 +639,7 @@ words, and ends 3 words (`*' represents a space). X comes before 8
different letters, after 7 different letters, and is next to a total
of 11 different letters. It occurs 14 times, making up 9% of the
ciphertext."
- (interactive (list (upcase (following-char))))
+ (interactive (list (upcase (following-char))) decipher-mode)
(decipher-analyze)
(let (start end)
(with-current-buffer (decipher-stats-buffer)
@@ -868,12 +859,12 @@ Creates the statistics buffer if it doesn't exist."
(aset decipher--after i (make-vector 27 0))))
(if decipher-ignore-spaces
(progn
- (decipher-loop-no-breaks 'decipher--analyze)
+ (decipher-loop-no-breaks #'decipher--analyze)
;; The first character of ciphertext was marked as following a space:
(let ((i 26))
(while (>= (cl-decf i) 0)
(aset (aref decipher--after i) 26 0))))
- (decipher-loop-with-breaks 'decipher--analyze))
+ (decipher-loop-with-breaks #'decipher--analyze))
(message "Processing results...")
(setcdr (last decipher--digram-list 2) nil) ;Delete the phony "* " digram
;; Sort the digram list by frequency and alphabetical order:
@@ -954,18 +945,12 @@ Creates the statistics buffer if it doesn't exist."
;; Statistics Buffer:
;;====================================================================
-(defun decipher-stats-mode ()
+(define-derived-mode decipher-stats-mode nil "Decipher-Stats"
"Major mode for displaying ciphertext statistics."
- (interactive)
- (kill-all-local-variables)
(setq buffer-read-only t
buffer-undo-list t ;Disable undo
case-fold-search nil ;Case is significant when searching
- indent-tabs-mode nil ;Do not use tab characters
- major-mode 'decipher-stats-mode
- mode-name "Decipher-Stats")
- (use-local-map decipher-stats-mode-map)
- (run-mode-hooks 'decipher-stats-mode-hook))
+ indent-tabs-mode nil)) ;Do not use tab characters
(put 'decipher-stats-mode 'mode-class 'special)
;;--------------------------------------------------------------------
@@ -1001,9 +986,8 @@ if it can't, it signals an error."
(let ((stats-name (concat "*" (buffer-name) "*")))
(setq decipher-stats-buffer
(if (eq 'decipher-stats-mode
- (cdr-safe (assoc 'major-mode
- (buffer-local-variables
- (get-buffer stats-name)))))
+ (buffer-local-value 'major-mode
+ (get-buffer stats-name)))
;; We just lost track of the statistics buffer:
(get-buffer stats-name)
(generate-new-buffer stats-name))))