From fad6f3288ba7c9451bd0b2e0d38c67d267b7fc19 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 12 May 2026 13:29:38 -0500 Subject: refactor(org-drill): hoist the commands out of :config and clear the byte-compile warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `cj/drill-*` defuns and `cj/drill-map` lived inside the `use-package org-drill` `:config` block, so the byte-compiler never registered them — every cross-reference between them warned ("function `cj/drill-this-file' is not known", and so on). I moved all of that to module top level, where the compiler sees it. The ten `(setq org-drill-...)` lines became a `:custom` block (no more "assignment to free variable"). Added `(require 'user-constants)` and `(require 'keybindings)` for `drill-dir` and `cj/custom-keymap`, plus `declare-function` for `org-drill`, `org-drill-resume`, `org-capture`, and `org-refile`. The module byte-compiles clean now, and `C-; D` still mounts the drill submenu with the same leaf keys. I also gave `tests/test-org-drill-first-function.el` a `cj/custom-keymap` stub: its "loads without error" test does a bare `load` of the module, which now runs the keymap mount at load time instead of deferring it inside `:config`. --- modules/org-drill-config.el | 182 +++++++++++++++++++++++--------------------- 1 file changed, 94 insertions(+), 88 deletions(-) (limited to 'modules/org-drill-config.el') diff --git a/modules/org-drill-config.el b/modules/org-drill-config.el index 8555d30f..79fdb027 100644 --- a/modules/org-drill-config.el +++ b/modules/org-drill-config.el @@ -18,6 +18,88 @@ ;;; Code: +(require 'user-constants) ;; `drill-dir' +(require 'keybindings) ;; provides `cj/custom-keymap' +(declare-function org-drill "org-drill" (&optional scope drill-match resume-p)) +(declare-function org-drill-resume "org-drill" ()) +(declare-function org-capture "org-capture" (&optional goto keys)) +(declare-function org-refile "org-refile" (&optional arg1 default-buffer rfloc msg)) +(defvar org-refile-targets) ;; org-refile.el + +;; ------------------------------ Drill Commands ------------------------------- + +(defun cj/--drill-files-in (dir) + "Return the drill Org file names directly inside DIR (no leading dots)." + (directory-files dir nil "^[^.].*\\.org$")) + +(defun cj/--drill-pick-file (dir) + "Prompt for one of the drill Org files in DIR; return its absolute path." + (expand-file-name + (completing-read "Choose flashcard file: " (cj/--drill-files-in dir) nil t) + dir)) + +(defun cj/--drill-pick-dir (other-dir) + "Return the directory to pick drill files from. +With OTHER-DIR non-nil, prompt for one; otherwise use `drill-dir'." + (if other-dir (read-directory-name "Drill files in: ") drill-dir)) + +(defun cj/drill-start (&optional other-dir) + "Pick a drill Org file and start an `org-drill' session. +With a prefix arg OTHER-DIR, prompt for the directory to choose from +instead of the default `drill-dir'." + (interactive "P") + (find-file (cj/--drill-pick-file (cj/--drill-pick-dir other-dir))) + (org-drill)) + +(defun cj/drill-this-file () + "Start an `org-drill' session on the current Org buffer. +Use this to drill any drill file you have open, wherever it lives." + (interactive) + (unless (derived-mode-p 'org-mode) + (user-error "Not an Org buffer -- visit a `.org' file first")) + (org-drill)) + +(defun cj/drill-edit (&optional other-dir) + "Pick a drill Org file and open it for editing. +With a prefix arg OTHER-DIR, prompt for the directory instead of `drill-dir'." + (interactive "P") + (find-file (cj/--drill-pick-file (cj/--drill-pick-dir other-dir)))) + +(defun cj/drill-capture () + "Quickly capture a drill question." + (interactive) + (org-capture nil "d")) + +(defun cj/drill-refile () + "Refile to a drill file." + (interactive) + (setq org-refile-targets '((nil :maxlevel . 1) + (drill-dir :maxlevel . 1))) + (call-interactively 'org-refile)) + +;; ------------------------------- Drill Keymap -------------------------------- + +(defvar-keymap cj/drill-map + :doc "Keymap for org-drill" + "s" #'cj/drill-start + "f" #'cj/drill-this-file + "e" #'cj/drill-edit + "c" #'cj/drill-capture + "r" #'cj/drill-refile + "R" #'org-drill-resume) + +(keymap-set cj/custom-keymap "D" cj/drill-map) + +(with-eval-after-load 'which-key + (which-key-add-key-based-replacements + "C-; D" "org-drill menu" + "C-; D s" "start drill (C-u: pick dir)" + "C-; D f" "drill current file" + "C-; D e" "edit drill file (C-u: pick dir)" + "C-; D c" "capture question" + "C-; D r" "refile to drill" + "C-; D R" "resume drill")) + ;; --------------------------------- Org Drill --------------------------------- (use-package org-drill @@ -27,94 +109,18 @@ :load-path "~/code/org-drill" ;; local dev checkout — switch back to :vc above when done :after (org org-capture) :demand t - :commands (org-drill cj/drill-start) - :config - (setq org-drill-leech-failure-threshold 50) ;; leech cards = 50 wrong anwers - (setq org-drill-leech-method 'warn) ;; leech cards show warnings - (setq org-drill-use-visible-cloze-face-p t) ;; cloze text show up in a different font - (setq org-drill-hide-item-headings-p t) ;; don't show heading text - (setq org-drill-maximum-items-per-session 100) ;; drill sessions end after 100 cards - (setq org-drill-maximum-duration 30) ;; each drill session can last up to 30 mins - (setq org-drill-add-random-noise-to-intervals-p t) ;; slightly vary number of days to repetition - - ;; ------------------------------ Display Settings ----------------------------- - - ;; Configure display settings for drill sessions - (setq org-drill-text-size-during-session 24) ;; 24-point font for comfortable reading - (setq org-drill-use-variable-pitch t) ;; use variable-pitch font for readability - (setq org-drill-hide-modeline-during-session t) ;; hide modeline for cleaner display - - (defun cj/--drill-files-in (dir) - "Return the drill Org file names directly inside DIR (no leading dots)." - (directory-files dir nil "^[^.].*\\.org$")) - - (defun cj/--drill-pick-file (dir) - "Prompt for one of the drill Org files in DIR; return its absolute path." - (expand-file-name - (completing-read "Choose flashcard file: " (cj/--drill-files-in dir) nil t) - dir)) - - (defun cj/--drill-pick-dir (other-dir) - "Return the directory to pick drill files from. -With OTHER-DIR non-nil, prompt for one; otherwise use `drill-dir'." - (if other-dir (read-directory-name "Drill files in: ") drill-dir)) - - (defun cj/drill-start (&optional other-dir) - "Pick a drill Org file and start an `org-drill' session. -With a prefix arg OTHER-DIR, prompt for the directory to choose from -instead of the default `drill-dir'." - (interactive "P") - (find-file (cj/--drill-pick-file (cj/--drill-pick-dir other-dir))) - (org-drill)) - - (defun cj/drill-this-file () - "Start an `org-drill' session on the current Org buffer. -Use this to drill any drill file you have open, wherever it lives." - (interactive) - (unless (derived-mode-p 'org-mode) - (user-error "Not an Org buffer -- visit a `.org' file first")) - (org-drill)) - - (defun cj/drill-edit (&optional other-dir) - "Pick a drill Org file and open it for editing. -With a prefix arg OTHER-DIR, prompt for the directory instead of `drill-dir'." - (interactive "P") - (find-file (cj/--drill-pick-file (cj/--drill-pick-dir other-dir)))) - - (defun cj/drill-capture () - "Quickly capture a drill question." - (interactive) - (org-capture nil "d")) - - (defun cj/drill-refile () - "Refile to a drill file." - (interactive) - (setq org-refile-targets '((nil :maxlevel . 1) - (drill-dir :maxlevel . 1))) - (call-interactively 'org-refile)) - - ;; ------------------------------ Org Drill Keymap ----------------------------- - - ;; Org drill operations keymap - (defvar-keymap cj/drill-map - :doc "Keymap for org-drill" - "s" #'cj/drill-start - "f" #'cj/drill-this-file - "e" #'cj/drill-edit - "c" #'cj/drill-capture - "r" #'cj/drill-refile - "R" #'org-drill-resume) - - (keymap-set cj/custom-keymap "D" cj/drill-map) - (with-eval-after-load 'which-key - (which-key-add-key-based-replacements - "C-; D" "org-drill menu" - "C-; D s" "start drill (C-u: pick dir)" - "C-; D f" "drill current file" - "C-; D e" "edit drill file (C-u: pick dir)" - "C-; D c" "capture question" - "C-; D r" "refile to drill" - "C-; D R" "resume drill"))) + :commands (org-drill org-drill-resume) + :custom + (org-drill-leech-failure-threshold 50 "leech cards = 50 wrong answers") + (org-drill-leech-method 'warn "leech cards show warnings") + (org-drill-use-visible-cloze-face-p t "cloze text shows up in a different font") + (org-drill-hide-item-headings-p t "don't show heading text") + (org-drill-maximum-items-per-session 100 "drill sessions end after 100 cards") + (org-drill-maximum-duration 30 "each drill session can last up to 30 mins") + (org-drill-add-random-noise-to-intervals-p t "vary the days to repetition slightly") + (org-drill-text-size-during-session 24 "24-point font for comfortable reading") + (org-drill-use-variable-pitch t "variable-pitch font for readability") + (org-drill-hide-modeline-during-session t "hide the modeline for a cleaner display")) (provide 'org-drill-config) ;;; org-drill-config.el ends here. -- cgit v1.2.3