From 52d0fd3efa2654d417325a5635cc8d254e8b15ad Mon Sep 17 00:00:00 2001 From: Andreas Tsouchlos Date: Sat, 8 Nov 2025 16:47:12 +0100 Subject: [PATCH] Add luasnip and snippets --- nvim/.config/nvim/lua/plugins/luasnip.lua | 69 +++ nvim/.config/nvim/snippets/all/surround.lua | 83 ++++ nvim/.config/nvim/snippets/tex/general.lua | 285 +++++++++++++ nvim/.config/nvim/snippets/tex/greek.lua | 439 ++++++++++++++++++++ nvim/.config/nvim/snippets/tex/math.lua | 418 +++++++++++++++++++ nvim/.config/nvim/snippets/tex/plotting.lua | 132 ++++++ 6 files changed, 1426 insertions(+) create mode 100644 nvim/.config/nvim/lua/plugins/luasnip.lua create mode 100644 nvim/.config/nvim/snippets/all/surround.lua create mode 100644 nvim/.config/nvim/snippets/tex/general.lua create mode 100644 nvim/.config/nvim/snippets/tex/greek.lua create mode 100644 nvim/.config/nvim/snippets/tex/math.lua create mode 100644 nvim/.config/nvim/snippets/tex/plotting.lua diff --git a/nvim/.config/nvim/lua/plugins/luasnip.lua b/nvim/.config/nvim/lua/plugins/luasnip.lua new file mode 100644 index 0000000..176baab --- /dev/null +++ b/nvim/.config/nvim/lua/plugins/luasnip.lua @@ -0,0 +1,69 @@ +vim.pack.add({ + { src = "https://github.com/rafamadriz/friendly-snippets" }, + { src = "https://github.com/L3MON4D3/LuaSnip" } +}) + +local loader = require("luasnip.loaders.from_lua") +loader.lazy_load({ paths = { "./snippets" } }) + +require("luasnip").config.set_config({ + enable_autosnippets = true, + store_selection_keys = "v" +}) + +vim.keymap.set({ "i" }, "", function() require("luasnip").expand() end, { silent = true }) +vim.keymap.set({ "i", "s" }, "", function() require("luasnip").jump(1) end, { silent = true }) +vim.keymap.set({ "i", "s" }, "", function() require("luasnip").jump(-1) end, { silent = true }) + +-- TODO: Custom LSP server to supply snippets to completion + + +-- return { +-- { +-- "L3MON4D3/LuaSnip", +-- dependencies = { +-- { +-- 'rafamadriz/friendly-snippets' +-- } +-- }, +-- version = "v2.*", +-- build = "make install_jsregexp", +-- event = { +-- "InsertEnter" +-- }, +-- init = function() +-- vim.keymap.set({ +-- "i" +-- }, "", function() require("luasnip").expand() end, { +-- silent = true +-- }) +-- vim.keymap.set({ +-- "i", +-- "s" +-- }, "", function() require("luasnip").jump(1) end, { +-- silent = true +-- }) +-- vim.keymap.set({ +-- "i", +-- "s" +-- }, "", function() require("luasnip").jump(-1) end, { +-- silent = true +-- }) +-- +-- end, +-- config = function() +-- local loader = require("luasnip.loaders.from_lua") +-- loader.lazy_load({ +-- paths = { +-- "./snippets" +-- } +-- }) +-- +-- require("luasnip").config.set_config({ +-- enable_autosnippets = true, +-- store_selection_keys = "v" +-- }) +-- end +-- } +-- } +-- diff --git a/nvim/.config/nvim/snippets/all/surround.lua b/nvim/.config/nvim/snippets/all/surround.lua new file mode 100644 index 0000000..5c83c85 --- /dev/null +++ b/nvim/.config/nvim/snippets/all/surround.lua @@ -0,0 +1,83 @@ +---------------- +-- Abbreviations +---------------- + + +local ls = require("luasnip") +local s = ls.snippet +local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local d = ls.dynamic_node +local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +local rep = require("luasnip.extras").rep +local line_begin = require("luasnip.extras.expand_conditions").line_begin + + +----------- +-- Snippets +----------- + + +return { + + +s({trig="()", wordTrig=false, snippetType="autosnippet"}, + { + t("("), + i(1), + t(")"), + i(0), + } +), + +s({trig="[]", wordTrig=false, snippetType="autosnippet"}, + { + t("["), + i(1), + t("]"), + i(0), + } +), + +s({trig="{}", wordTrig=false, snippetType="autosnippet"}, + { + t("{"), + i(1), + t("}"), + i(0), + } +), + +s({trig="<>", wordTrig=false, snippetType="autosnippet"}, + { + t("<"), + i(1), + t(">"), + i(0), + } +), + +s({trig="\"\"", wordTrig=false, snippetType="autosnippet"}, + { + t("\""), + i(1), + t("\""), + i(0), + } +), + +s({trig="''", wordTrig=false, snippetType="autosnippet"}, + { + t("'"), + i(1), + t("'"), + i(0), + } +), + + +} -- return + diff --git a/nvim/.config/nvim/snippets/tex/general.lua b/nvim/.config/nvim/snippets/tex/general.lua new file mode 100644 index 0000000..79b0ce1 --- /dev/null +++ b/nvim/.config/nvim/snippets/tex/general.lua @@ -0,0 +1,285 @@ +---------------- +-- Abbreviations +---------------- + + +local ls = require("luasnip") +local s = ls.snippet +local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local d = ls.dynamic_node +local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +local rep = require("luasnip.extras").rep +local conds = require("luasnip.extras.expand_conditions") + +--------------- +-- Environments +--------------- + + +local tex_utils = {} + +tex_utils.in_mathzone = function() -- math context detection + return vim.fn['vimtex#syntax#in_mathzone']() == 1 +end + +tex_utils.in_text = function() + return not tex_utils.in_mathzone() +end + +tex_utils.in_comment = function() -- comment detection + return vim.fn['vimtex#syntax#in_comment']() == 1 +end + +tex_utils.in_env = function(name) -- generic environment detection + local is_inside = vim.fn['vimtex#env#is_inside'](name) + return (is_inside[1] > 0 and is_inside[2] > 0) +end + +local get_visual = function(args, parent) + if (#parent.snippet.env.LS_SELECT_RAW > 0) then + return sn(nil, i(1, parent.snippet.env.LS_SELECT_RAW)) + else + return sn(nil, i(1)) + end +end + + +----------- +-- Snippets +----------- + + +return { + + +s({trig="notes"}, + fmta( + [[ + \documentclass[dvipsnames]{article} + + \usepackage{float} + \usepackage{amsmath} + \usepackage{amsfonts} + \usepackage{mleftright} + \usepackage{bm} + \usepackage{tikz} + \usepackage{xcolor} + \usepackage{pgfplots} + \pgfplotsset{compat=newest} + + \title{<>} + \author{<>} + \date{<>} + + \begin{document} + \maketitle + + <> + \end{document} + ]], + { + i(1, "Notes"), + i(2, "Andreas Tsouchlos"), + i(3, os.date("%d.%m.%y")), + i(0), + } + ) +), + +s({trig="beg", wordTrig=true, snippetType="autosnippet", condition=conds.line_begin}, + fmta( + [[ + \begin{<>} + <> + \end{<>} + ]], + { + i(1), + i(2), + rep(1), + } + ) +), + +s({trig="item", wordTrig=true, snippetType="autosnippet", +condition = function(line_to_cursor, matched_trigger, captures) + return tex_utils.in_env("itemize") and conds.line_begin(line_to_cursor, matched_trigger, captures) +end}, + fmta( + [[ + \item <> + ]], + { + i(0), + } + ) +), + +s({trig="item", wordTrig=true, snippetType="autosnippet", condition=conds.line_begin}, + fmta( + [[ + \begin{itemize} + \item <> + \end{itemize} + ]], + { + i(0), + } + ) +), + +s({trig="frame", wordTrig=true, snippetType="autosnippet", condition=conds.line_begin}, + fmta( + [[ + \begin{frame} + \frametitle{<>} + \end{frame} + ]], + { + i(0), + } + ) +), + +s({trig="sec"}, + fmta( + [[ + \section{<>} + \label{sec:<>} + ]], + { + i(1), + rep(1), + } + ) +), + +s({trig="ssec"}, + fmta( + [[ + \subsection{<>} + \label{subsec:<>} + ]], + { + i(1), + rep(1), + } + ) +), + +s({trig="sssec"}, + fmta( + [[ + \subsubsection{<>} + \label{subsubsec:<>} + ]], + { + i(1), + rep(1), + } + ) +), + +s({trig="csec"}, + fmta( + [[ + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + \section{<>} + \label{sec:<>} + ]], + { + i(1), + rep(1), + } + ) +), + +s({trig="cssec"}, + fmta( + [[ + %%%%%%%%%%%%%%%% + \subsection{<>} + \label{subsec:<>} + ]], + { + i(1), + rep(1), + } + ) +), + +s({trig="csssec"}, + fmta( + [[ + %%%%%%%% + \subsubsection{<>} + \label{subsubsec:<>} + ]], + { + i(1), + rep(1), + } + ) +), + +-- s({trig="()", wordTrig=false, snippetType="autosnippet"}, +-- { +-- t("("), +-- i(1), +-- t(")"), +-- i(0), +-- } +-- ), +-- +-- s({trig="[]", wordTrig=false, snippetType="autosnippet"}, +-- { +-- t("["), +-- i(1), +-- t("]"), +-- i(0), +-- } +-- ), +-- +-- s({trig="{}", wordTrig=false, snippetType="autosnippet"}, +-- { +-- t("{"), +-- i(1), +-- t("}"), +-- i(0), +-- } +-- ), +-- +-- s({trig="<>", wordTrig=false, snippetType="autosnippet"}, +-- { +-- t("<"), +-- i(1), +-- t(">"), +-- i(0), +-- } +-- ), +-- +-- s({trig="\"\"", wordTrig=false, snippetType="autosnippet"}, +-- { +-- t("\""), +-- i(1), +-- t("\""), +-- i(0), +-- } +-- ), +-- +-- s({trig="''", wordTrig=false, snippetType="autosnippet"}, +-- { +-- t("'"), +-- i(1), +-- t("'"), +-- i(0), +-- } +-- ), + + +} -- return + diff --git a/nvim/.config/nvim/snippets/tex/greek.lua b/nvim/.config/nvim/snippets/tex/greek.lua new file mode 100644 index 0000000..29b0154 --- /dev/null +++ b/nvim/.config/nvim/snippets/tex/greek.lua @@ -0,0 +1,439 @@ +---------------- +-- Abbreviations +---------------- + + +local ls = require("luasnip") +local s = ls.snippet +local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local d = ls.dynamic_node +local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +local rep = require("luasnip.extras").rep + + +--------------- +-- Environments +--------------- + + +local tex_utils = {} + +tex_utils.in_mathzone = function() -- math context detection + return vim.fn['vimtex#syntax#in_mathzone']() == 1 +end + +tex_utils.in_text = function() + return not tex_utils.in_mathzone() +end + +tex_utils.in_comment = function() -- comment detection + return vim.fn['vimtex#syntax#in_comment']() == 1 +end + +tex_utils.in_env = function(name) -- generic environment detection + local is_inside = vim.fn['vimtex#env#is_inside'](name) + return (is_inside[1] > 0 and is_inside[2] > 0) +end + +local get_visual = function(args, parent) + if (#parent.snippet.env.LS_SELECT_RAW > 0) then + return sn(nil, i(1, parent.snippet.env.LS_SELECT_RAW)) + else + return sn(nil, i(1)) + end +end + + +----------- +-- Snippets +----------- + + +return { + + +s({trig="´a", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\alpha ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´b", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\beta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´g", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\gamma ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´d", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\delta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´e", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\epsilon ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´z", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\zeta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´h", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\eta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´q", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\theta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´i", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\iota ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´k", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\kappa ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´l", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\lambda ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´m", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\mu ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´n", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\nu ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´x", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\xi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´p", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\pi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´r", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\rho ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´s", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\sigma ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´t", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\tau ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´u", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\upsilon ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´f", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\phi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´c", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\chi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´y", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\psi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´w", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\omega ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´A", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Alpha ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´B", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Beta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´G", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Gamma ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´D", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Delta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´E", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Epsilon ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´Z", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Zeta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´H", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Eta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´Q", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Theta ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´I", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Iota ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´K", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Kappa ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´L", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Lambda ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´M", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Mu ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´N", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Nu ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´X", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Xi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´P", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Pi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´R", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Rho ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´S", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Sigma ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´T", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Tau ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´U", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Upsilon ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´F", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Phi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´C", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Chi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´Y", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Psi ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="´W", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Omega ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + + +-- Strictly speaking not greek characters, but very similar snippets + +s({trig="´6", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\partial ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +} -- return + diff --git a/nvim/.config/nvim/snippets/tex/math.lua b/nvim/.config/nvim/snippets/tex/math.lua new file mode 100644 index 0000000..de759f4 --- /dev/null +++ b/nvim/.config/nvim/snippets/tex/math.lua @@ -0,0 +1,418 @@ +---------------- +-- Abbreviations +---------------- + + +local ls = require("luasnip") +local s = ls.snippet +local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local d = ls.dynamic_node +local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +local rep = require("luasnip.extras").rep +local conds = require("luasnip.extras.expand_conditions") + + +--------------- +-- Environments +--------------- + + +local tex_utils = {} + +tex_utils.in_mathzone = function() -- math context detection + return vim.fn['vimtex#syntax#in_mathzone']() == 1 +end + +tex_utils.in_text = function() + return not tex_utils.in_mathzone() +end + +tex_utils.in_comment = function() -- comment detection + return vim.fn['vimtex#syntax#in_comment']() == 1 +end + +tex_utils.in_env = function(name) -- generic environment detection + local is_inside = vim.fn['vimtex#env#is_inside'](name) + return (is_inside[1] > 0 and is_inside[2] > 0) +end + +local get_visual = function(args, parent) + if (#parent.snippet.env.LS_SELECT_RAW > 0) then + return sn(nil, i(1, parent.snippet.env.LS_SELECT_RAW)) + else + return sn(nil, i(1)) + end +end + + +----------- +-- Snippets +----------- + + +return { + + +s({trig="ali", wordTrig=true, snippetType="autosnippet", condition=conds.line_begin}, + fmta( + [[ + \begin{align*} + <> + <>% + \end{align*} + ]], + { + i(0), + i(1, "."), + } + ) +), + +s({trig="cases"}, + fmta( + [[ + \begin{cases} + <> + \end{cases} + ]], + { + i(0), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="matb"}, + fmta( + [[ + \begin{bmatrix} + <> + \end{bmatrix} + ]], + { + i(0), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="matp"}, + fmta( + [[ + \begin{pmatrix} + <> + \end{pmatrix} + ]], + { + i(0), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])array", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[ + <>\begin{array}{c} + <> + \end{array} + ]], + { + f( function(_, snip) return snip.captures[1] end ), + i(0), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig='([^%a])int', regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\int_{<>}^{<>} <> d<> ]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1, "-\\infty"), + i(2, "\\infty"), + i(4), + i(3, "x"), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig='([^%a])prod', regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\prod_{<>}^{<>} ]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1, "-\\infty"), + i(2, "\\infty"), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig='([^%a])sum', regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\sum_{<>}^{<>} ]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1, "n=0"), + i(2, "N-1"), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig='([^%a])sqrt', regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\sqrt{<>} ]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig='([^%a])log', regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\log_{<>} ]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1, "2"), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig='([^%a])ln', regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\ln ]], + { + f( function(_, snip) return snip.captures[1] end ), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig = '([^%a])ff', regTrig = true, wordTrig = false, snippetType = "autosnippet"}, + fmta( + [[<>\frac{<>}{<>} ]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + i(2) + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])mcal", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\mathcal{<>}]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])mbb", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\mathbb{<>}]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])tt", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\text{<>}]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +-- s({trig=".", snippetType="autosnippet"}, +-- fmta( +-- [[\cdot ]], +-- {} +-- ), +-- {condition = tex_utils.in_mathzone} +-- ), + +s({trig="$$", snippetType="autosnippet"}, + fmta( + [[$<>$]], + { + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])bm", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\bm{<>}]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="__", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[_{<>}]], + { + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="^^", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[^{<>}]], + { + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])oo", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\infty]], + { + f( function(_, snip) return snip.captures[1] end ), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="((", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\mleft( <> \mright)]], + { + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="[[", wordTrig=false, snippetType="autosnippet"}, + fmta( + "\\mleft[ <> \\mright]", + { + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="{{", wordTrig=false, snippetType="autosnippet"}, + fmta( + "\\mleft\\{ <> \\mright\\}", + { + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="||", wordTrig=false, snippetType="autosnippet"}, + fmta( + "\\lvert <> \\rvert ", + { + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])hat", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\hat{<>}]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])tld", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\tilde{<>}]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])bar", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\overline{<>}]], + { + f( function(_, snip) return snip.captures[1] end ), + i(1), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([%a])([%d])", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>_<>]], + { + f( function(_, snip) return snip.captures[1] end ), + f( function(_, snip) return snip.captures[2] end ), + } + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="=>", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\Rightarrow ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="->", wordTrig=false, snippetType="autosnippet"}, + fmta( + [[\rightarrow ]], + {} + ), + {condition = tex_utils.in_mathzone} +), + +s({trig="([^%a])nab", regTrig=true, wordTrig=false, snippetType="autosnippet"}, + fmta( + [[<>\nabla ]], + { + f( function(_, snip) return snip.captures[1] end ), + } + ), + {condition = tex_utils.in_mathzone} +), + + +} -- return + diff --git a/nvim/.config/nvim/snippets/tex/plotting.lua b/nvim/.config/nvim/snippets/tex/plotting.lua new file mode 100644 index 0000000..1c64a09 --- /dev/null +++ b/nvim/.config/nvim/snippets/tex/plotting.lua @@ -0,0 +1,132 @@ +---------------- +-- Abbreviations +---------------- + + +local ls = require("luasnip") +local s = ls.snippet +local sn = ls.snippet_node +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local d = ls.dynamic_node +local fmt = require("luasnip.extras.fmt").fmt +local fmta = require("luasnip.extras.fmt").fmta +local rep = require("luasnip.extras").rep + + +--------------- +-- Environments +--------------- + + +local tex_utils = {} + +tex_utils.in_mathzone = function() -- math context detection + return vim.fn['vimtex#syntax#in_mathzone']() == 1 +end + +tex_utils.in_text = function() + return not tex_utils.in_mathzone() +end + +tex_utils.in_comment = function() -- comment detection + return vim.fn['vimtex#syntax#in_comment']() == 1 +end + +tex_utils.in_env = function(name) -- generic environment detection + local is_inside = vim.fn['vimtex#env#is_inside'](name) + return (is_inside[1] > 0 and is_inside[2] > 0) +end + +local get_visual = function(args, parent) + if (#parent.snippet.env.LS_SELECT_RAW > 0) then + return sn(nil, i(1, parent.snippet.env.LS_SELECT_RAW)) + else + return sn(nil, i(1)) + end +end + + +----------- +-- Snippets +----------- + + +return { + + +s({trig="funcplot"}, + fmta( + [[ + \begin{figure}[H] + \centering + + \begin{tikzpicture} + \begin{axis}[ + domain=<>, + width=\textwidth, + height=0.75\textwidth, + ] + \addplot+[mark=none, line width=1pt] + {<>}; + \end{axis} + \end{tikzpicture} + \end{figure} + ]], + { + i(1, "-5:5"), + i(2), + } + ) +), + +s({trig="csvplot"}, + fmta( + [[ + \begin{figure}[H] + \centering + + \begin{tikzpicture} + \begin{axis}[ + width=\textwidth, + height=0.75\textwidth, + ] + \addplot+[mark=none, line width=1pt] + table[col sep=comma, x=<>, y=<>] + {<>}; + \end{axis} + \end{tikzpicture} + \end{figure} + ]], + { + i(1, "x"), + i(2, "y"), + i(3), + } + ) +), + +s({trig="fig"}, + fmta( + [[ + \begin{figure}[H] + \centering + + <> + + \caption{<>} + \label{fig:<>} + \end{figure} + ]], + { + i(0), + i(1), + i(2), + } + ) +), + + +} -- return +