aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authormattn <mattn.jp@gmail.com>2011-10-25 10:23:00 +0900
committermattn <mattn.jp@gmail.com>2011-10-25 10:23:00 +0900
commit2c81841396b1505461775c3ba65f7880a57bb7b1 (patch)
treec1a220f4ce8c5583f08103dfcc7d3c5745664561 /plugin
downloadvim-sonictemplate-2c81841396b1505461775c3ba65f7880a57bb7b1.tar.gz
vim-sonictemplate-2c81841396b1505461775c3ba65f7880a57bb7b1.tar.bz2
vim-sonictemplate-2c81841396b1505461775c3ba65f7880a57bb7b1.zip
first import.
Diffstat (limited to 'plugin')
-rw-r--r--plugin/template.vim49
1 files changed, 49 insertions, 0 deletions
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(<f-args>)
+
+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")')
+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