1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
local n = require("luasnip-nodes")
local h = require("luasnip-helpers")
-- pump snippet input into wolframscript and get the output
-- if an error occurs, find out and redo snippet
local mathematica = function (_, snip)
local cmd = "'Check[ToString[" .. snip.captures[1] .. ", TeXForm], Exit[1]]'"
local output = string.sub(vim.fn.system("wolframscript -code " .. cmd), 1, -2)
if string.sub(output, -7, -1) ~= "$Failed" and vim.v.shell_error == 0 then
return n.sn(nil, n.t(output))
else
print("there was an error")
return n.sn(nil, n.fmta("math " .. snip.captures[1] .. "<> math", { n.i(1) }))
end
end
-- creates a matrix row as a snippet node
local rowGenerator = function (j, rows, columns)
local column = {}
local isSquare = rows == columns
for k=1,columns do
local digit = "0"
if isSquare and j==k then
digit = "1"
end
column[2*k-1] = n.i(k, digit)
column[2*k] = n.t(" & ")
end
column[2*columns] = n.t({ " \\\\", "\t" })
if j==rows then
column[2*columns] = nil
end
return n.sn(j, column)
end
-- creates a table row as a snippet node
local tableRowGenerator = function (j, rows, columns)
local column = {}
for k=1,columns do
column[2*k-1] = n.i(k)
column[2*k] = n.t(" & ")
end
column[2*columns] = n.t({ " \\\\ \\hline", "\t" })
if j==rows then
column[2*columns] = n.t({ " \\\\ \\hline" })
end
return n.sn(j, column)
end
-- generates a matrix with dimensions as snippet capture groups
local matrix = function (_, snip)
local rows = tonumber(snip.captures[1])
local columns = tonumber(snip.captures[2])
local nodes = {}
for j=1,rows do
nodes[j] = rowGenerator(j, rows, columns)
end
return n.sn(1, nodes)
end
-- generates a table with dimensions as snippet capture groups
local table = function (_, snip)
local rows = tonumber(snip.captures[1])
local columns = tonumber(snip.captures[2])
local nodes = {}
for j=1,rows do
nodes[j] = tableRowGenerator(j, rows, columns)
end
return n.sn(1, nodes)
end
-- generates little partition definition thing for table environment
local tableCols = function (_, snip)
local columns = tonumber(snip.captures[2])
local output = string.rep("|l", columns)
return output .. "|"
end
-- turns identifier into enum prefix
local enumType = function (_, snip)
local type = snip.captures[1]
if type == "n" then
return "\\arabic*."
elseif type == "a" then
return "\\alph*)"
elseif type == "i" then
return "(\\roman*)"
end
end
return {
-- environment
n.s({trig="beg", snippetType="autosnippet"},
n.fmta(
[[
\begin{<>}
<>
\end{<>}
]],
{ n.i(1), n.i(0), n.rep(1) }),
{ condition = h.line_begin }),
-- named environment
n.s({trig="beng", snippetType="autosnippet"},
n.fmta(
[[
\begin{<>}[<>]
<>
\end{<>}
]],
{ n.i(1), n.i(2), n.i(0), n.rep(1) }),
{ condition = h.line_begin }),
-- equation
n.s({trig="beq", snippetType="autosnippet"},
n.fmta(
[[
\begin{equation}
<>
\end{equation}
]],
{ n.i(0) }),
{ condition = h.line_begin }),
-- WIP plot snippet
n.s({trig="plot(", snippetType="autosnippet"},
n.fmta(
"plot(<>) <> plot",
{ n.i(1), n.i(2) })
),
-- mathematica snippet
n.s({trig="math", snippetType="autosnippet"},
n.fmta("math <> math",
{ n.i(1) }),
{ condition = h.in_mathzone }
),
n.s({trig="math (.*) math", regTrig=true, wordTrig=false},
{ n.d(1, mathematica) },
{ condition = h.in_mathzone }
),
-- bmatrix
n.s({trig="bm", snippetType="autosnippet"},
n.fmta(
[[
\begin{bmatrix}{<>,<>} \end{bmatrix}
]],
{ n.i(1), n.i(2) }),
{ condition = h.line_begin * h.in_mathzone }),
-- matrix with arbitrary brackets
n.s({trig="zmat", snippetType="autosnippet"},
n.fmta(
[[
\begin{<>matrix}{<>,<>} \end{<>matrix}
]],
{ n.i(1), n.i(2), n.i(3), n.rep(1) }),
{ condition = h.line_begin * h.in_mathzone }),
n.s({trig="\\begin{.matrix}{(%d+),(%d+)} \\end{(.)matrix}", regTrig=true, wordTrig=false},
n.fmta(
[[
\begin{<>matrix}
<>
\end{<>matrix}
]],
{ n.f(function(_, parent) return parent.captures[3] end), n.d(1, matrix), n.f(function(_, parent) return parent.captures[3] end) }),
{ condition = h.in_mathzone }),
-- table
n.s({trig="tab", snippetType="autosnippet"},
n.fmta(
[[
\begin{table}{<>,<>} \end{table}
]],
{ n.i(1), n.i(2) }),
{ condition = h.line_begin }),
n.s({trig="\\begin{table}{(%d+),(%d+)} \\end{table}", regTrig=true, wordTrig=false},
n.fmta(
[[
\begin{tabular}{<>}
\hline
<>
\end{tabular}
]],
{ n.f(tableCols), n.d(1, table) })),
-- enum with n=numbers, a=alphas, or i=roman numerals
n.s({trig="enum([nai])", regTrig=true, snippetType="autosnippet"},
n.fmta(
[[
\begin{enumerate}[label=<>]
\item <>
\end{enumerate}
]],
{ n.f(enumType), n.i(0) }),
{ condition = h.in_text * h.line_begin }),
-- itemize
n.s({trig="item", snippetType="autosnippet"},
n.fmta(
[[
\begin{itemize}
\item <>
\end{itemize}
]],
{ n.i(0) }),
{ condition = h.in_text * h.line_begin }),
-- split equation environment
n.s({trig="slt", snippetType="autosnippet"},
n.fmta(
[[
\begin{equation}\begin{split}
<>
\end{split}\end{equation}
]],
{ n.i(0) }),
{ condition = h.in_text * h.line_begin })
-- split display equation
n.s({trig="sld", snippetType="autosnippet"},
n.fmta(
[[
\[ \begin{split}
<>
\end{split} \]
]],
{ n.i(0) }),
{ condition = h.in_text * h.line_begin })
}
|