@@ -11,7 +11,6 @@ local function setup_keymap(mode, key, cmd, desc, opts)
11
11
if key and key ~= " " then
12
12
opts = opts or {}
13
13
opts .desc = desc
14
- opts .buffer = true
15
14
vim .keymap .set (mode , key , cmd , opts )
16
15
end
17
16
end
21
20
--- @param commands_enabled table Command enable configuration
22
21
--- @param file_types string[] List of file types to apply keymaps to
23
22
function M .setup_keymaps (keymaps , commands_enabled , file_types )
24
- -- Create a dedicated augroup for keymaps
25
- local augroup = vim .api .nvim_create_augroup (" MarkdownShortcutsKeymaps" , { clear = true })
23
+ -- Register the global keymap for create_from_template first
24
+ if commands_enabled .create_from_template then
25
+ setup_keymap (
26
+ " n" , -- Mode
27
+ keymaps .create_from_template , -- Key
28
+ " <cmd>MarkdownNewTemplate<CR>" , -- Command
29
+ " Create from template" , -- Description
30
+ {} -- Global keymap, no buffer option needed
31
+ )
32
+ end
33
+
34
+ -- Create a dedicated augroup for buffer-local keymaps
35
+ local augroup = vim .api .nvim_create_augroup (" MarkdownShortcutsBufferKeymaps" , { clear = true })
26
36
27
37
vim .api .nvim_create_autocmd (" FileType" , {
28
38
group = augroup ,
29
39
pattern = file_types , -- Use the configured file types
30
40
callback = function ()
31
- -- Define keymap configurations
41
+ -- Define keymap configurations (excluding create_from_template)
32
42
local keymap_configs = {
33
- {
34
- command_key = " create_from_template" ,
35
- mode = " n" ,
36
- key = keymaps .create_from_template ,
37
- cmd = " <cmd>MarkdownNewTemplate<CR>" ,
38
- desc = " Create from template" ,
39
- },
40
43
{
41
44
command_key = " insert_header" ,
42
45
mode = { " n" , " v" },
@@ -177,7 +180,7 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
177
180
command_key = " insert_checkbox" ,
178
181
mode = " v" , -- Visual mode only
179
182
key = keymaps .insert_checkbox ,
180
- -- Call the Lua function directly, which handles line insertion
183
+ -- Call the Lua function directly, which handles line insertion
181
184
cmd = function ()
182
185
-- Exit visual mode before calling the command
183
186
vim .api .nvim_feedkeys (vim .api .nvim_replace_termcodes (' <Esc>' , true , false , true ), ' n' , false )
@@ -202,11 +205,12 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
202
205
},
203
206
}
204
207
205
- -- Apply all keymap configurations
208
+ -- Apply all buffer-local keymap configurations
206
209
for _ , config in ipairs (keymap_configs ) do
207
210
-- Only set keymap if the command is enabled
208
211
if commands_enabled [config .command_key ] then
209
212
-- Combine base opts with config-specific opts
213
+ -- Add buffer = true for these buffer-local keymaps
210
214
local base_opts = { desc = config .desc , buffer = true }
211
215
local final_opts = vim .tbl_extend (" force" , base_opts , config .opts or {})
212
216
@@ -215,7 +219,7 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
215
219
end
216
220
end
217
221
218
- -- Add keymap for continuing lists on Enter if enabled
222
+ -- Add keymap for continuing lists on Enter if enabled (buffer-local)
219
223
if require (" markdown-tools.config" ).options .continue_lists_on_enter then
220
224
vim .keymap .set (" i" , " <CR>" , function ()
221
225
local line = vim .api .nvim_get_current_line ()
0 commit comments