-
Notifications
You must be signed in to change notification settings - Fork 12
/
xkbswitch.lua
122 lines (109 loc) · 4.14 KB
/
xkbswitch.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
local M = {}
-- nvim_create_autocmd shortcut
local autocmd = vim.api.nvim_create_autocmd
local xkb_switch_lib = nil
local user_os_name = vim.loop.os_uname().sysname
-- Find the path to the xkbswitch shared object (macOS)
if user_os_name == 'Darwin' then
if vim.fn.filereadable('/usr/local/lib/libInputSourceSwitcher.dylib') then xkb_switch_lib = '/usr/local/lib/libInputSourceSwitcher.dylib'
elseif vim.fn.filereadable('/usr/lib/libInputSourceSwitcher.dylib') then xkb_switch_lib = '/usr/lib/libInputSourceSwitcher.dylib' end
-- Find the path to the xkbswitch shared object (Linux)
else
local all_libs_locations = vim.fn.systemlist('ldd $(which xkb-switch)')
for _, value in ipairs(all_libs_locations) do
if string.find(value, 'libxkbswitch.so.1') then
if string.find(value, 'not found') then
xkb_switch_lib = nil
else
xkb_switch_lib = string.sub(
value, string.find(value, "/"), string.find(value, "%(") - 2
)
end
end
end
end
if xkb_switch_lib == nil then
if vim.fn.filereadable('/usr/local/lib/libg3kbswitch.so') then xkb_switch_lib = '/usr/local/lib/libg3kbswitch.so' end
end
if xkb_switch_lib == nil then
error("(xkbswitch.lua) Error occured: layout switcher file was not found.")
end
local function get_current_layout()
return vim.fn.libcall(xkb_switch_lib, 'Xkb_Switch_getXkbLayout', '')
end
local saved_layout = get_current_layout()
local user_us_layout_variation = nil
local user_layouts = vim.fn.systemlist(string.find(xkb_switch_lib, 'dylib') and 'issw -l' or string.find(xkb_switch_lib, 'xkb') and 'xkb-switch -l' or string.find(xkb_switch_lib, 'g3kb') and 'g3kb-switch -l')
-- Find the used US layout (us/us(qwerty)/us(dvorak)/...)
for _, value in ipairs(user_layouts) do
if string.find(value, user_os_name == 'Darwin' and 'ABC' or '^us') then
user_us_layout_variation = value
else if string.find(value, '.US$') then
user_us_layout_variation = value
end
end
if user_us_layout_variation == nil then
error("(xkbswitch.lua) Error occured: could not find the English layout. Check your layout list. (xkb-switch -l / issw -l / g3kb-switch -l)")
end
function M.setup()
-- When leaving insert mode:
-- 1. Save the current layout
-- 2. Switch to the US layout
autocmd(
'InsertLeave',
{
pattern = "*",
callback = function ()
vim.schedule(function()
saved_layout = get_current_layout()
vim.fn.libcall(xkb_switch_lib, 'Xkb_Switch_setXkbLayout', user_us_layout_variation)
end)
end
}
)
-- When Neovim gets focus:
-- 1. Save the current layout
-- 2. Switch to the US layout if Normal mode or Visual mode is the current mode
autocmd(
'FocusGained',
{
pattern = "*",
callback = function ()
vim.schedule(function()
saved_layout = get_current_layout()
local current_mode = vim.api.nvim_get_mode().mode
if current_mode == "n" or current_mode == "no" or current_mode == "v" or current_mode == "V" or current_mode == "^V" then
vim.fn.libcall(xkb_switch_lib, 'Xkb_Switch_setXkbLayout', user_us_layout_variation)
end
end)
end
}
)
-- When entering Insert mode:
-- 1. Switch to the previously saved layout
autocmd(
'InsertEnter',
{
pattern = "*",
callback = function ()
vim.schedule(function()
vim.fn.libcall(xkb_switch_lib, 'Xkb_Switch_setXkbLayout', saved_layout)
end)
end
}
)
-- When Neovim loses focus:
-- 1. Switch to the previously saved layout
autocmd(
'FocusLost',
{
pattern = "*",
callback = function ()
vim.schedule(function()
vim.fn.libcall(xkb_switch_lib, 'Xkb_Switch_setXkbLayout', saved_layout)
end)
end
}
)
end
return M