From d804600610b61908b02ead8c98dc52008c2d698e Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Fri, 8 Mar 2024 08:19:01 +0200 Subject: Rewrite addint mcq note types Use gnosis-prompt-mcq-input, and from that split into stem, choices & correct-choice. This should make creation of mcq note types faster --- gnosis.el | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/gnosis.el b/gnosis.el index e17fb7d..866d906 100644 --- a/gnosis.el +++ b/gnosis.el @@ -539,10 +539,10 @@ TAGS: Used to organize notes Refer to `gnosis-add-note--mcq' for more." (let ((deck (gnosis--get-deck-name))) (while (y-or-n-p (format "Add note of type `MCQ' to `%s' deck? " deck)) - (let* ((stem (read-string-from-buffer "Question: " "")) - (input-choices (gnosis-prompt-mcq-choices)) - (choices (car input-choices)) - (correct-choice (cadr input-choices))) + (let* ((input (gnosis-prompt-mcq-input)) + (stem (caar input)) + (choices (cdr (car input))) + (correct-choice (cadr input))) (gnosis-add-note--mcq :deck deck :question stem :choices choices @@ -921,15 +921,24 @@ default value." (setf gnosis-previous-note-hint hint) hint)) -(defun gnosis-prompt-mcq-choices () - "Prompt user for mcq choices." - (let* ((input (split-string - (read-string-from-buffer "Options\nEach '-' corresponds to an option\n-Example Option 1\n-Example Option 2\nYou can add as many options as you want\nCorrect Option must be inside {}" "-\n-") - "-" 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 opiton"))) - (choices (mapcar (lambda (string) (replace-regexp-in-string "{\\|}" "" string)) input))) - (list choices (+ correct-choice-index 1)))) +(defun gnosis-prompt-mcq-input () + "Prompt for MCQ content. + +Return a list of the form ((QUESTION CHOICES) CORRECT-CHOICE-INDEX)." + (let ((user-input (read-string-from-buffer (or (car gnosis-mcq-guidance) "") + (or (cdr gnosis-mcq-guidance) "")))) + (unless (string-match-p gnosis-mcq-seperator user-input) + (error "Seperator %s not found" gnosis-mcq-seperator)) + (let* ((input-seperated (split-string user-input gnosis-mcq-seperator t "[\s\n]")) + (stem (car input-seperated)) + (input (split-string + (mapconcat 'identity (cdr input-seperated) "\n") + gnosis-mcq-option-seperator 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-tags--split (&optional previous-note-tags) "Prompt user for tags, split string by space. -- cgit v1.2.3