global !p
def math():
	return vim.eval('vimtex#syntax#in_mathzone()') == '1'

def comment(): 
	return vim.eval('vimtex#syntax#in_comment()') == '1'

def env(name):
	[x,y] = vim.eval("vimtex#env#is_inside('" + name + "')") 
	return x != '0' and y != '0'

endglobal

# General Formatting
snippet preamble "preamble" w
\\usepackage[a4paper, margin=0.5in]{geometry}
\\usepackage{amsmath}
\\usepackage{amsthm}
\\usepackage{amssymb}
\\usepackage{enumitem}
\\usepackage{libertine}
\\usepackage{euler}

\\usepackage{import}
\\usepackage{pdfpages}
\\usepackage{transparent}
\\usepackage{xcolor}

\\newcommand{\\incfig}[2][1]{%
\t\\def\\svgwidth{#1\\columnwidth}
\t\\import{./figures/}{#2.pdf_tex}
}

\\pdfsuppresswarningpagegroup=1

\\theoremstyle{definition}
\\newtheorem{all}{Theorem}[section]
\\theoremstyle{definition}
\\newtheorem{corollary}[all]{Corollary}
\\theoremstyle{definition}
\\newtheorem{lemma}[all]{Lemma}
\\theoremstyle{definition}
\\newtheorem{definition}[all]{Definition}
\\theoremstyle{definition}
\\newtheorem{exercise}[all]{Exercise}
\\theoremstyle{definition}
\\newtheorem{proposition}[all]{Proposition}
\\theoremstyle{definition}
\\newtheorem{example}[all]{Example}

\\newenvironment{lecture}[1]{
\t\\section{\\MakeUppercase{#1}}
\t\\vspace{-0.5em}
\t\\hrule
\t\\vspace{1em}
\t\\begin{list}{}{
\t\t\\setlength{\\leftmargin}{1.5em}
\t}
\t\\item[]
}{
\t\\end{list}
}

\\newcommand{\\N}{\\mathbb{N}}
\\newcommand{\\R}{\\mathbb{R}}
\\newcommand{\\I}{\\mathbb{I}}
\\newcommand{\\Z}{\\mathbb{Z}}

endsnippet

snippet beg "begin{} / end{}" bA
\\begin{$1}
	$0
\\end{$1}
endsnippet

snippet tit "Italics" w
\textit{$1}$0
endsnippet

snippet tob "Bold" w
\textbf{$1}$0
endsnippet

# Text Formatting
snippet ... "ldots" A
\ldots
endsnippet

# Math Environments
snippet im "Inline math" w
\(${1}\)`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
	snip.rv = ' '
else:
	snip.rv = ''
`$2
endsnippet

snippet dm "Display math" w
\[
${1:${VISUAL}}
\] $0
endsnippet

# Math Snippets
context "math()"
snippet iff "iff" iA
\iff $0
endsnippet

context "math()"
snippet => "implies" iA
\Rightarrow
endsnippet

context "math()"
snippet =< "implied by" iA
\Leftarrow
endsnippet

context "math()"
snippet -> "maps to" iA
\rightarrow
endsnippet

context "math()"
snippet -< "maps from" iA
\leftarrow
endsnippet

context "math()"
snippet / "Fraction" i
\\frac{$1}{$2}$0
endsnippet

context "math()"
snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet

context "math()"
snippet '([A-Za-z])_(\d\d)' "auto subscript2" wrA
`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
endsnippet

snippet __ "subscript" iA
_{$1}$0
endsnippet

snippet ^^ "superscript" iA
^{$1}$0
endsnippet

context "math()"
snippet ceil "ceil" iA
\left\lceil $1 \right\rceil $0
endsnippet

context "math()"
snippet floor "floor" iA
\left\lfloor $1 \right\rfloor$0
endsnippet

context "math()"
snippet ** "cdot" iA
\cdot
endsnippet

priority 100
context "math()"
snippet '(?<!\\)(sin|cos|arccot|cot|csc|ln|log|exp|star|perp)' "ln" rwA
\\`!p snip.rv = match.group(1)`
endsnippet

priority 300
context "math()"
snippet dint "integral" wA
\int_{${1}}^{${2}} ${3} $0
endsnippet

priority 200
context "math()"
snippet '(?<!\\)(arcsin|arccos|arctan|arccot|arccsc|arcsec|pi|zeta|int|iint|iiint)' "ln" rwA
\\`!p snip.rv = match.group(1)`
endsnippet

priority 100
context "math()"
snippet -> "to" iA
\to 
endsnippet

priority 200
context "math()"
snippet <-> "leftrightarrow" iA
\leftrightarrow
endsnippet

context "math()"
snippet !> "mapsto" iA
\mapsto 
endsnippet

context "math()"
snippet invs "inverse" iA
^{-1}
endsnippet

context "math()"
snippet compl "complement" iA
^{c}
endsnippet

context "math()"
snippet txt "text" iA
\\text{ $1 } $0
endsnippet

context "math()"
snippet set "set" wA
\\{$1\\} $0
endsnippet

context "math()"
snippet ccc "complement" Ai
^{c}
endsnippet

context "math()"
snippet ooo "infinity" iA
\\infty
endsnippet

snippet notin "not in " iA
\not\in 
endsnippet

context "math()"
snippet inn "in " iA
\in 
endsnippet

snippet NN "n" iA
\N
endsnippet

context "math()"
snippet cap "cap" iA
\cap 
endsnippet

context "math()"
snippet cup "cup" iA
\cup 
endsnippet

context "math()"
snippet limm "limit" iA
\\lim\\limits_{$1}{$2}$0
endsnippet

priority 10
context "math()"
snippet "bar" "bar" riA
\overline{$1}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])bar" "bar" riA
\overline{`!p snip.rv=match.group(1)`}
endsnippet

priority 10
context "math()"
snippet "hat" "hat" riA
\hat{$1}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])hat" "hat" riA
\hat{`!p snip.rv=match.group(1)`}
endsnippet
