2022-04-26 15:48:59 +02:00
|
|
|
local core = require "core"
|
2019-12-28 12:16:32 +01:00
|
|
|
local command = require "core.command"
|
2021-11-14 21:45:46 +01:00
|
|
|
local config = require "core.config"
|
2019-12-28 12:16:32 +01:00
|
|
|
local keymap = {}
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
---@alias keymap.shortcut string
|
|
|
|
---@alias keymap.command string
|
|
|
|
---@alias keymap.modkey string
|
|
|
|
---@alias keymap.pressed boolean
|
|
|
|
---@alias keymap.map table<keymap.shortcut,keymap.command|keymap.command[]>
|
|
|
|
---@alias keymap.rmap table<keymap.command, keymap.shortcut|keymap.shortcut[]>
|
|
|
|
|
|
|
|
---Pressed status of mod keys.
|
|
|
|
---@type table<keymap.modkey, keymap.pressed>
|
2019-12-28 12:16:32 +01:00
|
|
|
keymap.modkeys = {}
|
2022-05-10 02:02:32 +02:00
|
|
|
|
|
|
|
---List of commands assigned to a shortcut been the key of the map the shortcut.
|
|
|
|
---@type keymap.map
|
2019-12-28 12:16:32 +01:00
|
|
|
keymap.map = {}
|
2022-05-10 02:02:32 +02:00
|
|
|
|
|
|
|
---List of shortcuts assigned to a command been the key of the map the command.
|
|
|
|
---@type keymap.rmap
|
2019-12-28 12:16:32 +01:00
|
|
|
keymap.reverse_map = {}
|
|
|
|
|
2021-09-21 05:50:06 +02:00
|
|
|
local macos = PLATFORM == "Mac OS X"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-04-21 09:48:30 +02:00
|
|
|
-- Thanks to mathewmariani, taken from his lite-macos github repository.
|
|
|
|
local modkeys_os = require("core.modkeys-" .. (macos and "macos" or "generic"))
|
2022-05-10 02:02:32 +02:00
|
|
|
|
|
|
|
---@type table<keymap.modkey, keymap.modkey>
|
2021-04-21 09:48:30 +02:00
|
|
|
local modkey_map = modkeys_os.map
|
2022-05-10 02:02:32 +02:00
|
|
|
|
|
|
|
---@type keymap.modkey[]
|
2021-04-21 09:48:30 +02:00
|
|
|
local modkeys = modkeys_os.keys
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
|
|
|
|
---Generates a stroke sequence including currently pressed mod keys.
|
|
|
|
---@param key string
|
|
|
|
---@return string
|
|
|
|
local function key_to_stroke(key)
|
2019-12-28 12:16:32 +01:00
|
|
|
local stroke = ""
|
|
|
|
for _, mk in ipairs(modkeys) do
|
|
|
|
if keymap.modkeys[mk] then
|
|
|
|
stroke = stroke .. mk .. "+"
|
|
|
|
end
|
|
|
|
end
|
2022-05-10 02:02:32 +02:00
|
|
|
return stroke .. key
|
|
|
|
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
|
|
|
|
---@param v? string The value to remove from the array
|
|
|
|
local function remove_only(tbl, k, v)
|
|
|
|
if tbl[k] then
|
|
|
|
if v then
|
|
|
|
local j = 0
|
|
|
|
for i=1, #tbl[k] do
|
|
|
|
while tbl[k][i + j] == v do
|
|
|
|
j = j + 1
|
|
|
|
end
|
|
|
|
tbl[k][i] = tbl[k][i + j]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
tbl[k] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
---Removes from a keymap.map the bindings that are already registered.
|
|
|
|
---@param map keymap.map
|
|
|
|
local function remove_duplicates(map)
|
|
|
|
for stroke, commands in pairs(map) do
|
|
|
|
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
|
|
|
|
local j = 0
|
|
|
|
for i=1, #commands do
|
|
|
|
while commands[i + j] == registered_cmd do
|
|
|
|
j = j + 1
|
|
|
|
end
|
|
|
|
commands[i] = commands[i + j]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #commands < 1 then
|
|
|
|
map[stroke] = nil
|
|
|
|
else
|
|
|
|
map[stroke] = commands
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
---Add bindings by replacing commands that were previously assigned to a shortcut.
|
|
|
|
---@param map keymap.map
|
2021-04-21 10:16:51 +02:00
|
|
|
function keymap.add_direct(map)
|
2019-12-28 12:16:32 +01:00
|
|
|
for stroke, commands in pairs(map) do
|
2022-05-10 02:02:32 +02:00
|
|
|
if type(commands) == "string" or type(commands) == "function" then
|
2019-12-28 12:16:32 +01:00
|
|
|
commands = { commands }
|
|
|
|
end
|
2022-05-10 02:02:32 +02:00
|
|
|
if keymap.map[stroke] then
|
|
|
|
for _, cmd in ipairs(keymap.map[stroke]) do
|
|
|
|
remove_only(keymap.reverse_map, cmd, stroke)
|
|
|
|
end
|
|
|
|
end
|
2021-04-21 10:16:51 +02:00
|
|
|
keymap.map[stroke] = commands
|
|
|
|
for _, cmd in ipairs(commands) do
|
2021-09-08 17:09:20 +02:00
|
|
|
keymap.reverse_map[cmd] = keymap.reverse_map[cmd] or {}
|
|
|
|
table.insert(keymap.reverse_map[cmd], stroke)
|
2021-04-21 10:16:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
|
|
|
|
---Adds bindings by appending commands to already registered shortcut or by
|
|
|
|
---replacing currently assigned commands if overwrite is specified.
|
|
|
|
---@param map keymap.map
|
|
|
|
---@param overwrite? boolean
|
2021-04-21 10:16:51 +02:00
|
|
|
function keymap.add(map, overwrite)
|
2022-05-10 02:02:32 +02:00
|
|
|
remove_duplicates(map)
|
2021-04-21 10:16:51 +02:00
|
|
|
for stroke, commands in pairs(map) do
|
2021-04-21 10:42:56 +02:00
|
|
|
if macos then
|
|
|
|
stroke = stroke:gsub("%f[%a]ctrl%f[%A]", "cmd")
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
if overwrite then
|
2022-05-10 02:02:32 +02:00
|
|
|
if keymap.map[stroke] then
|
|
|
|
for _, cmd in ipairs(keymap.map[stroke]) do
|
|
|
|
remove_only(keymap.reverse_map, cmd, stroke)
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
keymap.map[stroke] = commands
|
|
|
|
else
|
|
|
|
keymap.map[stroke] = keymap.map[stroke] or {}
|
|
|
|
for i = #commands, 1, -1 do
|
|
|
|
table.insert(keymap.map[stroke], 1, commands[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _, cmd in ipairs(commands) do
|
2021-09-08 17:09:20 +02:00
|
|
|
keymap.reverse_map[cmd] = keymap.reverse_map[cmd] or {}
|
|
|
|
table.insert(keymap.reverse_map[cmd], stroke)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
---Unregisters the given shortcut and associated command.
|
|
|
|
---@param shortcut string
|
|
|
|
---@param cmd string
|
|
|
|
function keymap.unbind(shortcut, cmd)
|
|
|
|
remove_only(keymap.map, shortcut, cmd)
|
|
|
|
remove_only(keymap.reverse_map, cmd, shortcut)
|
2021-09-08 17:09:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
---Returns all the shortcuts associated to a command unpacked for easy assignment.
|
|
|
|
---@param cmd string
|
|
|
|
---@return ...
|
|
|
|
function keymap.get_binding(cmd)
|
|
|
|
return table.unpack(keymap.reverse_map[cmd] or {})
|
2021-09-08 17:09:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
---Returns all the shortcuts associated to a command packed in a table.
|
|
|
|
---@param cmd string
|
|
|
|
---@return table<integer, string> | nil shortcuts
|
|
|
|
function keymap.get_bindings(cmd)
|
|
|
|
return keymap.reverse_map[cmd]
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Events listening
|
|
|
|
--------------------------------------------------------------------------------
|
2021-10-06 00:22:43 +02:00
|
|
|
function keymap.on_key_pressed(k, ...)
|
2019-12-29 17:09:56 +01:00
|
|
|
local mk = modkey_map[k]
|
2019-12-28 12:16:32 +01:00
|
|
|
if mk then
|
|
|
|
keymap.modkeys[mk] = true
|
2019-12-29 17:32:04 +01:00
|
|
|
-- work-around for windows where `altgr` is treated as `ctrl+alt`
|
|
|
|
if mk == "altgr" then
|
|
|
|
keymap.modkeys["ctrl"] = false
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
else
|
|
|
|
local stroke = key_to_stroke(k)
|
2022-05-10 02:02:32 +02:00
|
|
|
local commands, performed = keymap.map[stroke], false
|
2019-12-28 12:16:32 +01:00
|
|
|
if commands then
|
|
|
|
for _, cmd in ipairs(commands) do
|
2022-04-26 15:48:59 +02:00
|
|
|
if type(cmd) == "function" then
|
|
|
|
local ok, res = core.try(cmd, ...)
|
|
|
|
if ok then
|
|
|
|
performed = not (res == false)
|
|
|
|
else
|
|
|
|
performed = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
performed = command.perform(cmd, ...)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
if performed then break end
|
|
|
|
end
|
2021-10-06 00:22:43 +02:00
|
|
|
return performed
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-11-07 20:31:15 +01:00
|
|
|
function keymap.on_mouse_wheel(delta, ...)
|
2021-11-17 01:12:39 +01:00
|
|
|
return not (keymap.on_key_pressed("wheel" .. (delta > 0 and "up" or "down"), delta, ...)
|
|
|
|
or keymap.on_key_pressed("wheel", delta, ...))
|
2021-11-07 20:31:15 +01:00
|
|
|
end
|
|
|
|
|
2021-10-06 01:42:04 +02:00
|
|
|
function keymap.on_mouse_pressed(button, x, y, clicks)
|
2021-11-14 21:51:27 +01:00
|
|
|
local click_number = (((clicks - 1) % config.max_clicks) + 1)
|
|
|
|
return not (keymap.on_key_pressed(click_number .. button:sub(1,1) .. "click", x, y, clicks) or
|
|
|
|
keymap.on_key_pressed(button:sub(1,1) .. "click", x, y, clicks) or
|
|
|
|
keymap.on_key_pressed(click_number .. "click", x, y, clicks) or
|
|
|
|
keymap.on_key_pressed("click", x, y, clicks))
|
2021-10-06 01:42:04 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
function keymap.on_key_released(k)
|
2019-12-29 17:09:56 +01:00
|
|
|
local mk = modkey_map[k]
|
2019-12-28 12:16:32 +01:00
|
|
|
if mk then
|
|
|
|
keymap.modkeys[mk] = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-05-10 02:02:32 +02:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Register default bindings
|
|
|
|
--------------------------------------------------------------------------------
|
2021-04-21 09:48:30 +02:00
|
|
|
if macos then
|
|
|
|
local keymap_macos = require("core.keymap-macos")
|
|
|
|
keymap_macos(keymap)
|
|
|
|
return keymap
|
|
|
|
end
|
|
|
|
|
2021-04-21 10:16:51 +02:00
|
|
|
keymap.add_direct {
|
2020-05-30 10:11:42 +02:00
|
|
|
["ctrl+shift+p"] = "core:find-command",
|
|
|
|
["ctrl+p"] = "core:find-file",
|
2019-12-28 12:16:32 +01:00
|
|
|
["ctrl+o"] = "core:open-file",
|
|
|
|
["ctrl+n"] = "core:new-doc",
|
2020-12-10 17:56:53 +01:00
|
|
|
["ctrl+shift+c"] = "core:change-project-folder",
|
|
|
|
["ctrl+shift+o"] = "core:open-project-folder",
|
2022-09-16 17:28:16 +02:00
|
|
|
["ctrl+alt+r"] = "core:restart",
|
2020-03-25 23:44:59 +01:00
|
|
|
["alt+return"] = "core:toggle-fullscreen",
|
2021-07-20 20:39:50 +02:00
|
|
|
["f11"] = "core:toggle-fullscreen",
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
["alt+shift+j"] = "root:split-left",
|
|
|
|
["alt+shift+l"] = "root:split-right",
|
|
|
|
["alt+shift+i"] = "root:split-up",
|
|
|
|
["alt+shift+k"] = "root:split-down",
|
|
|
|
["alt+j"] = "root:switch-to-left",
|
|
|
|
["alt+l"] = "root:switch-to-right",
|
|
|
|
["alt+i"] = "root:switch-to-up",
|
|
|
|
["alt+k"] = "root:switch-to-down",
|
|
|
|
|
|
|
|
["ctrl+w"] = "root:close",
|
|
|
|
["ctrl+tab"] = "root:switch-to-next-tab",
|
|
|
|
["ctrl+shift+tab"] = "root:switch-to-previous-tab",
|
|
|
|
["ctrl+pageup"] = "root:move-tab-left",
|
|
|
|
["ctrl+pagedown"] = "root:move-tab-right",
|
|
|
|
["alt+1"] = "root:switch-to-tab-1",
|
|
|
|
["alt+2"] = "root:switch-to-tab-2",
|
|
|
|
["alt+3"] = "root:switch-to-tab-3",
|
|
|
|
["alt+4"] = "root:switch-to-tab-4",
|
|
|
|
["alt+5"] = "root:switch-to-tab-5",
|
|
|
|
["alt+6"] = "root:switch-to-tab-6",
|
|
|
|
["alt+7"] = "root:switch-to-tab-7",
|
|
|
|
["alt+8"] = "root:switch-to-tab-8",
|
|
|
|
["alt+9"] = "root:switch-to-tab-9",
|
2021-11-07 21:42:03 +01:00
|
|
|
["wheel"] = "root:scroll",
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
["ctrl+f"] = "find-replace:find",
|
|
|
|
["ctrl+r"] = "find-replace:replace",
|
|
|
|
["f3"] = "find-replace:repeat-find",
|
|
|
|
["shift+f3"] = "find-replace:previous-find",
|
2021-07-16 00:21:54 +02:00
|
|
|
["ctrl+i"] = "find-replace:toggle-sensitivity",
|
|
|
|
["ctrl+shift+i"] = "find-replace:toggle-regex",
|
2019-12-28 12:16:32 +01:00
|
|
|
["ctrl+g"] = "doc:go-to-line",
|
|
|
|
["ctrl+s"] = "doc:save",
|
|
|
|
["ctrl+shift+s"] = "doc:save-as",
|
|
|
|
|
|
|
|
["ctrl+z"] = "doc:undo",
|
|
|
|
["ctrl+y"] = "doc:redo",
|
|
|
|
["ctrl+x"] = "doc:cut",
|
|
|
|
["ctrl+c"] = "doc:copy",
|
|
|
|
["ctrl+v"] = "doc:paste",
|
2021-01-06 15:12:26 +01:00
|
|
|
["ctrl+insert"] = "doc:copy",
|
|
|
|
["shift+insert"] = "doc:paste",
|
2021-04-04 16:11:47 +02:00
|
|
|
["escape"] = { "command:escape", "doc:select-none", "dialog:select-no" },
|
2019-12-28 12:16:32 +01:00
|
|
|
["tab"] = { "command:complete", "doc:indent" },
|
|
|
|
["shift+tab"] = "doc:unindent",
|
|
|
|
["backspace"] = "doc:backspace",
|
|
|
|
["shift+backspace"] = "doc:backspace",
|
2020-05-28 12:50:03 +02:00
|
|
|
["ctrl+backspace"] = "doc:delete-to-previous-word-start",
|
|
|
|
["ctrl+shift+backspace"] = "doc:delete-to-previous-word-start",
|
2020-05-03 19:37:06 +02:00
|
|
|
["delete"] = "doc:delete",
|
|
|
|
["shift+delete"] = "doc:delete",
|
2020-05-28 12:50:03 +02:00
|
|
|
["ctrl+delete"] = "doc:delete-to-next-word-end",
|
|
|
|
["ctrl+shift+delete"] = "doc:delete-to-next-word-end",
|
2021-04-04 16:11:47 +02:00
|
|
|
["return"] = { "command:submit", "doc:newline", "dialog:select" },
|
|
|
|
["keypad enter"] = { "command:submit", "doc:newline", "dialog:select" },
|
2019-12-28 12:16:32 +01:00
|
|
|
["ctrl+return"] = "doc:newline-below",
|
|
|
|
["ctrl+shift+return"] = "doc:newline-above",
|
|
|
|
["ctrl+j"] = "doc:join-lines",
|
|
|
|
["ctrl+a"] = "doc:select-all",
|
2021-09-09 19:12:56 +02:00
|
|
|
["ctrl+d"] = { "find-replace:select-add-next", "doc:select-word" },
|
|
|
|
["ctrl+f3"] = "find-replace:select-next",
|
2021-09-10 21:54:50 +02:00
|
|
|
["ctrl+shift+f3"] = "find-replace:select-previous",
|
2019-12-28 12:16:32 +01:00
|
|
|
["ctrl+l"] = "doc:select-lines",
|
2021-09-09 19:12:56 +02:00
|
|
|
["ctrl+shift+l"] = { "find-replace:select-add-all", "doc:select-word" },
|
2019-12-28 12:16:32 +01:00
|
|
|
["ctrl+/"] = "doc:toggle-line-comments",
|
2021-12-25 05:57:00 +01:00
|
|
|
["ctrl+shift+/"] = "doc:toggle-block-comments",
|
2019-12-28 12:16:32 +01:00
|
|
|
["ctrl+up"] = "doc:move-lines-up",
|
|
|
|
["ctrl+down"] = "doc:move-lines-down",
|
|
|
|
["ctrl+shift+d"] = "doc:duplicate-lines",
|
|
|
|
["ctrl+shift+k"] = "doc:delete-lines",
|
|
|
|
|
2021-04-04 16:11:47 +02:00
|
|
|
["left"] = { "doc:move-to-previous-char", "dialog:previous-entry" },
|
|
|
|
["right"] = { "doc:move-to-next-char", "dialog:next-entry"},
|
2019-12-28 12:16:32 +01:00
|
|
|
["up"] = { "command:select-previous", "doc:move-to-previous-line" },
|
|
|
|
["down"] = { "command:select-next", "doc:move-to-next-line" },
|
2020-05-28 12:50:03 +02:00
|
|
|
["ctrl+left"] = "doc:move-to-previous-word-start",
|
|
|
|
["ctrl+right"] = "doc:move-to-next-word-end",
|
2020-05-28 14:08:18 +02:00
|
|
|
["ctrl+["] = "doc:move-to-previous-block-start",
|
|
|
|
["ctrl+]"] = "doc:move-to-next-block-end",
|
2021-08-07 00:08:08 +02:00
|
|
|
["home"] = "doc:move-to-start-of-indentation",
|
2019-12-28 12:16:32 +01:00
|
|
|
["end"] = "doc:move-to-end-of-line",
|
|
|
|
["ctrl+home"] = "doc:move-to-start-of-doc",
|
|
|
|
["ctrl+end"] = "doc:move-to-end-of-doc",
|
|
|
|
["pageup"] = "doc:move-to-previous-page",
|
|
|
|
["pagedown"] = "doc:move-to-next-page",
|
|
|
|
|
2021-11-14 21:44:54 +01:00
|
|
|
["shift+1lclick"] = "doc:select-to-cursor",
|
|
|
|
["ctrl+1lclick"] = "doc:split-cursor",
|
|
|
|
["1lclick"] = "doc:set-cursor",
|
|
|
|
["2lclick"] = "doc:set-cursor-word",
|
|
|
|
["3lclick"] = "doc:set-cursor-line",
|
2019-12-28 12:16:32 +01:00
|
|
|
["shift+left"] = "doc:select-to-previous-char",
|
|
|
|
["shift+right"] = "doc:select-to-next-char",
|
|
|
|
["shift+up"] = "doc:select-to-previous-line",
|
|
|
|
["shift+down"] = "doc:select-to-next-line",
|
2020-05-28 12:50:03 +02:00
|
|
|
["ctrl+shift+left"] = "doc:select-to-previous-word-start",
|
|
|
|
["ctrl+shift+right"] = "doc:select-to-next-word-end",
|
2020-05-28 14:08:18 +02:00
|
|
|
["ctrl+shift+["] = "doc:select-to-previous-block-start",
|
|
|
|
["ctrl+shift+]"] = "doc:select-to-next-block-end",
|
2021-08-07 00:09:36 +02:00
|
|
|
["shift+home"] = "doc:select-to-start-of-indentation",
|
2019-12-28 12:16:32 +01:00
|
|
|
["shift+end"] = "doc:select-to-end-of-line",
|
|
|
|
["ctrl+shift+home"] = "doc:select-to-start-of-doc",
|
|
|
|
["ctrl+shift+end"] = "doc:select-to-end-of-doc",
|
|
|
|
["shift+pageup"] = "doc:select-to-previous-page",
|
|
|
|
["shift+pagedown"] = "doc:select-to-next-page",
|
2021-06-09 05:09:09 +02:00
|
|
|
["ctrl+shift+up"] = "doc:create-cursor-previous-line",
|
|
|
|
["ctrl+shift+down"] = "doc:create-cursor-next-line"
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return keymap
|