diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-31 06:53:06 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-31 06:53:06 -0500 |
| commit | bafa281b9c0b3ccc78b4a8420a817662d50ca86f (patch) | |
| tree | 855a7f668145c41bb09670136c537e1d7244128e | |
| parent | 79f454cbfcfcf61bae34a9fdf85841617e2b1c00 (diff) | |
| download | org-drill-bafa281b9c0b3ccc78b4a8420a817662d50ca86f.tar.gz org-drill-bafa281b9c0b3ccc78b4a8420a817662d50ca86f.zip | |
feat: add org-drill-treat-headline-as-card-p for empty-bodied cards
A drill entry with an empty body is skipped unless its card type opts into empty bodies via the DRILL-EMPTY-P slot of org-drill-card-type-alist. That left no global way to drill headline-only items, or hierarchical-notes decks where the heading is the question and the answer lives in child entries (upstream #30, #41).
I added org-drill-treat-headline-as-card-p, default nil so existing decks are unchanged. When it's on, the empty-skip gate in org-drill--entry-empty-and-not-empty-friendly-p short-circuits, so every empty-bodied entry is drilled with its heading as the question regardless of card type. I added the safe-local-variable booleanp declaration alongside the other booleans and documented the switch in org-drill.org.
Tests pin the predicate and the classify-status outcome both on and off, and confirm the per-card-type DRILL-EMPTY-P path stays independent of the new switch.
| -rw-r--r-- | org-drill.el | 21 | ||||
| -rw-r--r-- | org-drill.org | 4 | ||||
| -rw-r--r-- | tests/test-org-drill-treat-headline-as-card.el | 87 |
3 files changed, 110 insertions, 2 deletions
diff --git a/org-drill.el b/org-drill.el index 2ad00f0..1a03b1f 100644 --- a/org-drill.el +++ b/org-drill.el @@ -414,6 +414,19 @@ even if their bodies are empty." :type '(alist :key-type (choice string (const nil)) :value-type function)) +(defcustom org-drill-treat-headline-as-card-p nil + "When non-nil, treat a drill entry with an empty body as a valid card. + +By default a drill entry whose body is empty is skipped during a +session unless its card type opts in via the DRILL-EMPTY-P slot of +`org-drill-card-type-alist'. When this is non-nil, an empty-bodied +entry is presented as a card with the heading itself as the question, +regardless of card type. This covers hierarchical-notes decks where +the heading is the prompt and the answer lives in child entries, and +decks where the heading alone is the card (upstream issues #30 and #41)." + :group 'org-drill-session + :type 'boolean) + (defcustom org-drill-card-tags-alist '(("explain" nil org-drill-explain-answer-presenter org-drill-explain-cleaner)) @@ -925,6 +938,7 @@ regardless of whether the test was successful.") '(lambda (val) (memq val '(nil skip warn)))) (put 'org-drill-use-visible-cloze-face-p 'safe-local-variable 'booleanp) (put 'org-drill-hide-item-headings-p 'safe-local-variable 'booleanp) +(put 'org-drill-treat-headline-as-card-p 'safe-local-variable 'booleanp) (put 'org-drill-spaced-repetition-algorithm 'safe-local-variable '(lambda (val) (memq val '(simple8 sm5 sm2)))) (put 'org-drill-sm5-initial-interval 'safe-local-variable 'floatp) @@ -3343,8 +3357,11 @@ treat empty bodies as meaningful. A card type that wants empty bodies is one whose entry in `org-drill-card-type-alist' has a non-nil third element (the -DRILL-EMPTY-P slot)." - (and (org-drill-entry-empty-p) +DRILL-EMPTY-P slot). When `org-drill-treat-headline-as-card-p' is +non-nil, no empty entry is treated as skippable — the heading itself +is the card." + (and (not org-drill-treat-headline-as-card-p) + (org-drill-entry-empty-p) (let* ((card-type (org-entry-get (point) "DRILL_CARD_TYPE" nil)) (card-def (cdr (assoc card-type org-drill-card-type-alist)))) (or (null card-type) diff --git a/org-drill.org b/org-drill.org index d0a0857..34092d0 100644 --- a/org-drill.org +++ b/org-drill.org @@ -386,6 +386,10 @@ drill session, you can either: 2. Change the entry for its card type in =org-drill-card-type-alist= so that items of this type will always be tested, even if they have an empty body. See the documentation for =org-drill-card-type-alist= for details. +3. Set =org-drill-treat-headline-as-card-p= to =t=. This is a global switch: + when on, every empty-bodied drill item is tested with its heading as the + question, regardless of card type. Useful for hierarchical-notes decks where + the heading is the prompt and the answer lives in child items. * Running the drill session diff --git a/tests/test-org-drill-treat-headline-as-card.el b/tests/test-org-drill-treat-headline-as-card.el new file mode 100644 index 0000000..5b369e7 --- /dev/null +++ b/tests/test-org-drill-treat-headline-as-card.el @@ -0,0 +1,87 @@ +;;; test-org-drill-treat-headline-as-card.el --- Tests for headline-as-card -*- lexical-binding: t; -*- + +;;; Commentary: +;; `org-drill-treat-headline-as-card-p' controls whether a drill entry with an +;; empty body is skipped (the default) or presented as a card with the heading +;; itself as the question (upstream issues #30 and #41). +;; +;; The gate lives in `org-drill--entry-empty-and-not-empty-friendly-p', whose +;; non-nil result makes `org-drill--classify-status' return nil (the entry is +;; not a drillable card). These tests pin both the predicate and the +;; classification outcome, on and off, and confirm the per-card-type +;; DRILL-EMPTY-P mechanism still works independently of the new global switch. + +;;; Code: + +(require 'ert) +(require 'org-drill) + +;;;; Defcustom default + +(ert-deftest test-org-drill-treat-headline-as-card-defaults-off () + "The defcustom ships nil so existing decks keep skipping empty entries." + (should (eq nil (default-value 'org-drill-treat-headline-as-card-p)))) + +;;;; Predicate — org-drill--entry-empty-and-not-empty-friendly-p + +(ert-deftest test-org-drill-empty-entry-skipped-when-headline-card-off () + "With the switch off, an empty-bodied drill entry is treated as +empty-and-skippable (predicate returns non-nil)." + (let ((org-drill-treat-headline-as-card-p nil)) + (with-temp-buffer + (insert "* A headline-only card :drill:\n") + (org-mode) + (goto-char (point-min)) + (should (org-drill--entry-empty-and-not-empty-friendly-p))))) + +(ert-deftest test-org-drill-empty-entry-not-skipped-when-headline-card-on () + "With the switch on, an empty-bodied drill entry is NOT skippable +\(predicate returns nil) — the heading becomes the card." + (let ((org-drill-treat-headline-as-card-p t)) + (with-temp-buffer + (insert "* A headline-only card :drill:\n") + (org-mode) + (goto-char (point-min)) + (should-not (org-drill--entry-empty-and-not-empty-friendly-p))))) + +(ert-deftest test-org-drill-non-empty-entry-not-skipped-regardless () + "An entry with a body is never empty-and-skippable, switch on or off." + (dolist (flag '(nil t)) + (let ((org-drill-treat-headline-as-card-p flag)) + (with-temp-buffer + (insert "* A normal card :drill:\nThe answer body.\n") + (org-mode) + (goto-char (point-min)) + (should-not (org-drill--entry-empty-and-not-empty-friendly-p)))))) + +(ert-deftest test-org-drill-empty-friendly-card-type-unaffected-by-switch () + "A card type that opts into empty bodies via DRILL-EMPTY-P (e.g. twosided) +is never skippable, independent of the global switch." + (dolist (flag '(nil t)) + (let ((org-drill-treat-headline-as-card-p flag)) + (with-temp-buffer + (insert "* A two-sided card :drill:\n" + ":PROPERTIES:\n:DRILL_CARD_TYPE: twosided\n:END:\n") + (org-mode) + (goto-char (point-min)) + (should-not (org-drill--entry-empty-and-not-empty-friendly-p)))))) + +;;;; Classification outcome — org-drill--classify-status + +(ert-deftest test-org-drill-classify-empty-entry-flips-with-switch () + "An empty drill entry classifies as nil (skipped) with the switch off, and +as a real status with the switch on — the user-facing effect of the feature." + (with-temp-buffer + (insert "* A headline-only card :drill:\n") + (org-mode) + (goto-char (point-min)) + ;; due=nil reaches :unscheduled only once the empty gate is passed; session + ;; is unused on that path, so nil is safe here. + (let ((org-drill-treat-headline-as-card-p nil)) + (should (null (org-drill--classify-status nil nil 1)))) + (let ((org-drill-treat-headline-as-card-p t)) + (should (org-drill--classify-status nil nil 1))))) + +(provide 'test-org-drill-treat-headline-as-card) + +;;; test-org-drill-treat-headline-as-card.el ends here |
