Normalize stroke before adding keybind (#1334)
* Normalize stroke before adding keybind * improve normalization algorithm and implement normalization in several functions Signed-off-by: delta <darkussdelta@gmail.com> --------- Signed-off-by: delta <darkussdelta@gmail.com>
This commit is contained in:
parent
07818934b6
commit
e62a672d7e
|
@ -35,6 +35,23 @@ local modkey_map = modkeys_os.map
|
||||||
local modkeys = modkeys_os.keys
|
local modkeys = modkeys_os.keys
|
||||||
|
|
||||||
|
|
||||||
|
---Normalizes a stroke sequence to follow the modkeys table
|
||||||
|
---@param stroke string
|
||||||
|
---@return string
|
||||||
|
local function normalize_stroke(stroke)
|
||||||
|
local stroke_table = {}
|
||||||
|
for modkey in stroke:gmatch("(%w+)%+") do
|
||||||
|
table.insert(stroke_table, modkey)
|
||||||
|
end
|
||||||
|
if not next(stroke_table) then
|
||||||
|
return stroke
|
||||||
|
end
|
||||||
|
table.sort(stroke_table)
|
||||||
|
local new_stroke = table.concat(stroke_table, "+") .. "+"
|
||||||
|
return new_stroke .. stroke:sub(new_stroke:len() + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
---Generates a stroke sequence including currently pressed mod keys.
|
---Generates a stroke sequence including currently pressed mod keys.
|
||||||
---@param key string
|
---@param key string
|
||||||
---@return string
|
---@return string
|
||||||
|
@ -45,10 +62,9 @@ local function key_to_stroke(key)
|
||||||
stroke = stroke .. mk .. "+"
|
stroke = stroke .. mk .. "+"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return stroke .. key
|
return normalize_stroke(stroke) .. key
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Remove the given value from an array associated to a key in a table.
|
---Remove the given value from an array associated to a key in a table.
|
||||||
---@param tbl table<string, string> The table containing the key
|
---@param tbl table<string, string> The table containing the key
|
||||||
---@param k string The key containing the array
|
---@param k string The key containing the array
|
||||||
|
@ -74,6 +90,7 @@ end
|
||||||
---@param map keymap.map
|
---@param map keymap.map
|
||||||
local function remove_duplicates(map)
|
local function remove_duplicates(map)
|
||||||
for stroke, commands in pairs(map) do
|
for stroke, commands in pairs(map) do
|
||||||
|
stroke = normalize_stroke(stroke)
|
||||||
if type(commands) == "string" or type(commands) == "function" then
|
if type(commands) == "string" or type(commands) == "function" then
|
||||||
commands = { commands }
|
commands = { commands }
|
||||||
end
|
end
|
||||||
|
@ -96,11 +113,12 @@ local function remove_duplicates(map)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Add bindings by replacing commands that were previously assigned to a shortcut.
|
---Add bindings by replacing commands that were previously assigned to a shortcut.
|
||||||
---@param map keymap.map
|
---@param map keymap.map
|
||||||
function keymap.add_direct(map)
|
function keymap.add_direct(map)
|
||||||
for stroke, commands in pairs(map) do
|
for stroke, commands in pairs(map) do
|
||||||
|
stroke = normalize_stroke(stroke)
|
||||||
|
|
||||||
if type(commands) == "string" or type(commands) == "function" then
|
if type(commands) == "string" or type(commands) == "function" then
|
||||||
commands = { commands }
|
commands = { commands }
|
||||||
end
|
end
|
||||||
|
@ -128,6 +146,7 @@ function keymap.add(map, overwrite)
|
||||||
if macos then
|
if macos then
|
||||||
stroke = stroke:gsub("%f[%a]ctrl%f[%A]", "cmd")
|
stroke = stroke:gsub("%f[%a]ctrl%f[%A]", "cmd")
|
||||||
end
|
end
|
||||||
|
stroke = normalize_stroke(stroke)
|
||||||
if overwrite then
|
if overwrite then
|
||||||
if keymap.map[stroke] then
|
if keymap.map[stroke] then
|
||||||
for _, cmd in ipairs(keymap.map[stroke]) do
|
for _, cmd in ipairs(keymap.map[stroke]) do
|
||||||
|
@ -153,7 +172,7 @@ end
|
||||||
---@param shortcut string
|
---@param shortcut string
|
||||||
---@param cmd string
|
---@param cmd string
|
||||||
function keymap.unbind(shortcut, cmd)
|
function keymap.unbind(shortcut, cmd)
|
||||||
remove_only(keymap.map, shortcut, cmd)
|
remove_only(keymap.map, normalize_stroke(shortcut), cmd)
|
||||||
remove_only(keymap.reverse_map, cmd, shortcut)
|
remove_only(keymap.reverse_map, cmd, shortcut)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue