Skip to content

Commit 7b35162

Browse files
committed
feat: add default_mappings option and update docs
1 parent c74c22c commit 7b35162

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lua/better_escape.lua

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
local M = {}
2-
local uv = vim.uv
2+
local uv = vim.uv or vim.loop
33

44
M.waiting = false
55

66
local settings = {
77
timeout = vim.o.timeoutlen,
8+
default_mappings = true,
89
mappings = {
910
i = {
10-
-- first_key[s]
11+
-- first_key[s]
1112
j = {
12-
-- second_key[s]
13+
-- second_key[s]
1314
k = "<Esc>",
1415
j = "<Esc>",
1516
},
@@ -51,9 +52,9 @@ local function unmap_keys()
5152
end
5253

5354
-- WIP: move this into recorder.lua ?
54-
-- When a first_key is pressed, `recorded_key` is set to it
55+
-- When a first_key is pressed, `recorded_key` is set to it
5556
-- (e.g. if jk is a mapping, when 'j' is pressed, `recorded_key` is set to 'j')
56-
local recorded_key = nil
57+
local recorded_key = nil
5758
local bufmodified = nil
5859
local timeout_timer = uv.new_timer()
5960
local has_recorded = false -- See `vim.on_key` below
@@ -63,7 +64,7 @@ local function record_key(key)
6364
end
6465
bufmodified = vim.bo.modified
6566
recorded_key = key
66-
has_recorded = true
67+
has_recorded = true
6768
M.waiting = true
6869
timeout_timer:start(settings.timeout, 0, function()
6970
M.waiting = false
@@ -96,7 +97,7 @@ local function map_keys()
9697
return first_key
9798
end, map_opts)
9899
end
99-
for first_key, second_keys in pairs(first_keys) do
100+
for _, second_keys in pairs(first_keys) do
100101
for second_key, mapping in pairs(second_keys) do
101102
if not mapping then
102103
goto continue
@@ -110,7 +111,12 @@ local function map_keys()
110111
end
111112
-- If a key was recorded, but it isn't the first_key for second_key, record second_key(second_key might be a first_key for another sequence)
112113
-- Or if the recorded_key was just a second_key
113-
if not (first_keys[recorded_key] and first_keys[recorded_key][second_key]) then
114+
if
115+
not (
116+
first_keys[recorded_key]
117+
and first_keys[recorded_key][second_key]
118+
)
119+
then
114120
record_key(second_key)
115121
return second_key
116122
end
@@ -134,6 +140,9 @@ end
134140

135141
function M.setup(update)
136142
unmap_keys()
143+
if update and update.default_mappings == false then
144+
settings.mappings = {}
145+
end
137146
settings = vim.tbl_deep_extend("force", settings, update or {})
138147
if settings.keys or settings.clear_empty_lines then
139148
vim.notify(

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ The biggest change was that the `mapping` config option was removed. Check the
4545
default configuration below to see the new structure.
4646

4747
This also deprecated the `clear_empty_lines` setting. You can replicate this
48-
behavior with a function like this:
48+
behavior by setting a mapping to a function like this:
4949

5050
```lua
51+
-- `k` would be the second key of a mapping
5152
k = function()
5253
vim.api.nvim_input("<esc>")
5354
local current_line = vim.api.nvim_get_current_line()
@@ -81,7 +82,9 @@ i = {
8182
}
8283
```
8384

85+
### Disable mappings
8486
To disable keys set them to `false` in the configuration.
87+
You can also disable all default mappings by setting the `default_mappings` option to false.
8588

8689
<details>
8790
<summary>Default Config</summary>
@@ -90,6 +93,7 @@ To disable keys set them to `false` in the configuration.
9093
-- lua, default settings
9194
require("better_escape").setup {
9295
timeout = vim.o.timeoutlen,
96+
default_mappings = true,
9397
mappings = {
9498
i = {
9599
j = {

0 commit comments

Comments
 (0)