feat(snip): series of numbered letters

This commit is contained in:
arne314
2025-01-14 20:25:16 +01:00
parent 804d9d334d
commit c273afbf4e
2 changed files with 32 additions and 14 deletions

View File

@@ -14,7 +14,7 @@ Available snippets can mostly be intuitively derived from [here](././lua/typstar
Markup snippets: Markup snippets:
- Begin inline math with `ll` and multiline math with `dm` - Begin inline math with `ll` and multiline math with `dm`
- [Markup shorthands](./lua/typstar/snippets/markup.lua) (e.g. `HIG` &#8594; `#highlight[<cursor>]`, `IMP` &#8594; `$=>$ `) - [Markup shorthands](./lua/typstar/snippets/markup.lua) (e.g. `HIG` &#8594; `#highlight[<cursor>]`, `IMP` &#8594; `$==>$ `)
- [ctheorems shorthands](./lua/typstar/snippets/markup.lua) (e.g. `tem` &#8594; empty theorem, `exa` &#8594; empty example) - [ctheorems shorthands](./lua/typstar/snippets/markup.lua) (e.g. `tem` &#8594; empty theorem, `exa` &#8594; empty example)
- [Flashcards](#anki): `fla` and `flA` - [Flashcards](#anki): `fla` and `flA`
- All above snippets support visual mode via the [selection key](#installation) - All above snippets support visual mode via the [selection key](#installation)
@@ -23,7 +23,8 @@ Math snippets:
- [Many shorthands](./lua/typstar/snippets/math.lua) for mathematical expressions - [Many shorthands](./lua/typstar/snippets/math.lua) for mathematical expressions
- Alphanumeric characters: `:<char>` &#8594; `$<char>$ ` in markup (e.g. `:X` &#8594; `$X$ `, `:5` &#8594; `$5$ `) - Alphanumeric characters: `:<char>` &#8594; `$<char>$ ` in markup (e.g. `:X` &#8594; `$X$ `, `:5` &#8594; `$5$ `)
- Greek letters: `;<latin>` &#8594; `<greek>` in math and `$<greek>$ ` in markup (e.g. `;a` &#8594; `alpha`/`$alpha$ `) - Greek letters: `;<latin>` &#8594; `<greek>` in math and `$<greek>$ ` in markup (e.g. `;a` &#8594; `alpha`/`$alpha$ `)
- Common indices (numbers and letters `i-n`): `<letter><index>` &#8594; `<letter>_<index>` in math and `$<letter>$<index> ` &#8594; `$<letter>_<index>$ ` in markup (e.g `A314` &#8594; `A_314`, `$alpha$n ` &#8594; `$alpha_n$ `) - Common indices (numbers and letters `i-n`): `<letter><index> ` &#8594; `<letter>_<index> ` in math and `$<letter>$<index> ` &#8594; `$<letter>_<index>$ ` in markup (e.g `A314 ` &#8594; `A_314 `, `$alpha$n ` &#8594; `$alpha_n$ `)
- Series of numbered letters: `<letter> ot<optional last index> ` &#8594; `<letter>_1, <letter>_2, ... ` (e.g. `a ot ` &#8594; `a_1, a_2, ... `, `a ot4 ` &#8594; `a_1, a_2, a_3, a_4 `, `alpha ot k ` &#8594; `alpha_1, alpha_2, ..., alpha_k `)
- Wrapping of any mathematical expression (see [operations](./lua/typstar/snippets/visual.lua), works nested, multiline and in visual mode via the [selection key](#installation)): `<expression><operation>` &#8594; `<operation>(<expression>)` (e.g. `(a^2+b^2)rt` &#8594; `sqrt(a^2+b^2)`, `lambdatd` &#8594; `tilde(lambda)`, `(1+1)sQ` &#8594; `[1+1]`, `(1+1)sq` &#8594; `[(1+1)]`) - Wrapping of any mathematical expression (see [operations](./lua/typstar/snippets/visual.lua), works nested, multiline and in visual mode via the [selection key](#installation)): `<expression><operation>` &#8594; `<operation>(<expression>)` (e.g. `(a^2+b^2)rt` &#8594; `sqrt(a^2+b^2)`, `lambdatd` &#8594; `tilde(lambda)`, `(1+1)sQ` &#8594; `[1+1]`, `(1+1)sq` &#8594; `[(1+1)]`)
- Matrices: `<size>ma` and `<size>lma` (e.g. `23ma` &#8594; 2x3 matrix) - Matrices: `<size>ma` and `<size>lma` (e.g. `23ma` &#8594; 2x3 matrix)

View File

@@ -76,24 +76,41 @@ local get_index = function(_, snippet)
return s(nil, t(letter .. '_' .. index)) return s(nil, t(letter .. '_' .. index))
end end
table.insert(letter_snippets, snip(':([A-Za-z0-9])', '$<>$ ', { cap(1) }, markup)) local get_series = function(_, snippet)
table.insert(letter_snippets, snip(';(' .. trigger_greek .. ')', '$<>$ ', { d(1, get_greek) }, markup)) local letter, target = snippet.captures[1], snippet.captures[2]
table.insert(letter_snippets, snip(';(' .. trigger_greek .. ')', '<>', { d(1, get_greek) }, math)) local target_num = tonumber(target)
table.insert( local result
letter_snippets, if target_num then
local res = {}
for n = 1, target_num do
table.insert(res, string.format('%s_%d', letter, n))
if n ~= target_num then table.insert(res, ', ') end
end
result = table.concat(res, '')
else
result = string.format('%s_1, %s_2, ... %s_%s', letter, letter, letter, target)
end
return s(nil, t(result))
end
return {
-- latin/greek
snip(':([A-Za-z0-9])', '$<>$ ', { cap(1) }, markup),
snip(';(' .. trigger_greek .. ')', '$<>$ ', { d(1, get_greek) }, markup),
snip(';(' .. trigger_greek .. ')', '<>', { d(1, get_greek) }, math),
-- indices
snip( snip(
'\\$(' .. trigger_index_pre .. ')\\$' .. '(' .. trigger_index_post .. ') ', '\\$(' .. trigger_index_pre .. ')\\$' .. '(' .. trigger_index_post .. ') ',
'$<>$ ', '$<>$ ',
{ d(1, get_index) }, { d(1, get_index) },
markup, markup,
500 500
) ),
) snip('(' .. trigger_index_pre .. ')' .. '(' .. trigger_index_post .. ') ', '<> ', { d(1, get_index) }, math, 200),
table.insert(
letter_snippets,
snip('(' .. trigger_index_pre .. ')' .. '(' .. trigger_index_post .. ') ', '<> ', { d(1, get_index) }, math, 200)
)
return { -- series of numbered letters
snip('(' .. trigger_index_pre .. ') ot ', '<>_1, <>_2, ... ', { cap(1), cap(1) }, math), -- a_1, a_2, ...
snip('(' .. trigger_index_pre .. ') ot(\\w+) ', '<> ', { d(1, get_series) }, math), -- a_1, a_2, ... a_j or a_1, a_2, a_2, a_3, a_4, a_5
unpack(letter_snippets), unpack(letter_snippets),
} }