Debian Codegraph @

texmf-dist/tex/lualatex/luatbls/luatbls.sty

raw
1 % Kale Ewasiuk ([email protected])
2 % 2025-02-14
3 % Copyright (C) 2025 Kale Ewasiuk
4 %
5 % Permission is hereby granted, free of charge, to any person obtaining a copy
6 % of this software and associated documentation files (the "Software"), to deal
7 % in the Software without restriction, including without limitation the rights
8 % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 % copies of the Software, and to permit persons to whom the Software is
10 % furnished to do so, subject to the following conditions:
11 %
12 % The above copyright notice and this permission notice shall be included in
13 % all copies or substantial portions of the Software.
14 %
15 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16 % ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 % TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
18 % PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19 % SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
20 % ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 % ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23 % OR OTHER DEALINGS IN THE SOFTWARE.
24
25 \RequirePackage{luacode}
26 \RequirePackage{luakeys}
27 \RequirePackage{penlightplus}
28
29
30 \luadirect{luatbls = require'luatbls'}
31
32
33 \NewDocumentCommand{\tblnew}{m}{\luadirect{% initialize a tbl and set empty
34 luatbls[\luastring{#1}] = {}
35 luatbls._rec_tbl = \luastring{#1}
36 }}
37
38
39 \NewDocumentCommand{\tblchg}{ m }{\luadirect{% change recent table
40 luatbls._rec_tbl = luatbls._get_tbl_name(\luastring{#1})
41 }}
42
43 \NewDocumentCommand{\tblfrkv}{m +m O{}}{\luadirect{% parse a tbl from key-vals, naked are set to boolean
44 luatbls._rec_tbl_opts = luatbls._luakeys.parse(\luastring{#3}, {naked_as_value=true})
45 luatbls[\luastring{#1}] = luatbls._luakeys.parse(string.subpar(\luastring{#2}), luatbls._rec_tbl_opts)
46 luatbls._rec_tbl = \luastring{#1}
47 }}
48
49 \NewDocumentCommand{\tblfrkvN}{m +m O{}}{\luadirect{%
50 luatbls._rec_tbl_opts = luatbls._luakeys.parse(\luastring{#3}, {naked_as_value=true})
51 luatbls[\luastring{#1}] = luatbls._luakeys.parse(string.subpar(\luastringN{#2}), luatbls._rec_tbl_opts)
52 luatbls._rec_tbl = \luastring{#1}
53 }}
54
55 \NewDocumentCommand{\tblfrcsv}{m +m O{}}{\tblfrkv{#1}{#2}[naked_as_value=true,#3]}% naked are set to array
56
57 \NewDocumentCommand{\tblfrcsvN}{m +m O{}}{\tblfrkvN{#1}{#2}[naked_as_value=true,#3]}
58
59 \NewDocumentCommand{\tblkvundefcheck}{}{\luadirect{luatbls._check_recent_tbl_undefault()}}% check defaults list and throw error if foreign keys were used
60
61 \NewDocumentCommand{\tblenforcechoices}{m m}{\luadirect{luatbls._check_choices(\luastring{#1},\luastring{#2})}}
62
63
64
65
66 \NewDocumentCommand{\tblapp}{m m}{\luadirect{% append to a table (ie using integer index) with a value (second arg)
67 __tbl__ = luatbls._get_tbl_name(\luastring{#1})
68 table.insert(luatbls[__tbl__], \luastring{#2})
69 }}
70
71
72 \NewDocumentCommand{\tblappN}{m m}{\luadirect{% append to a table (ie using integer index) with a value (second arg) #
73 __tbl__ = luatbls._get_tbl_name(\luastring{#1})
74 table.insert(luatbls[__tbl__], \luastringN{#2})
75 }}
76
77
78 \NewDocumentCommand{\tblcon}{m m}{\luadirect{% concatenate to a table (ie using integer index) with a list of comma separated values (second arg) #
79 __tbl__ = luatbls._get_tbl_name(\luastring{#1})
80 for k, v in ipairs(luatbls._luakeys.parse(string.subpar(\luastring{#2}), {naked_as_value=true})) do
81 table.insert(luatbls[__tbl__], v)
82 end
83 }}
84
85
86 \NewDocumentCommand{\tblconN}{m m}{\luadirect{% concatenate to a table (ie using integer index) with a list of comma separated values (second arg) #
87 __tbl__ = luatbls._get_tbl_name(\luastring{#1})
88 for k, v in ipairs(luatbls._luakeys.parse(string.subpar(\luastringN{#2}), {naked_as_value=true})) do
89 table.insert(luatbls[__tbl__], v)
90 end
91 }}
92
93
94 \NewDocumentCommand{\tbladd}{m m}{\luadirect{% add a kv pair to a table
95 __tbl__, __key__ = luatbls._get_tbl_index(\luastring{#1}, true)
96 luatbls[__tbl__][__key__] = \luastring{#2}
97 }}
98
99 \NewDocumentCommand{\tbladdN}{m m}{\luadirect{% add a kv pair to a table
100 __tbl__, __key__ = luatbls._get_tbl_index(\luastring{#1}, true)
101 luatbls[__tbl__][__key__] = \luastringN{#2}
102 }}
103
104
105 \NewDocumentCommand{\tblupd}{m m}{\luadirect{% update to a table (ie using integer index) with a list of comma separated values (second arg) #
106 __tbl__ = luatbls._get_tbl_name(\luastring{#1})
107 __tbl_new__ = luatbls._luakeys.parse(string.subpar(\luastring{#2})
108 penlight.tablex.update(luatbls[__tbl__], __tbl_new__)
109 }}
110
111
112 \NewDocumentCommand{\tblupdN}{m m}{\luadirect{% update to a table (ie using integer index) with a list of comma separated values (second arg) #
113 __tbl__ = luatbls._get_tbl_name(\luastring{#1})
114 __tbl_new__ = luatbls._luakeys.parse(string.subpar(\luastringN{#2})
115 penlight.tablex.update(luatbls[__tbl__], __tbl_new__)
116 }}
117
118
119
120
121 \NewDocumentCommand{\tblget}{m}{\luadirect{% get an item using i syntax
122 luatbls._get_tbl_item(\luastring{#1}, true)
123 }}
124
125 \NewDocumentCommand{\tblgetE}{m}{\luadirect{% get an item explicitly
126 tex.sprint(tostring(luatbls.#1))
127 }}
128
129 \NewDocumentCommand{\tblset}{m m}{\luadirect{% set item with {value}
130 luatbls._set_tbl_item(\luastring{#1}, \luastring{#2})
131 }}
132
133 \NewDocumentCommand{\tblsetN}{m m}{\luadirect{% set item with {value}
134 luatbls._set_tbl_item(\luastring{#1}, \luastringN{#2})
135 }}
136
137 \NewDocumentCommand{\tblsetE}{m m}{\luadirect{luatbls.#1 = #2}}% set item with {value}
138
139
140 \NewDocumentCommand{\tblif}{ m +m +O{}}{\ifluax{luatbls._get_tbl_item(\luastring{#1})}{#2}[#3]}
141
142 \NewDocumentCommand{\tblifv}{m +m +O{}}{\ifluaxv{luatbls._get_tbl_item(\luastring{#1})}{#2}[#3]}
143
144 \NewDocumentCommand{\tblifeq}{m m +m +O{}}{\ifluax{luatbls._get_tbl_item(\luastring{#1}) == \expanded{#2}}{#3}[#4]}
145
146 \NewDocumentCommand{\tblifeqstr}{m m +m +O{}}{\ifluax{luatbls._get_tbl_item(\luastring{#1}) == \luastring{#2}}{#3}[#4]}
147
148
149 \NewDocumentCommand{\tblfor}{ m +m }{\luadirect{luatbls._for_tbl(\luastring{#1}, \luastring{#2})}}
150 \NewDocumentCommand{\tblforN}{ m +m }{\luadirect{luatbls._for_tbl(\luastring{#1}, \luastringN{#2})}}
151
152 \NewDocumentCommand{\tblforE}{ m +m }{\luadirect{luatbls._for_tbl_e(luatbls.#1, \luastring{#2})}}
153 \NewDocumentCommand{\tblforEN}{ m +m }{\luadirect{luatbls._for_tbl_e(luatbls.#1, \luastringN{#2})}}
154
155
156 \NewDocumentCommand{\tbldef}{ m o }{\luadirect{luatbls._def_tbl(\luastring{#1}, \luastring{#2})}}
157 \NewDocumentCommand{\tblgdef}{ m o }{\luadirect{luatbls._def_tbl(\luastring{#1}, \luastring{#2}, 'global')}}
158
159 \NewDocumentCommand{\tbldefs}{ m o }{\luadirect{luatbls._def_tbl_some(\luastring{#1}, \luastring{#2})}}
160 \NewDocumentCommand{\tblgdefs}{ m o }{\luadirect{luatbls._def_tbl_some(\luastring{#1}, \luastring{#2}, 'global')}}
161
162
163
164 \ProvideDocumentCommand{\providenewlength}{ m }{\ifdeflength{#1}{}{\newlength{#1}}}
165
166 \NewDocumentCommand{\tblmaketoggle}{ m o }{\luadirect{luatbls._make_toggle_tbl(\luastring{#1}, \luastring{#2})}}
167 \NewDocumentCommand{\tblmakegtoggle}{ m o }{\luadirect{luatbls._make_toggle_tbl(\luastring{#1}, \luastring{#2}, '\\global')}}
168
169 \NewDocumentCommand{\tblmaketoggles}{ m o }{\luadirect{luatbls._make_toggles_tbl(\luastring{#1}, \luastring{#2})}}
170 \NewDocumentCommand{\tblmakegtoggles}{ m o }{\luadirect{luatbls._make_toggles_tbl(\luastring{#1}, \luastring{#2}, '\\global')}}
171
172
173 \NewDocumentCommand{\tblmakelength}{ m o }{\luadirect{luatbls._make_length_tbl(\luastring{#1}, \luastring{#2})}}
174 \NewDocumentCommand{\tblmakeglength}{ m o }{\luadirect{luatbls._make_length_tbl(\luastring{#1}, \luastring{#2}, '\\global')}}
175
176 \NewDocumentCommand{\tblmakelengths}{ m o }{\luadirect{luatbls._make_lengths_tbl(\luastring{#1}, \luastring{#2})}}
177 \NewDocumentCommand{\tblmakeglengths}{ m o }{\luadirect{luatbls._make_lengths_tbl(\luastring{#1}, \luastring{#2}, '\\global')}}
178
179
180 \NewDocumentCommand{\tbldefxy}{ m o }{\luadirect{luatbls._def_tbl_coords(\luastring{#1}, \luastring{#2})}}% define #2x and #2y from a space delimited x-y pair
181
182 \NewDocumentCommand{\tblapply}{m m O{}}{\luadirect{luatbls._apply_tbl(\luastring{#1}, \luastring{#2}, \luastring{#3})}}
183
184 \NewDocumentCommand{\tblprt}{s m}{%
185 \luadirect{penlight.wrth(luatbls._get_tbl(\luastring{#2}),'luatbls: '..\luastring{#2})}%
186 \IfBooleanTF{#1}{\PackageError{luatbls}{Program terminated by tblprt*}{}\stop}{}%
187 }
188