cohama / lexima.vim

Auto close parentheses and repeat by dot dot dot...
996 stars 46 forks source link

Own <CR> mapping should invoke as fallback lexima.vim #32

Closed Hotschke closed 8 years ago

Hotschke commented 8 years ago

delimitmate provides following possibility:

 inoremap <expr> <CR> delimitMate#WithinEmptyPair() ?
   \ "\<C-R>=delimitMate#ExpandReturn()\<CR>" :
    \ "external_mapping"

Does lexima provide something similar?

My mapping looks as follows:

let g:ulti_expand_or_jump_res = 0
function! ExpandSnippetOrReturnCR()
        let snippet = UltiSnips#ExpandSnippetOrJump()
        if g:ulti_expand_or_jump_res > 0
                return snippet
        else
                return "\<CR>"
        endif
endfunction

inoremap <expr> <CR>
    \ pumvisible() ?
    \ "<C-R>=ExpandSnippetOrReturnCR()<CR>" : "\<CR>"

How can I adapt it if pumvisible is false that lexima is called?

cohama commented 8 years ago

Sorry, current lexima.vim does not provide such a feature. However there is unofficial (not documented) function to do what you want.

function! Foo() abort
  if pumvisible()
    return "foo"
  else
    return ""
  endif
endfunction
call lexima#insmode#map_hook('before', '<CR>', "\<C-r>=Foo()\<CR>")

lexima#insmode#map_hook provides hook point to trigger user-defined mapping before (or after) certain key pressed. Please note that this fucntion is now experimental and not assured future compatibility.

cohama commented 8 years ago

oh... I have a misunderstanding. You want to disable lexima rule when a specified condition is fullfiled, right?

I'm sorry there is no feature to do this.

I need to implement a new function to expand lexima's mapping Like this?

inoremap <expr> <CR>
    \ pumvisible() ?
    \ "\<C-r>=lexima#insmode#expand('<lt>CR>')\<CR>" : "ExternalMapping"
"      ^ lexima's <CR> rule will be applied             ^ lexima does not care
Hotschke commented 8 years ago

Yes, your second comment describes what I want. Trigger either lexima or something else, not both.

I think your proposal looks good. Is this very difficult to implement?

Hotschke commented 8 years ago

The developer of neocomplete Shougo has given a solution for endwise (:h neocomplete-faq). As far as I can see this also works for lexima.vim.

inoremap <silent> <CR> <C-r>=<SID>ExpandSnippetOrReturnEmptyString()<CR>
function! s:ExpandSnippetOrReturnEmptyString()
    if pumvisible()
    let snippet = UltiSnips#ExpandSnippetOrJump()
    if g:ulti_expand_or_jump_res > 0
        return snippet
    else
        return "\<C-y>\<CR>"
    endif
    else
        return "\<CR>"
endfunction