From bfb708cc879d17296770a37ea251fba2d4f3841c Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Fri, 3 Jan 2025 19:53:53 +0200 Subject: cloze: Do not insert "nil" strings as hints. --- gnosis.el | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/gnosis.el b/gnosis.el index f09bfa7..1791246 100644 --- a/gnosis.el +++ b/gnosis.el @@ -521,17 +521,20 @@ If FILL-PARAGRAPH-P, insert question using `fill-paragraph'." (buffer-string)))) (defun gnosis-cloze-add-hints (str hints &optional cloze-string) - "Replace CLOZE-STRING in STR with HINTS." + "Replace CLOZE-STRING in STR with HINTS, skipping empty hints." (cl-assert (listp hints) nil "Hints must be a list.") - (let ((cloze-string (or cloze-string gnosis-cloze-string)) - (count 0)) + (let ((cloze-string (or cloze-string gnosis-cloze-string))) (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))) + (cl-loop for hint in hints + while (search-forward cloze-string nil t) + do + (when (and hint (not (string-empty-p hint)) (not (string= hint "nil")) + (search-backward cloze-string nil t)) + (replace-match (propertize (format "[%s]" hint) 'face 'gnosis-face-cloze)) + (goto-char (match-end 0)))) ; Move point to end of match + (buffer-string)))) (defun gnosis-cloze-mark-answers (str answers face) -- cgit v1.2.3