Skip to content

Commit

Permalink
feat: safe call keymap.del (#26)
Browse files Browse the repository at this point in the history
* feat: add `pcall` to each `del` method

* docs: update docs
  • Loading branch information
Wansmer authored Mar 29, 2024
1 parent c2fba2f commit 3d5c9e8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ and `vim.keymap.set('n', '<leader>ф', ':echo 123')`.

`lhs` with `<Plug>`, `<Sid>` and `<Snr>` will not translate and will be mapped as is.

`del()` works in the same way, but with mappings removing.
`del()` works in the same way, but with mappings removing. Also, `del()` is
wrapped with a safetely call (`pcall`) to avoid errors on duplicate characters
(helpful when using`nvim-cmp`).

```lua
---@param mode string|table Same mode short names as |nvim_set_keymap()|
Expand Down
8 changes: 4 additions & 4 deletions lua/langmapper/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ end
---@param mode string Mode short-name
---@param lhs string Left-hand-side |{lhs}| of the mapping.
function M.wrap_nvim_del_keymap(mode, lhs)
M.original_del_keymap(mode, lhs)
pcall(M.original_del_keymap, mode, lhs)
-- Delete translated mapping for each langs in config.use_layouts
for _, lang in ipairs(config.config.use_layouts) do
local tr_lhs = u.translate_keycode(lhs, lang)
local has = vim.fn.maparg(tr_lhs, mode) ~= ''

if tr_lhs ~= lhs and has then
M.original_del_keymap(mode, tr_lhs)
pcall(M.original_del_keymap, mode, tr_lhs)
end
end
end
Expand All @@ -148,14 +148,14 @@ end
---@param mode string Mode short-name
---@param lhs string Left-hand-side |{lhs}| of the mapping.
function M.wrap_nvim_buf_del_keymap(buffer, mode, lhs)
M.original_buf_del_keymap(buffer, mode, lhs)
pcall(M.original_buf_del_keymap, buffer, mode, lhs)
-- Delete translated mapping for each langs in config.use_layouts
for _, lang in ipairs(config.config.use_layouts) do
local tr_lhs = u.translate_keycode(lhs, lang)
local has = vim.fn.maparg(tr_lhs, mode) ~= ''

if tr_lhs ~= lhs and has then
M.original_buf_del_keymap(buffer, mode, tr_lhs)
pcall(M.original_buf_del_keymap, buffer, mode, tr_lhs)
end
end
end
Expand Down

0 comments on commit 3d5c9e8

Please sign in to comment.