4d4a88f4f497 — Danielle Hutzley 2 years ago
Expose form-entry macro
2 files changed, 21 insertions(+), 20 deletions(-)

M .tup/db
M yalib-macros.fnl
M .tup/db +0 -0

        
M yalib-macros.fnl +21 -20
@@ 1,20 1,20 @@ 
 ; YALib macros, load using --add-macro-path
 ;; Formspec DSL
+(fn form-entry [command ...]
+  (fn parse-arg [v]
+    (match v
+      [:escape text] `(minetest.formspec_escape ,text)
+      [:run f & args] `(,f ,(unpack args))
+      [] `(table.concat ,(icollect [_ v (ipairs v)] (parse-arg v)) ",")
+      _ (tostring v)))
+
+  (let [args (icollect [_ v (ipairs [...])] (parse-arg v))]
+    `(.. ,(.. (tostring command) "[") (table.concat ,args ";") "]")))
+
 (fn form [{: version : size : position : anchor : padding : no-prepend : real-coordinates} ...]
   "Define a new formspec using a DSL (returns an expression which produces a formspec string)"
   (local unpack (or unpack table.unpack))
 
-  (fn fs-entry [command ...]
-    (fn parse-arg [v]
-      (match v
-        [:escape text] `(minetest.formspec_escape ,text)
-        [:run f & args] `(,f ,(unpack args))
-        [] `(table.concat ,(icollect [_ v (ipairs v)] (parse-arg v)) ",")
-        _ (tostring v)))
-
-    (let [args (icollect [_ v (ipairs [...])] (parse-arg v))]
-      `(.. ,(.. (tostring command) "[") (table.concat ,args ";") "]")))
-
   (fn or-empty [target action]
     (if target (action target) ""))
 

          
@@ 24,17 24,17 @@ 
 
     (icollect [_ entry (ipairs [...])]
       (match (tostring (. entry 1))
-        (where block (. blocks block)) `(.. ,(fs-entry block (. entry 2)) ,(unpack entry 3) ,(fs-entry (. blocks block)))
-        _ (fs-entry (unpack entry)))))
+        (where block (. blocks block)) `(.. ,(form-entry block (. entry 2)) ,(unpack entry 3) ,(form-entry (. blocks block)))
+        _ (form-entry (unpack entry)))))
 
   ; Prefer a more up-to-date formspec default
-  (let [version-h (fs-entry :formspec_version (or version 4))
-        size-h (fs-entry :size size)
-        position-h (or-empty position (partial fs-entry :position))
-        anchor-h (or-empty anchor (partial fs-entry :anchor))
-        padding-h (or-empty padding (partial fs-entry :padding))
-        no-prepend-h (or-empty no-prepend #(fs-entry :no_prepend))
-        real-coordinates-h (or-empty real-coordinates (partial fs-entry :real_coordinates))
+  (let [version-h (form-entry :formspec_version (or version 4))
+        size-h (form-entry :size size)
+        position-h (or-empty position (partial form-entry :position))
+        anchor-h (or-empty anchor (partial form-entry :anchor))
+        padding-h (or-empty padding (partial form-entry :padding))
+        no-prepend-h (or-empty no-prepend #(form-entry :no_prepend))
+        real-coordinates-h (or-empty real-coordinates (partial form-entry :real_coordinates))
         body (parse-entries ...)]
     `(table.concat [,version-h ,size-h ,position-h ,anchor-h ,padding-h ,no-prepend-h ,real-coordinates-h ,(unpack body)] "")))
 

          
@@ 127,6 127,7 @@ 
        (,hook-fn ,action ...))))
 
 {: form
+ : form-entry
  : defnode
  : defrecipe
  : defcommand