im-select.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. local M = {}
  2. M.defaultIM = "com.apple.keylayout.ABC"
  3. M.currentIM = M.defaultIM
  4. local macInsertEnter = function()
  5. if M.currentIM then
  6. vim.cmd(":silent :!im-select" .. " " .. M.currentIM)
  7. else
  8. vim.cmd(":silent :!im-select" .. " " .. M.defaultIM)
  9. end
  10. end
  11. local macInsertLeave = function()
  12. M.currentIM = vim.fn.system({ "im-select" })
  13. vim.cmd(":silent :!im-select" .. " " .. M.defaultIM)
  14. end
  15. local windowsInsertLeave = function()
  16. vim.cmd(":silent :!~/.config/nvim/im-select.exe 1033")
  17. end
  18. local windowsInsertEnter = function()
  19. vim.cmd(":silent :!~/.config/nvim/im-select.exe 2052")
  20. end
  21. M.insertEnter = function()
  22. if vim.fn.executable("im-select") ~= 1 and vim.fn.executable("im-select.exe") ~= 1 then
  23. vim.notify("没有找到 im-select 无法切换输入法, https://github.com/daipeihust/im-select")
  24. return
  25. end
  26. if vim.fn.has("macunix") == 1 then
  27. macInsertEnter()
  28. elseif vim.fn.has("win32") then
  29. windowsInsertEnter()
  30. end
  31. end
  32. M.insertLeave = function()
  33. if vim.fn.executable("im-select") ~= 1 and vim.fn.executable("im-select.exe") ~= 1 then
  34. vim.notify("没有找到 im-select 无法切换输入法, https://github.com/daipeihust/im-select")
  35. return
  36. end
  37. if vim.fn.has("macunix") == 1 then
  38. macInsertLeave()
  39. elseif vim.fn.has("win32") then
  40. windowsInsertLeave()
  41. end
  42. end
  43. return M