dotfiles/home/dot_config/nvim/lua/keymaps/buffer.lua
2025-11-13 07:30:00 +00:00

96 lines
3.9 KiB
Lua

-- Use leader + m for `major`, inspired by emacs
-- If under mode = 'n' / 'v': <leader>m and <localleader> will be added by default
-- If under `i`
local bufmap = {
markdown = {
{ mode = "x", keys = "i", cmd = 'c*<C-r>"*', opt = { desc = "Add italic to selected text" } },
{ mode = "x", keys = "b", cmd = 'c**<C-r>"**', opt = { desc = "Add bold to selected text" } },
{ mode = "x", keys = "c", cmd = 'c`<CR><C-r>"<CR>`', opt = { desc = "Add code block to selected text" } },
{ mode = "x", keys = "d", cmd = 'c~~<C-r>"~~', opt = { desc = "Add strikethrough to selected text" } },
{ mode = "x", keys = "h", cmd = 'c==<C-r>"==', opt = { desc = "Add highlight to selected text" } },
},
tex = {
{ mode = "x", keys = "i", cmd = 'c\\textit{<C-r>"}', opt = { desc = "Add italic to selected text" } },
{ mode = "x", keys = "b", cmd = 'c\\textbf{<C-r>"}', opt = { desc = "Add bold to selected text" } },
{
mode = "x",
keys = "c",
cmd = 'c\\begin{verbatim}<CR><C-r>"<CR>\\end{verbatim}',
opt = { desc = "Add code block to selected text" },
},
{ mode = "x", keys = "d", cmd = 'c\\sout{<C-r>"}', opt = { desc = "Add strikethrough to selected text" } },
{ mode = "x", keys = "h", cmd = 'c\\hl{<C-r>"}', opt = { desc = "Add highlight to selected text" } },
{ mode = "n", keys = "<leader>cc", cmd = "<cmd>w<CR>", opt = { desc = "Save and compile tex file" } },
-- { mode = "i", keys = "<C-m><C-m>", cmd = "<cmd>w<CR>", opt = { desc = "Save and compile tex file" } },
},
org = {
{ mode = "x", keys = "i", cmd = 'c/<C-r>/"', opt = { desc = "Add italic to selected text" } },
{ mode = "x", keys = "b", cmd = 'c*<C-r>"*', opt = { desc = "Add bold to selected text" } },
-- {
-- mode = "x",
-- keys = "c",
-- cmd = 'c#+BEGIN_SRC<CR><C-r>"<CR>#+END_SRC',
-- opt = { desc = "Add code block to selected text" },
-- },
{ mode = "x", keys = "d", cmd = 'c+<C-r>"+', opt = { desc = "Add strikethrough to selected text" } },
{ mode = "x", keys = "h", cmd = 'c~<C-r>"~', opt = { desc = "Add highlight to selected text" } },
},
sh = {
{ mode = "n", keys = "<leader>mx", cmd = "<cmd>!chmod u+x %<CR>", opt = { desc = "Mark the file as executable" } },
},
json = {
{ mode = "n", keys = "<leader>mp", cmd = "<cmd>%!jq", opt = { desc = "Prettify json" } },
},
}
-- Shallow copy
local function extend(tbl1, tbl2)
local t = {}
for _, v in ipairs(tbl1 or {}) do
table.insert(t, v)
end
for _, v in ipairs(tbl2 or {}) do
table.insert(t, v)
end
return t
end
for _, ft in ipairs({ "bash", "zsh", "fish", "python" }) do
bufmap[ft] = extend(bufmap[ft], bufmap.sh)
end
local function setup_buffer_maps(buffer_map)
-- 遍历 buffer_map 中的每个文件类型
for ft, mappings in pairs(buffer_map) do
-- 1. 为现有缓冲区设置键位映射
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_get_option(buf, "filetype") == ft then
for _, mapping in ipairs(mappings) do
-- 合并选项,添加 buffer 以确保映射是缓冲区局部的
if mapping.mode ~= "n" then
mapping.keys = "<C-m>" .. mapping.keys
end
local opts = vim.tbl_extend("force", mapping.opt, { buffer = buf })
vim.keymap.set(mapping.mode, mapping.keys, mapping.cmd, opts)
end
end
end
-- 2. 为未来缓冲区设置自动命令
vim.api.nvim_create_autocmd("FileType", {
pattern = ft, -- 匹配文件类型,例如 "markdown"
callback = function(args)
local buf = args.buf -- 获取触发事件的缓冲区号
for _, mapping in ipairs(mappings) do
local opts = vim.tbl_extend("force", mapping.opt, { buffer = buf })
if mapping.mode ~= "n" then
mapping.keys = "<C-m>" .. mapping.keys
end
vim.keymap.set(mapping.mode, mapping.keys, mapping.cmd, opts)
end
end,
})
end
end
setup_buffer_maps(bufmap)