Skip to content

Commit 10b102f

Browse files
Merge pull request #319 from jgollenz/fix/multi-key-mappings
Add handling of multi-key mappings
2 parents b0649b5 + 44b106e commit 10b102f

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

lua/orgmode/config/init.lua

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,24 @@ function Config:setup_mappings(category)
208208

209209
for name, lhs in pairs(self.opts.mappings[category]) do
210210
if mappings[category] and mappings[category][name] and lhs then
211+
-- lhs
212+
local keys = {}
213+
if type(lhs) == 'string' or vim.tbl_islist(lhs) then
214+
-- no mode specified, default to normal-mode
215+
keys['n'] = lhs
216+
else
217+
keys = lhs
218+
end
219+
-- rhs
211220
local map = {}
212-
if type(mappings[category][name]) == 'table' and not vim.tbl_islist(mappings[category][name]) then
221+
if not type(mappings[category][name]) == 'table' or vim.tbl_islist(mappings[category][name]) then
222+
-- no mode specified, default to normal-mode
223+
map['n'] = {}
224+
map['n']['action'] = vim.tbl_map(function(i)
225+
return string.format('"%s"', i)
226+
end, mappings[category][name])
227+
map['n']['opts'] = {}
228+
else
213229
-- multi-mode mapping
214230
for mode, mapping in pairs(mappings[category][name]) do
215231
map[mode] = {}
@@ -228,33 +244,24 @@ function Config:setup_mappings(category)
228244
map[mode]['opts'] = {}
229245
end
230246
end
231-
else
232-
-- single-mode defaults to normal-mode
233-
map['n'] = {}
234-
map['n']['action'] = vim.tbl_map(function(i)
235-
return string.format('"%s"', i)
236-
end, mappings[category][name])
237-
map['n']['opts'] = {}
238247
end
239-
240-
local keys = {}
241-
if type(lhs) == 'string' then
242-
keys['n'] = lhs
243-
else
244-
keys = lhs
245-
end
246-
248+
-- register mappings
247249
for mode, key in pairs(keys) do
248-
local mapping = map[mode] or false
249-
if mapping then
250-
local action = table.concat(mapping['action'], ',')
251-
utils.buf_keymap(
252-
0,
253-
mode,
254-
key,
255-
string.format('<cmd>lua require("orgmode").action(%s)<CR>', action),
256-
mapping['opts']
257-
)
250+
if type(key) == 'string' then
251+
key = { key }
252+
end
253+
for _, k in pairs(key) do
254+
local mapping = map[mode] or false
255+
if mapping then
256+
local action = table.concat(mapping['action'], ',')
257+
utils.buf_keymap(
258+
0,
259+
mode,
260+
k,
261+
string.format('<cmd>lua require("orgmode").action(%s)<CR>', action),
262+
mapping['opts']
263+
)
264+
end
258265
end
259266
end
260267
end

0 commit comments

Comments
 (0)