summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThanos Apollo <[email protected]>2025-01-03 19:53:53 +0200
committerThanos Apollo <[email protected]>2025-01-03 19:53:53 +0200
commitbfb708cc879d17296770a37ea251fba2d4f3841c (patch)
tree7a910bdb536f7d1fe6fb02b5c9ad246359086620
parent86334d5800df55abfb0db563dca14fb7a85a92d0 (diff)
cloze: Do not insert "nil" strings as hints.
-rw-r--r--gnosis.el17
1 files 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)