emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/mines e127c84 30/43: Add fallback function for read-mul


From: Stefan Monnier
Subject: [elpa] externals/mines e127c84 30/43: Add fallback function for read-multiple-choice
Date: Mon, 30 Nov 2020 18:44:18 -0500 (EST)

branch: externals/mines
commit e127c8487994ca08a3116aee3d5eccf32d404428
Author: Tino Calancha <tino.calancha@gmail.com>
Commit: Tino Calancha <tino.calancha@gmail.com>

    Add fallback function for read-multiple-choice
    
    read-multiple-choice appeared in Emacs 26.  Add fallback
    function for Emacs < 26.
    * mines.el (mines--read-multiple-choice): New function.
    (mines): Use it.
    * mines-test.el (mines-test-read-multiple-choice): Add test.
---
 mines-tests.el | 16 ++++++++++++++++
 mines.el       | 41 +++++++++++++++++++++++++++++++++++------
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/mines-tests.el b/mines-tests.el
index 9bdf9ca..c5deca1 100644
--- a/mines-tests.el
+++ b/mines-tests.el
@@ -229,6 +229,22 @@
               (should (= numb (mines--near-bombs row col))))))))
     (when (buffer-live-p buf) (kill-buffer buf))))
 
+(ert-deftest mines-test-read-multiple-choice ()
+  (if (> emacs-major-version 25)
+      (dolist (char '(?e ?m ?h ??))
+        (cl-letf* (((symbol-function #'read-char) (lambda () char))
+                   (str (cdr (assoc char '((?e . "Easy") (?m . "Medium") (?h . 
"Hard") (?? . "Help"))))))
+          (should (equal (list char str)
+                         (read-multiple-choice "Choose level "
+                                               '((?e "Easy") (?m "Medium") (?h 
"Hard") (?? "Help")))))))
+    (cl-block :help
+      ;; When char is ? `mines--read-multiple-choice' enter a loop until char
+      ;; is a different character.  Exit the loop.
+      (dolist (char '(?e ?m ?h ??))
+        (cl-letf (((symbol-function #'read-char)
+                   (lambda (&rest _) (if (eq char ??) (cl-return-from :help 
nil) char))))
+          (should (mines--read-multiple-choice)))))))
+
 
 (provide 'mines-tests)
 ;;; mines-tests.el ends here
diff --git a/mines.el b/mines.el
index 7cb9496..744ce4d 100644
--- a/mines.el
+++ b/mines.el
@@ -509,18 +509,47 @@ If called again then unflag it."
                 (mines-goto to)
                 (uncover-fn)))))))))
 
+;; `read-multiple-choice' requires Emacs > 25.
+(defun mines--read-multiple-choice ()
+  (let (choice)
+    (if (> emacs-major-version 25)
+        (setq choice
+              (read-multiple-choice "Choose difficulty level: "
+                                    '((?e "Easy" "8 columns x 8 rows and 10 
mines")
+                                      (?m "Medium" "16 columns x 16 rows and 
40 mines")
+                                      (?h "Hard" "30 columns x 16 rows and 99 
mines")
+                                      (?c "Custom" "C columns x R rows and M 
mines"))))
+      (let ((help-msg "Choose difficulty level: 
+
+e: [e] Easy              m: Medium                h: [h] Hard              c: 
[c] Custom
+8 columns x 8 rows       16 columns x 16 rows     30 columns x 16 rows     C 
columns x R rows
+and 10 mines             and 40 mines             and 99 mines             and 
M mines
+")                                                                           
+            (answer
+             (read-char "Choose difficulty level:  ([e] Easy, [m] Medium, [h] 
Hard, [c] Custom, [?]): ")))
+        (cl-flet ((show-help ()
+                             (when (eq answer ??)
+                               (let ((help-buf (get-buffer-create "*Multiple 
Choice Help*")))
+                                 (setq answer nil)
+                                 (with-current-buffer help-buf
+                                   (and (zerop (buffer-size)) (insert 
help-msg))
+                                   (display-buffer help-buf))))))
+          (if (eq answer ??) (show-help))
+          (while (not (memq answer '(?e ?m ?h ?c ??)))
+            (setq answer (read-char "Choose difficulty level:  ([e] Easy, [m] 
Medium, [h] Hard, [c] Custom, [?]): "))
+            (show-help))
+          (cond ((eq answer ?e) (list ?e "Easy" "8 columns x 8 rows and 10 
mines"))
+                ((eq answer ?m) (list ?m "Medium" "16 columns x 16 rows and 40 
mines"))
+                ((eq answer ?h) (list ?h "Hard" "30 columns x 16 rows and 99 
mines"))
+                ((eq answer ?c) (list ?c "Custom" "C columns x R rows and M 
mines"))))))))
+
 ;;;###autoload
 (defun mines (&optional arg)
   "Play the minesweeper game.
 Called with a prefix prompt for the difficulty level."
   (interactive
    (let* ((prefix current-prefix-arg)
-          (choice (and prefix
-                       (read-multiple-choice "Choose difficulty level: "
-                                             '((?e "Easy" "8 columns x 8 rows 
and 10 mines")
-                                               (?m "Medium" "16 columns x 16 
rows and 40 mines")
-                                               (?h "Hard" "30 columns x 16 
rows and 99 mines")
-                                               (?c "Custom" "C columns x R 
rows and M mines"))))))
+          (choice (and prefix (mines--read-multiple-choice))))
      (when choice
        (mines-init (eq ?e (car choice))
                    (eq ?m (car choice))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]