Normalize strokes in fixed order (#1572)
* Use normalized strokes when removing duplicates only when appropriate * Use normalized stroke in `keymap.unbind` * Normalize strokes by sorting the modifiers before the keys This also sorts the modifiers in a fixed manner, decided by `modkeys.keys`. We need to do this because we display the strokes in a few places like the command palette.
This commit is contained in:
parent
3993d689fb
commit
2ed17dd03f
|
@ -40,15 +40,19 @@ local modkeys = modkeys_os.keys
|
||||||
---@return string
|
---@return string
|
||||||
local function normalize_stroke(stroke)
|
local function normalize_stroke(stroke)
|
||||||
local stroke_table = {}
|
local stroke_table = {}
|
||||||
for modkey in stroke:gmatch("(%w+)%+") do
|
for key in stroke:gmatch("[^+]+") do
|
||||||
table.insert(stroke_table, modkey)
|
table.insert(stroke_table, key)
|
||||||
end
|
end
|
||||||
if not next(stroke_table) then
|
table.sort(stroke_table, function(a, b)
|
||||||
return stroke
|
if a == b then return false end
|
||||||
end
|
for _, mod in ipairs(modkeys) do
|
||||||
table.sort(stroke_table)
|
if a == mod or b == mod then
|
||||||
local new_stroke = table.concat(stroke_table, "+") .. "+"
|
return a == mod
|
||||||
return new_stroke .. stroke:sub(new_stroke:len() + 1)
|
end
|
||||||
|
end
|
||||||
|
return a < b
|
||||||
|
end)
|
||||||
|
return table.concat(stroke_table, "+")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,15 +60,16 @@ end
|
||||||
---@param key string
|
---@param key string
|
||||||
---@return string
|
---@return string
|
||||||
local function key_to_stroke(key)
|
local function key_to_stroke(key)
|
||||||
local stroke = ""
|
local keys = { key }
|
||||||
for _, mk in ipairs(modkeys) do
|
for _, mk in ipairs(modkeys) do
|
||||||
if keymap.modkeys[mk] then
|
if keymap.modkeys[mk] then
|
||||||
stroke = stroke .. mk .. "+"
|
table.insert(keys, mk)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return normalize_stroke(stroke) .. key
|
return normalize_stroke(table.concat(keys, "+"))
|
||||||
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
|
||||||
|
@ -90,12 +95,12 @@ 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)
|
local normalized_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
|
||||||
if keymap.map[stroke] then
|
if keymap.map[normalized_stroke] then
|
||||||
for _, registered_cmd in ipairs(keymap.map[stroke]) do
|
for _, registered_cmd in ipairs(keymap.map[normalized_stroke]) do
|
||||||
local j = 0
|
local j = 0
|
||||||
for i=1, #commands do
|
for i=1, #commands do
|
||||||
while commands[i + j] == registered_cmd do
|
while commands[i + j] == registered_cmd do
|
||||||
|
@ -118,7 +123,7 @@ end
|
||||||
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)
|
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
|
||||||
|
@ -172,7 +177,8 @@ 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, normalize_stroke(shortcut), cmd)
|
shortcut = normalize_stroke(shortcut)
|
||||||
|
remove_only(keymap.map, shortcut, cmd)
|
||||||
remove_only(keymap.reverse_map, cmd, shortcut)
|
remove_only(keymap.reverse_map, cmd, shortcut)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,6 @@ modkeys.map = {
|
||||||
["right alt"] = "altgr",
|
["right alt"] = "altgr",
|
||||||
}
|
}
|
||||||
|
|
||||||
modkeys.keys = { "ctrl", "alt", "altgr", "shift" }
|
modkeys.keys = { "ctrl", "shift", "alt", "altgr" }
|
||||||
|
|
||||||
return modkeys
|
return modkeys
|
||||||
|
|
|
@ -13,6 +13,6 @@ modkeys.map = {
|
||||||
["right alt"] = "altgr",
|
["right alt"] = "altgr",
|
||||||
}
|
}
|
||||||
|
|
||||||
modkeys.keys = { "cmd", "ctrl", "alt", "option", "altgr", "shift" }
|
modkeys.keys = { "ctrl", "alt", "option", "altgr", "shift", "cmd" }
|
||||||
|
|
||||||
return modkeys
|
return modkeys
|
||||||
|
|
Loading…
Reference in New Issue