Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ and trees

- Modular, maintainable pure Lua way to define keymaps
- Written in a single file of ~100 lines
- Supports mapping keys to Lua functions with `expr` support
- Allows grouping keymaps the way you think about them concisely

## Installation
Expand Down Expand Up @@ -110,7 +109,7 @@ You can change the defaults used by `applyKeymaps`:
local nest = require('nest')

nest.defaults.options = {
noremap = false,
remap = true,
}
```

Expand All @@ -122,7 +121,6 @@ Defaults start out as
prefix = '',
buffer = false,
options = {
noremap = true,
silent = true,
},
}
Expand Down Expand Up @@ -184,7 +182,7 @@ to all containing sub-`keymapConfig`s:
Sets the Vim mode(s) for keymaps contained in the `keymapConfig`.

Accepts a string with each char representing a mode. The modes follow the Vim
prefixes (`n` for normal, `i` for insert...) **except the special character `_`**, which
prefixes (`n` for normal, `i` for insert...) **except the special character `_`**, which
stands for the empty mode (`:map `). See `:help map-table` for a reference.

#### `buffer`
Expand All @@ -204,8 +202,8 @@ the `keymapConfig`.
that you only have to pass the `options` you want to change instead of replacing
the whole object.

Accepts all values `nvim_set_keymap`s `options` parameter accepts. See `:help
nvim_set_keymap`.
Accepts all values `vim.keymap.set`s `options` parameter accepts. See `:help
vim.keymap.set`.

### `nest.defaults`

Expand Down
99 changes: 15 additions & 84 deletions lua/nest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,19 @@ module.defaults = {
prefix = '',
buffer = false,
options = {
noremap = true,
silent = true,
},
}

local rhsFns = {}

module._callRhsFn = function(index)
rhsFns[index]()
end

module._getRhsExpr = function(index)
local keys = rhsFns[index]()

return vim.api.nvim_replace_termcodes(keys, true, true, true)
end

local function functionToRhs(func, expr)
table.insert(rhsFns, func)

local insertedIndex = #rhsFns

return expr
and 'v:lua.package.loaded.nest._getRhsExpr(' .. insertedIndex .. ')'
or '<cmd>lua package.loaded.nest._callRhsFn(' .. insertedIndex .. ')<cr>'
end

local function copy(table)
local ret = {}

for key, value in pairs(table) do
ret[key] = value
end

return ret
end

local function mergeTables(left, right)
local ret = copy(left)

for key, value in pairs(right) do
ret[key] = value
end

return ret
end

local function mergeOptions(left, right)
local ret = copy(left)

if right == nil then
return ret
end

if right.mode ~= nil then
ret.mode = right.mode
return left or {}
end

if right.buffer ~= nil then
ret.buffer = right.buffer
end
local ret = vim.tbl_deep_extend("force", left, right) or {}

if right.prefix ~= nil then
ret.prefix = ret.prefix .. right.prefix
end

if right.options ~= nil then
ret.options = mergeTables(ret.options, right.options)
ret.prefix = left.prefix .. right.prefix
end

return ret
Expand Down Expand Up @@ -107,36 +52,22 @@ module.applyKeymaps = function (config, presets)
return
end

local rhs = type(second) == 'function'
and functionToRhs(second, mergedPresets.options.expr)
or second

local sanitizedMode = {}
for mode in string.gmatch(mergedPresets.mode, '.') do
local sanitizedMode = mode == '_'
local sMode = mode == '_'
and ''
or mode

if mergedPresets.buffer then
local buffer = (mergedPresets.buffer == true)
and 0
or mergedPresets.buffer

vim.api.nvim_buf_set_keymap(
buffer,
sanitizedMode,
mergedPresets.prefix,
rhs,
mergedPresets.options
)
else
vim.api.nvim_set_keymap(
sanitizedMode,
mergedPresets.prefix,
rhs,
mergedPresets.options
)
end
table.insert(sanitizedMode, sMode)
end

mergedPresets.options.buffer = mergedPresets.buffer

vim.keymap.set(
sanitizedMode,
mergedPresets.prefix,
second,
mergedPresets.options
)
end

return module