summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorJustin Gassner <justin.gassner@mailbox.org>2023-09-15 20:18:24 +0200
committerJustin Gassner <justin.gassner@mailbox.org>2023-09-15 20:18:24 +0200
commite1238f63de756e285c4f01ad87fee381d083116a (patch)
tree50c4478782bd07e322df6fc4eb707de21597b258 /lua
downloadmy-quicktex-keywords-e1238f63de756e285c4f01ad87fee381d083116a.tar.zst
Initial commit
Diffstat (limited to 'lua')
-rw-r--r--lua/my-quicktex-keywords/init.lua105
1 files changed, 105 insertions, 0 deletions
diff --git a/lua/my-quicktex-keywords/init.lua b/lua/my-quicktex-keywords/init.lua
new file mode 100644
index 0000000..c2bdb69
--- /dev/null
+++ b/lua/my-quicktex-keywords/init.lua
@@ -0,0 +1,105 @@
+-- cspell:words quicktex nvim
+
+local mode = { normal = 0, math = 1, both = 2 }
+
+local quicktex_tex = {}
+local quicktex_math = {}
+
+local function read_item(line)
+ if string.match(line, '^#') then
+ return
+ end
+
+ local item_mode = mode.normal
+ local item_keyword
+ local item_expansion
+
+ if string.match(line, '^MATH ') then
+ item_mode = mode.math
+ line = string.sub(line, 6)
+ elseif string.match(line, '^BOTH ') then
+ item_mode = mode.both
+ line = string.sub(line, 6)
+ end
+
+ local capture = string.match(line, '^ENV (%a+)$')
+ if capture then
+ item_keyword = capture
+ item_expansion = '\\begin{' .. capture .. '}\n<+++>\n\\end{' .. capture .. '}'
+ return item_mode, item_keyword, item_expansion
+ end
+ local cap1, cap2 = string.match(line, '^ENV (%a+) (%g+)$')
+ if cap1 then
+ item_keyword = cap1
+ item_expansion = '\\begin{' .. cap2 .. '}\n<+++>\n\\end{' .. cap2 .. '}'
+ return item_mode, item_keyword, item_expansion
+ end
+ local cap1, cap2 = string.match(line, '^ENV2 (%a+) (%g+)$')
+ if cap1 then
+ item_keyword = cap1
+ item_expansion = '\\begin{' .. cap2 .. '}{}{}\n<+++>\n\\end{' .. cap2 .. '}'
+ return item_mode, item_keyword, item_expansion
+ end
+ local cap1, cap2 = string.match(line, '^ENV3 (%a+) (%g+)$')
+ if cap1 then
+ item_keyword = cap1
+ item_expansion = '\\begin{' .. cap2 .. '}[<++>]\n<+++>\n\\end{' .. cap2 .. '}'
+ return item_mode, item_keyword, item_expansion
+ end
+
+ local capture = string.match(line, '^(%a+)$')
+ if capture then
+ item_keyword = capture
+ item_expansion = '\\' .. capture .. ' '
+ return item_mode, item_keyword, item_expansion
+ end
+
+ local cap1, cap2 = string.match(line, '^(%a+) (%a+)$')
+ if cap1 then
+ item_keyword = cap1
+ item_expansion = '\\' .. cap2 .. ' '
+ return item_mode, item_keyword, item_expansion
+ end
+
+ local cap1, cap2 = string.match(line, '^(%g+) (.+)')
+ if cap1 then
+ item_keyword = cap1
+ item_expansion = cap2
+ return item_mode, item_keyword, item_expansion
+ end
+end
+
+local function parse(path)
+ io.input(path)
+ for line in io.lines() do
+ local m, keyword, expansion = read_item(line)
+ if m == mode.normal then
+ quicktex_tex[keyword] = expansion
+ end
+ if m == mode.math then
+ quicktex_math[keyword] = expansion
+ end
+ if m == mode.both then
+ quicktex_tex[keyword] = expansion
+ quicktex_math[keyword] = expansion
+ end
+ end
+end
+
+local function generate_quicktex_dictionaries()
+ local files = vim.api.nvim_get_runtime_file('*src/tex/*', true)
+ for _, file in pairs(files) do
+ parse(file)
+ end
+ vim.g.quicktex_tex = quicktex_tex
+ vim.g.quicktex_math = quicktex_math
+end
+
+vim.api.nvim_create_user_command('GenerateQuicktexDictionaries', function()
+ generate_quicktex_dictionaries()
+ print('Something should happen here...')
+end, { bang = true, desc = 'generate QuickTex dictionaries' })
+
+return {
+ generate_quicktex_dictionaries = generate_quicktex_dictionaries,
+}