This commit is contained in:
sharings 2025-07-26 12:43:01 +00:00
commit 1e2ca5a6fc
71 changed files with 3330 additions and 0 deletions

View file

@ -0,0 +1,68 @@
local M = {}
-- stop lsp error messages
-- https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#loaders
local ls = require("luasnip")
local s = ls.snippet
local sn = ls.snippet_node
local isn = ls.indent_snippet_node
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local c = ls.choice_node
local d = ls.dynamic_node
local r = ls.restore_node
local events = require("luasnip.util.events")
local ai = require("luasnip.nodes.absolute_indexer")
local extras = require("luasnip.extras")
local l = extras.lambda
local rep = extras.rep
local p = extras.partial
local m = extras.match
local n = extras.nonempty
local dl = extras.dynamic_lambda
local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta
local conds = require("luasnip.extras.expand_conditions")
local postfix = require("luasnip.extras.postfix").postfix
local types = require("luasnip.util.types")
local parse = require("luasnip.util.parser").parse_snippet
local ms = ls.multi_snippet
local k = require("luasnip.nodes.key_indexer").new_key
-- Example: expanding a snippet on a new line only.
-- In a snippet file, first require the line_begin condition...
local line_begin = conds.line_begin
-- This is the `get_visual` function I've been talking about.
-- ----------------------------------------------------------------------------
-- Summary: When `LS_SELECT_RAW` is populated with a visual selection, the function
-- returns an insert node whose initial text is set to the visual selection.
-- When `LS_SELECT_RAW` is empty, the function simply returns an empty insert node.
M.get_visual = function(_, parent, _, user_args)
if #parent.snippet.env.LS_SELECT_RAW > 0 then
return sn(nil, i(1, parent.snippet.env.LS_SELECT_RAW))
else -- If LS_SELECT_RAW is empty, return a blank insert node
return sn(nil, i(1, user_args))
end
end
-- https://github.com/L3MON4D3/LuaSnip/discussions/1195
M.make_single_curly_bracket_snippets = function(type, trig, command, user_args)
-- return s({trig=keyword,desc=desc}, {t"local function " .. keyword .. "(", i(1), t")"})
return s({
trig = trig,
desc = "Expand " .. trig .. " into \\" .. command .. "{}",
snippetType = type,
}, fmt("\\" .. command .. "{<>}", { d(1, M.get_visual, { user_args = { user_args } }) }, { delimiters = "<>" }))
end
M.make_scope_snippets = function(type, trig, command, user_args)
-- return s({trig=keyword,desc=desc}, {t"local function " .. keyword .. "(", i(1), t")"})
return s({
trig = trig,
desc = "Expand " .. trig .. " into {\\" .. command .. " ... }",
snippetType = type,
}, fmt("{\\" .. command .. " <>}", { d(1, M.get_visual, { user_args = { user_args } }) }, { delimiters = "<>" }))
end
return M

View file

@ -0,0 +1,102 @@
{ helpers, ... }:
let
inherit (helpers) mkRaw;
in
{
plugins.luasnip = {
enable = true;
filetypeExtend = {
lua = [
"c"
"cpp"
];
};
settings = {
# https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#config-options
enable_autosnippets = true;
# https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#variables
cut_selection_keys = "<Tab>";
update_events = "TextChanged,TextChangedI";
};
# https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#lua
fromLua = [
{ paths = ./snippets; } # snippts attached to nixvim repo
{ paths = "~/snippets"; } # snippts in home dir
];
};
extraFiles = {
"lua/luasnip-helper-funcs.lua".source = ./helpers/luasnip-helper-funcs.lua;
};
# https://github.com/L3MON4D3/LuaSnip/wiki/Nice-Configs#hint-node-type-with-virtual-text
extraConfigLua = ''
local types = require("luasnip.util.types")
require'luasnip'.config.setup({
ext_opts = {
[types.choiceNode] = {
active = {
-- virt_text = {{"", "GruvboxOrange"}}
-- see ":h group-name" (syntax highlighting)
virt_text = {{ '««« Choice Node', 'Constant', }}
}
},
[types.insertNode] = {
active = {
-- virt_text = {{"", "GruvboxBlue"}}
-- see ":h group-name" (syntax highlighting)
virt_text = {{ '««« Insert Node', 'Comment', }}
}
}
},
})
'';
keymaps = [
{
mode = [
"i"
"s"
];
key = "<C-j>";
action = mkRaw ''
function()
local ls = require('luasnip')
if ls.expand_or_jumpable() then
ls.expand_or_jump()
end
end
'';
options.silent = true;
}
{
mode = [
"i"
"s"
];
key = "<C-k>";
action = mkRaw ''
function()
local ls = require('luasnip')
if ls.jumpable(-1) then
ls.jump(-1)
end
end
'';
options.silent = true;
}
{
mode = [
"i"
"s"
];
key = "<C-e>";
action = mkRaw ''
function()
local ls = require('luasnip')
if ls.choice_active() then
ls.change_choice(1)
end
end
'';
options.silent = true;
}
];
}

View file

@ -0,0 +1,62 @@
-- stop lsp error messages
-- https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#loaders
local ls = require("luasnip")
local s = ls.snippet
local sn = ls.snippet_node
local isn = ls.indent_snippet_node
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local c = ls.choice_node
local d = ls.dynamic_node
local r = ls.restore_node
local events = require("luasnip.util.events")
local ai = require("luasnip.nodes.absolute_indexer")
local extras = require("luasnip.extras")
local l = extras.lambda
local rep = extras.rep
local p = extras.partial
local m = extras.match
local n = extras.nonempty
local dl = extras.dynamic_lambda
local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta
local conds = require("luasnip.extras.expand_conditions")
local postfix = require("luasnip.extras.postfix").postfix
local types = require("luasnip.util.types")
local parse = require("luasnip.util.parser").parse_snippet
local ms = ls.multi_snippet
local k = require("luasnip.nodes.key_indexer").new_key
-- tutorials:
-- https://ejmastnak.com/tutorials/vim-latex/luasnip/#anatomy
-- https://evesdropper.dev/files/luasnip/
return
-- https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#lua
-- the files may return two lists of snippets, the snippets in the first are
-- all added as regular snippets, while the snippets in the second will be
-- added as autosnippets (both are the defaults, if a snippet defines a
-- different snippetType, that will have preference)
-- First list of snippets (snippetType = "snippet") start
{
s(
{trig = "attrs", dscr = "nix new attrs"},
fmta(
[[
<> = {
<> = <>
};
]]
, { i(1),i(2),i(3) })
),
},
-- First list of snippets (snippetType = "snippet") end
-- Second list of snippets (snippetType = "autosnippet") start
{
}
-- Second list of snippets (snippetType = "autosnippet") end

View file

@ -0,0 +1,20 @@
-- https://github.com/L3MON4D3/LuaSnip/wiki/Cool-Snippets#endless-list
local rec_ls
rec_ls = function()
return sn(nil, {
c(1, {
-- important!! Having the sn(...) as the first choice will cause infinite recursion.
t({""}),
-- The same dynamicNode as in the snippet (also note: self reference).
sn(nil, {t({"", "\t\\item "}), i(1), d(2, rec_ls, {})}),
}),
});
end
return {
s(";endlessitem", {
t({"\\begin{itemize}",
"\t\\item "}), i(1), d(2, rec_ls, {}),
t({"", "\\end{itemize}"}), i(0)
})
}

View file

@ -0,0 +1,40 @@
-- https://github.com/L3MON4D3/LuaSnip/wiki/Cool-Snippets#tabular
table_node= function(args)
local tabs = {}
local count
table = args[1][1]:gsub("%s",""):gsub("|","")
count = table:len()
for j=1, count do
local iNode
iNode = i(j)
tabs[2*j-1] = iNode
if j~=count then
tabs[2*j] = t" & "
end
end
return sn(nil, tabs)
end
rec_table = function ()
return sn(nil, {
c(1, {
t({""}),
sn(nil, {t{"\\\\",""} ,d(1,table_node, {ai[1]}), d(2, rec_table, {ai[1]})})
}),
});
end
return {
s(";table", {
t"\\begin{tabular}{",
i(1,"0"),
t{"}",""},
d(2, table_node, {1}, {}),
d(3, rec_table, {1}),
t{"","\\end{tabular}"}
}),
}

View file

@ -0,0 +1,102 @@
local line_begin = require("luasnip.extras.expand_conditions").line_begin
return {
s(
{ trig = ";dcenvelope", dscr = "A LaTeX equation environment" },
fmta(
[[
% https://en.wikibooks.org/wiki/LaTeX/Letters#Envelopes
% https://tex.stackexchange.com/questions/268693/how-do-you-print-c4-envelopes
\documentclass{letter}
\usepackage{geometry}
\geometry{paperheight=9.25in,paperwidth=4.13in}
\usepackage{graphics}
\usepackage{envlab}
\usepackage[AutoFallBack=true]{xeCJK}
\setCJKmainfont<>
% from envlab
\SetEnvelope{9.25in}{4.13in}
% from envlab
\setlength{\ToAddressTopMargin}{0in} %Arbitrarily decided by me
% from envlab
\setlength{\ToAddressLeftMargin}{1in} %Arbitrarily decided by me
% from envlab
\makelabels
\begin{document}
\startlabels
% from envlab
\mlabel
{% This "%" is required to make alignment right
%
% Sender Address
%
% append \\ at the end of the line before inserting newline
%
% e.g. Title\\
% Address line1\\
% Address line2\\
% Address line3
<>
}
{% This "%" is required to make alignment right
%
% Reciever Address
%
% append \\ at the end of the line before inserting newline
%
% e.g. Title\\
% Address line1\\
% Address line2\\
% Address line3
<>
% if print with lpr use command "lpr -o media=EnvYou4"
}
\end{document}
]],
{
c(1, {
t({
"{Source Han Serif TC}",
"\\newCJKfontfamily\\secondaryfont{Source Han Sans TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"{Source Han Sans TC}",
"\\newCJKfontfamily\\secondaryfont{Source Han Serif TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=13}",
",BoldFeatures = {FontIndex=28}",
"]{SourceHanSerif.ttc}",
"\\newCJKfontfamily[Path=\\string~/.local/share/fonts/",
" ,UprightFeatures = {FontIndex=28}",
" ,BoldFeatures = {FontIndex=38}",
"]\\secondaryfont{Source Han Sans TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=28}",
",BoldFeatures = {FontIndex=38}",
"]{SourceHanSans.ttc}",
"\\newCJKfontfamily[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=13}",
",BoldFeatures = {FontIndex=28}",
"]\\secondaryfont{Source Han Serif TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
}),
i(3, "% Sender Address"),
i(2, "% Reciever Address"),
}
),
{ condition = line_begin } -- set condition in the `opts` table
),
}

View file

@ -0,0 +1,74 @@
local helpers = require("luasnip-helper-funcs")
local get_visual = helpers.get_visual
return {
s(
{ trig = ";dcinit", snippetType = "snippet" },
fmta(
[[
\documentclass[<>]{<>}
\usepackage[<>]{geometry}
\usepackage[AutoFallBack=true]{xeCJK}
\setCJKmainfont<>
\begin{document}
<><>
\end{document}
]],
{
c(1, {
t(""),
t("a4paper"),
t("letter"),
t("20pt"),
}),
c(2, {
t("article"),
t("extarticle"),
t("report"),
t("moderncv"),
}),
c(3, {
t(""),
t("margin=3cm"),
t("top=3cm,bottom=3cm,left=3cm,right=3cm"),
}),
c(4, {
t({
"{Source Han Serif TC}",
"\\newCJKfontfamily\\secondaryfont{Source Han Sans TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"{Source Han Sans TC}",
"\\newCJKfontfamily\\secondaryfont{Source Han Serif TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=13}",
",BoldFeatures = {FontIndex=28}",
"]{SourceHanSerif.ttc}",
"\\newCJKfontfamily[Path=\\string~/.local/share/fonts/",
" ,UprightFeatures = {FontIndex=28}",
" ,BoldFeatures = {FontIndex=38}",
"]\\secondaryfont{Source Han Sans TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=28}",
",BoldFeatures = {FontIndex=38}",
"]{SourceHanSans.ttc}",
"\\newCJKfontfamily[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=13}",
",BoldFeatures = {FontIndex=28}",
"]\\secondaryfont{Source Han Serif TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
}),
d(5, get_visual, {}, { user_args = { "Content here" } }),
i(0),
}
)
),
}

View file

@ -0,0 +1,30 @@
local line_begin = require("luasnip.extras.expand_conditions").line_begin
return {
s(
{ trig = ";dcwatermark", dscr = "Top-right corner watermark" },
fmta(
[[
\documentclass[a4paper]{article}
\usepackage[top=.2in,right=.2in]{geometry}
\usepackage{xcolor}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
% switch off page number
% https://tex.stackexchange.com/questions/7355/how-to-suppress-page-number
\pagenumbering{gobble}
% https://superuser.com/questions/280659/how-can-i-apply-a-watermark-on-every-page-of-a-pdf-file
% pdftk original.pdf stamp watermark.pdf output final.pdf
\hfill {\Large \textcolor{red}{``<>''}}
\end{document}
]],
{
i(1, "Insert watermark"),
}
),
{ condition = line_begin } -- set condition in the `opts` table
),
}

View file

@ -0,0 +1,72 @@
local line_begin = require("luasnip.extras.expand_conditions").line_begin
local helpers = require("luasnip-helper-funcs")
local get_visual = helpers.get_visual
local mkscbs = helpers.make_single_curly_bracket_snippets
local mkss = helpers.make_scope_snippets
-- mkscbs ("snippetType", "trig", "command", "desc")
return
-- https://github.com/L3MON4D3/LuaSnip/blob/master/DOC.md#lua
-- First list of snippets (snippetType = "snippet") start
{
mkscbs("snippet", ";tt", "texttt", "teletypefont"),
mkscbs("snippet", ";ti", "textit", "italic shape"),
mkscbs("snippet", ";bf", "textbf", "bold"),
mkscbs("snippet", ";em", "emph", "emphasis"),
mkscbs("snippet", ";em", "emph", "emphasis"),
mkscbs("snippet", ";sf", "sfont", "secondary font"),
s(
{ trig = ";url", dscr = "The hyperref package's href{}{} command (for url links)" },
fmta("\\href{<>}{<>}", {
d(1, get_visual, { 1 }, { user_args = { "url" } }),
i(2, "display name"),
})
),
s(
-- https://en.wikibooks.org/wiki/LaTeX/Paragraph_Formatting#Verbatim_text
{ trig = ";vb", dscr = "Expands 'vb' into '\\verb++' (verbatim phrase, + as delimiters (can be changed))" },
fmta("\\verb<><><>", { i(1, "+"), d(2, get_visual, {}, { user_args = { "verbatim phrase" } }), rep(1) })
),
s(
{ trig = ";begin", dscr = "A LaTeX equation environment" },
fmta(
[[
\begin{<>}
<><>
\end{<>}
]],
-- The insert node is placed in the <> angle brackets
{ i(1), d(2, get_visual, {}, { user_args = { "Content here" } }), i(0), rep(1) }
),
{ condition = line_begin } -- set condition in the `opts` table
),
s(
{ trig = ";ff", dscr = "Expands 'ff' into '\\frac{}{}'" },
fmt("\\fanc{<>}{<>}", { i(1), i(2) }, { delimiters = "<>" })
),
-- Example use of insert node placeholder text
s({ trig = ";s", desc = "square bracket" }, fmta("\\<>[<>]", { i(1, "command"), d(2, get_visual) })),
s({ trig = ";c", desc = "curly bracket" }, fmta("\\<>{<>}", { i(1, "command"), d(2, get_visual) })),
s({ trig = ";cc", desc = "curly bracket + curly bracket" }, fmta("\\<>{<>}{<>}", { i(1, "command"), i(3), i(2) })),
s({ trig = ";sc", desc = "square bracket + curly bracket" }, fmta("\\<>[<>]{<>}", { i(1, "command"), i(3), i(2) })),
-- s(
-- { trig = ";tt", dscr = "Expands 'tt' into '\\texttt{}' (teletypefont family)" },
-- fmta("\\texttt{<>}", { d(1, get_visual, {}, { user_args = { "teletypefont" } }) })
-- ),
-- s(
-- { trig = ";ti", dscr = "Expands 'ti' into '\\textit{}' (italic shape)" },
-- fmta("\\textit{<>}", { d(1, get_visual, {}, { user_args = { "italic shape" } }) })
-- ),
-- s(
-- { trig = ";bf", dscr = "Expands 'bf' into '\\textbf{}' (bold)" },
-- fmta("\\textbf{<>}", { d(1, get_visual, {}, { user_args = { "bold" } }) })
-- ),
-- s(
-- { trig = ";em", dscr = "Expands 'em' into '\\emph{}' (emphasis)" },
-- fmta("\\emph{<>}", { d(1, get_visual, {}, { user_args = { "emphasis" } }) })
-- ),
},
-- First list of snippets (snippetType = "snippet") end
-- Second list of snippets (snippetType = "autosnippet") start
{}
-- Second list of snippets (snippetType = "autosnippet") end

View file

@ -0,0 +1,75 @@
return {
s(
";updarkmode",
t({
"\\usepackage{xcolor}",
"\\pagecolor[rgb]{0.1,0.1,0.1}",
"\\color[rgb]{1,1,1}",
})
),
s(";upurlpackage", t("\\usepackage{hyperref}")),
-- % https://old.reddit.com/r/LaTeX/comments/m8frij/aligning_text_across_lines/
-- % allign dates in working experience
-- for \brlap or \stackunder or \bllap
s(";upstack", t("\\usepackage{stackengine}")),
-- % for \CurrentLineWidth
s(";uptabto", t("\\usepackage{tabto}")),
-- % https://tex.stackexchange.com/questions/5036/how-to-prevent-latex-from-hyphenating-the-entire-document
-- % disable hyphenation at the edge
s(";upnohyphen", t("\\usepackage[none]{hyphenat}")),
s(";upinpututf8", t("\\usepackage[utf8]{inputenc}")),
s(";up", fmta("\\usepackage[<>]{<>}", { i(2), i(1) })),
-- Help using xeCJK
-- https://tex.stackexchange.com/questions/510853/using-one-xecjk-font-family-on-english-and-chinese-at-the-same-time
-- https://tex.stackexchange.com/questions/264265/defining-subfamilies-for-a-font/264275#264275
s(
";upxecjk",
fmta(
[[
\usepackage[AutoFallBack=true]{xeCJK}
\setCJKmainfont<>
]],
{
c(1, {
t({
"{Source Han Serif TC}",
"\\newCJKfontfamily\\secondaryfont{Source Han Sans TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"{Source Han Sans TC}",
"\\newCJKfontfamily\\secondaryfont{Source Han Serif TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=13}",
",BoldFeatures = {FontIndex=28}",
"]{SourceHanSerif.ttc}",
"\\newCJKfontfamily[Path=\\string~/.local/share/fonts/",
" ,UprightFeatures = {FontIndex=28}",
" ,BoldFeatures = {FontIndex=38}",
"]\\secondaryfont{Source Han Sans TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
t({
"[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=28}",
",BoldFeatures = {FontIndex=38}",
"]{SourceHanSans.ttc}",
"\\newCJKfontfamily[Path=\\string~/.local/share/fonts/",
",UprightFeatures = {FontIndex=13}",
",BoldFeatures = {FontIndex=28}",
"]\\secondaryfont{Source Han Serif TC}",
"\\newcommand*{\\sfont}[1]{{\\secondaryfont #1}}",
}),
}),
}
)
),
}