From 2c81841396b1505461775c3ba65f7880a57bb7b1 Mon Sep 17 00:00:00 2001 From: mattn Date: Tue, 25 Oct 2011 10:23:00 +0900 Subject: first import. --- plugin/template.vim | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 plugin/template.vim (limited to 'plugin') diff --git a/plugin/template.vim b/plugin/template.vim new file mode 100644 index 0000000..de6c001 --- /dev/null +++ b/plugin/template.vim @@ -0,0 +1,49 @@ +" template.vim: Vim commands to load template. +" +" This filetype plugin adds one command for the buffers: +" +" :Template {name} +" +" Load template named as {name} in the current buffer. +" Template file is stored in ~/.vim/template. +" If you are using pathogen.vim, template folder is located at following. +" +" ~/.vim/bundle/template-vim +" plugin +" template.vim # This file. +" template +" main.go +" package.go +" package.perl +" script.perl +" + +command! -buffer -nargs=1 -complete=customlist,TemplateComplete Template call s:Template() + +let s:tmpldir = expand(':p:h:h') . '/template/' + +function! TemplateComplete(lead, cmdline, curpos) + return map(split(globpath(s:tmpldir, '*.'.&ft), "\n"), 'fnamemodify(v:val, ":t:r")') +endfunction + +function! s:Template(name) + if search('[^ \t]', 'w') != 0 + echomsg 'This buffer is already modified.' + return + endif + let f = s:tmpldir . a:name . '.' . &ft + if !filereadable(f) + echomsg 'Template '.a:name.' is not exists.' . f + return + 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') + silent! %d _ + silent! put = c + silent! normal! ggdd + silent! call search('{{_cursor_}}', 'w') + silent! %s/{{_cursor_}}//g +endfunction + +" vim:ts=4:sw=4:et -- cgit v1.2.3-54-g00ecf