|
5 | 5 | -- Sets up default keymaps.
|
6 | 6 | --
|
7 | 7 |
|
| 8 | +local api = vim.api |
8 | 9 | local config = require("papis.config")
|
9 | 10 |
|
10 | 11 | local M = {}
|
11 | 12 |
|
12 | 13 | ---@class PapisKeymaps
|
13 | 14 | ---@type table<string, table>
|
14 |
| -local keymaps = {} |
| 15 | +local keymaps_tbl = {} |
| 16 | + |
| 17 | +local function create_keymaps() |
| 18 | + for _, module_keymaps in pairs(keymaps_tbl) do |
| 19 | + for _, keymap in pairs(module_keymaps) do |
| 20 | + local opts = vim.deepcopy(keymap.opts) |
| 21 | + opts.silent = true |
| 22 | + opts.buffer = true |
| 23 | + vim.keymap.set(keymap.mode, keymap.lhs, keymap.rhs, opts) |
| 24 | + end |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +---Creates the `autocmd` that starts papis.nvim when configured conditions are fulfilled |
| 29 | +local function make_keymap_autocmd() |
| 30 | + local create_papis_keymap = api.nvim_create_augroup("createPapisKeymap", { clear = true }) |
| 31 | + api.nvim_create_autocmd("FileType", { |
| 32 | + pattern = config.init_filetypes, |
| 33 | + callback = create_keymaps, |
| 34 | + group = create_papis_keymap, |
| 35 | + desc = "Set Papis keymap", |
| 36 | + }) |
| 37 | +end |
15 | 38 |
|
16 | 39 | ---Sets up the keymaps for all enabled modules
|
17 | 40 | function M:setup()
|
18 |
| - self:add_keymaps(keymaps) |
| 41 | + -- create keymaps for the buffer when papis is first started |
| 42 | + create_keymaps() |
| 43 | + -- creates keymaps for all subsequent buffers |
| 44 | + make_keymap_autocmd() |
19 | 45 | end
|
20 | 46 |
|
21 | 47 | --- Recursively merges the provided table with the keymaps table.
|
22 | 48 | ---@param module_keymaps table #A table with a module's keymaps
|
23 | 49 | function M:add_keymaps(module_keymaps)
|
24 | 50 | if config.enable_keymaps then
|
25 |
| - for _, keymap in pairs(module_keymaps) do |
26 |
| - local opts = vim.deepcopy(keymap.opts) |
27 |
| - opts.silent = true |
28 |
| - opts.buffer = true |
29 |
| - vim.keymap.set(keymap.mode, keymap.lhs, keymap.rhs, opts) |
30 |
| - end |
| 51 | + table.insert(keymaps_tbl, module_keymaps) |
31 | 52 | end
|
32 | 53 | end
|
33 | 54 |
|
|
0 commit comments