neosnippet.vim/autoload/neosnippet/mappings.vim

273 lines
8.3 KiB
VimL
Raw Normal View History

2013-11-18 11:14:01 +00:00
"=============================================================================
" FILE: mappings.vim
2017-06-15 00:11:15 +00:00
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
2017-06-15 00:04:27 +00:00
" License: MIT license
2013-11-18 11:14:01 +00:00
"=============================================================================
2017-10-01 13:40:17 +00:00
function! neosnippet#mappings#expandable_or_jumpable() abort
return neosnippet#mappings#expandable() || neosnippet#mappings#jumpable()
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#mappings#expandable() abort
" Check snippet trigger.
return neosnippet#mappings#completed_expandable()
\ || neosnippet#helpers#get_cursor_snippet(
\ neosnippet#helpers#get_snippets('i'),
\ neosnippet#util#get_cur_text()) != ''
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#mappings#jumpable() abort
" Found snippet placeholder.
return search(neosnippet#get_placeholder_marker_pattern(). '\|'
\ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#mappings#completed_expandable() abort
if !s:enabled_completed_snippet()
return 0
endif
let snippet = neosnippet#parser#_get_completed_snippet(
2016-11-21 18:51:38 +00:00
\ v:completed_item, neosnippet#util#get_cur_text(),
\ neosnippet#util#get_next_text())
return snippet != ''
2017-10-01 13:40:17 +00:00
endfunction
function! s:enabled_completed_snippet() abort
return exists('v:completed_item')
\ && !empty(v:completed_item)
\ && g:neosnippet#enable_completed_snippet
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! neosnippet#mappings#_clear_select_mode_mappings() abort
2013-11-19 07:07:04 +00:00
if !g:neosnippet#disable_select_mode_mappings
return
endif
redir => mappings
silent! smap
redir END
for map in map(filter(split(mappings, '\n'),
\ "v:val !~# '^s' && v:val !~ '^\\a*\\s*<\\S\\+>'"),
\ "matchstr(v:val, '^\\a*\\s*\\zs\\S\\+')")
2013-11-19 07:07:04 +00:00
silent! execute 'sunmap' map
silent! execute 'sunmap <buffer>' map
endfor
" Define default select mode mappings.
snoremap <CR> a<BS>
snoremap <BS> a<BS>
snoremap <Del> a<BS>
snoremap <C-h> a<BS>
2017-10-01 13:40:17 +00:00
endfunction
2013-11-19 07:07:04 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#mappings#_register_oneshot_snippet() abort
2013-11-21 09:14:07 +00:00
let trigger = input('Please input snippet trigger: ', 'oneshot')
if trigger == ''
return
endif
let selected_text = substitute(
2013-11-21 09:40:40 +00:00
\ neosnippet#helpers#get_selected_text(visualmode(), 1), '\n$', '', '')
call neosnippet#helpers#delete_selected_text(visualmode(), 1)
2013-11-21 09:14:07 +00:00
let base_indent = matchstr(selected_text, '^\s*')
" Delete base_indent.
let selected_text = substitute(selected_text,
\'^' . base_indent, '', 'g')
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2013-11-21 09:14:07 +00:00
let options = neosnippet#parser#_initialize_snippet_options()
let options.word = 1
2015-08-21 21:36:11 +00:00
let options.oneshot = 1
2013-11-21 09:14:07 +00:00
let neosnippet.snippets[trigger] = neosnippet#parser#_initialize_snippet(
\ { 'name' : trigger, 'word' : selected_text, 'options' : options },
\ '', 0, '', trigger)
echo 'Registered trigger : ' . trigger
2017-10-01 13:40:17 +00:00
endfunction
2013-11-21 09:14:07 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#mappings#_expand_target() abort
2013-11-21 09:54:04 +00:00
let trigger = input('Please input snippet trigger: ',
\ '', 'customlist,neosnippet#commands#_complete_target_snippets')
let neosnippet = neosnippet#variables#current_neosnippet()
2016-07-10 03:21:41 +00:00
if !has_key(neosnippet#helpers#get_snippets('i'), trigger) ||
\ neosnippet#helpers#get_snippets('i')[trigger].snip !~#
2013-11-21 09:54:04 +00:00
\ neosnippet#get_placeholder_target_marker_pattern()
if trigger != ''
echo 'The trigger is invalid.'
endif
let neosnippet.target = ''
return
endif
call neosnippet#mappings#_expand_target_trigger(trigger)
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#mappings#_expand_target_trigger(trigger) abort
2013-11-21 09:54:04 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
let neosnippet.target = substitute(
\ neosnippet#helpers#get_selected_text(visualmode(), 1), '\n$', '', '')
let line = getpos("'<")[1]
let col = getpos("'<")[2]
call neosnippet#helpers#delete_selected_text(visualmode())
call cursor(line, col)
2014-07-30 11:19:09 +00:00
if col == 1
let cur_text = a:trigger
else
let cur_text = neosnippet#util#get_cur_text()
let cur_text = cur_text[: col-2] . a:trigger . cur_text[col :]
endif
call neosnippet#view#_expand(cur_text, col, a:trigger)
if !neosnippet#mappings#jumpable()
call cursor(0, col('.') - 1)
stopinsert
endif
2017-10-01 13:40:17 +00:00
endfunction
2013-11-21 09:54:04 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#mappings#_anonymous(snippet) abort
2015-12-11 18:07:13 +00:00
let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
let expr .= printf("\<ESC>:call neosnippet#view#_insert(%s, {}, %s, %d)\<CR>",
\ string(a:snippet), string(cur_text), col)
return expr
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#mappings#_expand(trigger) abort
2015-12-11 18:07:13 +00:00
let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
2015-11-03 06:06:23 +00:00
let expr .= printf("\<ESC>:call neosnippet#view#_expand(%s, %d, %s)\<CR>",
2015-11-03 08:45:03 +00:00
\ string(cur_text), col, string(a:trigger))
return expr
2017-10-01 13:40:17 +00:00
endfunction
2015-10-21 12:13:14 +00:00
2017-10-01 13:40:17 +00:00
function! s:snippets_expand(cur_text, col) abort
if s:expand_completed_snippets(a:cur_text, a:col)
return 0
endif
2013-11-21 09:25:37 +00:00
let cur_word = neosnippet#helpers#get_cursor_snippet(
2016-07-10 03:21:41 +00:00
\ neosnippet#helpers#get_snippets('i'),
2013-11-19 07:07:04 +00:00
\ a:cur_text)
2013-11-18 11:14:01 +00:00
if cur_word != ''
" Found snippet trigger.
2013-11-21 09:54:04 +00:00
call neosnippet#view#_expand(
2015-12-13 08:06:24 +00:00
\ neosnippet#util#get_cur_text(), a:col, cur_word)
return 0
endif
return 1
2017-10-01 13:40:17 +00:00
endfunction
function! s:expand_completed_snippets(cur_text, col) abort
if !s:enabled_completed_snippet()
return 0
endif
let cur_text = a:cur_text
if !empty(get(g:, 'deoplete#_context', []))
\ && has_key(v:completed_item, 'word')
let completed_candidates = filter(copy(g:deoplete#_context),
\ "has_key(v:val, 'snippet') && has_key(v:val, 'snippet_trigger')
\ && v:val.word ==# v:completed_item.word")
if !empty(completed_candidates)
let v:completed_item.snippet = completed_candidates[0].snippet
let v:completed_item.snippet_trigger =
\ completed_candidates[0].snippet_trigger
endif
endif
let snippet = neosnippet#parser#_get_completed_snippet(
\ v:completed_item, cur_text, neosnippet#util#get_next_text())
if snippet == ''
return 0
endif
if has_key(v:completed_item, 'snippet_trigger')
let cur_text = cur_text[: -1-len(v:completed_item.snippet_trigger)]
endif
call neosnippet#view#_insert(snippet, {}, cur_text, a:col)
return 1
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! s:snippets_expand_or_jump(cur_text, col) abort
if s:snippets_expand(a:cur_text, a:col)
2015-12-13 08:06:24 +00:00
call neosnippet#view#_jump('', a:col)
2013-11-18 11:14:01 +00:00
endif
2017-10-01 13:40:17 +00:00
endfunction
2013-11-18 11:14:01 +00:00
2017-10-01 13:40:17 +00:00
function! s:snippets_jump_or_expand(cur_text, col) abort
2013-11-18 11:14:01 +00:00
if search(neosnippet#get_placeholder_marker_pattern(). '\|'
\ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
" Found snippet placeholder.
2015-12-13 08:06:24 +00:00
call neosnippet#view#_jump('', a:col)
2013-11-18 11:14:01 +00:00
else
return s:snippets_expand(a:cur_text, a:col)
2013-11-18 11:14:01 +00:00
endif
2017-10-01 13:40:17 +00:00
endfunction
2013-11-18 11:14:01 +00:00
2017-10-01 13:40:17 +00:00
function! s:SID_PREFIX() abort
2013-11-30 15:50:17 +00:00
return matchstr(expand('<sfile>'), '<SNR>\d\+_\ze\w\+$')
2017-10-01 13:40:17 +00:00
endfunction
2013-11-18 11:14:01 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#mappings#_trigger(function) abort
2015-12-11 18:07:13 +00:00
let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
2016-02-11 11:33:08 +00:00
if !neosnippet#mappings#expandable_or_jumpable()
return ''
endif
let expr .= printf("\<ESC>:call %s(%s,%d)\<CR>",
\ a:function, string(cur_text), col)
return expr
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! neosnippet#mappings#_pre_trigger() abort
2013-11-19 07:04:32 +00:00
call neosnippet#init#check()
2013-11-18 11:14:01 +00:00
let cur_text = neosnippet#util#get_cur_text()
let col = col('.')
let expr = ''
if mode() !=# 'i'
" Fix column.
let col += 2
endif
" Get selected text.
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2013-11-18 11:14:01 +00:00
let neosnippet.trigger = 1
if mode() ==# 's' && neosnippet.optional_tabstop
2013-11-20 09:01:49 +00:00
let expr .= "\<C-o>\"_d"
2013-11-18 11:14:01 +00:00
endif
return [cur_text, col, expr]
2017-10-01 13:40:17 +00:00
endfunction
2013-11-18 11:14:01 +00:00
" Plugin key-mappings.
2016-02-08 12:48:14 +00:00
function! neosnippet#mappings#expand_or_jump_impl() abort
2015-12-31 01:33:00 +00:00
return mode() ==# 's' ?
\ neosnippet#mappings#_trigger('neosnippet#view#_jump') :
\ neosnippet#mappings#_trigger(
\ s:SID_PREFIX().'snippets_expand_or_jump')
2013-11-18 11:14:01 +00:00
endfunction
2016-02-08 12:48:14 +00:00
function! neosnippet#mappings#jump_or_expand_impl() abort
2015-12-31 01:33:00 +00:00
return mode() ==# 's' ?
\ neosnippet#mappings#_trigger('neosnippet#view#_jump') :
\ neosnippet#mappings#_trigger(
\ s:SID_PREFIX().'snippets_jump_or_expand')
2013-11-18 11:14:01 +00:00
endfunction
2016-02-08 12:48:14 +00:00
function! neosnippet#mappings#expand_impl() abort
2015-11-03 06:06:23 +00:00
return neosnippet#mappings#_trigger(s:SID_PREFIX().'snippets_expand')
2013-11-18 11:14:01 +00:00
endfunction
2016-02-08 12:48:14 +00:00
function! neosnippet#mappings#jump_impl() abort
2015-11-03 06:06:23 +00:00
return neosnippet#mappings#_trigger('neosnippet#view#_jump')
2013-11-18 11:14:01 +00:00
endfunction