[vim] set up ultisnips
authorAndrew Lorimer <andrew@lorimer.id.au>
Sat, 13 Jul 2019 07:08:40 +0000 (17:08 +1000)
committerAndrew Lorimer <andrew@lorimer.id.au>
Sat, 13 Jul 2019 07:08:40 +0000 (17:08 +1000)
vim/.snippets/all.snippets [new file with mode: 0644]
vim/.snippets/plaintex.snippets [new file with mode: 0644]
vim/.vimrc
diff --git a/vim/.snippets/all.snippets b/vim/.snippets/all.snippets
new file mode 100644 (file)
index 0000000..bf78a02
--- /dev/null
@@ -0,0 +1,40 @@
+snippet ( "Parentheses" A
+($1)$0
+endsnippet
+
+snippet { "Braces" A
+{$1}$0
+endsnippet
+
+snippet [ "Square brackets" A
+[$1]$0
+endsnippet
+
+snippet < "Chevrons"
+<$1>$0
+endsnippet
+
+snippet box "Box"
+`!p snip.rv = '┌' + '─' * (len(t[1]) + 2) + '┐'`
+│ $1 │
+`!p snip.rv = '└' + '─' * (len(t[1]) + 2) + '┘'`
+$0
+endsnippet
+
+snippet today "Date"
+`date '+%d %b %Y'`
+endsnippet
+
+priority 1000
+snippet wolf "Mathematica" w
+wolf $1 wolf$0
+endsnippet
+
+priority 10000
+snippet 'wolf(.*)wolf' "evaluate mathematica" wr
+`!p
+import subprocess
+code = 'ToString[' + match.group(1) + ', TeXForm]'
+snip.rv = subprocess.check_output(['wolframscript', '-code', code]).rstrip()
+`
+endsnippet
diff --git a/vim/.snippets/plaintex.snippets b/vim/.snippets/plaintex.snippets
new file mode 100644 (file)
index 0000000..2f955c0
--- /dev/null
@@ -0,0 +1,102 @@
+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
index 7658d41acd8df9e76cbfb43dfdc5b2b9c182e0de..3d452ab549e0b20056cb5ff9af3b92864edfdef4 100644 (file)
@@ -8,7 +8,6 @@ set expandtab
 set shiftwidth=2
 set softtabstop=2
 
-
 " setup Vundle:
 set nocompatible              " be iMproved, required
 filetype off                  " required
@@ -21,11 +20,26 @@ Plugin 'VundleVim/Vundle.vim' " let Vundle manage Vundle, required
 
 Plugin 'vim-pandoc/vim-pandoc-syntax'
 Plugin 'godlygeek/tabular'
+Plugin 'SirVer/ultisnips'
+Plugin 'honza/vim-snippets'
 
 " All of your Plugins must be added before the following line
 call vundle#end()            " required
 filetype plugin indent on    " required
 
+" UltiSnips configuration
+let g:UltiSnipsSnippetDirectories = ['/home/andrew/.snippets']
+let g:UltiSnipsExpandTrigger = '<tab>'
+let g:UltiSnipsJumpForwardTrigger = '<tab>'
+let g:UltiSnipsJumpBackwardTrigger = '<s-tab>'
+set rtp-=$HOME/.vim
+set rtp^=~/.vim/bundle/ultisnips
+set rtp+=~/.vim/bundle/ultisnips/after
+set rtp^=$HOME/.vim
+set vi=
+filetype plugin on
+
+
 " markdown group for vim-pandoc-syntax:
 augroup pandoc_syntax
  au! BufNewFile,BufFilePre,BufRead *.md set filetype=markdown.pandoc
@@ -75,3 +89,4 @@ if exists("+showtabline")
   endfunction
   set tabline=%!MyTabLine()
 endif
+