Skip to content

Commit f1fe5f2

Browse files
committed
Add support for multi-mode mappings
1 parent 1473218 commit f1fe5f2

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

lua/orgmode/config/init.lua

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,56 @@ function Config:setup_mappings(category)
202202
return
203203
end
204204

205-
for name, key in pairs(self.opts.mappings[category]) do
205+
for name, lhs in pairs(self.opts.mappings[category]) do
206206
if mappings[category] and mappings[category][name] then
207-
local map = vim.tbl_map(function(i)
208-
return string.format('"%s"', i)
209-
end, mappings[category][name])
210-
local keys = key
211-
if type(keys) == 'string' then
212-
keys = { keys }
207+
local map = {}
208+
if type(mappings[category][name]) == 'table' and not vim.tbl_islist(mappings[category][name]) then
209+
-- multi-mode mapping
210+
for mode, mapping in pairs(mappings[category][name]) do
211+
map[mode] = {}
212+
if type(mapping) == 'table' and not vim.tbl_islist(mapping) then
213+
local action = {}
214+
table.insert(action, mapping['callback'])
215+
vim.list_extend(action, mapping['args'])
216+
map[mode]['action'] = vim.tbl_map(function(i)
217+
return string.format('"%s"', i)
218+
end, action)
219+
map[mode]['opts'] = mapping['opts']
220+
else
221+
map[mode]['action'] = vim.tbl_map(function(i)
222+
return string.format('"%s"', i)
223+
end, mapping)
224+
map[mode]['opts'] = {}
225+
end
226+
end
227+
else
228+
-- single-mode defaults to normal-mode
229+
map['n'] = {}
230+
map['n']['action'] = vim.tbl_map(function(i)
231+
return string.format('"%s"', i)
232+
end, mappings[category][name])
233+
map['n']['opts'] = {}
213234
end
214-
for _, k in ipairs(keys) do
215-
utils.buf_keymap(
216-
0,
217-
'n',
218-
k,
219-
string.format('<cmd>lua require("orgmode").action(%s)<CR>', table.concat(map, ', '))
220-
)
235+
236+
local keys = {}
237+
if type(lhs) == 'string' then
238+
keys['n'] = lhs
239+
else
240+
keys = lhs
241+
end
242+
243+
for mode, key in pairs(keys) do
244+
local mapping = map[mode] or false
245+
if mapping then
246+
action = table.concat(mapping['action'], ', ')
247+
utils.buf_keymap(
248+
0,
249+
mode,
250+
key,
251+
string.format('<cmd>lua require("orgmode").action(%s)<CR>', action),
252+
mapping['opts']
253+
)
254+
end
221255
end
222256
end
223257
end

0 commit comments

Comments
 (0)