Skip to content

Commit e427131

Browse files
authored
feat: insert_template_after_cursor option (#130)
Add `insert_template_after_cursor` option. This option make it possible to bind `<leader>p` to paste after and `<leader>P` to paste before.
1 parent 08a02e1 commit e427131

File tree

5 files changed

+8
-3
lines changed

5 files changed

+8
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ The plugin is highly configurable. Please refer to the default configuration bel
9898
relative_template_path = true, ---@type boolean | fun(): boolean
9999
use_cursor_in_template = true, ---@type boolean | fun(): boolean
100100
insert_mode_after_paste = true, ---@type boolean | fun(): boolean
101+
insert_template_after_cursor = true, ---@type boolean | fun(): boolean
101102

102103
-- prompt options
103104
prompt_for_file_name = true, ---@type boolean | fun(): boolean

lua/img-clip/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ local defaults = {
2626
relative_template_path = true, ---@type boolean
2727
use_cursor_in_template = true, ---@type boolean
2828
insert_mode_after_paste = true, ---@type boolean
29+
insert_template_after_cursor = true, ---@type boolean
2930

3031
-- prompt options
3132
prompt_for_file_name = true, ---@type boolean

lua/img-clip/markup.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ end
2525
---@return string matched_line The matched line in the tempalte (or the last line if no match)
2626
---@return number matched_line_index The index of the matched line
2727
function M.get_new_cursor_row(cur_row, lines)
28+
local delta = config.get_opt("insert_template_after_cursor") and 0 or -1
2829
for i, line in ipairs(lines) do
2930
if line:match("$CURSOR") then
30-
return cur_row + i, line, i
31+
return cur_row + i + delta, line, i
3132
end
3233
end
3334

34-
return cur_row + #lines, lines[#lines], #lines
35+
return cur_row + #lines + delta, lines[#lines], #lines
3536
end
3637

3738
---@param line string
@@ -135,7 +136,7 @@ function M.insert_markup(input, is_file_path)
135136
lines[index] = line:gsub("$CURSOR", "")
136137

137138
-- paste lines and place cursor in correct position
138-
vim.api.nvim_put(lines, "l", true, true)
139+
vim.api.nvim_put(lines, "l", config.get_opt("insert_template_after_cursor"), true)
139140
vim.api.nvim_win_set_cursor(0, { new_row, new_col })
140141

141142
-- enter insert mode if configured

tests/config_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe("config", function()
2222
assert.is_false(config.get_opt("show_dir_path_in_prompt"))
2323
assert.is_true(config.get_opt("use_cursor_in_template"))
2424
assert.is_true(config.get_opt("insert_mode_after_paste"))
25+
assert.is_true(config.get_opt("insert_template_after_cursor"))
2526
assert.is_false(config.get_opt("embed_image_as_base64"))
2627
assert.equal(10, config.get_opt("max_base64_size"))
2728
assert.equals("$FILE_PATH", config.get_opt("template"))

vimdoc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The plugin is highly configurable. Please refer to the default configuration bel
101101
relative_template_path = true, ---@type boolean | fun(): boolean
102102
use_cursor_in_template = true, ---@type boolean | fun(): boolean
103103
insert_mode_after_paste = true, ---@type boolean | fun(): boolean
104+
insert_template_after_cursor = true, ---@type boolean | fun(): boolean
104105

105106
-- prompt options
106107
prompt_for_file_name = true, ---@type boolean | fun(): boolean

0 commit comments

Comments
 (0)