From 5b53be7658bdb7f800e6b36b9b154c9a4fd55f63 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Wed, 24 Apr 2024 00:12:28 +0300 Subject: Refactor gnosis-prompt-mcq-input - Use recursion when incorrect formatting is used - Separate the process of input to a different function --- gnosis.el | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/gnosis.el b/gnosis.el index f614e84..fe128b8 100644 --- a/gnosis.el +++ b/gnosis.el @@ -989,24 +989,20 @@ default value." (setf gnosis-previous-note-hint hint) hint)) -(defun gnosis-prompt-mcq-input () - "Prompt for MCQ content. - -Return a list of the form ((QUESTION CHOICES) CORRECT-CHOICE-INDEX)." - (let ((user-input (gnosis-read-string-from-buffer (or (car gnosis-mcq-guidance) "") - (or (cdr gnosis-mcq-guidance) "")))) - (unless (string-match-p gnosis-mcq-separator user-input) - (error "Separator %s not found" gnosis-mcq-separator)) - (let* ((input-seperated (split-string user-input gnosis-mcq-separator t "[\s\n]")) - (stem (car input-seperated)) - (input (split-string - (mapconcat 'identity (cdr input-seperated) "\n") - gnosis-mcq-option-separator t "[\s\n]")) - (correct-choice-index - (or (cl-position-if (lambda (string) (string-match "{.*}" string)) input) - (error "Correct choice not found. Use {} to indicate the correct option"))) - (choices (mapcar (lambda (string) (replace-regexp-in-string "{\\|}" "" string)) input))) - (list (cons stem choices) (+ correct-choice-index 1))))) +(defun gnosis-prompt-mcq-input (&optional prompt string) + "PROMPT for MCQ note content. + +STRING: Guidance string." + (let ((user-input (gnosis-read-string-from-buffer (or prompt (car gnosis-mcq-guidance) "") + (or string (cdr gnosis-mcq-guidance) "")))) + (cond ((not (string-match-p gnosis-mcq-separator user-input)) + (gnosis-prompt-mcq-input (format "`gnosis-mcq-separator': %s not found!" gnosis-mcq-separator) + user-input)) + ((not (string-match "{.*}" user-input)) + (gnosis-prompt-mcq-input (format "Please wrap the right option with {}") + user-input)) + (t (gnosis-mcq-process-input user-input))))) + (defun gnosis-prompt-tags--split (&optional previous-note-tags) "Prompt user for tags, split string by space. -- cgit v1.2.3