emacs-super-duper-yes-or-no

Replace the yes-or-no function with an even more demanding yes or no prompt, rather than with y-or-n.
Log | Files | Refs | LICENSE

commit b440b16626e7aaf3e971eccf2cbb55c09fff429a
parent af952687a43822efcb6f98f5c7aa8590ba0ac072
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Tue,  3 Oct 2023 01:28:00 +0300

Stop misusing throw and catch.

Diffstat:
Msuper-duper-yes-or-no.el | 121++++++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 58 insertions(+), 63 deletions(-)

diff --git a/super-duper-yes-or-no.el b/super-duper-yes-or-no.el @@ -47,79 +47,74 @@ (defun sd--make-random-input-string (wanted-number-of-words) (let ((words ()) - (wordlist-size (seq-length sd-word-list)) - (result-label (gensym))) - (catch result-label - (while t - (let* ((word-index (random wordlist-size)) - (random-word (aref sd-word-list - word-index))) - - (when (= (seq-length words) - wanted-number-of-words) - (throw result-label - words)) - (setq words (cons random-word - words))))))) + (wordlist-size (seq-length sd-word-list))) + (while (not (= (seq-length words) + wanted-number-of-words)) + (let* ((word-index (random wordlist-size)) + (random-word (aref sd-word-list + word-index))) + (setq words (cons random-word + words)))) + words)) (defun sd--make-random-yes-or-no-input-pair (number-of-words) (let ((result-label (gensym)) - (yes-input (sd--make-random-input-string number-of-words))) - (catch result-label - (while t - (let ((no-input (sd--make-random-input-string number-of-words))) - (when (not (equal yes-input - no-input)) - (throw result-label - (list yes-input - no-input)))))))) + (yes-input (sd--make-random-input-string number-of-words)) + (no-input (sd--make-random-input-string number-of-words))) + (while (equal yes-input + no-input) + (setq no-input + (sd--make-random-input-string number-of-words))) + (list yes-input + no-input))) (defun sd--list-intersperse (input-list intersperser) - (cond - ((null input-list) - '()) - ((null (cdr input-list)) - input-list) - (t - (cons (car input-list) - (cons intersperser - (sd--list-intersperse (cdr input-list) - intersperser)))))) + (cond + ((null input-list) + '()) + ((null (cdr input-list)) + input-list) + (t + (cons (car input-list) + (cons intersperser + (sd--list-intersperse (cdr input-list) + intersperser)))))) (defun sd-yes-or-no-p (prompt) "" (interactive) - (let ((result-label (gensym))) - (catch result-label - (while t - (let* ((wanted-yes-or-no (sd--make-random-yes-or-no-input-pair - sd-number-of-words)) - (wanted-yes (car wanted-yes-or-no)) - (wanted-no (cadr wanted-yes-or-no)) - (wanted-yes-string (apply 'concat - (sd--list-intersperse wanted-yes - " "))) - (wanted-no-string (apply 'concat - (sd--list-intersperse wanted-no - " "))) - (user-input - (read-from-minibuffer - (concat prompt - "(Enter \"" - wanted-yes-string - "\" for yes, \"" - wanted-no-string - "\" for no) ")))) - - (when (equal user-input - wanted-yes-string) - (throw result-label - t)) - (when (equal user-input - wanted-no-string) - (throw result-label - '()))))))) + (let ((result nil)) + (while (not result) + (let* ((wanted-yes-or-no (sd--make-random-yes-or-no-input-pair + sd-number-of-words)) + (wanted-yes (car wanted-yes-or-no)) + (wanted-no (cadr wanted-yes-or-no)) + (wanted-yes-string (apply 'concat + (sd--list-intersperse wanted-yes + " "))) + (wanted-no-string (apply 'concat + (sd--list-intersperse wanted-no + " "))) + (user-input + (read-from-minibuffer + (concat prompt + "(Enter \"" + wanted-yes-string + "\" for yes, \"" + wanted-no-string + "\" for no) ")))) + + (cond + ((equal user-input + wanted-yes-string) + (setq result + t)) + ((equal user-input + wanted-no-string) + (setq result + '()))))) + result)) ;; Local Variables: ;; read-symbol-shorthands: (("sd-" . "super-duper-yes-or-no-"))