diff options
author | Yasuhiro Matsumoto <mattn.jp@gmail.com> | 2016-07-01 19:02:41 +0900 |
---|---|---|
committer | Yasuhiro Matsumoto <mattn.jp@gmail.com> | 2016-07-01 19:02:41 +0900 |
commit | ec26576455628e716467b657c170c7ba4c3375d1 (patch) | |
tree | 20b72a3b61a5197f27a286b9d7d0d3019b8c80de | |
parent | 0c661f5f888ea180d7aaeca13a9ef44bf22165d2 (diff) | |
download | vim-sonictemplate-ec26576455628e716467b657c170c7ba4c3375d1.tar.gz vim-sonictemplate-ec26576455628e716467b657c170c7ba4c3375d1.tar.bz2 vim-sonictemplate-ec26576455628e716467b657c170c7ba4c3375d1.zip |
pattern input
-rw-r--r-- | autoload/sonictemplate.vim | 68 | ||||
-rw-r--r-- | plugin/sonictemplate.vim | 9 |
2 files changed, 76 insertions, 1 deletions
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-r>=c\<cr>" + if stridx(c, '{{_cursor_}}') != -1 + silent! call search('{{_cursor_}}\zs', 'w') + silent! foldopen + silent! call feedkeys(repeat("\<bs>", 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 "<c-r>=sonictemplate#select_intelligent('i')<cr>" 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 <plug>(sonictemplate-pattern) <c-r>=sonictemplate#pattern()<cr> + imap <unique> <c-y><c-b> <plug>(sonictemplate-pattern) +endif + let &cpo = s:save_cpo unlet s:save_cpo |