aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-07-11 21:12:13 -0500
committerCraig Jennings <c@cjennings.net>2026-07-11 21:12:13 -0500
commit9b906b32b6eccc289aca61b412d828f0daf32f72 (patch)
tree630db249faa801b1d01cfacdd5551119a77bbf52 /modules
parent5d8262e7854e43c3a5013d60b8d32fa869bbb924 (diff)
downloaddotemacs-9b906b32b6eccc289aca61b412d828f0daf32f72.tar.gz
dotemacs-9b906b32b6eccc289aca61b412d828f0daf32f72.zip
fix(lorem-optimum): guard empty chain, make cj/lipsum a command
cj/lipsum returned nil when the Markov chain was empty (for example with the training file missing), so cj/lipsum-insert ran (insert nil) and raised a cryptic wrong-type error far from the cause. It now signals a user-error naming the fix: train the chain or restore the file. The Commentary advertised M-x cj/lipsum, but the function had no interactive spec. I added one, echoing the words interactively while still returning the string for cj/lipsum-insert and cj/lipsum-paragraphs.
Diffstat (limited to 'modules')
-rw-r--r--modules/lorem-optimum.el18
1 files changed, 16 insertions, 2 deletions
diff --git a/modules/lorem-optimum.el b/modules/lorem-optimum.el
index 8aa96345..14f1d666 100644
--- a/modules/lorem-optimum.el
+++ b/modules/lorem-optimum.el
@@ -219,8 +219,22 @@ Builds and caches the keys list lazily if not already cached."
(message "Lorem-optimum learned from file: %s" file))
(defun cj/lipsum (n)
- "Return N words of lorem ipsum."
- (cj/markov-generate cj/lipsum-chain n '("Lorem" "ipsum")))
+ "Return N words of lorem ipsum.
+Interactively, prompt for N and echo the generated words.
+
+Signal a `user-error' when the Markov chain is empty (for example when the
+training file `cj/lipsum-default-file' is missing). Without this, callers
+such as `cj/lipsum-insert' would insert nil and raise a cryptic wrong-type
+error far from the cause. Train the chain with `cj/lipsum-learn-file',
+`cj/lipsum-learn-buffer', or `cj/lipsum-learn-region', or restore the file."
+ (interactive "nNumber of words: ")
+ (let ((text (cj/markov-generate cj/lipsum-chain n '("Lorem" "ipsum"))))
+ (unless (and (stringp text) (not (string-empty-p text)))
+ (user-error "Lorem-optimum chain is empty; train it with cj/lipsum-learn-file or restore %s"
+ cj/lipsum-default-file))
+ (when (called-interactively-p 'any)
+ (message "%s" text))
+ text))
(defun cj/lipsum-insert (n)
"Insert N words of lorem ipsum at point."