Add map-<expr> support in neosnippet#anonymous()

This commit is contained in:
Shougo Matsushita 2015-11-04 23:57:00 +09:00
parent 6ce1d55242
commit 625382fab1
4 changed files with 30 additions and 35 deletions

View File

@ -52,8 +52,8 @@ endfunction"}}}
function! neosnippet#jumpable() "{{{ function! neosnippet#jumpable() "{{{
return neosnippet#mappings#jumpable() return neosnippet#mappings#jumpable()
endfunction"}}} endfunction"}}}
function! neosnippet#anonymous(snippet, ...) "{{{ function! neosnippet#anonymous(snippet) "{{{
return neosnippet#mappings#_anonymous(a:snippet, get(a:000, 0, {})) return neosnippet#mappings#_anonymous(a:snippet)
endfunction"}}} endfunction"}}}
function! neosnippet#expand(trigger) "{{{ function! neosnippet#expand(trigger) "{{{
return neosnippet#mappings#_expand(a:trigger) return neosnippet#mappings#_expand(a:trigger)

View File

@ -136,32 +136,15 @@ function! neosnippet#mappings#_expand_target_trigger(trigger) "{{{
endif endif
endfunction"}}} endfunction"}}}
function! neosnippet#mappings#_anonymous(snippet, options) "{{{ function! neosnippet#mappings#_anonymous(snippet) "{{{
let cur_text = neosnippet#util#get_cur_text() let [cur_text, col, expr] = s:pre_trigger()
let options = extend( let expr .= printf("\<ESC>:call neosnippet#view#_insert(%s, {}, %s, %d)\<CR>",
\ neosnippet#parser#_initialize_snippet_options(), \ string(a:snippet), string(cur_text), col)
\ a:options)
call neosnippet#view#_insert(a:snippet, options, cur_text, col('.')) return expr
return ''
endfunction"}}} endfunction"}}}
function! neosnippet#mappings#_expand(trigger) "{{{ function! neosnippet#mappings#_expand(trigger) "{{{
call neosnippet#init#check() let [cur_text, col, expr] = s:pre_trigger()
let cur_text = neosnippet#util#get_cur_text()
let col = col('.')
if mode() !=# 'i'
" Fix column.
let col += 2
endif
" Get selected text.
let neosnippet = neosnippet#variables#current_neosnippet()
let neosnippet.trigger = 1
let expr = ''
if mode() ==# 's' && neosnippet.optional_tabstop
let expr .= "\<C-o>\"_d"
endif
let expr .= printf("\<ESC>:call neosnippet#view#_expand(%s, %d, %s)\<CR>", let expr .= printf("\<ESC>:call neosnippet#view#_expand(%s, %d, %s)\<CR>",
\ string(cur_text), col, string(a:trigger)) \ string(cur_text), col, string(a:trigger))
@ -208,6 +191,15 @@ function! s:SID_PREFIX() "{{{
endfunction"}}} endfunction"}}}
function! neosnippet#mappings#_trigger(function) "{{{ function! neosnippet#mappings#_trigger(function) "{{{
let [cur_text, col, expr] = s:pre_trigger()
let expr .= printf("\<ESC>:call %s(%s,%d)\<CR>",
\ a:function, string(cur_text), col)
return expr
endfunction"}}}
function! s:pre_trigger() "{{{
call neosnippet#init#check() call neosnippet#init#check()
let cur_text = neosnippet#util#get_cur_text() let cur_text = neosnippet#util#get_cur_text()
@ -226,10 +218,7 @@ function! neosnippet#mappings#_trigger(function) "{{{
let expr .= "\<C-o>\"_d" let expr .= "\<C-o>\"_d"
endif endif
let expr .= printf("\<ESC>:call %s(%s,%d)\<CR>", return [cur_text, col, expr]
\ a:function, string(cur_text), col)
return expr
endfunction"}}} endfunction"}}}
" Plugin key-mappings. " Plugin key-mappings.

View File

@ -51,6 +51,10 @@ function! neosnippet#view#_expand(cur_text, col, trigger_name) "{{{
call neosnippet#view#_insert(snippet.snip, snippet.options, cur_text, a:col) call neosnippet#view#_insert(snippet.snip, snippet.options, cur_text, a:col)
endfunction"}}} endfunction"}}}
function! neosnippet#view#_insert(snippet, options, cur_text, col) "{{{ function! neosnippet#view#_insert(snippet, options, cur_text, col) "{{{
let options = extend(
\ neosnippet#parser#_initialize_snippet_options(),
\ a:options)
let snip_word = a:snippet let snip_word = a:snippet
if snip_word =~ '\\\@<!`.*\\\@<!`' if snip_word =~ '\\\@<!`.*\\\@<!`'
let snip_word = s:eval_snippet(snip_word) let snip_word = s:eval_snippet(snip_word)
@ -100,7 +104,7 @@ function! neosnippet#view#_insert(snippet, options, cur_text, col) "{{{
endif endif
call setline('.', snippet_lines[0]) call setline('.', snippet_lines[0])
if begin_line != end_line || a:options.indent if begin_line != end_line || options.indent
call s:indent_snippet(begin_line, end_line) call s:indent_snippet(begin_line, end_line)
endif endif

View File

@ -320,17 +320,19 @@ neosnippet#expandable_or_jumpable()
\ "\<Plug>(neosnippet_expand_or_jump)" : "\<C-n>" \ "\<Plug>(neosnippet_expand_or_jump)" : "\<C-n>"
< <
*neosnippet#anonymous()* *neosnippet#anonymous()*
neosnippet#anonymous({snippet}, [{options}]) neosnippet#anonymous({snippet})
It defines anonymous snippet. It defines anonymous snippet.
{snippet} is snippet definition. {snippet} is snippet definition.
{options} is snippet option. {options} is snippet option.
You can expand snippet definition without defining snippet You can expand snippet definition without defining snippet
trigger. trigger.
Note: You cannot use it in |map-<expr>|. It changes the Note: You can use this function with |map-<expr>|.
buffer text.
> >
inoremap <silent> (( inoremap <silent> ((
\ <C-r>=neosnippet#anonymous('\left(${1}\right)${0}')<CR> \ <C-r>=neosnippet#anonymous('\left(${1}\right)${0}')<CR>
" OR
inoremap <expr><silent> ((
\ neosnippet#anonymous('\left(${1}\right)${0}')
< <
*neosnippet#expand()* *neosnippet#expand()*
neosnippet#expand({trigger}) neosnippet#expand({trigger})