From 8213801b89e9d2a27c6b1df50881b8476679b870 Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Thu, 2 Dec 2021 23:02:50 +0100 Subject: LunarVim updates --- .config/nvim/lua/keymappings.lua | 230 +++++++++++++++++++-------------------- 1 file changed, 114 insertions(+), 116 deletions(-) (limited to '.config/nvim/lua/keymappings.lua') diff --git a/.config/nvim/lua/keymappings.lua b/.config/nvim/lua/keymappings.lua index 74f4233..8319ab3 100644 --- a/.config/nvim/lua/keymappings.lua +++ b/.config/nvim/lua/keymappings.lua @@ -21,15 +21,113 @@ local mode_adapters = { command_mode = "c", } +local defaults = { + ---@usage change or add keymappings for insert mode + insert_mode = { + -- 'jk' for quitting insert mode + ["jk"] = "", + -- 'kj' for quitting insert mode + ["kj"] = "", + -- 'jj' for quitting insert mode + ["jj"] = "", + -- Move current line / block with Alt-j/k ala vscode. + [""] = ":m .+1==gi", + -- Move current line / block with Alt-j/k ala vscode. + [""] = ":m .-2==gi", + -- navigation + [""] = "k", + [""] = "j", + [""] = "h", + [""] = "l", + }, + + ---@usage change or add keymappings for normal mode + normal_mode = { + -- Better window movement + [""] = "h", + [""] = "j", + [""] = "k", + [""] = "l", + + -- Resize with arrows + [""] = ":resize -2", + [""] = ":resize +2", + [""] = ":vertical resize -2", + [""] = ":vertical resize +2", + + -- Tab switch buffer + [""] = ":BufferNext", + [""] = ":BufferPrevious", + + -- Move current line / block with Alt-j/k a la vscode. + [""] = ":m .+1==", + [""] = ":m .-2==", + + -- QuickFix + ["]q"] = ":cnext", + ["[q"] = ":cprev", + [""] = ":call QuickFixToggle()", + }, + + ---@usage change or add keymappings for terminal mode + term_mode = { + -- Terminal window navigation + [""] = "h", + [""] = "j", + [""] = "k", + [""] = "l", + }, + + ---@usage change or add keymappings for visual mode + visual_mode = { + -- Better indenting + ["<"] = ""] = ">gv", + + -- ["p"] = '"0p', + -- ["P"] = '"0P', + }, + + ---@usage change or add keymappings for visual block mode + visual_block_mode = { + -- Move selected line / block of text in visual mode + ["K"] = ":move '<-2gv-gv", + ["J"] = ":move '>+1gv-gv", + + -- Move current line / block with Alt-j/k ala vscode. + [""] = ":m '>+1gv-gv", + [""] = ":m '<-2gv-gv", + }, + + ---@usage change or add keymappings for command mode + command_mode = { + -- navigate tab completion with and + -- runs conditionally + [""] = { + 'pumvisible() ? "\\" : "\\"', + { expr = true, noremap = true }, + }, + [""] = { + 'pumvisible() ? "\\" : "\\"', + { expr = true, noremap = true }, + }, + }, +} + +if vim.fn.has "mac" == 1 then + defaults.normal_mode[""] = defaults.normal_mode[""] + defaults.normal_mode[""] = defaults.normal_mode[""] + defaults.normal_mode[""] = defaults.normal_mode[""] + defaults.normal_mode[""] = defaults.normal_mode[""] + Log:debug "Activated mac keymappings" +end + -- Append key mappings to lunarvim's defaults for a given mode -- @param keymaps The table of key mappings containing a list per mode (normal_mode, insert_mode, ..) function M.append_to_defaults(keymaps) - local default = M.get_defaults() - options.keys = options.keys or default for mode, mappings in pairs(keymaps) do - options.keys[mode] = options.keys[mode] or default[mode] for k, v in pairs(mappings) do - options.keys[mode][k] = v + defaults[mode][k] = v end end end @@ -39,7 +137,7 @@ end function M.clear(keymaps) local default = M.get_defaults() for mode, mappings in pairs(keymaps) do - local translated_mode = mode_adapters[mode] and mode_adapters[mode] or mode + local translated_mode = mode_adapters[mode] or mode for key, _ in pairs(mappings) do -- some plugins may override default bindings that the user hasn't manually overriden if @@ -57,7 +155,7 @@ end -- @param key The key of keymap -- @param val Can be form as a mapping or tuple of mapping and user defined opt function M.set_keymaps(mode, key, val) - local opt = generic_opts[mode] and generic_opts[mode] or generic_opts_any + local opt = generic_opts[mode] or generic_opts_any if type(val) == "table" then opt = val[2] val = val[1] @@ -73,7 +171,7 @@ end -- @param mode The keymap mode, can be one of the keys of mode_adapters -- @param keymaps The list of key mappings function M.load_mode(mode, keymaps) - mode = mode_adapters[mode] and mode_adapters[mode] or mode + mode = mode_adapters[mode] or mode for k, v in pairs(keymaps) do M.set_keymaps(mode, k, v) end @@ -88,118 +186,18 @@ function M.load(keymaps) end end -function M.get_defaults() - local keys = { - ---@usage change or add keymappings for insert mode - insert_mode = { - -- 'jk' for quitting insert mode - ["jk"] = "", - -- 'kj' for quitting insert mode - ["kj"] = "", - -- 'jj' for quitting insert mode - ["jj"] = "", - -- Move current line / block with Alt-j/k ala vscode. - [""] = ":m .+1==gi", - -- Move current line / block with Alt-j/k ala vscode. - [""] = ":m .-2==gi", - -- navigation - [""] = "k", - [""] = "j", - [""] = "h", - [""] = "l", - }, - - ---@usage change or add keymappings for normal mode - normal_mode = { - -- Better window movement - [""] = "h", - [""] = "j", - [""] = "k", - [""] = "l", - - -- Resize with arrows - [""] = ":resize -2", - [""] = ":resize +2", - [""] = ":vertical resize -2", - [""] = ":vertical resize +2", - - -- Tab switch buffer - [""] = ":BufferNext", - [""] = ":BufferPrevious", - - -- Move current line / block with Alt-j/k a la vscode. - [""] = ":m .+1==", - [""] = ":m .-2==", - - -- QuickFix - ["]q"] = ":cnext", - ["[q"] = ":cprev", - [""] = ":call QuickFixToggle()", - }, - - ---@usage change or add keymappings for terminal mode - term_mode = { - -- Terminal window navigation - [""] = "h", - [""] = "j", - [""] = "k", - [""] = "l", - }, - - ---@usage change or add keymappings for visual mode - visual_mode = { - -- Better indenting - ["<"] = ""] = ">gv", - - -- ["p"] = '"0p', - -- ["P"] = '"0P', - }, - - ---@usage change or add keymappings for visual block mode - visual_block_mode = { - -- Move selected line / block of text in visual mode - ["K"] = ":move '<-2gv-gv", - ["J"] = ":move '>+1gv-gv", - - -- Move current line / block with Alt-j/k ala vscode. - [""] = ":m '>+1gv-gv", - [""] = ":m '<-2gv-gv", - }, - - ---@usage change or add keymappings for command mode - command_mode = { - -- navigate tab completion with and - -- runs conditionally - [""] = { - 'pumvisible() ? "\\" : "\\"', - { expr = true, noremap = true }, - }, - [""] = { - 'pumvisible() ? "\\" : "\\"', - { expr = true, noremap = true }, - }, - }, - } - - if vim.fn.has "mac" == 1 then - keys.normal_mode[""] = keys.normal_mode[""] - keys.normal_mode[""] = keys.normal_mode[""] - keys.normal_mode[""] = keys.normal_mode[""] - keys.normal_mode[""] = keys.normal_mode[""] - Log:debug "Activated mac keymappings" +-- Load the default keymappings +function M.load_defaults() + M.load(M.get_defaults()) + options.keys = {} + for idx, _ in pairs(defaults) do + options.keys[idx] = {} end - - return keys end -function M.print(mode) - print "List of LunarVim's default keymappings (not including which-key)" - if mode then - print(vim.inspect(options.keys[mode])) - else - print(vim.inspect(options.keys)) - end +-- Get the default keymappings +function M.get_defaults() + return defaults end return M -- cgit v1.2.3-70-g09d2