1+ local globals = require ' libmodal/src/globals'
2+
13--- Normalizes a `buffer = true|false|0` argument into a number.
24--- @param buffer boolean | number the argument to normalize
35--- @return nil | number
@@ -11,6 +13,29 @@ local function normalize_buffer(buffer)
1113 return buffer
1214end
1315
16+ --- Normalizes a keymap from `vim.api.nvim_get_keymap` so it can be passed to `vim.keymap.set`
17+ --- @param keymap table
18+ --- @return table normalized
19+ local function normalize_keymap (keymap )
20+ -- `buffer == 0` just means "not a buffer mapping"
21+ keymap .buffer = keymap .buffer > 0 and keymap .buffer or nil
22+
23+ -- Keys which are `v:true` or `v:false`
24+ keymap .expr = globals .is_true (keymap .expr )
25+ keymap .noremap = globals .is_true (keymap .noremap )
26+ keymap .nowait = globals .is_true (keymap .nowait )
27+ keymap .silent = globals .is_true (keymap .silent )
28+
29+ -- Keys which should not exist
30+ keymap .lhs = nil
31+ keymap .lnum = nil
32+ keymap .script = nil
33+ keymap .sid = nil
34+ keymap .mode = nil
35+
36+ return keymap
37+ end
38+
1439--- remove and return the right-hand side of a `keymap`.
1540--- @param keymap table the keymap to unpack
1641--- @return function | string rhs , table options
@@ -84,8 +109,7 @@ function Layer:map(mode, lhs, rhs, options)
84109 vim .api .nvim_get_keymap (mode )
85110 ) do -- check if this keymap will overwrite something
86111 if existing_keymap .lhs == lhs then -- mapping this will overwrite something; log the old mapping
87- existing_keymap .lhs = nil
88- self .existing_keymaps_by_mode [mode ][lhs ] = existing_keymap
112+ self .existing_keymaps_by_mode [mode ][lhs ] = normalize_keymap (existing_keymap )
89113 break
90114 end
91115 end
0 commit comments