From ec26576455628e716467b657c170c7ba4c3375d1 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 1 Jul 2016 19:02:41 +0900 Subject: pattern input --- autoload/sonictemplate.vim | 68 +++++++++++++++++++++++++++++++++++++++++++++- plugin/sonictemplate.vim | 9 ++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/autoload/sonictemplate.vim b/autoload/sonictemplate.vim index dc4f33a..2db9d77 100644 --- a/autoload/sonictemplate.vim +++ b/autoload/sonictemplate.vim @@ -117,7 +117,7 @@ function! s:get_candidate(fts, lead) endif return candidate endfunction - + function! sonictemplate#complete(lead, cmdline, curpos) abort return s:get_candidate([&ft, s:get_filetype(), sonictemplate#get_filetype()], a:lead) endfunction @@ -315,6 +315,72 @@ function! sonictemplate#apply(name, mode, ...) abort endif endfunction +let s:pat = {} + +function! sonictemplate#pattern() + if !has_key(s:pat, &ft) + return '' + endif + let line = getline('.')[:col('.')] + for k in keys(s:pat[&ft]) + let m = matchstr(line, k) + if len(m) > 0 + let ml = matchlist(line, k) + let line = line[:-len(m)-1] + let c = join(s:pat[&ft][k], "\n") + for i in range(1, 9) + let c = substitute(c, '{{$' . i . '}}', ml[i], 'g') + endfor + if line !~ '^\s*$' + let indent = matchstr(line, '^\(\s*\)') + let lhs = col('.') > 1 ? line[:col('.')-2] : '' + let rhs = line[len(lhs):] + let lhs = lhs[len(indent):] + let c = lhs . c . rhs + endif + let c = join(split(c, "\n"), "") + call setline('.', line) + silent! exe "normal! a\=c\" + if stridx(c, '{{_cursor_}}') != -1 + silent! call search('{{_cursor_}}\zs', 'w') + silent! foldopen + silent! call feedkeys(repeat("\", 12)) + endif + break + endif + endfor + return '' +endfunction + +function! sonictemplate#load_pattern() + let ft = &ft + let tmp = [] + for tmpldir in s:tmpldir + let tmp += split(globpath(join([tmpldir, ft], '/'), 'pattern.tpl'), "\n") + endfor + if len(tmp) == 0 + return + endif + let s:pat[ft] = {} + for f in tmp + let k = '' + let l = [] + for line in add(readfile(f), '__END__') + if line == '' + continue + elseif line !~ '^\t' + if k != '' + let s:pat[ft][k] = l + endif + let k = line + let l = [] + else + call add(l, line[1:]) + endif + endfor + endfor +endfunction + let &cpo = s:save_cpo unlet s:save_cpo diff --git a/plugin/sonictemplate.vim b/plugin/sonictemplate.vim index 717bde2..aa5873b 100644 --- a/plugin/sonictemplate.vim +++ b/plugin/sonictemplate.vim @@ -43,6 +43,15 @@ else exe "inoremap" g:sonictemplate_intelligent_key "=sonictemplate#select_intelligent('i')" endif +" TODO fix better name +if get(g:, 'sonictemplate_enable_pattern', 0) == 0 + augroup sonictemplate + au! filetype * silent! call sonictemplate#load_pattern() + augroup END + inoremap (sonictemplate-pattern) =sonictemplate#pattern() + imap (sonictemplate-pattern) +endif + let &cpo = s:save_cpo unlet s:save_cpo -- cgit v1.2.3-54-g00ecf