Giter VIP home page Giter VIP logo

im-select.nvim's Introduction

A goal-oriented senior backend engineer with 7 years of experience.

  • Strong skills in Java, Go and Python, familiar with Javascript, C# and Lua.
  • Deep understanding of Design Pattern, OOP, and RESTful API.
  • Linux user, Docker, CI/CD, AWS.
  • Goal-oriented, with excellent problem-solving skills and the ability to work in a fast-paced, dynamic and remote environment.
  • Strong communication skills and the ability to collaborate effectively with cross-functional teams.

I'm looking for a job in Europe or remotely, feel free to contact me via:

linkedin: @shuxiaowang

email: [email protected]

blog: shuxiao.wang

im-select.nvim's People

Contributors

chujdk avatar cworld1 avatar davidyz avatar hieulw avatar hongyuanjia avatar keaising avatar martins3 avatar todesking avatar xarthurx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

im-select.nvim's Issues

Alternative to im-select.exe on Windows

I find that we can use luajit ffi to call win32api to control keyboard layout and ime state on Windows, without the burden of a extra binary and another language pack.
I'm not a pro coder yet, so I'm not confident enough to make a good pull request.
Below code is my personal config which doesn't support switching between keyboard-layouts, I think it's doable by similar method.
If a pull request is needed, I can try to read im-select.nvim's source code and make a configurable pull request.
Feel free to use below code with ❤️

-- Reference:
-- https://www.cnblogs.com/yf-zhao/p/16018481.html
-- https://zhuanlan.zhihu.com/p/425951648

local ffi = require "ffi"

ffi.cdef [[
    typedef unsigned int UINT, HWND, WPARAM;
    typedef unsigned long LPARAM, LRESULT;
    LRESULT SendMessageA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    HWND ImmGetDefaultIMEWnd(HWND unnamedParam1);
    HWND GetForegroundWindow();
]]

local user32 = ffi.load "user32.dll"
local imm32 = ffi.load "imm32.dll"

local ime_hwnd
local ime_group = vim.api.nvim_create_augroup("ime_toggle", { clear = true })

-- Get ime control's hwnd after InsertEnter or CmdlineEnter
-- to ensure getting correct foregroundwindow
vim.api.nvim_create_autocmd({ "InsertEnter", "CmdlineEnter" }, {
    group = ime_group,
    once = true,
    desc = "Get ime control hwnd attached to nvim window",
    callback = function()
        ime_hwnd = imm32.ImmGetDefaultIMEWnd(user32.GetForegroundWindow())
    end,
})

local WM_IME_CONTROL = 0x283
local IMC_GETCONVERSIONMODE = 0x001
local IMC_SETCONVERSIONMODE = 0x002 -- It's said this value differs on different ime, I'm not sure
local ime_mode_ch = 1025
local ime_mode_en = 0

local function set_ime_mode(mode)
    return user32.SendMessageA(ime_hwnd, WM_IME_CONTROL, IMC_SETCONVERSIONMODE, mode)
end

local function get_ime_mode()
    return user32.SendMessageA(ime_hwnd, WM_IME_CONTROL, IMC_GETCONVERSIONMODE, 0)
end

vim.api.nvim_create_autocmd({ "InsertLeave", "CmdlineLeave" }, {
    group = ime_group,
    desc = "Toggle ime to English mode on normal mode",
    callback = function()
        if ime_mode_ch == get_ime_mode() then
            set_ime_mode(ime_mode_en)
        end
    end,
})

[Bug] macOS中Rime和落格输入法切换失败

问题描述:在macOS 14.4.1中,只有苹果内置的双拼输入法在切换回insert模式时可以正确唤出中文输入,而Rime和落格输入法则不行。并且查看输入法的配置中,没有正常显示。
image
而在terminal中通过命令行直接执行

im-select com.apple.keylayout.ABC
im-select com.logcg.inputmethod.LogInputMac3.LogInputMac3IME

是可以正常切换到中文输入法并输出中文,配置中也是正常的。
image

配置文件:

return {
  "keaising/im-select.nvim",
  config = function()
    require("im_select").setup({
      -- IM will be set to `default_im_select` in `normal` mode
      -- For Windows/WSL, default: "1033", aka: English US Keyboard
      -- For macOS, default: "com.apple.keylayout.ABC", aka: US
      -- For Linux, default:
      --               "keyboard-us" for Fcitx5
      --               "1" for Fcitx
      --               "xkb:us::eng" for ibus
      -- You can use `im-select` or `fcitx5-remote -n` to get the IM's name
      default_im_select = "com.apple.keylayout.ABC",

      -- Can be binary's name or binary's full path,
      -- e.g. 'im-select' or '/usr/local/bin/im-select'
      -- For Windows/WSL, default: "im-select.exe"
      -- For macOS, default: "im-select"
      -- For Linux, default: "fcitx5-remote" or "fcitx-remote" or "ibus"
      default_command = "im-select",

      -- Restore the default input method state when the following events are triggered
      set_default_events = { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },

      -- Restore the previous used input method state when the following events
      -- are triggered, if you don't want to restore previous used im in Insert mode,
      -- e.g. deprecated `disable_auto_restore = 1`, just let it empty
      -- as `set_previous_events = {}`
      set_previous_events = { "InsertEnter" },

      -- Show notification about how to install executable binary when binary missed
      keep_quiet_on_no_binary = false,

      -- Async run `default_command` to switch IM or not
      async_switch_im = true,
    })
  end,
}

Astronvim里不起作用

  1. 运行环境有2个,都不行,macOS13 Apple silicon,macOS12 黑苹果。用的Astronvim
  2. homebrew安装的im-select,路径都配置好了,在命令行执行im-select com.apple.keylayout.ABC能正常切换
  3. 用了Astronvim-community的模板,在~/.config/nvim/lua/user/plugins/里创建了im-select.lua文件,内容如下
return {
    {
        "keaising/im-select.nvim",
        config = function()
            require('im_select').setup({
                -- IM will be set to `default_im_select` in `normal` mode
                -- For Windows/WSL, default: "1033", aka: English US Keyboard
                -- For macOS, default: "com.apple.keylayout.ABC", aka: US
                -- For Linux, default:
                --               "keyboard-us" for Fcitx5
                --               "1" for Fcitx
                --               "xkb:us::eng" for ibus
                -- You can use `im-select` or `fcitx5-remote -n` to get the IM's name
                default_im_select  = "com.apple.keylayout.ABC",

                -- Can be binary's name or binary's full path,
                -- e.g. 'im-select' or '/usr/local/bin/im-select'
                -- For Windows/WSL, default: "im-select.exe"
                -- For macOS, default: "im-select"
                -- For Linux, default: "fcitx5-remote" or "fcitx-remote" or "ibus"
                default_command = '/usr/local/bin/im-select', -- macOS13 Apple silicon上用的不是这个,而是/opt/homebrew那个

                -- Restore the default input method state when the following events are triggered
                -- set_default_events = { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },

                -- Restore the previous used input method state when the following events
                -- are triggered, if you don't want to restore previous used im in Insert mode,
                -- e.g. deprecated `disable_auto_restore = 1`, just let it empty
                -- as `set_previous_events = {}`
                -- set_previous_events = { "InsertEnter" },

                -- Show notification about how to install executable binary when binary missed
                keep_quiet_on_no_binary = false,

                -- Async run `default_command` to switch IM or not
                async_switch_im = false
            })
        end,
    }
}

配置之后的效果就是切换到Normal模式下,再输入还是中文,比如按两下J,会进行中文输入,然后按下Shift(我的squirrel配置的中英文切换),会切换到英文模式,并且光标往前移动了2个。

反复实验多次,参数也改了几次,都不生效,就像没有配置这个一样。我平时用squirrel比较多,但即使用原生的输入法也不行,感觉是这里面配置的事件没有被触发,但又不知道怎么调试。多谢!

Switching problem in WSL

When I use this plugin in neovim on WSL, I encounter a issue.

  • When I use the American keyboard layout in Neovim, it doesn't automatically switch the input method when entering insert mode; it remains in the American keyboard layout unless I manually switch to another layout. However, every time I exit Neovim, it returns to normal mode, which means it retains the American keyboard layout. Consequently, the issue persists the next time I use Neovim.

Could you give me some advice to avoid this issue?

my config:

 -- im-select
  {
    "keaising/im-select.nvim",
    lazy = false,
    config = function()
      require("im_select").setup {
        default_im_select = "1033",
        default_command = "im-select.exe",
        set_default_events = { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },
        set_previous_events = { "InsertEnter" },
        keep_quiet_on_no_binary = false,
        async_switch_im = true,
      }
    end,
  },

I use Nvchad in neovim.
Does this issue relate to the Nvchad framework?

flatpak fcitx5

Hi, I'm currently using flatpak fcitx5 on Ubuntu 20.04, every time the nvim is launched, I get an error message stating that "please intall im-select binary first, ...". Please figure out what I'm missing, the README just says that im-select is not required on Linux but does not cover the flatpak/appimage cases. BTW, Ubuntu 20.04 delivers fcitx5 that is not fully packaged due to some dependency restrictions.

[Feature] 在 vscode 中不起作用

在终端模拟器启动 neovim 切换输入法正常,但在 VS Code 中使用 VSCode Neovim 插件不生效。

“我知道可能因为作者没有类似的需求,因此没有做兼容。”

如果有在 VS Code 中使用 neovim 的伙伴,或者知道如何在 VS Code 中实现类似功能的,能否交流一下。

[Feature Request] IM could switch intelligently maybe....

First of all, I wanted to express my gratitude for creating such an impressive plugin!

I have a suggestion: it would be great if it could intelligently recognize which input method (IM) is needed. As a Chinese user, I use English in normal mode and also in insert mode, except when inputting comments and strings. Therefore, by using treesitter to logically analyze the current position, it can infer which language should be used at the moment.

However, currently, this plugin seems to only support switching between the default input method (IM) and the IM used by the user last time. Adding this logic would lead to huge changes in the codebase. I am very willing to contribute code to this repository and would like to hear your thoughts.

-- get node right before user's cursor
local function get_node_before_cursor()
    local cursor_pos = vim.api.nvim_win_get_cursor(0)
    -- because nvim_win_get_cursor is (1, 0)-indexed
    -- but vim.treesitter.get_node is (0, 0)-indexed
    -- so row must decrease 1
    -- and in insert mode, cursor's position is in cursor right
    -- but we need detect the char just before the cursor
    -- so col need decrease 1
    local row, col = cursor_pos[1] - 1, cursor_pos[2] - 1
    if col < 0 then
        return nil
    end
    local node = vim.treesitter.get_node({ pos = { row, col } })
    return node
end

local function would_alternative_im_better(t)
    local specific_type = { "comment", "comment_content", "string_content", "string" }
    for _, v in ipairs(specific_type) do
        if t == v then
            return true
        end
    end
    return false
end

local function determine_which_IM_and_switch(ignore_last_node_type)
    local current_node = get_node_before_cursor()
    if current_node == nil then
        return
    end
    local current_node_type = current_node:type()

    -- when type comment or string, switch IM to alternative IM
    if would_alternative_im_better(current_node_type) then
        if ignore_last_node_type or not would_alternative_im_better(last_node_type) then
            switch_to_alternative_im()
        end
    end

    -- when type comment or string finish, switch IM to default IM
    if not would_alternative_im_better(current_node_type) then
        if ignore_last_node_type or would_alternative_im_better(last_node_type) then
            switch_to_default_im()
        end
    end
    last_node_type = current_node:type()
end

M.setup = function(opts)
    -- some code here .......
    local group_id = vim.api.nvim_create_augroup("im-select", { clear = true })

    if #C.set_default_events > 0 then
        vim.api.nvim_create_autocmd(C.set_default_events, {
            callback = switch_to_default_im,
            group = group_id,
        })
    end
    vim.api.nvim_create_autocmd({ "InsertEnter" }, {
        callback = function()
            determine_which_IM_and_switch(true)
        end
    })
    vim.api.nvim_create_autocmd({ "TextChangedI" }, {
        callback = function()
            determine_which_IM_and_switch(false)
        end
    })
    -- some code here .......
end

In this code, alternative_im means zh, default _im means en

If enter in cmdline the language is changing

If I want to go to cmd mode in normal mode, the language changes to the one I had in insert mode. At the same time, most of the commands in neovim are in English. I think it's worth adding a setting to the configuration that disables language change when entering cmd mode

如何在单字符替换完成、回到NORMAL模式时自动切换输入法

现象

在 NORMAL 模式下将光标移动到想修改的单个字符,点击 r 进行单字符修改,切换为中文输入法打完字后替换完成,自动回到 NORMAL 模式后没有切换到 default_im_select 配置的输入法。

环境

插件版本(commit id) c2b414b,插件配置如下(全部配置:https://github.com/cncsl/dotfiles/tree/main/nvim)

  {
    "keaising/im-select.nvim",
    config = function()
      require("im_select").setup {
        default_im_select = "com.apple.keylayout.ABC",
        default_command = "im-select",

        -- Restore the default input method state when the following events are triggered
        set_default_events = { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },

        -- Restore the previous used input method state when the following events
        -- are triggered, if you don't want to restore previous used im in Insert mode,
        -- e.g. deprecated `disable_auto_restore = 1`, just let it empty
        -- as `set_previous_events = {}`
        set_previous_events = { "InsertEnter" },

        -- Async run `default_command` to switch IM or not
        async_switch_im = true,
      }
    end,
  },

im-select is not working with astronvim?

  1. OS: Ubuntu 22.04(Fcitx5 installed)
  2. astronvim setting file (directory .config/nvim/lua/user/plugins/im-select.lua)
return {
  "keaising/im-select.nvim",
  config = function()
    require("im-select").setup {
      default_im_select = "com.apple.keylayout.ABC",
    }
  end,
}
  1. behavior
    I am switching between Chinese and English after I installed the plugin the input method did not switch automatically.
    the following is my Fctix config:
    Group1(for Chinese)
    image
    Group2(for English)
    image
    Anyone know why im-select doesn't work?

Not working on Windows

I am using Windows 11 and managing plugins via lazy.nvim. The im-select binary is placed along with the nvim binary in the same directory, it can be accessed in Command Prompt and Powershell. However, with the default config, nvim keeps spawning terminal windows once launched.

I have to remove the "FocusGained" from the default events to prevent nvim from spawning terminal windows:

  {
    "keaising/im-select.nvim",
    opts = {
      set_default_events = { "VimEnter", "InsertLeave", "CmdlineLeave" },
    },
    config = true
  }

Now a terminal windows flashes around 2~3 times on each press of Esc, but input method does not get switched.

BTW, I could mange to switch to the US keyboard by directly executing :!im-select 1033.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.