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:
Guldoman 2023-08-25 20:03:23 +02:00 committed by George Sokianos
parent 3993d689fb
commit 2ed17dd03f
3 changed files with 24 additions and 18 deletions

View File

@ -40,15 +40,19 @@ local modkeys = modkeys_os.keys
---@return string
local function normalize_stroke(stroke)
local stroke_table = {}
for modkey in stroke:gmatch("(%w+)%+") do
table.insert(stroke_table, modkey)
for key in stroke:gmatch("[^+]+") do
table.insert(stroke_table, key)
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)
table.sort(stroke_table, function(a, b)
if a == b then return false end
for _, mod in ipairs(modkeys) do
if a == mod or b == mod then
return a == mod
end
end
return a < b
end)
return table.concat(stroke_table, "+")
end
@ -56,15 +60,16 @@ end
---@param key string
---@return string
local function key_to_stroke(key)
local stroke = ""
local keys = { key }
for _, mk in ipairs(modkeys) do
if keymap.modkeys[mk] then
stroke = stroke .. mk .. "+"
table.insert(keys, mk)
end
end
return normalize_stroke(stroke) .. key
return normalize_stroke(table.concat(keys, "+"))
end
---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 k string The key containing the array
@ -90,12 +95,12 @@ end
---@param map keymap.map
local function remove_duplicates(map)
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
commands = { commands }
end
if keymap.map[stroke] then
for _, registered_cmd in ipairs(keymap.map[stroke]) do
if keymap.map[normalized_stroke] then
for _, registered_cmd in ipairs(keymap.map[normalized_stroke]) do
local j = 0
for i=1, #commands do
while commands[i + j] == registered_cmd do
@ -118,7 +123,7 @@ end
function keymap.add_direct(map)
for stroke, commands in pairs(map) do
stroke = normalize_stroke(stroke)
if type(commands) == "string" or type(commands) == "function" then
commands = { commands }
end
@ -172,7 +177,8 @@ end
---@param shortcut string
---@param cmd string
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)
end

View File

@ -9,6 +9,6 @@ modkeys.map = {
["right alt"] = "altgr",
}
modkeys.keys = { "ctrl", "alt", "altgr", "shift" }
modkeys.keys = { "ctrl", "shift", "alt", "altgr" }
return modkeys

View File

@ -13,6 +13,6 @@ modkeys.map = {
["right alt"] = "altgr",
}
modkeys.keys = { "cmd", "ctrl", "alt", "option", "altgr", "shift" }
modkeys.keys = { "ctrl", "alt", "option", "altgr", "shift", "cmd" }
return modkeys