neosnippet.vim/autoload/neosnippet.vim

762 lines
23 KiB
VimL
Raw Normal View History

2012-09-27 15:42:34 +00:00
"=============================================================================
" FILE: neosnippet.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 21 Nov 2013.
2012-09-27 15:42:34 +00:00
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
" "Software"), to deal in the Software without restriction, including
" without limitation the rights to use, copy, modify, merge, publish,
" distribute, sublicense, and/or sell copies of the Software, and to
" permit persons to whom the Software is furnished to do so, subject to
" the following conditions:
"
" The above copyright notice and this permission notice shall be included
" in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
"=============================================================================
let s:save_cpo = &cpo
set cpo&vim
2012-12-13 08:34:20 +00:00
" Global options definition. "{{{
call neosnippet#util#set_default(
\ 'g:neosnippet#disable_runtime_snippets', {})
call neosnippet#util#set_default(
\ 'g:neosnippet#snippets_directory', '')
call neosnippet#util#set_default(
2013-06-04 13:39:30 +00:00
\ 'g:neosnippet#disable_select_mode_mappings', 1)
call neosnippet#util#set_default(
\ 'g:neosnippet#enable_snipmate_compatibility', 0)
2012-09-30 08:17:31 +00:00
"}}}
function! neosnippet#expandable_or_jumpable() "{{{
return neosnippet#mappings#expandable_or_jumpable()
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#expandable() "{{{
return neosnippet#mappings#expandable()
2012-09-27 15:42:34 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#jumpable() "{{{
return neosnippet#mappings#jumpable()
2012-11-03 05:08:11 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:is_beginning_of_line(cur_text) "{{{
let keyword_pattern = '\S\+'
2012-10-04 09:49:16 +00:00
let cur_keyword_str = matchstr(a:cur_text, keyword_pattern.'$')
let line_part = a:cur_text[: -1-len(cur_keyword_str)]
let prev_word_end = matchend(line_part, keyword_pattern)
2012-09-27 15:42:34 +00:00
2012-10-04 09:49:16 +00:00
return prev_word_end <= 0
2012-09-27 15:42:34 +00:00
endfunction"}}}
2012-10-30 09:54:07 +00:00
2012-12-13 08:34:20 +00:00
function! neosnippet#jump(cur_text, col) "{{{
call s:skip_next_auto_completion()
2013-11-21 09:20:36 +00:00
let expand_stack = neosnippet#variables#expand_stack()
2013-11-19 07:04:32 +00:00
2012-10-04 01:22:35 +00:00
" Get patterns and count.
2013-11-19 07:04:32 +00:00
if empty(expand_stack)
2012-10-04 01:22:35 +00:00
return s:search_outof_range(a:col)
endif
2013-11-19 07:04:32 +00:00
let expand_info = expand_stack[-1]
2012-10-04 01:22:35 +00:00
" Search patterns.
2013-11-19 07:04:32 +00:00
let [begin, end] = neosnippet#_get_snippet_range(
2012-10-04 01:22:35 +00:00
\ expand_info.begin_line,
\ expand_info.begin_patterns,
\ expand_info.end_line,
\ expand_info.end_patterns)
2013-11-19 07:04:32 +00:00
if neosnippet#_search_snippet_range(begin, end, expand_info.holder_cnt)
2012-10-04 01:22:35 +00:00
" Next count.
let expand_info.holder_cnt += 1
return 1
endif
" Search placeholder 0.
2013-11-19 07:04:32 +00:00
if neosnippet#_search_snippet_range(begin, end, 0)
2012-10-04 01:22:35 +00:00
return 1
endif
" Not found.
2013-11-21 09:20:36 +00:00
let expand_stack = neosnippet#variables#expand_stack()
2013-11-19 07:04:32 +00:00
let expand_stack = expand_stack[: -2]
2012-10-04 01:22:35 +00:00
return s:search_outof_range(a:col)
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#expand(cur_text, col, trigger_name) "{{{
call s:skip_next_auto_completion()
2012-10-29 22:38:37 +00:00
let snippets = neosnippet#get_snippets()
if a:trigger_name == '' || !has_key(snippets, a:trigger_name)
2012-09-27 15:42:34 +00:00
let pos = getpos('.')
let pos[2] = len(a:cur_text)+1
call setpos('.', pos)
if pos[2] < col('$')
startinsert
else
startinsert!
endif
return
endif
let snippet = snippets[a:trigger_name]
let cur_text = a:cur_text[: -1-len(a:trigger_name)]
let snip_word = snippet.snip
if snip_word =~ '\\\@<!`.*\\\@<!`'
let snip_word = s:eval_snippet(snip_word)
endif
" Substitute escaped `.
let snip_word = substitute(snip_word, '\\`', '`', 'g')
" Substitute markers.
let snip_word = substitute(snip_word,
2013-11-18 11:14:01 +00:00
\ '\\\@<!'.neosnippet#get_placeholder_marker_substitute_pattern(),
2012-09-27 15:42:34 +00:00
\ '<`\1`>', 'g')
let snip_word = substitute(snip_word,
2013-11-18 11:14:01 +00:00
\ '\\\@<!'.neosnippet#get_mirror_placeholder_marker_substitute_pattern(),
2012-09-27 15:42:34 +00:00
\ '<|\1|>', 'g')
let snip_word = substitute(snip_word,
2013-11-18 11:14:01 +00:00
\ '\\'.neosnippet#get_mirror_placeholder_marker_substitute_pattern().'\|'.
\ '\\'.neosnippet#get_placeholder_marker_substitute_pattern(),
\ '\=submatch(0)[1:]', 'g')
2012-09-27 15:42:34 +00:00
" Insert snippets.
let next_line = getline('.')[a:col-1 :]
let snippet_lines = split(snip_word, '\n', 1)
if empty(snippet_lines)
return
endif
let begin_line = line('.')
let end_line = line('.') + len(snippet_lines) - 1
let snippet_lines[0] = cur_text . snippet_lines[0]
let next_col = len(snippet_lines[-1]) + 1
let snippet_lines[-1] = snippet_lines[-1] . next_line
if has('folding')
" Note: Change foldmethod to "manual". Because, if you use foldmethod is
" expr, whole snippet is visually selected.
let foldmethod_save = &l:foldmethod
let &l:foldmethod = 'manual'
endif
2013-11-21 09:20:36 +00:00
let expand_stack = neosnippet#variables#expand_stack()
2013-11-19 07:04:32 +00:00
2012-10-28 09:58:55 +00:00
try
call setline('.', snippet_lines[0])
if len(snippet_lines) > 1
call append('.', snippet_lines[1:])
endif
2012-09-27 15:42:34 +00:00
2012-10-31 02:10:56 +00:00
if begin_line != end_line || snippet.options.indent
call s:indent_snippet(begin_line, end_line)
endif
2012-10-28 09:58:55 +00:00
let begin_patterns = (begin_line > 1) ?
\ [getline(begin_line - 1)] : []
let end_patterns = (end_line < line('$')) ?
\ [getline(end_line + 1)] : []
2013-11-19 07:04:32 +00:00
call add(expand_stack, {
2012-10-28 09:58:55 +00:00
\ 'begin_line' : begin_line,
\ 'begin_patterns' : begin_patterns,
\ 'end_line' : end_line,
\ 'end_patterns' : end_patterns,
\ 'holder_cnt' : 1,
\ })
2013-11-18 11:14:01 +00:00
if snip_word =~ neosnippet#get_placeholder_marker_pattern()
2012-10-29 23:03:53 +00:00
call neosnippet#jump(a:cur_text, a:col)
2012-10-28 09:58:55 +00:00
endif
finally
if has('folding')
if foldmethod_save !=# &l:foldmethod
let &l:foldmethod = foldmethod_save
endif
2012-10-28 09:58:55 +00:00
silent! execute begin_line . ',' . end_line . 'foldopen!'
endif
endtry
2012-09-27 15:42:34 +00:00
let &l:iminsert = 0
let &l:imsearch = 0
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#expand_target() "{{{
2012-10-29 22:38:37 +00:00
let trigger = input('Please input snippet trigger: ',
2012-10-29 23:03:53 +00:00
\ '', 'customlist,neosnippet#complete_target_snippets')
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2012-10-29 23:03:53 +00:00
if !has_key(neosnippet#get_snippets(), trigger) ||
\ neosnippet#get_snippets()[trigger].snip !~#
\ neosnippet#get_placeholder_target_marker_pattern()
if trigger != ''
echo 'The trigger is invalid.'
endif
2012-10-29 22:38:37 +00:00
let neosnippet.target = ''
return
endif
2012-12-13 07:52:58 +00:00
call neosnippet#expand_target_trigger(trigger)
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#expand_target_trigger(trigger) "{{{
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2012-10-29 22:38:37 +00:00
let neosnippet.target = substitute(
\ neosnippet#get_selected_text(visualmode(), 1), '\n$', '', '')
2012-12-13 07:52:58 +00:00
let line = getpos("'<")[1]
let col = getpos("'<")[2]
call neosnippet#delete_selected_text(visualmode())
2012-12-13 07:52:58 +00:00
call cursor(line, col)
2012-10-29 22:38:37 +00:00
2012-12-13 07:52:58 +00:00
call neosnippet#expand(neosnippet#util#get_cur_text(), col, a:trigger)
2012-10-29 22:38:37 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:indent_snippet(begin, end) "{{{
if a:begin > a:end
return
endif
2012-09-27 15:42:34 +00:00
let pos = getpos('.')
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2012-10-30 14:53:35 +00:00
2012-11-08 02:56:07 +00:00
let equalprg = &l:equalprg
2012-10-06 07:22:36 +00:00
try
setlocal equalprg=
2012-09-27 15:42:34 +00:00
2012-11-08 02:56:07 +00:00
" Indent begin line?
let begin = (neosnippet.target == '') ? a:begin : a:begin + 1
2012-09-27 15:42:34 +00:00
2012-10-06 07:22:36 +00:00
let base_indent = matchstr(getline(a:begin), '^\s\+')
2012-11-08 02:56:07 +00:00
for line_nr in range(begin, a:end)
2012-10-06 07:22:36 +00:00
call cursor(line_nr, 0)
if getline('.') =~ '^\t\+'
" Delete head tab character.
let current_line = substitute(getline('.'), '^\t', '', '')
if &l:expandtab && current_line =~ '^\t\+'
" Expand tab.
cal setline('.', substitute(current_line,
\ '^\t\+', base_indent . repeat(' ', &shiftwidth *
\ len(matchstr(current_line, '^\t\+'))), ''))
elseif line_nr != a:begin
call setline('.', base_indent . current_line)
endif
else
silent normal! ==
endif
endfor
finally
let &l:equalprg = equalprg
call setpos('.', pos)
endtry
2012-09-27 15:42:34 +00:00
endfunction"}}}
2013-11-19 07:04:32 +00:00
function! neosnippet#_get_snippet_range(begin_line, begin_patterns, end_line, end_patterns) "{{{
2012-09-27 15:42:34 +00:00
let pos = getpos('.')
call cursor(a:begin_line, 0)
if empty(a:begin_patterns)
let begin = line('.') - 50
else
let [begin, _] = searchpos('^' . neosnippet#util#escape_pattern(
2012-09-27 15:42:34 +00:00
\ a:begin_patterns[0]) . '$', 'bnW')
if begin <= 0
let begin = line('.') - 50
endif
endif
if begin <= 0
let begin = 1
endif
call cursor(a:end_line, 0)
if empty(a:end_patterns)
let end = line('.') + 50
else
let [end, _] = searchpos('^' . neosnippet#util#escape_pattern(
2012-09-27 15:42:34 +00:00
\ a:end_patterns[0]) . '$', 'nW')
if end <= 0
let end = line('.') + 50
endif
endif
if end > line('$')
let end = line('$')
endif
call setpos('.', pos)
return [begin, end]
endfunction"}}}
2013-11-19 07:04:32 +00:00
function! neosnippet#_search_snippet_range(start, end, cnt, ...) "{{{
2013-03-05 14:23:07 +00:00
let is_select = get(a:000, 0, 1)
2012-09-27 15:42:34 +00:00
call s:substitute_placeholder_marker(a:start, a:end, a:cnt)
2012-11-01 02:18:58 +00:00
" Search marker pattern.
2013-11-18 11:14:01 +00:00
let pattern = substitute(neosnippet#get_placeholder_marker_pattern(),
2012-11-01 02:18:58 +00:00
\ '\\d\\+', a:cnt, '')
2012-09-27 15:42:34 +00:00
for line in filter(range(a:start, a:end),
\ 'getline(v:val) =~ pattern')
2013-03-05 14:23:07 +00:00
call s:expand_placeholder(a:start, a:end, a:cnt, line, is_select)
2012-09-27 15:42:34 +00:00
return 1
endfor
return 0
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:search_outof_range(col) "{{{
2012-09-27 15:42:34 +00:00
call s:substitute_placeholder_marker(1, 0, 0)
2013-11-18 11:14:01 +00:00
let pattern = neosnippet#get_placeholder_marker_pattern()
2012-09-27 15:42:34 +00:00
if search(pattern, 'w') > 0
2012-10-06 08:16:56 +00:00
call s:expand_placeholder(line('.'), 0, '\\d\\+', line('.'))
2012-09-27 15:42:34 +00:00
return 1
endif
let pos = getpos('.')
if a:col == 1
let pos[2] = 1
call setpos('.', pos)
startinsert
2012-11-01 02:18:58 +00:00
elseif a:col >= col('$')
2012-09-27 15:42:34 +00:00
startinsert!
else
let pos[2] = a:col+1
call setpos('.', pos)
startinsert
endif
" Not found.
return 0
endfunction"}}}
2013-03-05 14:23:07 +00:00
function! s:expand_placeholder(start, end, holder_cnt, line, ...) "{{{
let is_select = get(a:000, 0, 1)
2013-11-18 11:14:01 +00:00
let pattern = substitute(neosnippet#get_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', a:holder_cnt, '')
let current_line = getline(a:line)
let match = match(current_line, pattern)
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2012-09-27 15:42:34 +00:00
let default_pattern = substitute(
2013-11-18 11:14:01 +00:00
\ neosnippet#get_placeholder_marker_default_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', a:holder_cnt, '')
let default = substitute(
2012-10-29 22:38:37 +00:00
\ matchstr(current_line, default_pattern),
\ '\\\ze[^\\]', '', 'g')
if default =~ '^TARGET\>' && neosnippet.target != ''
let default = ''
let is_target = 1
else
let is_target = 0
endif
2012-11-01 02:26:40 +00:00
let default = substitute(default, '^TARGET:\?', '', '')
2012-10-29 20:09:50 +00:00
let neosnippet.selected_text = default
2012-09-27 15:42:34 +00:00
" Substitute marker.
let default = substitute(default,
2013-11-18 11:14:01 +00:00
\ neosnippet#get_placeholder_marker_substitute_pattern(),
2012-09-27 15:42:34 +00:00
\ '<`\1`>', 'g')
let default = substitute(default,
2013-11-18 11:14:01 +00:00
\ neosnippet#get_mirror_placeholder_marker_substitute_pattern(),
2012-09-27 15:42:34 +00:00
\ '<|\1|>', 'g')
2013-09-23 04:26:44 +00:00
" len() cannot use for multibyte.
let default_len = len(substitute(default, '.', 'x', 'g'))
2012-09-27 15:42:34 +00:00
let pos = getpos('.')
let pos[1] = a:line
let pos[2] = match+1
let cnt = s:search_sync_placeholder(a:start, a:end, a:holder_cnt)
2012-11-01 02:18:58 +00:00
if cnt >= 0
2013-11-18 11:14:01 +00:00
let pattern = substitute(neosnippet#get_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', cnt, '')
call setline(a:line, substitute(current_line, pattern,
\ '<{'.cnt.':'.escape(default, '\').'}>', ''))
let pos[2] += len('<{'.cnt.':')
else
" Substitute holder.
call setline(a:line,
\ substitute(current_line, pattern, escape(default, '\'), ''))
endif
call setpos('.', pos)
2012-10-29 22:38:37 +00:00
if is_target
" Expand target
return s:expand_target_placeholder(a:line, match+1)
endif
2013-03-05 14:23:07 +00:00
if default_len > 0 && is_select
2012-09-27 15:42:34 +00:00
" Select default value.
let len = default_len-1
if &l:selection == 'exclusive'
let len += 1
endif
stopinsert
execute 'normal! v'. repeat('l', len) . "\<C-g>"
elseif pos[2] < col('$')
startinsert
else
startinsert!
endif
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:expand_target_placeholder(line, col) "{{{
2012-10-29 22:38:37 +00:00
" Expand target
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2012-10-29 22:38:37 +00:00
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
2012-11-06 06:28:54 +00:00
let col = col('.')
2012-10-29 22:38:37 +00:00
try
let base_indent = matchstr(cur_text, '^\s\+')
2012-10-29 22:38:37 +00:00
call setline(a:line, target_lines[0])
if len(target_lines) > 1
call append(a:line, map(target_lines[1:],
\ 'base_indent . v:val'))
2012-10-29 22:38:37 +00:00
endif
call cursor(end_line, 0)
if next_line != ''
startinsert
2012-11-06 06:28:54 +00:00
let col = col('.')
2012-10-29 22:38:37 +00:00
else
startinsert!
2012-11-06 06:28:54 +00:00
let col = col('$')
2012-10-29 22:38:37 +00:00
endif
finally
if has('folding')
silent! execute begin_line . ',' . end_line . 'foldopen!'
endif
endtry
let neosnippet.target = ''
2012-10-29 23:03:53 +00:00
2012-11-06 06:28:54 +00:00
call neosnippet#jump(neosnippet#util#get_cur_text(), col)
2012-10-29 22:38:37 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:search_sync_placeholder(start, end, number) "{{{
2012-09-27 15:42:34 +00:00
if a:end == 0
" Search in current buffer.
let cnt = matchstr(getline('.'),
2013-11-18 11:14:01 +00:00
\ substitute(neosnippet#get_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', '\\zs\\d\\+\\ze', ''))
return search(substitute(
2013-11-18 11:14:01 +00:00
\ neosnippet#get_mirror_placeholder_marker_pattern(),
2012-11-01 02:26:40 +00:00
\ '\\d\\+', cnt, ''), 'nw') > 0 ? cnt : -1
2012-09-27 15:42:34 +00:00
endif
let pattern = substitute(
2013-11-18 11:14:01 +00:00
\ neosnippet#get_mirror_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', a:number, '')
for line in filter(range(a:start, a:end),
\ 'getline(v:val) =~ pattern')
return a:number
endfor
2012-11-01 02:26:40 +00:00
return -1
2012-09-27 15:42:34 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:substitute_placeholder_marker(start, end, snippet_holder_cnt) "{{{
2012-11-01 02:18:58 +00:00
if a:snippet_holder_cnt > 0
2012-09-27 15:42:34 +00:00
let cnt = a:snippet_holder_cnt-1
2013-11-18 11:14:01 +00:00
let sync_marker = substitute(neosnippet#get_sync_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', cnt, '')
let mirror_marker = substitute(
2013-11-18 11:14:01 +00:00
\ neosnippet#get_mirror_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', cnt, '')
let line = a:start
for line in range(a:start, a:end)
if getline(line) =~ sync_marker
let sub = escape(matchstr(getline(line),
2013-11-18 11:14:01 +00:00
\ substitute(neosnippet#get_sync_placeholder_marker_default_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', cnt, '')), '/\')
2013-06-27 02:16:30 +00:00
silent execute printf('%d,%ds/\m' . mirror_marker . '/%s/'
2012-09-27 15:42:34 +00:00
\ . (&gdefault ? '' : 'g'), a:start, a:end, sub)
call setline(line, substitute(getline(line), sync_marker, sub, ''))
endif
endfor
2013-11-18 11:14:01 +00:00
elseif search(neosnippet#get_sync_placeholder_marker_pattern(), 'wb') > 0
2012-09-27 15:42:34 +00:00
let sub = escape(matchstr(getline('.'),
2013-11-18 11:14:01 +00:00
\ neosnippet#get_sync_placeholder_marker_default_pattern()), '/\')
2012-09-27 15:42:34 +00:00
let cnt = matchstr(getline('.'),
2013-11-18 11:14:01 +00:00
\ substitute(neosnippet#get_sync_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', '\\zs\\d\\+\\ze', ''))
let mirror_marker = substitute(
2013-11-18 11:14:01 +00:00
\ neosnippet#get_mirror_placeholder_marker_pattern(),
\ '\\d\\+', cnt, '')
2013-06-27 02:16:30 +00:00
silent execute printf('%%s/\m' . mirror_marker . '/%s/'
2012-09-27 15:42:34 +00:00
\ . (&gdefault ? 'g' : ''), sub)
2013-11-18 11:14:01 +00:00
let sync_marker = substitute(neosnippet#get_sync_placeholder_marker_pattern(),
2012-09-27 15:42:34 +00:00
\ '\\d\\+', cnt, '')
call setline('.', substitute(getline('.'), sync_marker, sub, ''))
endif
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:eval_snippet(snippet_text) "{{{
2012-09-27 15:42:34 +00:00
let snip_word = ''
let prev_match = 0
let match = match(a:snippet_text, '\\\@<!`.\{-}\\\@<!`')
while match >= 0
if match - prev_match > 0
let snip_word .= a:snippet_text[prev_match : match - 1]
endif
let prev_match = matchend(a:snippet_text,
\ '\\\@<!`.\{-}\\\@<!`', match)
2012-10-25 01:50:25 +00:00
let snip_word .= eval(
2012-10-19 09:38:52 +00:00
\ a:snippet_text[match+1 : prev_match - 2])
2012-09-27 15:42:34 +00:00
let match = match(a:snippet_text, '\\\@<!`.\{-}\\\@<!`', prev_match)
endwhile
if prev_match >= 0
let snip_word .= a:snippet_text[prev_match :]
endif
return snip_word
endfunction"}}}
2013-11-21 09:20:36 +00:00
function! neosnippet#variables#current_neosnippet() "{{{
2012-10-29 10:27:18 +00:00
if !exists('b:neosnippet')
let b:neosnippet = {
\ 'snippets' : {},
2012-10-29 10:27:18 +00:00
\ 'selected_text' : '',
2012-10-29 22:38:37 +00:00
\ 'target' : '',
2013-02-16 12:28:14 +00:00
\ 'trigger' : 0,
2012-10-29 10:27:18 +00:00
\}
endif
return b:neosnippet
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#get_snippets() "{{{
2013-11-19 07:04:32 +00:00
call neosnippet#init#check()
2013-01-20 02:59:32 +00:00
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
let snippets = copy(neosnippet.snippets)
2012-10-19 09:31:55 +00:00
for filetype in s:get_sources_filetypes(neosnippet#get_filetype())
2013-11-19 07:19:33 +00:00
call neosnippet#commands#_make_cache(filetype)
2013-11-19 07:04:32 +00:00
call extend(snippets,
2013-11-21 09:20:36 +00:00
\ neosnippet#variables#snippets()[filetype], 'keep')
2012-09-27 15:42:34 +00:00
endfor
2013-11-18 03:02:49 +00:00
let cur_text = neosnippet#util#get_cur_text()
2012-11-03 06:35:27 +00:00
if mode() ==# 'i'
" Special filters.
if !s:is_beginning_of_line(cur_text)
call filter(snippets, '!v:val.options.head')
endif
2012-10-04 09:49:16 +00:00
endif
2012-10-04 01:22:35 +00:00
2013-11-18 03:02:49 +00:00
call filter(snippets, "cur_text =~# get(v:val, 'regexp', '')")
2012-10-04 09:49:16 +00:00
return snippets
2012-09-27 15:42:34 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#get_snippets_directory() "{{{
2013-11-21 09:20:36 +00:00
let snippets_dir = copy(neosnippet#variables#snippets_dir())
if !get(g:neosnippet#disable_runtime_snippets,
\ neosnippet#get_filetype(),
\ get(g:neosnippet#disable_runtime_snippets, '_', 0))
2013-11-21 09:20:36 +00:00
let snippets_dir += neosnippet#variables#runtime_dir()
endif
return snippets_dir
2012-09-27 15:42:34 +00:00
endfunction"}}}
2013-02-17 04:10:41 +00:00
function! neosnippet#get_user_snippets_directory() "{{{
2013-11-21 09:20:36 +00:00
return copy(neosnippet#variables#snippets_dir())
2013-02-17 04:10:41 +00:00
endfunction"}}}
function! neosnippet#get_runtime_snippets_directory() "{{{
2013-11-21 09:20:36 +00:00
return copy(neosnippet#variables#runtime_dir())
2013-02-17 04:10:41 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#get_filetype() "{{{
2013-06-08 00:03:30 +00:00
if !exists('s:exists_context_filetype')
" context_filetype.vim installation check.
try
call context_filetype#version()
let s:exists_context_filetype = 1
catch
let s:exists_context_filetype = 0
endtry
endif
2013-05-28 10:24:58 +00:00
let context_filetype =
\ s:exists_context_filetype ?
\ context_filetype#get_filetype() : &filetype
if context_filetype == ''
let context_filetype = 'nothing'
endif
return context_filetype
2012-09-30 08:33:50 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! s:get_sources_filetypes(filetype) "{{{
2013-07-11 05:45:40 +00:00
let filetypes =
\ exists('*neocomplete#get_source_filetypes') ?
\ neocomplete#get_source_filetypes(a:filetype) :
\ exists('*neocomplcache#get_source_filetypes') ?
\ neocomplcache#get_source_filetypes(a:filetype) :
\ [(a:filetype == '') ? 'nothing' : a:filetype]
return filetypes + ['_']
2012-09-30 08:45:41 +00:00
endfunction"}}}
" Complete filetype helper.
2012-12-13 08:34:20 +00:00
function! neosnippet#filetype_complete(arglead, cmdline, cursorpos) "{{{
2012-09-30 08:45:41 +00:00
" Dup check.
let ret = {}
for item in map(
\ split(globpath(&runtimepath, 'syntax/*.vim'), '\n') +
\ split(globpath(&runtimepath, 'indent/*.vim'), '\n') +
\ split(globpath(&runtimepath, 'ftplugin/*.vim'), '\n')
\ , 'fnamemodify(v:val, ":t:r")')
if !has_key(ret, item) && item =~ '^'.a:arglead
let ret[item] = 1
endif
endfor
return sort(keys(ret))
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#complete_target_snippets(arglead, cmdline, cursorpos) "{{{
2012-10-29 23:03:53 +00:00
return map(filter(values(neosnippet#get_snippets()),
\ "stridx(v:val.word, a:arglead) == 0
\ && v:val.snip =~# neosnippet#get_placeholder_target_marker_pattern()"), 'v:val.word')
2012-10-29 22:38:37 +00:00
endfunction"}}}
2012-09-27 15:42:34 +00:00
2013-11-21 09:20:36 +00:00
" Get marker patterns.
2012-12-13 08:34:20 +00:00
function! neosnippet#get_placeholder_target_marker_pattern() "{{{
2012-10-29 23:03:53 +00:00
return '\${\d\+:TARGET\%(:.\{-}\)\?\\\@<!}'
endfunction"}}}
2013-11-18 11:14:01 +00:00
function! neosnippet#get_placeholder_marker_pattern() "{{{
2012-09-27 15:42:34 +00:00
return '<`\d\+\%(:.\{-}\)\?\\\@<!`>'
endfunction"}}}
2013-11-18 11:14:01 +00:00
function! neosnippet#get_placeholder_marker_substitute_pattern() "{{{
2012-09-27 15:42:34 +00:00
return '\${\(\d\+\%(:.\{-}\)\?\\\@<!\)}'
endfunction"}}}
2013-11-18 11:14:01 +00:00
function! neosnippet#get_placeholder_marker_default_pattern() "{{{
2012-09-27 15:42:34 +00:00
return '<`\d\+:\zs.\{-}\ze\\\@<!`>'
endfunction"}}}
2013-11-18 11:14:01 +00:00
function! neosnippet#get_sync_placeholder_marker_pattern() "{{{
2012-09-27 15:42:34 +00:00
return '<{\d\+\%(:.\{-}\)\?\\\@<!}>'
endfunction"}}}
2013-11-18 11:14:01 +00:00
function! neosnippet#get_sync_placeholder_marker_default_pattern() "{{{
2012-09-27 15:42:34 +00:00
return '<{\d\+:\zs.\{-}\ze\\\@<!}>'
endfunction"}}}
2013-11-18 11:14:01 +00:00
function! neosnippet#get_mirror_placeholder_marker_pattern() "{{{
2012-09-27 15:42:34 +00:00
return '<|\d\+|>'
endfunction"}}}
2013-11-18 11:14:01 +00:00
function! neosnippet#get_mirror_placeholder_marker_substitute_pattern() "{{{
2012-09-27 15:42:34 +00:00
return '\$\(\d\+\)'
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#get_selected_text(type, ...) "{{{
2012-10-29 10:27:18 +00:00
let sel_save = &selection
let &selection = 'inclusive'
let reg_save = @@
2012-10-29 20:09:50 +00:00
let pos = getpos('.')
2012-10-29 10:27:18 +00:00
try
" Invoked from Visual mode, use '< and '> marks.
if a:0
silent exe "normal! `<" . a:type . "`>y"
elseif a:type == 'line'
silent exe "normal! '[V']y"
elseif a:type == 'block'
2012-12-13 07:52:58 +00:00
silent exe "normal! `[\<C-v>`]y"
2012-10-29 10:27:18 +00:00
else
silent exe "normal! `[v`]y"
endif
2012-10-29 22:38:37 +00:00
return @@
2012-10-29 10:27:18 +00:00
finally
let &selection = sel_save
let @@ = reg_save
2012-10-29 20:09:50 +00:00
call setpos('.', pos)
2012-10-29 10:27:18 +00:00
endtry
2012-10-29 22:38:37 +00:00
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#delete_selected_text(type, ...) "{{{
2012-10-29 22:38:37 +00:00
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"
2012-12-13 07:52:58 +00:00
elseif a:type ==# 'V'
silent exe "normal! `[V`]s"
elseif a:type ==# "\<C-v>"
silent exe "normal! `[\<C-v>`]d"
2012-10-29 22:38:37 +00:00
else
silent exe "normal! `[v`]d"
endif
finally
let &selection = sel_save
let @@ = reg_save
call setpos('.', pos)
endtry
endfunction"}}}
2012-12-13 08:34:20 +00:00
function! neosnippet#substitute_selected_text(type, text) "{{{
let sel_save = &selection
let &selection = 'inclusive'
let reg_save = @@
let pos = getpos('.')
try
" Invoked from Visual mode, use '< and '> marks.
2012-12-13 07:52:58 +00:00
if a:0
silent exe "normal! `<" . a:type . "`>s" . a:text
elseif a:type ==# 'V'
silent exe "normal! '[V']hs" . a:text
elseif a:type ==# "\<C-v>"
silent exe "normal! `[\<C-v>`]s" . a:text
else
silent exe "normal! `[v`]s" . a:text
endif
finally
let &selection = sel_save
let @@ = reg_save
call setpos('.', pos)
endtry
endfunction"}}}
2012-10-29 10:27:18 +00:00
function! s:skip_next_auto_completion() "{{{
" Skip next auto completion.
if exists('*neocomplcache#skip_next_complete')
call neocomplcache#skip_next_complete()
endif
2013-06-04 13:39:30 +00:00
if exists('*neocomplete#skip_next_complete')
call neocomplete#skip_next_complete()
endif
2013-02-16 12:28:14 +00:00
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2013-02-16 12:28:14 +00:00
let neosnippet.trigger = 0
endfunction"}}}
2012-09-27 15:42:34 +00:00
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: foldmethod=marker