diff options
author | mattn <mattn.jp@gmail.com> | 2011-10-27 19:04:16 +0900 |
---|---|---|
committer | mattn <mattn.jp@gmail.com> | 2011-10-27 19:04:16 +0900 |
commit | e412149167854f28dd6c5fec8a7c070467835d93 (patch) | |
tree | 1ce3853627a0136aa9a497d57119c368128de7f3 | |
parent | e2edb97bb2c44b36d745c6500b720a2553416570 (diff) | |
download | vim-sonictemplate-e412149167854f28dd6c5fec8a7c070467835d93.tar.gz vim-sonictemplate-e412149167854f28dd6c5fec8a7c070467835d93.tar.bz2 vim-sonictemplate-e412149167854f28dd6c5fec8a7c070467835d93.zip |
insert part.
-rw-r--r-- | plugin/template.vim | 64 | ||||
-rw-r--r-- | template/main.c | 4 | ||||
-rw-r--r-- | template/main.cpp | 4 |
3 files changed, 51 insertions, 21 deletions
diff --git a/plugin/template.vim b/plugin/template.vim index 20a4b41..336c2a6 100644 --- a/plugin/template.vim +++ b/plugin/template.vim @@ -23,13 +23,16 @@ command! -nargs=1 -complete=customlist,TemplateComplete Template call s:Template let s:tmpldir = expand('<sfile>:p:h:h') . '/template/' function! TemplateComplete(lead, cmdline, curpos) - return map(split(globpath(s:tmpldir, '*.'.&ft), "\n"), 'fnamemodify(v:val, ":t:r")') + return map(split(globpath(s:tmpldir, a:lead.'*.'.&ft), "\n"), 'fnamemodify(v:val, ":t:r")') endfunction function! s:Template(name) - if search('[^ \t]', 'w') != 0 - echomsg 'This buffer is already modified.' - return + let buffer_is_not_empty = search('[^ \t]', 'wn') + if !exists('g:template_vim_use_always') || g:template_vim_use_always == 0 + if buffer_is_not_empty + echomsg 'This buffer is already modified.' + return + endif endif let f = s:tmpldir . a:name . '.' . &ft if !filereadable(f) @@ -38,29 +41,56 @@ function! s:Template(name) endif let c = join(readfile(f, "b"), "\n") let c = substitute(c, '{{_name_}}', expand('%:t:r:'), 'g') - let c = substitute(c, '{{_expr_:\(.\{-}\)}}', '\=eval(submatch(1))', 'g') - let c = substitute(c, '{{_expr_:\(.\{-}\)}}', '\=eval(submatch(1))', 'g') let tmp = c let mx = '{{_input_:\(.\{-}\)}}' - let vars = {} + let vars = [] while 1 let match = matchstr(tmp, mx) if len(match) == 0 break endif - let token = substitute(match, mx, '\1', 'ig') - let vars[token] = 1 + let var = substitute(match, mx, '\1', 'ig') + if index(vars, var) == -1 + call add(vars, var) + endif let tmp = tmp[stridx(tmp, match) + len(match):] endwhile - for key in keys(vars) - let val = input(key . ":") - let c = substitute(c, '\V{{_input_:'.key.'}}', '\=val', 'g') + for var in vars + let val = input(var . ":") + let c = substitute(c, '\V{{_input_:'.var.'}}', '\=val', 'g') endfor - silent! %d _ - silent! put = c - silent! normal! ggdd - silent! call search('{{_cursor_}}', 'w') - silent! %s/{{_cursor_}}//g + let c = substitute(c, '{{_expr_:\(.\{-}\)}}', '\=eval(submatch(1))', 'g') + if len(c) == 0 + return + endif + if !exists('g:template_vim_use_always') || g:template_vim_use_always == 0 + silent! %d _ + silent! put = c + silent! normal! ggdd + silent! call search('{{_cursor_}}', 'w') + silent! %s/{{_cursor_}}//g + else + if !buffer_is_not_empty + silent! %d _ + silent! put = c + silent! normal! ggdd + else + if c[len(c)-1] == "\n" + let c = c[:-2] + endif + let line = getline('.') + let indent = substitute(line, '^\(\s*\)', '\1', '') + if line =~ '^\s*$' + silent! normal dd + endif + let c = indent . substitute(c, "\n", "\n".indent, 'g') + silent! put! = c + endif + if stridx(c, '{{_cursor_}}') + silent! call search('{{_cursor_}}', 'w') + silent! s/{{_cursor_}}//g + endif + endif endfunction " vim:ts=4:sw=4:et diff --git a/template/main.c b/template/main.c index f2a929b..794d581 100644 --- a/template/main.c +++ b/template/main.c @@ -2,6 +2,6 @@ int main(int argc, char* argv[]) { - {{_cursor_}} - return 0; + {{_cursor_}} + return 0; } diff --git a/template/main.cpp b/template/main.cpp index c413be8..1a92edb 100644 --- a/template/main.cpp +++ b/template/main.cpp @@ -3,6 +3,6 @@ int main(int argc, char* argv[]) { - {{_cursor_}} - return 0; + {{_cursor_}} + return 0; } |