Merge branch 'refactoring' into 'main'

♻️ Extract `rpgdm-core` and address deprecations

See merge request howardabrams/emacs-rpgdm!1
This commit is contained in:
Howard Abrams 2023-12-10 20:15:03 +00:00
commit 213391a54e
11 changed files with 93 additions and 76 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.

View file

@ -46,7 +46,7 @@ For instance, Xanathar's Guide to Everything, a Dungeons and Dragons supplement
To represent these types of tables, we create a special type, called a =dice-table=. Where the first "slot" is the dice expression (or the number of sides of a dice to roll), and an associative list of result values and the choice.
#+BEGIN_SRC emacs-lisp :results silent
(defstruct dice-table dice rows)
(cl-defstruct dice-table dice rows)
#+END_SRC
How is this used to render the example table above?

View file

@ -226,7 +226,7 @@ decrement the ROLL value."
;; (message "Comparing %d <= %d for %s" roll num-elems tag)
(if (<= roll num-elems)
(return tag)
(decf roll num-elems))))
(cl-decf roll num-elems))))
(ert-deftest rpgdm-tables--find-tag-test ()
(let ((weighted-tags

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

@ -81,7 +81,7 @@ This really tests the `rpgdm--test-rolls' function."
(8 1 8)
(20 1 20)
(100 1 100)))
(destructuring-bind (die lowest highest) test-data
(cl-destructuring-bind (die lowest highest) test-data
(rpgdm--test-rolls #'rpgdm--roll-die (list die) lowest highest))))
;; ----------------------------------------------------------------------
@ -155,7 +155,7 @@ average value of AVG, if given."
((3 6 4) 7 22)
((4 6 4 "-") 0 20))))
(dolist (test-seq test-data)
(destructuring-bind (dice-args lowest highest) test-seq
(cl-destructuring-bind (dice-args lowest highest) test-seq
(rpgdm--test-roll-series 'rpgdm--roll dice-args lowest highest)))))
@ -240,7 +240,7 @@ the following:
("2d12" 2 24)
("3d6+2" 5 20))))
(dolist (test-data test-cases)
(destructuring-bind (dice-expression lowest highest) test-data
(cl-destructuring-bind (dice-expression lowest highest) test-data
(rpgdm--test-roll-series 'rpgdm--roll-expression (list dice-expression) lowest highest)))))

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

@ -17,7 +17,7 @@
;;; Code:
(defstruct dice-table dice rows)
(cl-defstruct dice-table dice rows)
(defun rpgdm-tables--choose-dice-table (table)
"Choose a string from a random dice table."

View file

@ -142,7 +142,7 @@ decrement the ROLL value."
;; (message "Comparing %d <= %d for %s" roll num-elems tag)
(if (<= roll num-elems)
(return tag)
(decf roll num-elems))))
(cl-decf roll num-elems))))
(ert-deftest rpgdm-tables--find-tag-test ()
(let ((weighted-tags

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)
(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 ()