keymappings.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. local generic_opts_any = { noremap = true, silent = true }
  4. local generic_opts = {
  5. insert_mode = generic_opts_any,
  6. normal_mode = generic_opts_any,
  7. visual_mode = generic_opts_any,
  8. visual_block_mode = generic_opts_any,
  9. command_mode = generic_opts_any,
  10. term_mode = { silent = true },
  11. }
  12. local mode_adapters = {
  13. insert_mode = "i",
  14. normal_mode = "n",
  15. term_mode = "t",
  16. visual_mode = "v",
  17. visual_block_mode = "x",
  18. command_mode = "c",
  19. }
  20. ---@class Keys
  21. ---@field insert_mode table
  22. ---@field normal_mode table
  23. ---@field terminal_mode table
  24. ---@field visual_mode table
  25. ---@field visual_block_mode table
  26. ---@field command_mode table
  27. local defaults = {
  28. insert_mode = {
  29. -- 'jk' for quitting insert mode
  30. ["jk"] = "<ESC>",
  31. -- 'kj' for quitting insert mode
  32. ["kj"] = "<ESC>",
  33. -- 'jj' for quitting insert mode
  34. ["jj"] = "<ESC>",
  35. -- Move current line / block with Alt-j/k ala vscode.
  36. ["<A-j>"] = "<Esc>:m .+1<CR>==gi",
  37. -- Move current line / block with Alt-j/k ala vscode.
  38. ["<A-k>"] = "<Esc>:m .-2<CR>==gi",
  39. -- navigation
  40. ["<A-Up>"] = "<C-\\><C-N><C-w>k",
  41. ["<A-Down>"] = "<C-\\><C-N><C-w>j",
  42. ["<A-Left>"] = "<C-\\><C-N><C-w>h",
  43. ["<A-Right>"] = "<C-\\><C-N><C-w>l",
  44. },
  45. normal_mode = {
  46. -- Better window movement
  47. ["<C-h>"] = "<C-w>h",
  48. ["<C-j>"] = "<C-w>j",
  49. ["<C-k>"] = "<C-w>k",
  50. ["<C-l>"] = "<C-w>l",
  51. -- Resize with arrows
  52. ["<C-Up>"] = ":resize -2<CR>",
  53. ["<C-Down>"] = ":resize +2<CR>",
  54. ["<C-Left>"] = ":vertical resize -2<CR>",
  55. ["<C-Right>"] = ":vertical resize +2<CR>",
  56. -- Tab switch buffer
  57. ["<S-l>"] = ":BufferLineCycleNext<CR>",
  58. ["<S-h>"] = ":BufferLineCyclePrev<CR>",
  59. -- Move current line / block with Alt-j/k a la vscode.
  60. ["<A-j>"] = ":m .+1<CR>==",
  61. ["<A-k>"] = ":m .-2<CR>==",
  62. -- QuickFix
  63. ["]q"] = ":cnext<CR>",
  64. ["[q"] = ":cprev<CR>",
  65. ["<C-q>"] = ":call QuickFixToggle()<CR>",
  66. },
  67. term_mode = {
  68. -- Terminal window navigation
  69. ["<C-h>"] = "<C-\\><C-N><C-w>h",
  70. ["<C-j>"] = "<C-\\><C-N><C-w>j",
  71. ["<C-k>"] = "<C-\\><C-N><C-w>k",
  72. ["<C-l>"] = "<C-\\><C-N><C-w>l",
  73. },
  74. visual_mode = {
  75. -- Better indenting
  76. ["<"] = "<gv",
  77. [">"] = ">gv",
  78. -- ["p"] = '"0p',
  79. -- ["P"] = '"0P',
  80. },
  81. visual_block_mode = {
  82. -- Move selected line / block of text in visual mode
  83. ["K"] = ":move '<-2<CR>gv-gv",
  84. ["J"] = ":move '>+1<CR>gv-gv",
  85. -- Move current line / block with Alt-j/k ala vscode.
  86. ["<A-j>"] = ":m '>+1<CR>gv-gv",
  87. ["<A-k>"] = ":m '<-2<CR>gv-gv",
  88. },
  89. command_mode = {
  90. -- navigate tab completion with <c-j> and <c-k>
  91. -- runs conditionally
  92. ["<C-j>"] = { 'pumvisible() ? "\\<C-n>" : "\\<C-j>"', { expr = true, noremap = true } },
  93. ["<C-k>"] = { 'pumvisible() ? "\\<C-p>" : "\\<C-k>"', { expr = true, noremap = true } },
  94. },
  95. }
  96. if vim.fn.has "mac" == 1 then
  97. defaults.normal_mode["<A-Up>"] = defaults.normal_mode["<C-Up>"]
  98. defaults.normal_mode["<A-Down>"] = defaults.normal_mode["<C-Down>"]
  99. defaults.normal_mode["<A-Left>"] = defaults.normal_mode["<C-Left>"]
  100. defaults.normal_mode["<A-Right>"] = defaults.normal_mode["<C-Right>"]
  101. Log:debug "Activated mac keymappings"
  102. end
  103. -- Unsets all keybindings defined in keymaps
  104. -- @param keymaps The table of key mappings containing a list per mode (normal_mode, insert_mode, ..)
  105. function M.clear(keymaps)
  106. local default = M.get_defaults()
  107. for mode, mappings in pairs(keymaps) do
  108. local translated_mode = mode_adapters[mode] or mode
  109. for key, _ in pairs(mappings) do
  110. -- some plugins may override default bindings that the user hasn't manually overridden
  111. if default[mode][key] ~= nil or (default[translated_mode] ~= nil and default[translated_mode][key] ~= nil) then
  112. pcall(vim.keymap.del, translated_mode, key)
  113. end
  114. end
  115. end
  116. end
  117. -- Set key mappings individually
  118. -- @param mode The keymap mode, can be one of the keys of mode_adapters
  119. -- @param key The key of keymap
  120. -- @param val Can be form as a mapping or tuple of mapping and user defined opt
  121. function M.set_keymaps(mode, key, val)
  122. local opt = generic_opts[mode] or generic_opts_any
  123. if type(val) == "table" then
  124. opt = val[2]
  125. val = val[1]
  126. end
  127. if val then
  128. vim.keymap.set(mode, key, val, opt)
  129. else
  130. pcall(vim.api.nvim_del_keymap, mode, key)
  131. end
  132. end
  133. -- Load key mappings for a given mode
  134. -- @param mode The keymap mode, can be one of the keys of mode_adapters
  135. -- @param keymaps The list of key mappings
  136. function M.load_mode(mode, keymaps)
  137. mode = mode_adapters[mode] or mode
  138. for k, v in pairs(keymaps) do
  139. M.set_keymaps(mode, k, v)
  140. end
  141. end
  142. -- Load key mappings for all provided modes
  143. -- @param keymaps A list of key mappings for each mode
  144. function M.load(keymaps)
  145. keymaps = keymaps or {}
  146. for mode, mapping in pairs(keymaps) do
  147. M.load_mode(mode, mapping)
  148. end
  149. end
  150. -- Load the default keymappings
  151. function M.load_defaults()
  152. M.load(M.get_defaults())
  153. lvim.keys = lvim.keys or {}
  154. for idx, _ in pairs(defaults) do
  155. if not lvim.keys[idx] then
  156. lvim.keys[idx] = {}
  157. end
  158. end
  159. end
  160. -- Get the default keymappings
  161. function M.get_defaults()
  162. return defaults
  163. end
  164. return M