branch: master commit c6af1d9de4ceff7a9b4ad6296c6631706f8f57f2 Author: Tino Calancha <tino.calan...@gmail.com> Commit: Tino Calancha <tino.calan...@gmail.com>
Add fallback function for read-multiple-choice read-multiple-choice appeared in Emacs 26. Add fallback function for Emacs < 26. * packages/mines/mines.el/mines.el (mines--read-multiple-choice): New function. (mines): Use it. * packages/mines/mines.el/mines-test.el (mines-test-read-multiple-choice): Add test. --- packages/mines/mines-tests.el | 16 ++++++++++++++++ packages/mines/mines.el | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/packages/mines/mines-tests.el b/packages/mines/mines-tests.el index 9bdf9ca..c5deca1 100644 --- a/packages/mines/mines-tests.el +++ b/packages/mines/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/packages/mines/mines.el b/packages/mines/mines.el index 7cb9496..744ce4d 100644 --- a/packages/mines/mines.el +++ b/packages/mines/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))