From 6e029a7db1baaecb792eddfd4dca38ffd5f9a132 Mon Sep 17 00:00:00 2001 From: mattn Date: Fri, 28 Oct 2011 19:09:44 +0900 Subject: renamed to SonicTemplate. --- autoload/sonictemplate.vim | 102 +++++++++++++++++++++++++ autoload/template.vim | 102 ------------------------- doc/sonictemplate-vim.txt | 182 +++++++++++++++++++++++++++++++++++++++++++++ doc/template-vim.txt | 182 --------------------------------------------- plugin/sonictemplate.vim | 15 ++++ plugin/template.vim | 14 ---- 6 files changed, 299 insertions(+), 298 deletions(-) create mode 100644 autoload/sonictemplate.vim delete mode 100644 autoload/template.vim create mode 100644 doc/sonictemplate-vim.txt delete mode 100644 doc/template-vim.txt create mode 100644 plugin/sonictemplate.vim delete mode 100644 plugin/template.vim diff --git a/autoload/sonictemplate.vim b/autoload/sonictemplate.vim new file mode 100644 index 0000000..c6bf121 --- /dev/null +++ b/autoload/sonictemplate.vim @@ -0,0 +1,102 @@ +"============================================================================= +" sonictemplate.vim +" Author: Yasuhiro Matsumoto +" Last Change: 28-Oct-2011. + +let s:save_cpo = &cpo +set cpo&vim + +if exists('g:sonictemplate_vim_template_dir') + let s:tmpldir = g:sonictemplate_vim_template_dir +else + let s:tmpldir = expand(':p:h:h') . '/template/' +endif + +function! sonictemplate#select() abort + let name = input(':Template ', '', 'customlist,sonictemplate#complete') + if name == '' + return + endif + call sonictemplate#apply(name) +endfunction + +function! sonictemplate#complete(lead, cmdline, curpos) abort + if search('[^ \t]', 'wn') + return map(split(globpath(join([s:tmpldir, &ft], '/'), 'snip-' . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') + else + return map(split(globpath(join([s:tmpldir, &ft], '/'), 'base-' . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') + endif +endfunction + +function! sonictemplate#apply(name) abort + let buffer_is_not_empty = search('[^ \t]', 'wn') + if search('[^ \t]', 'wn') + let fs = split(globpath(join([s:tmpldir, &ft], '/'), 'snip-' . a:name . '.*'), "\n") + else + let fs = split(globpath(join([s:tmpldir, &ft], '/'), 'base-' . a:name . '.*'), "\n") + endif + if len(fs) == 0 + echomsg 'Template '.a:name.' is not exists.' + return + endif + let f = fs[0] + if !filereadable(f) + echomsg 'Template '.a:name.' is not exists.' + return + endif + let c = join(readfile(f, "b"), "\n") + let c = substitute(c, '{{_name_}}', expand('%:t:r:'), 'g') + let tmp = c + let mx = '{{_input_:\(.\{-}\)}}' + let vars = [] + while 1 + let match = matchstr(tmp, mx) + if len(match) == 0 + break + endif + 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 var in vars + let val = input(var . ":") + let c = substitute(c, '\V{{_input_:'.var.'}}', '\=val', 'g') + endfor + sandbox let c = substitute(c, '{{_if_:\(.\{-}\);\(.\{-}\)\(;\(.\{-}\)\)\{-}}}', '\=eval(submatch(1))?submatch(2):submatch(4)', 'g') + sandbox let c = substitute(c, '{{_expr_:\(.\{-}\)}}', '\=eval(submatch(1))', 'g') + if len(c) == 0 + return + endif + 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 = matchstr(line, '^\(\s*\)') + if line =~ '^\s*$' && line('.') != line('$') + silent! normal dd + endif + let c = indent . substitute(c, "\n", "\n".indent, 'g') + if len(indent) && (&expandtab || indent =~ '^ \+$') + let c = substitute(c, "\t", repeat(' ', min([len(indent), &tabstop])), 'g') + endif + silent! put! = c + endif + if stridx(c, '{{_cursor_}}') + silent! call search('{{_cursor_}}', 'w') + silent! s/{{_cursor_}}//g + endif + silent! exe "normal! \" + startinsert +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo + +" vim:set et: diff --git a/autoload/template.vim b/autoload/template.vim deleted file mode 100644 index b4b8ee6..0000000 --- a/autoload/template.vim +++ /dev/null @@ -1,102 +0,0 @@ -"============================================================================= -" template.vim -" Author: Yasuhiro Matsumoto -" Last Change: 28-Oct-2011. - -let s:save_cpo = &cpo -set cpo&vim - -if exists('g:template_vim_template_dir') - let s:tmpldir = g:template_vim_template_dir -else - let s:tmpldir = expand(':p:h:h') . '/template/' -endif - -function! template#select() abort - let name = input(':Template ', '', 'customlist,template#complete') - if name == '' - return - endif - call template#apply(name) -endfunction - -function! template#complete(lead, cmdline, curpos) abort - if search('[^ \t]', 'wn') - return map(split(globpath(join([s:tmpldir, &ft], '/'), 'snip-' . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') - else - return map(split(globpath(join([s:tmpldir, &ft], '/'), 'base-' . a:lead . '*.*'), "\n"), 'fnamemodify(v:val, ":t:r")[5:]') - endif -endfunction - -function! template#apply(name) abort - let buffer_is_not_empty = search('[^ \t]', 'wn') - if search('[^ \t]', 'wn') - let fs = split(globpath(join([s:tmpldir, &ft], '/'), 'snip-' . a:name . '.*'), "\n") - else - let fs = split(globpath(join([s:tmpldir, &ft], '/'), 'base-' . a:name . '.*'), "\n") - endif - if len(fs) == 0 - echomsg 'Template '.a:name.' is not exists.' - return - endif - let f = fs[0] - if !filereadable(f) - echomsg 'Template '.a:name.' is not exists.' - return - endif - let c = join(readfile(f, "b"), "\n") - let c = substitute(c, '{{_name_}}', expand('%:t:r:'), 'g') - let tmp = c - let mx = '{{_input_:\(.\{-}\)}}' - let vars = [] - while 1 - let match = matchstr(tmp, mx) - if len(match) == 0 - break - endif - 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 var in vars - let val = input(var . ":") - let c = substitute(c, '\V{{_input_:'.var.'}}', '\=val', 'g') - endfor - sandbox let c = substitute(c, '{{_if_:\(.\{-}\);\(.\{-}\)\(;\(.\{-}\)\)\{-}}}', '\=eval(submatch(1))?submatch(2):submatch(4)', 'g') - sandbox let c = substitute(c, '{{_expr_:\(.\{-}\)}}', '\=eval(submatch(1))', 'g') - if len(c) == 0 - return - endif - 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 = matchstr(line, '^\(\s*\)') - if line =~ '^\s*$' && line('.') != line('$') - silent! normal dd - endif - let c = indent . substitute(c, "\n", "\n".indent, 'g') - if len(indent) && (&expandtab || indent =~ '^ \+$') - let c = substitute(c, "\t", repeat(' ', min([len(indent), &tabstop])), 'g') - endif - silent! put! = c - endif - if stridx(c, '{{_cursor_}}') - silent! call search('{{_cursor_}}', 'w') - silent! s/{{_cursor_}}//g - endif - silent! exe "normal! \" - startinsert -endfunction - -let &cpo = s:save_cpo -unlet s:save_cpo - -" vim:set et: diff --git a/doc/sonictemplate-vim.txt b/doc/sonictemplate-vim.txt new file mode 100644 index 0000000..4295981 --- /dev/null +++ b/doc/sonictemplate-vim.txt @@ -0,0 +1,182 @@ +*sonictemplate-vim.txt* SonicTemplate for Vim + + ------------------------------------------------------- + SonicTemplate: hi speed coding method + ------------------------------------------------------- + +Author: Yasuhiro Matsumoto +WebSite: http://mattn.kaoriya.net/ +Repository: http://github.com/mattn/sonictemplate-vim +Site: http://mattn.github.com/sonictemplate-vim +License: BSD style license + +============================================================================== +CONTENTS *sonictemplate-vim-contents* + +Introduction |sonictemplate-introduction| +Install |sonictemplate-install| +Tutorial |sonictemplate-tutorial| +Customize |sonictemplate-customize| +Write Your Template |sonictemplate-writetemplate| + +============================================================================== +INTRODUCTION *sonictemplate-vim-introduction* + +|SonicTemplate| is easy and high speed coding method. + + * Choose template for the filetype + * Few typings. + * Flexible customization. + +============================================================================== +INSTALL *sonictemplate-vim-install* + +Install the distributed files into Vim runtime directory which is usually +~/.vim/, or $HOME/vimfiles on Windows. + +If you install pathogen that provided from Tim Pope, you should extract the +file into 'bundle' directory. + +============================================================================== +TUTORIAL *sonictemplate-vim-tutorial* + +For example, you are writing C++ file. + +1. Create main function + + Open new C++ file, and type following. +> + :Template boost-main +< + Then you can see following. +> + #include + #include + #include + + int + main(int argc, char* argv[]) { + _ + return 0; + } +< + The cursor is in the position of |_|. + +2. Add foreach loop + + Type following. +> + :Template boost-for +< + Then you will be asked like following. +> + variable: +< + Then type "foo". +> + #include + #include + #include + + int + main(int argc, char* argv[]) { + BOOST_FOREACH(auto x, foo) { + _ + } + return 0; + } +< +3. Add std::cout << xxx << std::endl; +> + :Template cout +< + Answer "x" for the question "string:". +> + #include + #include + #include + + int + main(int argc, char* argv[]) { + BOOST_FOREACH(auto x, foo) { + std::cout << x << std::endl; + } + return 0; + } +< +============================================================================== +CUSTOMIZE *sonictemplate-vim-customize* + +You can modify template directory for your-self templates. +> + let g:template_vim_template_dir = '/path/to/your/template/directory' +< +============================================================================== +WRITE YOUR TEMPLATE *sonictemplate-vim-writetemplate* + +Templates are stored in the directory that "g:sonictemplate_vim_template_dir" +variable specified. On unix, directory structure is following. +Note that I'm using pathogen-vim. +> + ~/.vim/bundle/sonictemplate-vim + plugin + sonictemplate.vim + doc + sonictemplate.txt + template + go ... filetype go + base-main.go ... base main + base-package.go ... base package + snip-goroutine.go ... snippet for goroutine + ... + perl ... filetype perl + base-package.pl ... base package + base-script.pl ... base script + snip-dbi-connect-sqlite.pl ... snippet for DBI/SQLite + ... + +1. The filename have following rule. + + |[kind]|-|[name]|.|[extension]| + + |[kind]| 'base' or 'snip' should be used. + + 'base' is used when buffer is empty. + + |[name]| template name + + Words in the name must join with '-'. + + |[extension]| file name extension like ".c". + + If several extensions are exists in same directory, the first found is used. + +2. Template can have some keywords. + + |{{_name_}}| the filename without extension. + + If you are opening "foo.pl" for perl, {{_name_}} become "foo". + + |{{_cursor_}}| : cursor position. + + When expanded template, cursor will be moved to there. + + |{{_input_:var}}| : ask the value of "var". + + When expanded template, cursor will be moved to there. + + |{{_expr_:xxx}}| : expression in vimscript. + + For example: "Current Time:|{{_expr_:strftime('%c')}}|" will be +> + Current Time: 2011/10/27 20:19:00 +< + |{{_if_:expr;foo;bar}}| : ternary operator + + For example: When today is saturday, + "Today is |{{_if_:strftime('%w')%6;OrdinaryDay;Holiday}}|" will be +> + Today is Holiday +< +============================================================================== +vim:tw=78:ts=8:ft=help:norl:noet:fen:fdl=0: diff --git a/doc/template-vim.txt b/doc/template-vim.txt deleted file mode 100644 index c407eff..0000000 --- a/doc/template-vim.txt +++ /dev/null @@ -1,182 +0,0 @@ -*template-vim.txt* Template for Vim - - ------------------------------------------------------- - Template: easy way coding - ------------------------------------------------------- - -Author: Yasuhiro Matsumoto -WebSite: http://mattn.kaoriya.net/ -Repository: http://github.com/mattn/template-vim -Site: http://mattn.github.com/template-vim -License: BSD style license - -============================================================================== -CONTENTS *template-vim-contents* - -Introduction |template-introduction| -Install |template-install| -Tutorial |template-tutorial| -Customize |template-customize| -Write Your Template |template-writetemplate| - -============================================================================== -INTRODUCTION *template-vim-introduction* - -|Template| is easy way to coding. - - * Choose template for the filetype - * Few typings. - * Flexible customization. - -============================================================================== -INSTALL *template-vim-install* - -Install the distributed files into Vim runtime directory which is usually -~/.vim/, or $HOME/vimfiles on Windows. - -If you install pathogen that provided from Tim Pope, you should extract the -file into 'bundle' directory. - -============================================================================== -TUTORIAL *template-vim-tutorial* - -For example, you are writing C++ file. - -1. Create main function - - Open new C++ file, and type following. -> - :Template boost-main -< - Then you can see following. -> - #include - #include - #include - - int - main(int argc, char* argv[]) { - _ - return 0; - } -< - The cursor is in the position of |_|. - -2. Add foreach loop - - Type following. -> - :Template boost-for -< - Then you will be asked like following. -> - variable: -< - Then type "foo". -> - #include - #include - #include - - int - main(int argc, char* argv[]) { - BOOST_FOREACH(auto x, foo) { - _ - } - return 0; - } -< -3. Add std::cout << xxx << std::endl; -> - :Template cout -< - Answer "x" for the question "string:". -> - #include - #include - #include - - int - main(int argc, char* argv[]) { - BOOST_FOREACH(auto x, foo) { - std::cout << x << std::endl; - } - return 0; - } -< -============================================================================== -CUSTOMIZE *template-vim-customize* - -You can modify template directory for your-self templates. -> - let g:template_vim_template_dir = '/path/to/your/template/directory' -< -============================================================================== -WRITE YOUR TEMPLATE *template-vim-writetemplate* - -Templates are stored in the directory that "g:template_vim_template_dir" -variable specified. On unix, directory structure is following. -Note that I'm using pathogen-vim. -> - ~/.vim/bundle/template-vim - plugin - template.vim - doc - template.txt - template - go ... filetype go - base-main.go ... base main - base-package.go ... base package - snip-goroutine.go ... snippet for goroutine - ... - perl ... filetype perl - base-package.pl ... base package - base-script.pl ... base script - snip-dbi-connect-sqlite.pl ... snippet for DBI/SQLite - ... - -1. The filename have following rule. - - |[kind]|-|[name]|.|[extension]| - - |[kind]| 'base' or 'snip' should be used. - - 'base' is used when buffer is empty. - - |[name]| template name - - Words in the name must join with '-'. - - |[extension]| file name extension like ".c". - - If several extensions are exists in same directory, the first found is used. - -2. Template can have some keywords. - - |{{_name_}}| the filename without extension. - - If you are opening "foo.pl" for perl, {{_name_}} become "foo". - - |{{_cursor_}}| : cursor position. - - When expanded template, cursor will be moved to there. - - |{{_input_:var}}| : ask the value of "var". - - When expanded template, cursor will be moved to there. - - |{{_expr_:xxx}}| : expression in vimscript. - - For example: "Current Time:|{{_expr_:strftime('%c')}}|" will be -> - Current Time: 2011/10/27 20:19:00 -< - |{{_if_:expr;foo;bar}}| : ternary operator - - For example: When today is saturday, - "Today is |{{_if_:strftime('%w')%6;OrdinaryDay;Holiday}}|" will be -> - Today is Holiday -< -============================================================================== -vim:tw=78:ts=8:ft=help:norl:noet:fen:fdl=0: diff --git a/plugin/sonictemplate.vim b/plugin/sonictemplate.vim new file mode 100644 index 0000000..5fe99bd --- /dev/null +++ b/plugin/sonictemplate.vim @@ -0,0 +1,15 @@ +"============================================================================= +" sonictemplate.vim +" Author: Yasuhiro Matsumoto +" Last Change: 28-Oct-2011. +" +" Vim commands to load template. +" +" :Template {name} +" Load template named as {name} in the current buffer. + +command! -nargs=1 -complete=customlist,sonictemplate#complete Template call sonictemplate#apply() +nnoremap t :call sonictemplate#select() +inoremap t :call sonictemplate#select() + +" vim:ts=4:sw=4:et diff --git a/plugin/template.vim b/plugin/template.vim deleted file mode 100644 index 1a832a6..0000000 --- a/plugin/template.vim +++ /dev/null @@ -1,14 +0,0 @@ -"============================================================================= -" template.vim -" Author: Yasuhiro Matsumoto -" Last Change: 28-Oct-2011. -" -" Vim commands to load template. -" -" :Template {name} -" Load template named as {name} in the current buffer. - -command! -nargs=1 -complete=customlist,template#complete Template call template#apply() -inoremap t :call template#select() - -" vim:ts=4:sw=4:et -- cgit v1.2.3-54-g00ecf