Skip to content

Commit c42e63c

Browse files
committedOct 5, 2023
feat(keys): you can now create buffer-local filetype keymaps by specifying ft=. Fixes #1076
1 parent 19d1b3a commit c42e63c

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ can be a `LazyKeys` table with the following key-value pairs:
147147
- **[1]**: (`string`) lhs **_(required)_**
148148
- **[2]**: (`string|fun()`) rhs **_(optional)_**
149149
- **mode**: (`string|string[]`) mode **_(optional, defaults to `"n"`)_**
150+
- **ft**: (`string|string[]`) `filetype` for buffer-local keymaps **_(optional)_**
150151
- any other option valid for `vim.keymap.set`
151152

152153
Key mappings will load the plugin the first time they get executed.

‎lua/lazy/core/handler/keys.lua

+51-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local Loader = require("lazy.core.loader")
99
---@field noremap? boolean
1010
---@field remap? boolean
1111
---@field expr? boolean
12+
---@field ft? string|string[]
1213
---@field id string
1314

1415
---@class LazyKeysHandler:LazyHandler
@@ -53,7 +54,7 @@ end
5354
function M.opts(keys)
5455
local opts = {}
5556
for k, v in pairs(keys) do
56-
if type(k) ~= "number" and k ~= "mode" and k ~= "id" then
57+
if type(k) ~= "number" and k ~= "mode" and k ~= "id" and k ~= "ft" then
5758
opts[k] = v
5859
end
5960
end
@@ -64,33 +65,62 @@ end
6465
function M:_add(keys)
6566
local lhs = keys[1]
6667
local opts = M.opts(keys)
67-
vim.keymap.set(keys.mode, lhs, function()
68-
local plugins = self.active[keys.id]
6968

70-
-- always delete the mapping immediately to prevent recursive mappings
71-
self:_del(keys)
72-
self.active[keys.id] = nil
69+
---@param buf? number
70+
local function add(buf)
71+
vim.keymap.set(keys.mode, lhs, function()
72+
local plugins = self.active[keys.id]
7373

74-
Util.track({ keys = lhs })
75-
Loader.load(plugins, { keys = lhs })
76-
Util.track()
74+
-- always delete the mapping immediately to prevent recursive mappings
75+
self:_del(keys, buf)
76+
self.active[keys.id] = nil
7777

78-
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
79-
-- insert instead of append the lhs
80-
vim.api.nvim_feedkeys(feed, "i", false)
81-
end, {
82-
desc = opts.desc,
83-
nowait = opts.nowait,
84-
-- we do not return anything, but this is still needed to make operator pending mappings work
85-
expr = true,
86-
})
78+
if plugins then
79+
Util.track({ keys = lhs })
80+
Loader.load(plugins, { keys = lhs })
81+
Util.track()
82+
end
83+
84+
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
85+
-- insert instead of append the lhs
86+
vim.api.nvim_feedkeys(feed, "i", false)
87+
end, {
88+
desc = opts.desc,
89+
nowait = opts.nowait,
90+
-- we do not return anything, but this is still needed to make operator pending mappings work
91+
expr = true,
92+
buffer = buf,
93+
})
94+
end
95+
96+
if keys.ft then
97+
vim.api.nvim_create_autocmd("FileType", {
98+
pattern = keys.ft,
99+
callback = function(event)
100+
if self.active[keys.id] then
101+
add(event.buf)
102+
else
103+
-- Only create the mapping if its managed by lazy
104+
-- otherwise the plugin is supposed to manage it
105+
if keys[2] then
106+
self:_del(keys, event.buf)
107+
end
108+
end
109+
end,
110+
})
111+
else
112+
add()
113+
end
87114
end
88115

89116
---@param keys LazyKeys
90-
function M:_del(keys)
91-
pcall(vim.keymap.del, keys.mode, keys[1])
117+
---@param buf number?
118+
function M:_del(keys, buf)
119+
pcall(vim.keymap.del, keys.mode, keys[1], { buffer = buf })
92120
if keys[2] then
93-
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys))
121+
local opts = M.opts(keys)
122+
opts.buffer = buf
123+
vim.keymap.set(keys.mode, keys[1], keys[2], opts)
94124
end
95125
end
96126

0 commit comments

Comments
 (0)
Please sign in to comment.