1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-16 10:50:49 -08:00

* lisp/play/decipher.el: Use lexical-binding

(decipher-mode-syntax-table): Move initialization into declaration.
(decipher-mode, decipher-stats-mode): Use `define-derived-mode`.
(decipher-stats-buffer): Use `buffer-local-value`.
This commit is contained in:
Stefan Monnier 2021-02-10 17:47:18 -05:00
parent 1b4435e6ea
commit 1be27e3bf3

View file

@ -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. ;; Copyright (C) 1995-1996, 2001-2021 Free Software Foundation, Inc.
;; ;;
@ -71,7 +71,7 @@
;; Emacs commands. ;; Emacs commands.
;; ;;
;; Decipher supports Font Lock mode. To use it, you can also add ;; 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 ;; 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 ;; the faces used. I'd like to thank Simon Marshall for his help in making
;; Decipher work well with Font Lock. ;; Decipher work well with Font Lock.
@ -84,6 +84,8 @@
;; 1. The consonant-line shortcut ;; 1. The consonant-line shortcut
;; 2. More functions for analyzing ciphertext ;; 2. More functions for analyzing ciphertext
;;; Code:
;;;=================================================================== ;;;===================================================================
;;; Variables: ;;; Variables:
;;;=================================================================== ;;;===================================================================
@ -139,20 +141,20 @@ the tail of the list."
(defvar decipher-mode-map (defvar decipher-mode-map
(let ((map (make-keymap))) (let ((map (make-keymap)))
(suppress-keymap map) (suppress-keymap map)
(define-key map "A" 'decipher-show-alphabet) (define-key map "A" #'decipher-show-alphabet)
(define-key map "C" 'decipher-complete-alphabet) (define-key map "C" #'decipher-complete-alphabet)
(define-key map "D" 'decipher-digram-list) (define-key map "D" #'decipher-digram-list)
(define-key map "F" 'decipher-frequency-count) (define-key map "F" #'decipher-frequency-count)
(define-key map "M" 'decipher-make-checkpoint) (define-key map "M" #'decipher-make-checkpoint)
(define-key map "N" 'decipher-adjacency-list) (define-key map "N" #'decipher-adjacency-list)
(define-key map "R" 'decipher-restore-checkpoint) (define-key map "R" #'decipher-restore-checkpoint)
(define-key map "U" 'decipher-undo) (define-key map "U" #'decipher-undo)
(define-key map " " 'decipher-keypress) (define-key map " " #'decipher-keypress)
(define-key map [remap undo] 'decipher-undo) (define-key map [remap undo] #'decipher-undo)
(define-key map [remap advertised-undo] 'decipher-undo) (define-key map [remap advertised-undo] #'decipher-undo)
(let ((key ?a)) (let ((key ?a))
(while (<= key ?z) (while (<= key ?z)
(define-key map (vector key) 'decipher-keypress) (define-key map (vector key) #'decipher-keypress)
(cl-incf key))) (cl-incf key)))
map) map)
"Keymap for Decipher mode.") "Keymap for Decipher mode.")
@ -161,24 +163,21 @@ the tail of the list."
(defvar decipher-stats-mode-map (defvar decipher-stats-mode-map
(let ((map (make-keymap))) (let ((map (make-keymap)))
(suppress-keymap map) (suppress-keymap map)
(define-key map "D" 'decipher-digram-list) (define-key map "D" #'decipher-digram-list)
(define-key map "F" 'decipher-frequency-count) (define-key map "F" #'decipher-frequency-count)
(define-key map "N" 'decipher-adjacency-list) (define-key map "N" #'decipher-adjacency-list)
map) map)
"Keymap for Decipher-Stats mode.") "Keymap for Decipher-Stats mode.")
(defvar decipher-mode-syntax-table nil (defvar decipher-mode-syntax-table
"Decipher mode syntax table")
(if decipher-mode-syntax-table
()
(let ((table (make-syntax-table)) (let ((table (make-syntax-table))
(c ?0)) (c ?0))
(while (<= c ?9) (while (<= c ?9)
(modify-syntax-entry c "_" table) ;Digits are not part of words (modify-syntax-entry c "_" table) ;Digits are not part of words
(cl-incf c)) (cl-incf c))
(setq decipher-mode-syntax-table table))) table)
"Decipher mode syntax table")
(defvar-local decipher-alphabet nil) (defvar-local decipher-alphabet nil)
;; This is an alist containing entries (PLAIN-CHAR . CIPHER-CHAR), ;; This is an alist containing entries (PLAIN-CHAR . CIPHER-CHAR),
@ -214,7 +213,6 @@ list of such cons cells.")
(defvar decipher--freqs) (defvar decipher--freqs)
;;;=================================================================== ;;;===================================================================
;;; Code:
;;;=================================================================== ;;;===================================================================
;; Main entry points: ;; Main entry points:
;;-------------------------------------------------------------------- ;;--------------------------------------------------------------------
@ -256,7 +254,7 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ -*-decipher-*-\n)\n\n")
(decipher-mode)) (decipher-mode))
;;;###autoload ;;;###autoload
(defun decipher-mode () (define-derived-mode decipher-mode nil "Decipher"
"Major mode for decrypting monoalphabetic substitution ciphers. "Major mode for decrypting monoalphabetic substitution ciphers.
Lower-case letters enter plaintext. Lower-case letters enter plaintext.
Upper-case letters are commands. 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) Show adjacency list for current letter (lists letters appearing next to it)
\\[decipher-make-checkpoint] Save the current cipher alphabet (checkpoint) \\[decipher-make-checkpoint] Save the current cipher alphabet (checkpoint)
\\[decipher-restore-checkpoint] Restore a saved cipher alphabet (checkpoint)" \\[decipher-restore-checkpoint] Restore a saved cipher alphabet (checkpoint)"
(interactive)
(kill-all-local-variables)
(setq buffer-undo-list t ;Disable undo (setq buffer-undo-list t ;Disable undo
indent-tabs-mode nil ;Do not use tab characters indent-tabs-mode nil) ;Do not use tab characters
major-mode 'decipher-mode
mode-name "Decipher")
(if decipher-force-uppercase (if decipher-force-uppercase
(setq case-fold-search nil)) ;Case is significant when searching (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)) (unless (= (point-min) (point-max))
(decipher-read-alphabet)) (decipher-read-alphabet))
(setq-local font-lock-defaults (setq-local font-lock-defaults
@ -291,7 +283,6 @@ The most useful commands are:
(lambda () (setq buffer-read-only nil (lambda () (setq buffer-read-only nil
buffer-undo-list nil)) buffer-undo-list nil))
nil t) nil t)
(run-mode-hooks 'decipher-mode-hook)
(setq buffer-read-only t)) (setq buffer-read-only t))
(put 'decipher-mode 'mode-class 'special) (put 'decipher-mode 'mode-class 'special)
@ -314,10 +305,10 @@ The most useful commands are:
((= ?> first-char) ((= ?> first-char)
nil) nil)
((= ?\( first-char) ((= ?\( first-char)
(setq decipher-function 'decipher-alphabet-keypress) (setq decipher-function #'decipher-alphabet-keypress)
t) t)
((= ?\) first-char) ((= ?\) first-char)
(setq decipher-function 'decipher-alphabet-keypress) (setq decipher-function #'decipher-alphabet-keypress)
nil) nil)
(t (t
(error "Bad location"))))) (error "Bad location")))))
@ -456,7 +447,7 @@ The most useful commands are:
(decipher-insert plain-char) (decipher-insert plain-char)
(setq case-fold-search t ;Case is not significant (setq case-fold-search t ;Case is not significant
cipher-string (downcase cipher-string)) 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 ;; insert-and-inherit will pick the right face automatically
(while (search-forward-regexp "^:" nil t) (while (search-forward-regexp "^:" nil t)
(setq bound (point-at-eol)) (setq bound (point-at-eol))
@ -868,12 +859,12 @@ Creates the statistics buffer if it doesn't exist."
(aset decipher--after i (make-vector 27 0)))) (aset decipher--after i (make-vector 27 0))))
(if decipher-ignore-spaces (if decipher-ignore-spaces
(progn (progn
(decipher-loop-no-breaks 'decipher--analyze) (decipher-loop-no-breaks #'decipher--analyze)
;; The first character of ciphertext was marked as following a space: ;; The first character of ciphertext was marked as following a space:
(let ((i 26)) (let ((i 26))
(while (>= (cl-decf i) 0) (while (>= (cl-decf i) 0)
(aset (aref decipher--after i) 26 0)))) (aset (aref decipher--after i) 26 0))))
(decipher-loop-with-breaks 'decipher--analyze)) (decipher-loop-with-breaks #'decipher--analyze))
(message "Processing results...") (message "Processing results...")
(setcdr (last decipher--digram-list 2) nil) ;Delete the phony "* " digram (setcdr (last decipher--digram-list 2) nil) ;Delete the phony "* " digram
;; Sort the digram list by frequency and alphabetical order: ;; Sort the digram list by frequency and alphabetical order:
@ -954,18 +945,12 @@ Creates the statistics buffer if it doesn't exist."
;; Statistics Buffer: ;; Statistics Buffer:
;;==================================================================== ;;====================================================================
(defun decipher-stats-mode () (define-derived-mode decipher-stats-mode nil "Decipher-Stats"
"Major mode for displaying ciphertext statistics." "Major mode for displaying ciphertext statistics."
(interactive)
(kill-all-local-variables)
(setq buffer-read-only t (setq buffer-read-only t
buffer-undo-list t ;Disable undo buffer-undo-list t ;Disable undo
case-fold-search nil ;Case is significant when searching case-fold-search nil ;Case is significant when searching
indent-tabs-mode nil ;Do not use tab characters 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))
(put 'decipher-stats-mode 'mode-class 'special) (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) "*"))) (let ((stats-name (concat "*" (buffer-name) "*")))
(setq decipher-stats-buffer (setq decipher-stats-buffer
(if (eq 'decipher-stats-mode (if (eq 'decipher-stats-mode
(cdr-safe (assoc 'major-mode (buffer-local-value 'major-mode
(buffer-local-variables (get-buffer stats-name)))
(get-buffer stats-name)))))
;; We just lost track of the statistics buffer: ;; We just lost track of the statistics buffer:
(get-buffer stats-name) (get-buffer stats-name)
(generate-new-buffer stats-name)))) (generate-new-buffer stats-name))))