Wansmer / langmapper.nvim

A plugin that makes Neovim more friendly to non-English input methods 🤝
MIT License
128 stars 7 forks source link

Returning English characters in which-key #19

Closed IvanKarpov-1 closed 5 months ago

IvanKarpov-1 commented 5 months ago

As far as I understood, the README section on which-key contains code that should return English characters back to which-key. I have rewritten this code for myself, but still have Cyrillic in which-key.

How can I make sure that the which-key does not have Cyrillic characters?

Which-key output: image

langmapper configuration:

return {
    "Wansmer/langmapper.nvim",
    lazy = false,
    priority = 1,
    config = function()
        local langmapper = require("langmapper")

        langmapper.setup({})
    end,
}

Which-key configuration:

return {
    "folke/which-key.nvim",
    enabled = true,
    dependencies = { "Wansmer/langmapper.nvim" },
    config = function()
        vim.o.timeout = true
        vim.o.timeoutlen = 300

        local lmu = require("langmapper.utils")
        local view = require("which-key.view")
        local execute = view.execute

        view.execute = function(prefix_i, mode, buf)
            prefix_i = lmu.translate_keycode(prefix_i, "default", "ru")
            execute(prefix_i, mode, buf)
        end

        require("which-key").setup()
    end,
}

Also, I didn't quite understand how to set up mapping for other keyboard layouts other than ru.

I defined the langmap option to the layout I need and called the langmapper plugin simply as langmapper.setup({}) and it seems to work.

But in the standard configuration of the plugin there is a section that configures it, it seems, only for Russian.

  layouts = {
    ---@type table Fallback layout item. Name of key is a name of language
    ru = {
      ---@type string Name of your second keyboard layout in system.
      ---It should be the same as result string of `get_current_layout_id()`
      id = 'com.apple.keylayout.RussianWin',
      ---@type string Fallback layout to translate. Should be same length as default layout
      layout = 'ФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯБЮЖЭХЪËфисвуапршолдьтщзйкыегмцчнябюжэхъё',
      ---@type string if you need to specify default layout for this fallback layout
      default_layout = nil,
    },
  },

So I'm a little confused whether I'm using the plugin correctly.

IvanKarpov-1 commented 5 months ago

I looked at the other issues and saw that I need to add hack_get_keymap() after calling setup() so that the which-key does not contain Cyrillic.

Now the question of keyboard layouts for other languages remains.

Wansmer commented 5 months ago

Now the question of keyboard layouts for other languages remains.

To add another language, you need to add a new layout to layouts (with all fields as in Russian layout), e.g:

require("langmapper").setup({
  layouts = {
    ---@type table Fallback layout item. Name of key is a name of language
    ru = { --[[layout preset]] },
    fr = { --[[layout preset]] },
    gr = { --[[layout preset]] },
  },
})

Also, don't forget to set vim.opt.langmap for the additional input method.

IvanKarpov-1 commented 5 months ago

you need to add a new layout to layouts

I did, but I kept getting errors, so I asked about it because I thought I was doing something wrong.

Errors: image

I was able to fix it by setting hack_keymap to false. This seemed to fix the problem, but I haven't used Neovim long enough after that to try all the keyboard shortcuts I use, so I can't say for sure.

Now my settings look like this. langmapper configuration:

return {
    "Wansmer/langmapper.nvim",
    lazy = false,
    priority = 1,
    config = function()
        local langmapper = require("langmapper")

        langmapper.setup({
            hack_keymap = false,
            layouts = {
                ua = {
                    id = "ua",
                    layout = "ФИСВУАПРШОЛДЬТЩЗЙКІЕГМЦЧНЯБЮЖЄХЇʼ,фисвуапршолдьтщзйкіегмцчнябюжєхї'.",
                    default_layout = [[ABCDEFGHIJKLMNOPQRSTUVWXYZ<>:"{}~?abcdefghijklmnopqrstuvwxyz,.;'[]`/]],
                }
            },
            os = {
                Linux = {
                    get_current_layout_id = function()
                        local cmd = "gsettings"
                        if vim.fn.executable(cmd) then
                            local output = vim.fn.system(
                                cmd .. " get org.gnome.desktop.input-sources mru-sources | sed -r \"s/\\S*\\s'([^']+).*/\\1/\""
                            )
                            return output
                        end
                    end,
                },
            },
        })

        langmapper.hack_get_keymap()
    end,
}

options.lua:

local function escape(str)
    local escape_chars = [[;,."|\]]
    return vim.fn.escape(str, escape_chars)
end

local en = [[abcdefghijklmnopqrstuvwxyz,.;'[]`/]]
local ua = [[фисвуапршолдьтщзйкіегмцчнябюжєхї'.]]
local en_shift = [[ABCDEFGHIJKLMNOPQRSTUVWXYZ<>:"{}~?]]
local ua_shift = [[ФИСВУАПРШОЛДЬТЩЗЙКІЕГМЦЧНЯБЮЖЄХЇʼ,]]
vim.opt.langmap = vim.fn.join({ escape(ua_shift) .. ";" .. escape(en_shift), escape(ua) .. ";" .. escape(en) }, ",")

I also added require("langmapper").automapping({ global = true, buffer = true }) to the end of init.lua.