global !p texMathZones = ['texMathZone'+x for x in ['A', 'AS', 'B', 'BS', 'C', 'CS', 'D', 'DS', 'V', 'W', 'X', 'Y', 'Z']] # If you have http://www.drchip.org/astronaut/vim/vbafiles/amsmath.vba.gz, # you want to add: # texMathZones += ['texMathZone'+x for x in ['E', 'ES', 'F', 'FS', 'G', 'GS', 'H', 'HS', 'I', 'IS', 'J', 'JS', 'K', 'KS', 'L', 'LS']] texIgnoreMathZones = ['texMathText'] texMathZoneIds = vim.eval('map('+str(texMathZones)+", 'hlID(v:val)')") texIgnoreMathZoneIds = vim.eval('map('+str(texIgnoreMathZones)+", 'hlID(v:val)')") def math(): # DEBUGGING synstack = vim.eval("map(synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0)), {i,v -> synIDattr(v, 'name')})") print("synstack: " + str(synstack)) syntaxgroup = vim.eval("get(reverse(map(synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0)), {i,v -> synIDattr(v, 'name')})), 0, '')") print("syntaxgroup (rev): "+ syntaxgroup) synstackids = vim.eval("synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0))") if not set(texIgnoreMathZoneIds).isdisjoint(synstackids): return False return not set(texMathZoneIds).isdisjoint(synstackids) # isdisjoint() can short-circuit as soon as a match is found endglobal snippet \beg "begin{} / end{}" bA \begin{$1} $0 \end{$1} endsnippet snippet $ "Math" wA $${1}$`!p if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']: snip.rv = ' ' else: snip.rv = '' `$2 endsnippet snippet \[ "Math" wA \[ $1 \] $0 endsnippet snippet '(.+)(pr)' "Superscript prime" Air `!p snip.rv = match.group(1) + "^{\prime}"` endsnippet #snippet '(\S+)/(\S+) ' "Fraction" Air #`!p snip.rv = "\\frac{" + match.group(1) + "}{" + match.group(2) + "} "` #endsnippet snippet // "Fraction" iA \\frac{$1}{$2}$0 endsnippet snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "Fraction" wrA \\frac{`!p snip.rv = match.group(1)`}{$1}$0 endsnippet priority 1000 snippet '^.*\)/' "() Fraction" wrA `!p stripped = match.string[:-1] depth = 0 i = len(stripped) - 1 while True: if stripped[i] == ')': depth += 1 if stripped[i] == '(': depth -= 1 if depth == 0: break; i -= 1 snip.rv = stripped[0:i] + "\\frac{" + stripped[i+1:-1] + "}" `{$1}$0 endsnippet priority 10 snippet "bar" "bar" riA \overline{$1}$0 endsnippet priority 100 snippet "([a-zA-Z])bar" "bar" riA \overline{`!p snip.rv=match.group(1)`} endsnippet priority 10 snippet "hat" "hat" riA \hat{$1}$0 endsnippet priority 100 snippet "([a-zA-Z])hat" "hat" riA \hat{`!p snip.rv=match.group(1)`} endsnippet snippet "(\\?\w+)(,\.|\.,)" "Vector postfix" riA \vec{`!p snip.rv=match.group(1)`} endsnippet snippet "fn" "Function" r f : $1 \to \R, \> y = $0 endsnippet