1 " Author: Kevin Ballard
2 " Description: Helper functions for Rust commands/mappings
3 " Last Modified: May 27, 2014
7 function! rust#Jump(mode, function) range
13 let foldenable = &foldenable
16 execute "call <SID>Jump_" . a:function . "()"
19 let &foldenable = foldenable
22 function! s:Jump_Back()
24 keepjumps normal! w99[{
27 function! s:Jump_Forward()
30 keepjumps normal! w99[{%
36 function! rust#Run(bang, args)
38 let idx = index(a:args, '--')
40 let rustc_args = idx == 0 ? [] : a:args[:idx-1]
41 let args = a:args[idx+1:]
43 let rustc_args = a:args
51 let b:rust_last_rustc_args = rustc_args
52 let b:rust_last_args = args
54 call s:WithPath(function("s:Run"), rustc_args, args)
57 function! s:Run(path, rustc_args, args)
59 let exepath = tempname()
64 let rustc_args = [a:path, '-o', exepath] + a:rustc_args
66 let rustc = exists("g:rustc_path") ? g:rustc_path : "rustc"
68 let output = system(shellescape(rustc) . " " . join(map(rustc_args, 'shellescape(v:val)')))
75 exe '!' . shellescape(exepath) . " " . join(map(a:args, 'shellescape(v:val)'))
79 silent! call delete(exepath)
86 function! rust#Expand(bang, args)
87 if a:bang && !empty(a:args)
88 let pretty = a:args[0]
91 let pretty = "expanded"
94 call s:WithPath(function("s:Expand"), pretty, args)
97 function! s:Expand(path, pretty, args)
99 let rustc = exists("g:rustc_path") ? g:rustc_path : "rustc"
101 let args = [a:path, '--pretty', a:pretty] + a:args
102 let output = system(shellescape(rustc) . " " . join(map(args, "shellescape(v:val)")))
120 function! rust#CompleteExpand(lead, line, pos)
121 if a:line[: a:pos-1] =~ '^Expand!\s*\S*$'
122 " first argument and it has a !
123 let list = ["normal", "expanded", "typed", "expanded,identified", "flowgraph="]
125 call filter(list, "v:val[:len(a:lead)-1] == a:lead")
130 return glob(escape(a:lead, "*?[") . '*', 0, 1)
133 " Utility functions {{{1
135 function! s:WithPath(func, ...)
137 let save_write = &write
139 let path = expand('%')
140 let pathisempty = empty(path)
141 if pathisempty || !save_write
142 " use a temporary file named 'unnamed.rs' inside a temporary
143 " directory. This produces better error messages
144 let tmpdir = tempname()
147 let save_cwd = getcwd()
148 silent exe 'lcd' tmpdir
150 let path = 'unnamed.rs'
155 silent exe 'keepalt write! ' . path
163 call call(a:func, [path] + a:000)
165 if exists("save_mod") | let &mod = save_mod | endif
166 if exists("save_write") | let &write = save_write | endif
167 if exists("save_cwd") | silent exe 'lcd' save_cwd | endif
168 if exists("tmpdir") | silent call s:RmDir(tmpdir) | endif
172 function! rust#AppendCmdLine(text)
173 call setcmdpos(getcmdpos())
174 let cmd = getcmdline() . a:text
178 function! s:RmDir(path)
179 " sanity check; make sure it's not empty, /, or $HOME
181 echoerr 'Attempted to delete empty path'
183 elseif a:path == '/' || a:path == $HOME
184 echoerr 'Attempted to delete protected path: ' . a:path
187 silent exe "!rm -rf " . shellescape(a:path)
192 " vim: set noet sw=4 ts=4: