diff --git a/README.org b/README.org index 012ba90..cd09a86 100644 --- a/README.org +++ b/README.org @@ -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. diff --git a/docs/rpgdm-tables-dice.org b/docs/rpgdm-tables-dice.org index 6cd062f..766a12c 100644 --- a/docs/rpgdm-tables-dice.org +++ b/docs/rpgdm-tables-dice.org @@ -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? diff --git a/docs/rpgdm-tables-freq.org b/docs/rpgdm-tables-freq.org index f511fb2..c09d072 100644 --- a/docs/rpgdm-tables-freq.org +++ b/docs/rpgdm-tables-freq.org @@ -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 diff --git a/rpgdm-core.el b/rpgdm-core.el new file mode 100644 index 0000000..531b16d --- /dev/null +++ b/rpgdm-core.el @@ -0,0 +1,80 @@ +;;; rpgdm-core --- Core functionality for rpgdm -*- lexical-binding: t -*- +;; +;; Copyright (C) 2021 Howard X. Abrams +;; +;; Author: Howard X. Abrams +;; Jeremy Friesen +;; 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 diff --git a/rpgdm-dice.el b/rpgdm-dice.el index 02e7859..03f2daa 100644 --- a/rpgdm-dice.el +++ b/rpgdm-dice.el @@ -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))))) diff --git a/rpgdm-npc.el b/rpgdm-npc.el index 680734b..5c424eb 100644 --- a/rpgdm-npc.el +++ b/rpgdm-npc.el @@ -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." diff --git a/rpgdm-screen.el b/rpgdm-screen.el index 24f8571..12e9ea5 100644 --- a/rpgdm-screen.el +++ b/rpgdm-screen.el @@ -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) diff --git a/rpgdm-tables-dice.el b/rpgdm-tables-dice.el index 6cd28a7..32b42f8 100644 --- a/rpgdm-tables-dice.el +++ b/rpgdm-tables-dice.el @@ -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." diff --git a/rpgdm-tables-freq.el b/rpgdm-tables-freq.el index 8a7f807..b257b5d 100644 --- a/rpgdm-tables-freq.el +++ b/rpgdm-tables-freq.el @@ -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 diff --git a/rpgdm-tables.el b/rpgdm-tables.el index aba54a7..0db6942 100644 --- a/rpgdm-tables.el +++ b/rpgdm-tables.el @@ -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) diff --git a/rpgdm.el b/rpgdm.el index 9d75c12..3d1ba66 100644 --- a/rpgdm.el +++ b/rpgdm.el @@ -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") ("" 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 ()