75 lines
1.9 KiB
Lua
75 lines
1.9 KiB
Lua
local M = {}
|
|
|
|
local reference_filename = "KEYMAPS.md"
|
|
local reference_path = vim.fn.stdpath("config") .. "/" .. reference_filename
|
|
|
|
local function read_reference()
|
|
local lines = {}
|
|
local fd = io.open(reference_path, "r")
|
|
if not fd then
|
|
return {
|
|
("Keymap reference file not found: %s"):format(reference_path),
|
|
"Create KEYMAPS.md to document mappings.",
|
|
}
|
|
end
|
|
|
|
for line in fd:lines() do
|
|
table.insert(lines, line)
|
|
end
|
|
fd:close()
|
|
|
|
if #lines == 0 then
|
|
lines = { "KEYMAPS.md is empty." }
|
|
end
|
|
|
|
return lines
|
|
end
|
|
|
|
local function open_float(lines)
|
|
local buf = vim.api.nvim_create_buf(false, true)
|
|
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
|
|
|
|
vim.bo[buf].buftype = "nofile"
|
|
vim.bo[buf].bufhidden = "wipe"
|
|
vim.bo[buf].filetype = "markdown"
|
|
vim.bo[buf].swapfile = false
|
|
vim.bo[buf].modifiable = false
|
|
|
|
local width = 0
|
|
for _, line in ipairs(lines) do
|
|
width = math.max(width, vim.fn.strdisplaywidth(line))
|
|
end
|
|
width = math.min(math.max(width + 4, 60), math.floor(vim.o.columns * 0.9))
|
|
|
|
local height = math.min(#lines, math.floor(vim.o.lines * 0.9))
|
|
if height < 10 then
|
|
height = math.min(10, vim.o.lines - 2)
|
|
end
|
|
|
|
local win_opts = {
|
|
relative = "editor",
|
|
width = width,
|
|
height = height,
|
|
row = math.floor((vim.o.lines - height) / 2),
|
|
col = math.floor((vim.o.columns - width) / 2),
|
|
style = "minimal",
|
|
border = "rounded",
|
|
}
|
|
|
|
local win = vim.api.nvim_open_win(buf, true, win_opts)
|
|
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = buf, silent = true })
|
|
vim.keymap.set("n", "<Esc>", "<cmd>close<cr>", { buffer = buf, silent = true })
|
|
return win
|
|
end
|
|
|
|
function M.open_reference()
|
|
local lines = read_reference()
|
|
open_float(lines)
|
|
end
|
|
|
|
vim.api.nvim_create_user_command("KeymapsGuide", function()
|
|
M.open_reference()
|
|
end, { desc = "Show KEYMAPS.md in a floating window" })
|
|
|
|
return M
|