Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for remap=true mapping #2072

Open
powerman opened this issue Oct 25, 2024 · 0 comments
Open

Add support for remap=true mapping #2072

powerman opened this issue Oct 25, 2024 · 0 comments

Comments

@powerman
Copy link

powerman commented Oct 25, 2024

The reason for this feature request is known conflict between cabbrev and cnoremap <CR>.

Showcase:

$ nvim --clean
:cabbrev info version
:info
:cnoremap <CR> <CR>
:info

First :info works while second result in "E492: Not an editor command: info". Using :cmap instead of :cnoremap fixes this issue.

Because of the conflict this code breaks all cabbrev:

cmp.setup {
    mapping = {
        ['<CR>'] = cmp.mapping(function(fallback)
            -- ANYTHING --
        end, { 'c' }),
    }
}

Example of similar issue/fix in another project: inside/vim-search-pulse#9.

At the moment I've worked around this issue in really ugly way: I replaced this trivial handler (which conflicts with cabbrev)

cmp.setup {
    mapping = {
        ['<CR>'] = cmp.mapping(function(fallback)
            if not cmp.confirm { select = false } then
                fallback()
            end
        end, { 'i', 'c' }),
    }
}

with these 3 handlers plus extra "fake" key <S-CR> (fake here means I do not actually press this key, it's used only to run handler function from <CR> mapping, see below):

cmp.setup {
    mapping = {
        ['<CR>'] = cmp.mapping(function(fallback)
            if not cmp.confirm { select = false } then
                fallback()
            end
        end, { 'i' }),
        ['<S-CR>'] = cmp.mapping(function(fallback)
            if not cmp.confirm { select = false } then
                fallback()
            end
        end, { 'c' }),
    }
}

vim.keymap.set('c', '<CR>', function()
    if cmp.visible() then
        return '<S-CR>'
    else
        return '<CR>'
    end
end, { remap = true, expr = true })

The "fake" <S-CR> key/handler is needed because this does not work (result in "E5108: Error executing lua: ....local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/window.lua:201: E565: Not allowed to change text or change window") for unknown (to me) reason:

vim.keymap.set('c', '<CR>', function()
    if not cmp.confirm { select = false } then
        return '<CR>'
    end
end, { remap = true, expr = true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant