-- Use leader + m for `major`, inspired by emacs -- If under mode = 'n' / 'v': m and will be added by default -- If under `i` local bufmap = { markdown = { { mode = "x", keys = "i", cmd = 'c*"*', opt = { desc = "Add italic to selected text" } }, { mode = "x", keys = "b", cmd = 'c**"**', opt = { desc = "Add bold to selected text" } }, { mode = "x", keys = "c", cmd = 'c`"`', opt = { desc = "Add code block to selected text" } }, { mode = "x", keys = "d", cmd = 'c~~"~~', opt = { desc = "Add strikethrough to selected text" } }, { mode = "x", keys = "h", cmd = 'c=="==', opt = { desc = "Add highlight to selected text" } }, }, tex = { { mode = "x", keys = "i", cmd = 'c\\textit{"}', opt = { desc = "Add italic to selected text" } }, { mode = "x", keys = "b", cmd = 'c\\textbf{"}', opt = { desc = "Add bold to selected text" } }, { mode = "x", keys = "c", cmd = 'c\\begin{verbatim}"\\end{verbatim}', opt = { desc = "Add code block to selected text" }, }, { mode = "x", keys = "d", cmd = 'c\\sout{"}', opt = { desc = "Add strikethrough to selected text" } }, { mode = "x", keys = "h", cmd = 'c\\hl{"}', opt = { desc = "Add highlight to selected text" } }, { mode = "n", keys = "cc", cmd = "w", opt = { desc = "Save and compile tex file" } }, -- { mode = "i", keys = "", cmd = "w", opt = { desc = "Save and compile tex file" } }, }, org = { { mode = "x", keys = "i", cmd = 'c//"', opt = { desc = "Add italic to selected text" } }, { mode = "x", keys = "b", cmd = 'c*"*', opt = { desc = "Add bold to selected text" } }, -- { -- mode = "x", -- keys = "c", -- cmd = 'c#+BEGIN_SRC"#+END_SRC', -- opt = { desc = "Add code block to selected text" }, -- }, { mode = "x", keys = "d", cmd = 'c+"+', opt = { desc = "Add strikethrough to selected text" } }, { mode = "x", keys = "h", cmd = 'c~"~', opt = { desc = "Add highlight to selected text" } }, }, sh = { { mode = "n", keys = "mx", cmd = "!chmod u+x %", opt = { desc = "Mark the file as executable" } }, }, } -- 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 = "" .. 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 = "" .. mapping.keys end vim.keymap.set(mapping.mode, mapping.keys, mapping.cmd, opts) end end, }) end end setup_buffer_maps(bufmap)