ref(Layer): move `replace_termcodes` to `utils.api`

It is a utility function for an API function, so it belongs here.
pull/17/head release/3.2.2
Iron-E 2 years ago
parent 6909e1a2cd
commit 4a31e56e63
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22

@ -1,4 +1,5 @@
local globals = require 'libmodal/src/globals'
local utils = require 'lilbmodal/src/utils'
--- Normalizes a `buffer = true|false|0` argument into a number.
--- @param buffer boolean|number the argument to normalize
@ -38,12 +39,6 @@ local function normalize_keymap(keymap)
return keymap
end
--- @param key string
--- @return string
local function replace_termcodes(key)
return vim.api.nvim_replace_termcodes(key, true, true, true)
end
--- remove and return the right-hand side of a `keymap`.
--- @param keymap table the keymap to unpack
--- @return function|string rhs, table options
@ -114,7 +109,7 @@ end
--- @param options table options for the keymap.
--- @see `vim.keymap.set`
function Layer:map(mode, lhs, rhs, options)
lhs = replace_termcodes(lhs)
lhs = utils.api.replace_termcodes(lhs)
options.buffer = normalize_buffer(options.buffer)
if self.existing_keymaps_by_mode then -- the layer has been activated
if not self.existing_keymaps_by_mode[mode] then -- this is the first time that a keymap with this mode is being set
@ -127,7 +122,7 @@ function Layer:map(mode, lhs, rhs, options)
vim.api.nvim_buf_get_keymap(options.buffer, mode) or
vim.api.nvim_get_keymap(mode)
) do -- check if this keymap will overwrite something
if replace_termcodes(existing_keymap.lhs) == lhs then -- mapping this will overwrite something; log the old mapping
if utils.api.replace_termcodes(existing_keymap.lhs) == lhs then -- mapping this will overwrite something; log the old mapping
self.existing_keymaps_by_mode[mode][lhs] = normalize_keymap(existing_keymap)
break
end
@ -152,7 +147,7 @@ end
--- @param lhs string the keys which invoke the keymap.
--- @see `vim.api.nvim_del_keymap`
function Layer:unmap(buffer, mode, lhs)
lhs = replace_termcodes(lhs)
lhs = utils.api.replace_termcodes(lhs)
if self.existing_keymaps_by_mode then
if self.existing_keymaps_by_mode[mode][lhs] then -- there is an older keymap to go back to; restore it
local rhs, options = unpack_keymap_rhs(self.existing_keymaps_by_mode[mode][lhs])

@ -37,4 +37,10 @@ function api.redraw()
vim.api.nvim_command 'mode'
end
--- @param termcodes string
--- @return string replaced
function api.replace_termcodes(termcodes)
return vim.api.nvim_replace_termcodes(termcodes, true, true, true)
end
return api

Loading…
Cancel
Save