Skip to content

Commit

Permalink
Metatable shennigans for optional map arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
KillTheMule committed Jul 21, 2018
1 parent 8fa9c44 commit 12a3583
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion runtime/lua/testplugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ end

local function init()
plugin:map("<F2>", stuff)
plugin:map("<buffer> <", stuff2)
plugin.map = plugin.map.buffer.silent
plugin:map("<", stuff2)
end

return {
Expand Down
20 changes: 18 additions & 2 deletions runtime/lua/tycho/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ local command = nvim.nvim_command

local messages = {}
local functions = {}
local map_args = " "

local function map_to_ns(keys, ns)
map_args = (map_args == " " and map_args) or map_args.." "
local keys_rhs = keys:gsub("<", "<lt>")
local rhs = "lua require('tycho').functions["..
tostring(ns).."]['"..keys_rhs.."']()"
table.insert(messages, { rhs = rhs })
command("map "..keys.." <Cmd>"..rhs.."<CR>")
command("map"..map_args..keys.." <Cmd>"..rhs.."<CR>")
map_args = " "
end

local function debug()
Expand All @@ -21,8 +24,9 @@ local function debug()
command("put =tycho_fns")
end

local map = {}

local function map(self, keys, fn)
local function fn_map(self, keys, fn)
assert(type(keys) == "string", "Must pass a string as 2nd argument to map")
assert(type(fn) == "function", "Must pass a function as 2nd argument to map")

Expand All @@ -40,6 +44,18 @@ local function map(self, keys, fn)
map_to_ns(keys, ns)
end

local function map_index(k)
if k == "buffer" then
map_args = map_args.."<buffer>"
elseif k == "silent" then
map_args = map_args.."<silent>"
end
return map
end

setmetatable(map, { __call = function(self, plugin, keys, fn) return fn_map(plugin, keys, fn) end,
__index = function(self, k) return map_index(k) end })

local function new_plugin(name)
-- TODO(KillTheMule): This code assumes that subsequent calls with the same
-- name return the same ns. Make sure of that, or adapt
Expand Down

0 comments on commit 12a3583

Please sign in to comment.