♻️ Extract `rpgdm-core.el'

Prior to this commit, I experienced the following use case:

Given I have the following code:

```emacs-lisp
(require 'rpgdm-dice "~/git/emacs-rpgdm/rpgdm-dice.el")
(require 'rpgdm-tables "~/git/emacs-rpgdm/rpgdm-tables.el")
(require 'rpgdm-tables-dice "~/git/emacs-rpgdm/rpgdm-tables-dice.el")
(require 'rpgdm-tables-freq "~/git/emacs-rpgdm/rpgdm-tables-freq.el")

(setq rpgdm-base "~/git/emacs-rpgdm/")
```

And I call `rpgdm-tables-load`
And load all the tables.

When I then call `rpgdm-tables-choose`

Then I get the following error:

```
let*: Symbol’s function definition is void: rpgdm-message
```

With this commit I'm able to skip requiring `rpgdm` and thus only use a
segment of the `rpgdm` package ecosystem.
This commit is contained in:
Jeremy Friesen 2023-12-10 10:50:14 -05:00
parent 25c282c094
commit cf24af20b6
6 changed files with 86 additions and 69 deletions

View file

@ -99,6 +99,7 @@ The second thing I realized is that Org's links can call Emacs functions. This a
My initial ideas for listing a bunch of random NPC names and having a link that displayed one of them, got supplanted for the ideas I described above.
* Code
What do I have here:
- [[file:rpgdm-core.el][rpgdm-core]] :: Package that provides common functionality; namely in messaging and the =rpgdm-last-results= ring.
- [[file:rpgdm.el][rpgdm]] :: Primary interface offering:
- =rpgdm-mode=, plus a Hydra interface for easily calling the rest of these functions.
- =rpgdm-yes-and-50/50=, flip a coin and make a give a result with or without complications or bonuses.

80
rpgdm-core.el Normal file
View file

@ -0,0 +1,80 @@
;;; rpgdm-core --- Core functionality for rpgdm -*- lexical-binding: t -*-
;;
;; Copyright (C) 2021 Howard X. Abrams
;;
;; Author: Howard X. Abrams <http://gitlab.com/howardabrams>
;; Jeremy Friesen <jeremy@jeremyfriesen.com>
;; Maintainer: Howard X. Abrams
;; Created: December 10, 2023
;;
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;;
;; There are functions shared across different `rpgdm' packages. These are
;; considered "core" functionality.
;;
;;; Code:
(defvar rpgdm-base
(seq-find (lambda (elt) (string-match "rpgdm" elt)) load-path (getenv "HOME"))
"Default directory to look for supporting data, like tables and charts.")
(defvar rpgdm-last-results (make-ring 10)
"The results from calls to `rpgdm-screen-' functions are stored here.")
(defvar rpgdm-last-results-ptr 0
"Keeps track of where we are in the message display ring.
Each call to `rpgdm-last-results' resets this to 0.")
(defun rpgdm-message (format-string &rest args)
"Replace `message' function allowing it to be re-displayed.
The FORMAT-STRING is a standard string for the `format' function,
and ARGS are substitued values."
(let ((message (apply 'format format-string args)))
(ring-insert rpgdm-last-results message)
(kill-new message)
(rpgdm-last-results)))
(defun rpgdm-last-results ()
"Display results from the last call to a `rpgdm-message' function."
(interactive)
(setq rpgdm-last-results-ptr 0)
(message (ring-ref rpgdm-last-results rpgdm-last-results-ptr)))
(defun rpgdm-last-results-previous ()
"Display results from an earlier call to `rpgdm-message'."
(interactive)
(cl-incf rpgdm-last-results-ptr)
(when (>= rpgdm-last-results-ptr (ring-length rpgdm-last-results))
(setq rpgdm-last-results-ptr 0))
(message "%d> %s" rpgdm-last-results-ptr (ring-ref rpgdm-last-results rpgdm-last-results-ptr)))
(defun rpgdm-last-results-next ()
"Display results from an later call to `rpgdm-message'.
Meant to be used with `rpgdm-last-results-previous'."
(interactive)
(when (> rpgdm-last-results-ptr 0)
(cl-decf rpgdm-last-results-ptr))
(message "%d> %s" rpgdm-last-results-ptr (ring-ref rpgdm-last-results rpgdm-last-results-ptr)))
(defun rpgdm-paste-last-message ()
"Yank, e.g. paste, the last displayed message."
(interactive)
(insert (rpgdm-last-results)))
(ert-deftest rpgdm-last-results-test ()
(progn
(setq rpgdm-last-results (make-ring 10))
(rpgdm-message "First in, so this is the oldest")
(rpgdm-message "Something or other")
(rpgdm-message "Almost the newest")
(rpgdm-message "Newest"))
(should (equal "Newest" (rpgdm-last-results)))
(should (equal "1> Almost the newest" (rpgdm-last-results-previous)))
(should (equal "2> Something other" (rpgdm-last-results-previous)))
(should (equal "1> Almost the newest" (rpgdm-last-results-next)))
(should (equal "0> Almost the newest" (rpgdm-last-results-next)))
(should (equal "0> Almost the newest" (rpgdm-last-results-next))))
(provide 'rpgdm-core)
;;; rpgdm-core.el ends here

View file

@ -28,9 +28,9 @@
;;;
;;; CCode:
(defvar rpgdm-base ".")
(require 'rpgdm-core (expand-file-name "rpgdm-core.el" rpgdm-base) t)
(require 'rpgdm-dice (expand-file-name "rpgdm-dice.el" rpgdm-base) t)
(require 'rpgdm-dice (expand-file-name "rpgdm-tables.el" rpgdm-base) t)
(require 'rpgdm-tables (expand-file-name "rpgdm-tables.el" rpgdm-base) t)
(defun rpgdm-npc-gender-name ()
"Return nil or non-nil for male or female names."

View file

@ -21,8 +21,7 @@
(require 'org)
(require 'org-element)
(require 's)
(defvar rpgdm-base ".")
(require 'rpgdm-core (expand-file-name "rpgdm-core.el" rpgdm-base) t)
(defvar rpgdm-screen-directory
(expand-file-name "dnd-5e" rpgdm-base)

View file

@ -25,7 +25,7 @@
;;
;;; Code:
(defvar rpgdm-base ".")
(require 'rpgdm-core (expand-file-name "rpgdm-core.el" rpgdm-base) t)
(require 'rpgdm-dice (expand-file-name "rpgdm-dice.el" rpgdm-base) t)
(require 'rpgdm-tables-freq (expand-file-name "rpgdm-tables-freq.el" rpgdm-base) t)
(require 'rpgdm-tables-dice (expand-file-name "rpgdm-tables-dice.el" rpgdm-base) t)

View file

@ -25,15 +25,11 @@
(require 'ert)
(require 'rpgdm-core)
(require 'rpgdm-dice)
(require 'rpgdm-screen)
(require 'rpgdm-tables)
(defvar rpgdm-base
(seq-find (lambda (elt) (string-match "rpgdm" elt)) load-path (getenv "HOME"))
"Default directory to look for supporting data, like tables and charts.")
(define-minor-mode rpgdm-mode
"Minor mode for layering role-playing game master functions over your notes."
:lighter " D&D"
@ -73,65 +69,6 @@
("q" nil "quit") ("<f12>" nil))
(defvar rpgdm-last-results (make-ring 10)
"The results from calls to `rpgdm-screen-' functions are stored here.")
(defvar rpgdm-last-results-ptr 0
"Keeps track of where we are in the message display ring.
Each call to `rpgdm-last-results' resets this to 0.")
(defun rpgdm-message (format-string &rest args)
"Replace `messasge' function allowing it to be re-displayed.
The FORMAT-STRING is a standard string for the `format' function,
and ARGS are substitued values."
(let ((message (apply 'format format-string args)))
(ring-insert rpgdm-last-results message)
(kill-new message)
(rpgdm-last-results)))
(defun rpgdm-last-results ()
"Display results from the last call to a `rpgdm-message' function."
(interactive)
(setq rpgdm-last-results-ptr 0)
(message (ring-ref rpgdm-last-results rpgdm-last-results-ptr)))
(defun rpgdm-last-results-previous ()
"Display results from an earlier call to `rpgdm-message'."
(interactive)
(incf rpgdm-last-results-ptr)
(when (>= rpgdm-last-results-ptr (ring-length rpgdm-last-results))
(setq rpgdm-last-results-ptr 0))
(message "%d> %s" rpgdm-last-results-ptr (ring-ref rpgdm-last-results rpgdm-last-results-ptr)))
(defun rpgdm-last-results-next ()
"Display results from an later call to `rpgdm-message'.
Meant to be used with `rpgdm-last-results-previous'."
(interactive)
(when (> rpgdm-last-results-ptr 0)
(cl-decf rpgdm-last-results-ptr))
(message "%d> %s" rpgdm-last-results-ptr (ring-ref rpgdm-last-results rpgdm-last-results-ptr)))
(defun rpgdm-paste-last-message ()
"Yank, e.g. paste, the last displayed message."
(interactive)
(insert (rpgdm-last-results)))
(ert-deftest rpgdm-last-results-test ()
(progn
(setq rpgdm-last-results (make-ring 10))
(rpgdm-message "First in, so this is the oldest")
(rpgdm-message "Something or other")
(rpgdm-message "Almost the newest")
(rpgdm-message "Newest"))
(should (equal "Newest" (rpgdm-last-results)))
(should (equal "1> Almost the newest" (rpgdm-last-results-previous)))
(should (equal "2> Something other" (rpgdm-last-results-previous)))
(should (equal "1> Almost the newest" (rpgdm-last-results-next)))
(should (equal "0> Almost the newest" (rpgdm-last-results-next)))
(should (equal "0> Almost the newest" (rpgdm-last-results-next))))
(defvar rpgdm-oracle-mod 0 "Cummulative skew to create more tension.")
(defun rpgdm-oracle ()