diff --git a/autoload/neosnippet.vim b/autoload/neosnippet.vim index b8b4e36..f3b1060 100644 --- a/autoload/neosnippet.vim +++ b/autoload/neosnippet.vim @@ -518,7 +518,9 @@ function! s:snippets_jump_or_expand(cur_text, col)"{{{ endfunction"}}} function! neosnippet#expand(cur_text, col, trigger_name)"{{{ - if a:trigger_name == '' + let snippets = neosnippet#get_snippets() + + if a:trigger_name == '' || !has_key(snippets, a:trigger_name) let pos = getpos('.') let pos[2] = len(a:cur_text)+1 call setpos('.', pos) @@ -532,7 +534,6 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{ return endif - let snippets = neosnippet#get_snippets() let snippet = snippets[a:trigger_name] let cur_text = a:cur_text[: -1-len(a:trigger_name)] @@ -577,7 +578,9 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{ call append('.', snippet_lines[1:]) endif - call s:indent_snippet(begin_line, end_line) + if begin_line != end_line + call s:indent_snippet(begin_line, end_line) + endif let begin_patterns = (begin_line > 1) ? \ [getline(begin_line - 1)] : [] @@ -591,7 +594,7 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{ \ 'holder_cnt' : 1, \ }) - if next_col < col('$') + if next_line != '' startinsert else startinsert! @@ -610,6 +613,24 @@ function! neosnippet#expand(cur_text, col, trigger_name)"{{{ let &l:iminsert = 0 let &l:imsearch = 0 endfunction"}}} +function! neosnippet#expand_target()"{{{ + let trigger = input('Please input snippet trigger: ', + \ '', 'customlist,neosnippet#snippet_complete') + let neosnippet = neosnippet#get_current_neosnippet() + if !has_key(neosnippet#get_snippets(), trigger) + let neosnippet.target = '' + return + endif + + let neosnippet.target = substitute( + \ neosnippet#get_selected_text(visualmode(), 1), '\n$', '', '') + call neosnippet#delete_selected_text(visualmode(), 1) + + call setline('.', matchstr(neosnippet.target, '^\s*') . trigger) + startinsert! + + call neosnippet#expand(getline('.'), col('$'), trigger) +endfunction"}}} function! s:indent_snippet(begin, end)"{{{ let equalprg = &l:equalprg let pos = getpos('.') @@ -729,14 +750,24 @@ function! s:expand_placeholder(start, end, holder_cnt, line)"{{{ \ '\\d\\+', a:holder_cnt, '') let current_line = getline(a:line) let match = match(current_line, pattern) + let neosnippet = neosnippet#get_current_neosnippet() let default_pattern = substitute( \ s:get_placeholder_marker_default_pattern(), \ '\\d\\+', a:holder_cnt, '') let default = substitute( - \ matchstr(current_line, default_pattern), '\\\ze[^\\]', '', 'g') + \ matchstr(current_line, default_pattern), + \ '\\\ze[^\\]', '', 'g') + + if default =~ '^TARGET\>' && neosnippet.target != '' + let default = '' + let is_target = 1 + else + let is_target = 0 + endif + + let default = substitute(default, '^TARGET:\?\>', '', '') - let neosnippet = neosnippet#get_current_neosnippet() let neosnippet.selected_text = default " Substitute marker. @@ -768,6 +799,11 @@ function! s:expand_placeholder(start, end, holder_cnt, line)"{{{ call setpos('.', pos) + if is_target + " Expand target + return s:expand_target_placeholder(a:line, match+1) + endif + if default_len > 0 " Select default value. let len = default_len-1 @@ -783,6 +819,51 @@ function! s:expand_placeholder(start, end, holder_cnt, line)"{{{ startinsert! endif endfunction"}}} +function! s:expand_target_placeholder(line, col)"{{{ + " Expand target + let neosnippet = neosnippet#get_current_neosnippet() + let next_line = getline(a:line)[a:col-1 :] + let target_lines = split(neosnippet.target, '\n', 1) + + let cur_text = getline(a:line)[: a:col-2] + let target_lines[0] = cur_text . target_lines[0] + let next_col = len(target_lines[-1]) + 1 + let target_lines[-1] = target_lines[-1] . next_line + + let begin_line = a:line + let end_line = a:line + len(target_lines) - 1 + + if has('folding') + let foldmethod = &l:foldmethod + let &l:foldmethod = 'manual' + endif + + try + call setline(a:line, target_lines[0]) + if len(target_lines) > 1 + call append(a:line, target_lines[1:]) + endif + + call cursor(end_line, 0) + + if begin_line != end_line + call s:indent_snippet(begin_line, end_line) + endif + + if next_line != '' + startinsert + else + startinsert! + endif + finally + if has('folding') + let &l:foldmethod = foldmethod + silent! execute begin_line . ',' . end_line . 'foldopen!' + endif + endtry + + let neosnippet.target = '' +endfunction"}}} function! s:search_sync_placeholder(start, end, number)"{{{ if a:end == 0 " Search in current buffer. @@ -866,6 +947,7 @@ function! neosnippet#get_current_neosnippet()"{{{ if !exists('b:neosnippet') let b:neosnippet = { \ 'selected_text' : '', + \ 'target' : '', \} endif @@ -924,6 +1006,10 @@ function! neosnippet#filetype_complete(arglead, cmdline, cursorpos)"{{{ return sort(keys(ret)) endfunction"}}} +function! neosnippet#snippet_complete(arglead, cmdline, cursorpos)"{{{ + return filter(keys(neosnippet#get_snippets()), + \ 'stridx(v:val, a:arglead) == 0') +endfunction"}}} function! s:get_placeholder_marker_pattern()"{{{ return '<`\d\+\%(:.\{-}\)\?\\\@' @@ -973,7 +1059,7 @@ function! s:trigger(function)"{{{ return expr endfunction"}}} -function! neosnippet#get_selected_text(type, ...) +function! neosnippet#get_selected_text(type, ...)"{{{ let sel_save = &selection let &selection = 'inclusive' let reg_save = @@ @@ -991,14 +1077,36 @@ function! neosnippet#get_selected_text(type, ...) silent exe "normal! `[v`]y" endif - let neosnippet = neosnippet#get_current_neosnippet() - let neosnippet.selected_text = @@ + return @@ finally let &selection = sel_save let @@ = reg_save call setpos('.', pos) endtry -endfunction +endfunction"}}} +function! neosnippet#delete_selected_text(type, ...)"{{{ + let sel_save = &selection + let &selection = 'inclusive' + let reg_save = @@ + let pos = getpos('.') + + try + " Invoked from Visual mode, use '< and '> marks. + if a:0 + silent exe "normal! `<" . a:type . "`>d" + elseif a:type == 'line' + silent exe "normal! '[V']d" + elseif a:type == 'block' + silent exe "normal! `[\`]d" + else + silent exe "normal! `[v`]d" + endif + finally + let &selection = sel_save + let @@ = reg_save + call setpos('.', pos) + endtry +endfunction"}}} function! neosnippet#clear_select_mode_mappings()"{{{ if !g:neosnippet#disable_select_mode_mappings diff --git a/autoload/neosnippet/snippets/vim.snip b/autoload/neosnippet/snippets/vim.snip index c10be1b..52e6242 100644 --- a/autoload/neosnippet/snippets/vim.snip +++ b/autoload/neosnippet/snippets/vim.snip @@ -2,19 +2,19 @@ snippet if abbr if endif options head if ${1:#:condition} - ${0} + ${0:TARGET} endif snippet elseif options head elseif ${1:/* condition */} - ${0} + ${0:TARGET} snippet ifelse abbr if else endif options head if ${1:#:condition} - ${2} + ${2:TARGET} else ${3} endif @@ -23,14 +23,14 @@ snippet for abbr for in endfor options head for ${1:#:var} in ${2:#:list} - ${0} + ${0:TARGET} endfor snippet while abbr while endwhile options head while ${1:#:condition} - ${0} + ${0:TARGET} endwhile snippet function @@ -38,14 +38,14 @@ abbr func endfunc alias func options head function! ${1:#:func_name}(${2}) - ${0} + ${0:TARGET} endfunction snippet try abbr try endtry options head try - ${1} + ${1:TARGET} catch /${2:#:pattern}/ ${3} endtry @@ -55,7 +55,7 @@ abbr try ... finally ... endtry alias tryf options head try - ${1} + ${1:TARGET} finally ${2} endtry @@ -67,7 +67,7 @@ options head snippet echomsg alias log options head - echomsg string(${1}) + echomsg string(${1:TARGET}) snippet command abbr command call function @@ -93,6 +93,6 @@ snippet redir abbr redir => var options head redir => ${1:#:var} - ${2:} + ${2::TARGET} redir END diff --git a/doc/neosnippet.txt b/doc/neosnippet.txt index 363d9bc..9cb100f 100644 --- a/doc/neosnippet.txt +++ b/doc/neosnippet.txt @@ -468,6 +468,7 @@ CHANGELOG *neosnippet-changelog* - Implemented commented placeholder. - Improved python snippets. - Fixed for alias. +- Implemented target placeholder. 2012-10-29 - Improved parse of snippets file. diff --git a/plugin/neosnippet.vim b/plugin/neosnippet.vim index b8afbef..0f3065a 100644 --- a/plugin/neosnippet.vim +++ b/plugin/neosnippet.vim @@ -75,6 +75,9 @@ imap (neocomplcache_snippets_force_jump) smap (neocomplcache_snippets_force_jump) \ (neosnippet_jump) +xnoremap (neosnippet_expand_target) + \ :call neosnippet#expand_target() + imap (neocomplcache_start_unite_snippet) \ (neosnippet_start_unite_snippet) inoremap (neosnippet_start_unite_snippet)