Skip to content

Breaks my typographic quotes definition #534

@mcepl

Description

@mcepl

Description

I have this in configuration for writing typographic quotes:

------------------------------------------------------------

-- require("nvim-autopairs").setup({
--   map_cr = true, --  map <CR> on insert mode
--   disable_filetype = { "TelescopePrompt", "markdown", "rst", "tex" },
-- })

------------------------------------------------------------
-- Simple Lua function to get typographic quotes based on spelllang
local function get_smart_quotes()
    local double_low_high = { de = true, cs = true, pl = true }
    local lang = vim.bo.spelllang or ""
    local active_langs = vim.split(lang, ",")

    for _, l in ipairs(active_langs) do
        local base = l:sub(1, 2):lower() -- Get "de" from "de_de"
        if double_low_high[base] then
            -- German/Czech „ “
            return vim.fn.nr2char(0x201E), vim.fn.nr2char(0x201C)
        elseif base == "fr" then
            -- French « » (with a &thinsp; inside)
            return vim.fn.nr2char(0x00ab) .. vim.fn.nr2char(0x2009),
                vim.fn.nr2char(0x2009) .. vim.fn.nr2char(0x00bb)
        end
    end

    -- Default fallback (English) “ ”
    return vim.fn.nr2char(0x201C), vim.fn.nr2char(0x201D)
end

-- Map " to insert the correct smart quote in Insert mode
vim.api.nvim_create_autocmd({ "FileType", "BufEnter" },
    {
        pattern = "*",
        callback = function()
            if vim.tbl_contains({ "markdown", "rst", "tex", "plaintex" }, vim.bo.filetype) then
                vim.keymap.set('i', '"', function()
                    local open, close = get_smart_quotes()
                    local line = vim.api.nvim_get_current_line()
                    local col = vim.api.nvim_win_get_cursor(0)[2]

                    local quote
                    -- for Unicode checking (not required here, because we
                    -- check for space only)
                    -- local prev_char = vim.fn.strcharpart(line, vim.fn.strchars(line, 0, col - 1), 1)
                    local prev_char = col > 0 and line:sub(col + 1, col + 1) or ""
                    if col == 0 or prev_char:match("%s") then
                        quote = open
                    else
                        quote = close
                    end

                    vim.api.nvim_put({ quote }, "c", true, true)
                    return "" -- Return empty string to prevent default quote insertion
                end, { buffer = true })
            end
        end
    })

Unfortunately, when I uncomment setup of nvim-autopairs the typographic quotes think stops working (even with disable_filetype configuration). Any idea why? Is there some way how to make the plugin not to break my mapping?

Mapping bug

  1. provide result of command :verbose imap <cr>.

no mapping (in Czech)

Steps to reproduce

see above

Minimal config

see above

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions