aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--autoload/sonictemplate.vim68
-rw-r--r--plugin/sonictemplate.vim9
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