Skip to content

Commit

Permalink
Add custom keymaps to :RMapsDesc
Browse files Browse the repository at this point in the history
Close #335
  • Loading branch information
jalvesaq committed Feb 23, 2025
1 parent 0a30857 commit f57858b
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions lua/r/maps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,45 @@ local map_desc = {
ROBToggle = { m = "", k = "", c = "Object_Browser", d = "Toggle the Object Browser" },
}

local map_it = function(plug, mode, plg, tgt, cmd, opts)
local map_list

local fill_map_list = function()
local plg = ""
local map = ""
map_list = { n = {}, v = {}, i = {}}
for _, mode in pairs({"n", "i", "v"}) do
local txt = vim.fn.execute(mode .. "map")
local lines = vim.split(txt, "\n")
for _, v in pairs(lines) do
if v:find("<Plug>R") and not v:find("^" .. mode .. " <Plug>R") then
plg = v:gsub(".*<Plug>R", "R")
plg = plg:gsub(" .*", "")
map = v:gsub("^" .. mode .. " ", "")
map = map:gsub(" .*", "")
table.insert(map_list[mode], { plg, map })
end
end
end
end

local get_map_to = function(plg, mode)
for _, v in pairs(map_list[mode]) do
if v[1] == plg then
return v[2]
end
end
return ""
end

local map_it = function(plug, mode, tgt, cmd, opts)
if not string.find(map_desc[plug].m, mode) then
map_desc[plug].m = map_desc[plug].m .. mode
end
vim.api.nvim_buf_set_keymap(0, mode, plg, tgt, opts)
vim.api.nvim_buf_set_keymap(0, mode, "<Plug>" .. plug, tgt, opts)

if vim.fn.hasmapto(plg, mode) == 1 then
map_desc[plug].k = "custom"
local key = get_map_to(plug, mode)
if key ~= "" then
map_desc[plug].k = key
return
end

Expand All @@ -100,7 +131,7 @@ local map_it = function(plug, mode, plg, tgt, cmd, opts)
return
end

vim.api.nvim_buf_set_keymap(0, mode, cmd, plg, opts)
vim.api.nvim_buf_set_keymap(0, mode, cmd, "<Plug>" .. plug, opts)
map_desc[plug].k = cmd
end

Expand All @@ -122,14 +153,13 @@ local create_maps = function(mode, plug, combo, target, esp)
end

local tgt = (esp and esp == 1) and target or target .. "<CR>"
local plg = "<Plug>" .. plug
local cmd = (esp and esp == 3) and combo or "<LocalLeader>" .. combo
local opts = { silent = true, noremap = true, expr = false, desc = map_desc[plug].d }

if mode:find("n") then map_it(plug, "n", plg, tgt, cmd, opts) end
if mode:find("v") then map_it(plug, "v", plg, tgt, cmd, opts) end
if mode:find("n") then map_it(plug, "n", tgt, cmd, opts) end
if mode:find("v") then map_it(plug, "v", tgt, cmd, opts) end
if (config.insert_mode_cmds or (esp and esp > 1)) and mode:find("i") then
map_it(plug, "i", plg, tgt, cmd, opts)
map_it(plug, "i", tgt, cmd, opts)
end
end

Expand Down Expand Up @@ -308,6 +338,7 @@ end
local M = {}

M.create = function(file_type)
if not map_list then fill_map_list() end
control(file_type)
if file_type == "rbrowser" then return end
send(file_type)
Expand Down

0 comments on commit f57858b

Please sign in to comment.