From 6fa11233d74e85246c56e54efabe65948d46a827 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 14:03:44 +0300 Subject: cloze: Add gnosis-create-cloze * Create a cloze string --- gnosis.el | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/gnosis.el b/gnosis.el index fb70cb6..aa94660 100644 --- a/gnosis.el +++ b/gnosis.el @@ -452,29 +452,17 @@ Refer to =gnosis-db-schema-extras' for informations on images stored." do (insert (format "\n%s. %s" option-num option)) (setf option-num (1+ option-num))))) -;; (defun gnosis-add-clozes (sentence clozes &optional cloze-string) -;; "Replace CLOZES in SENTENCE with CLOZE-STRING." -;; (let ((cloze-string (or cloze-string gnosis-cloze-string))) -;; (with-temp-buffer -;; (insert sentence) -;; (goto-char (point-min)) -;; (dolist (cloze clozes) -;; (when (search-forward cloze nil t) -;; (replace-match (propertize cloze-string 'face 'gnosis-face-cloze) nil t))) -;; (buffer-string)))) - -;; (defun gnosis-replace-clozes-with-hints (sentence hints &optional cloze-string) -;; "Replace CLOZE-STRING in SENTENCE with HINTS." -;; (let ((cloze-string (or cloze-string gnosis-cloze-string)) -;; (count 0)) -;; (with-temp-buffer -;; (insert sentence) -;; (goto-char (point-min)) -;; (while (search-forward cloze-string nil t) -;; (when (and (nth count hints) (search-backward cloze-string nil t)) -;; (replace-match (propertize (format "[%s]" (nth count hints)) 'face 'gnosis-face-cloze))) -;; (setq count (1+ count))) -;; (buffer-string)))) +(defun gnosis-cloze-create (str clozes &optional cloze-string) + "Replace CLOZES in STR with CLOZE-STRING." + (cl-assert (listp clozes) nil "Adding clozes: Clozes need to be a list.") + (let ((cloze-string (or cloze-string gnosis-cloze-string))) + (with-temp-buffer + (insert str) + (goto-char (point-min)) + (dolist (cloze clozes) + (when (search-forward cloze nil t) + (replace-match (propertize cloze-string 'face 'gnosis-face-cloze) nil t))) + (buffer-string)))) (defun gnosis-display-basic-answer (answer success user-input) "Display ANSWER. -- cgit v1.2.3 From 3389e5c53ebd0122d1fd3fa7f303a26ab0b80298 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 14:04:22 +0300 Subject: cloze: Add gnosis-cloze-add-hints. * Add hints to cloze string. --- gnosis.el | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gnosis.el b/gnosis.el index aa94660..f7bb550 100644 --- a/gnosis.el +++ b/gnosis.el @@ -464,6 +464,19 @@ Refer to =gnosis-db-schema-extras' for informations on images stored." (replace-match (propertize cloze-string 'face 'gnosis-face-cloze) nil t))) (buffer-string)))) +(defun gnosis-cloze-add-hints (str hints &optional cloze-string) + "Replace CLOZE-STRING in STR with HINTS." + (cl-assert (listp hints) nil "Hints must be a list.") + (let ((cloze-string (or cloze-string gnosis-cloze-string)) + (count 0)) + (with-temp-buffer + (insert str) + (goto-char (point-min)) + (while (search-forward cloze-string nil t) + (when (and (nth count hints) (search-backward cloze-string nil t)) + (replace-match (propertize (format "[%s]" (nth count hints)) 'face 'gnosis-face-cloze))) + (setq count (1+ count))) + (buffer-string)))) (defun gnosis-display-basic-answer (answer success user-input) "Display ANSWER. -- cgit v1.2.3 From 50be722192a13bb757d27295e8f107a41ad7229a Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 14:04:48 +0300 Subject: cloze: Add gnosis-cloze-mark-answers * Mark answers in cloze review using FACE. --- gnosis.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gnosis.el b/gnosis.el index f7bb550..7d66ab5 100644 --- a/gnosis.el +++ b/gnosis.el @@ -477,6 +477,18 @@ Refer to =gnosis-db-schema-extras' for informations on images stored." (replace-match (propertize (format "[%s]" (nth count hints)) 'face 'gnosis-face-cloze))) (setq count (1+ count))) (buffer-string)))) + +(defun gnosis-cloze-mark-answers (str answers face) + "Mark ANSWERS in STR with FACE." + (cl-assert (listp answers) nil "Answers to mark must be a list.") + (with-temp-buffer + (insert str) + (goto-char (point-min)) + (dolist (answer answers) + (when (search-forward answer nil t) + (replace-match (propertize answer 'face face) nil t))) + (buffer-string))) + (defun gnosis-display-basic-answer (answer success user-input) "Display ANSWER. -- cgit v1.2.3 From 9ec7dea464f0498fbe83f14158402f753495cd77 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 14:05:17 +0300 Subject: cloze: Add gnosis-cloze-mark-false * Mark false answers. --- gnosis.el | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gnosis.el b/gnosis.el index 7d66ab5..a8dd390 100644 --- a/gnosis.el +++ b/gnosis.el @@ -489,6 +489,20 @@ Refer to =gnosis-db-schema-extras' for informations on images stored." (replace-match (propertize answer 'face face) nil t))) (buffer-string))) +(defun gnosis-cloze-mark-false (str answers) + "Mark contents of STR as false for ANSWERS. + +First item of answers will be marked as false, while the rest unanswered." + (let* ((false (car answers)) + (unanswered (cdr answers)) + (str-with-false (and answers (gnosis-cloze-mark-answers str (list false) 'gnosis-face-false))) + final) + (if unanswered + (setq final (gnosis-cloze-mark-answers str-with-false (if (listp unanswered) unanswered + (list unanswered)) + 'underline)) + (setq final (or str-with-false str))) + final)) (defun gnosis-display-basic-answer (answer success user-input) "Display ANSWER. -- cgit v1.2.3 From ae64248b4ae49055d94e178e39edd0960132fd57 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 14:05:51 +0300 Subject: cloze: Add gnosis-display-cloze-string. * Display cloze string appropriately during note review. --- gnosis.el | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/gnosis.el b/gnosis.el index a8dd390..f5e6d1a 100644 --- a/gnosis.el +++ b/gnosis.el @@ -503,6 +503,20 @@ First item of answers will be marked as false, while the rest unanswered." 'underline)) (setq final (or str-with-false str))) final)) + +(defun gnosis-display-cloze-string (str clozes hints correct false) + "Display STR with CLOZES and HINTS. + +Applies highlighting for CORRECT & FALSE." + (let* ((cloze-str (gnosis-cloze-create str clozes)) + (str-with-hints (gnosis-cloze-add-hints cloze-str hints)) + (str-with-c-answers (gnosis-cloze-mark-answers str-with-hints correct 'gnosis-face-correct)) + (final (gnosis-cloze-mark-false str-with-c-answers false))) + (erase-buffer) + (insert "\n" (gnosis-center-string final)) + (gnosis-insert-separator) + (gnosis-apply-syntax-overlay))) + (defun gnosis-display-basic-answer (answer success user-input) "Display ANSWER. @@ -1480,21 +1494,18 @@ If user-input is equal to CLOZE, return t." (hints (gnosis-get 'options 'notes `(= id ,id))) (success nil)) ;; Initially display the sentence with no reveals - (message "reviewing %d" id) - ;; (when (or (stringp hints) - ;; (null hints)) - ;; (setq hints nil)) - ;; quick fix for old versions of gnosis cloze. - (message "Using deprecated cloze hints.") - (gnosis-display-cloze-sentence main clozes hints 0) + (gnosis-display-cloze-string main clozes hints nil nil) (cl-loop for cloze in clozes do (let ((input (gnosis-review-cloze--input cloze))) (if (equal (car input) t) ;; Correct answer -> reveal the current cloze - (progn (setq num (1+ num)) - (gnosis-display-cloze-sentence main clozes hints num)) - ;; Incorrect answer -> display current state with failure mode - (gnosis-display-cloze-sentence main clozes hints num t) + (progn (cl-incf num) + (gnosis-display-cloze-string main (nthcdr num clozes) + (nthcdr num hints) + (cl-subseq clozes 0 num) + nil)) + ;; Incorrect answer + (gnosis-display-cloze-string main nil nil (cl-subseq clozes 0 num) (member cloze clozes)) (gnosis-display-cloze-user-answer (cdr input)) (setq success nil) (cl-return))) -- cgit v1.2.3 From 10f24c92262fa38ccac8b51d91c22cd4105ca460 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 17:16:37 +0300 Subject: Remove MC-Cloze from the default note types. * MC-cloze is due for a rewrite, current versio is buggy, thus it should be enabled by default. --- gnosis.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gnosis.el b/gnosis.el index f5e6d1a..ff35238 100644 --- a/gnosis.el +++ b/gnosis.el @@ -142,7 +142,7 @@ a string describing the action." (defconst gnosis-db-version 2 "Gnosis database version.") -(defvar gnosis-note-types '("MCQ" "MC-Cloze" "Cloze" "Basic" "Double" "y-or-n") +(defvar gnosis-note-types '("MCQ" "Cloze" "Basic" "Double" "y-or-n") "Gnosis available note types.") (defvar gnosis-previous-note-tags '() -- cgit v1.2.3 From 092b971499e7b22247ffc506a9b32fe7a1bec25d Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 17:17:46 +0300 Subject: gnosis-review: Move review funcs closer together --- gnosis.el | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/gnosis.el b/gnosis.el index ff35238..270e4d9 100644 --- a/gnosis.el +++ b/gnosis.el @@ -1677,6 +1677,23 @@ NOTES: List of note ids" (gnosis-review-actions success note note-count)) finally (gnosis-review-commit note-count))))))) +;;;###autoload +(defun gnosis-review () + "Start gnosis review session." + (interactive) + ;; Refresh modeline + (setq gnosis-due-notes-total (length (gnosis-review-get-due-notes))) + (let ((review-type (gnosis-completing-read "Review: " '("Due notes" + "Due notes of deck" + "Due notes of specified tag(s)" + "All notes of tag(s)")))) + (pcase review-type + ("Due notes" (gnosis-review-session (gnosis-collect-note-ids :due t))) + ("Due notes of deck" (gnosis-review-session (gnosis-collect-note-ids :due t :deck (gnosis--get-deck-id)))) + ("Due notes of specified tag(s)" (gnosis-review-session (gnosis-collect-note-ids :due t :tags t))) + ("All notes of tag(s)" (gnosis-review-session (gnosis-collect-note-ids :tags t)))))) + + ;; Editing notes (defun gnosis-edit-read-only-values (&rest values) "Make the provided VALUES read-only in the whole buffer." @@ -1952,22 +1969,6 @@ to improve readability." (format "\n%s '%s" (symbol-name field) (prin1-to-string value))) (t (format "\n%s %s" (symbol-name field) (prin1-to-string value)))))))) -;;;###autoload -(defun gnosis-review () - "Start gnosis review session." - (interactive) - ;; Refresh modeline - (setq gnosis-due-notes-total (length (gnosis-review-get-due-notes))) - (let ((review-type (gnosis-completing-read "Review: " '("Due notes" - "Due notes of deck" - "Due notes of specified tag(s)" - "All notes of tag(s)")))) - (pcase review-type - ("Due notes" (gnosis-review-session (gnosis-collect-note-ids :due t))) - ("Due notes of deck" (gnosis-review-session (gnosis-collect-note-ids :due t :deck (gnosis--get-deck-id)))) - ("Due notes of specified tag(s)" (gnosis-review-session (gnosis-collect-note-ids :due t :tags t))) - ("All notes of tag(s)" (gnosis-review-session (gnosis-collect-note-ids :tags t)))))) - ;;; Database Schemas (defvar gnosis-db-schema-decks '([(id integer :primary-key :autoincrement) (name text :not-null) -- cgit v1.2.3 From 07c86227b321c19a86fd618811b386258488c0aa Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 17:18:14 +0300 Subject: cloze: Add quick fix for old cloze note format. --- gnosis.el | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gnosis.el b/gnosis.el index 270e4d9..f9ddd7d 100644 --- a/gnosis.el +++ b/gnosis.el @@ -1493,6 +1493,11 @@ If user-input is equal to CLOZE, return t." (num 0) ;; Number of clozes revealed (hints (gnosis-get 'options 'notes `(= id ,id))) (success nil)) + ;; Quick fix for old cloze note versions. + (cond ((and (stringp hints) (string-empty-p hints)) + (setq hints nil)) + ((and (not (listp hints)) (not (string-empty-p hints))) + (setq hints (list hints)))) ;; Initially display the sentence with no reveals (gnosis-display-cloze-string main clozes hints nil nil) (cl-loop for cloze in clozes -- cgit v1.2.3 From 68e7fcc6bd04ead7824cf717c657b907d66dc75d Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Tue, 16 Jul 2024 17:19:07 +0300 Subject: tests: Update cloze notes * Add hint --- gnosis-test.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gnosis-test.el b/gnosis-test.el index 46edcf1..3104789 100644 --- a/gnosis-test.el +++ b/gnosis-test.el @@ -97,8 +97,7 @@ by the thoracodorsal nerve." (when (y-or-n-p "Add multimple Clozes note?") (dotimes (_ num) (gnosis-add-note--cloze :deck testing-deck - :note "this is a {c1:note}, a note with multiple {c1:clozes}" - :hint "note" + :note "this is a {c1:note}, a note with multiple {c1:clozes::what}" :tags (gnosis-test-random-items gnosis-test-tags 2) :images (cons gnosis-test-image gnosis-test-image) :extra "extra"))) -- cgit v1.2.3