@@ 1,19 1,31 @@
local Patch = {}
--- Check if email is a patch; email is a patch is the subject line contains PATCH
+-- Check if email is a patch; email is a patch if the subject line contains PATCH
local function check_patch()
- local result = vim.fn.search("^Subject:.*PATCH.*", "nw")
+ local result = vim.fn.search("^Subject:.*PATCH.*$", "nw")
if result == 0 then
return false
end
return true
end
+-- Check if header already exists
+local function check_header()
+ local result = vim.fn.search("^X-Sourcehut-Patchset-Update.*$", "nw")
+ return result
+end
+
local function set_header(status)
if not check_patch() then
return
end
+ -- In case the header already exists, delete it
+ local header_position = check_header()
+ if header_position ~= 0 then
+ vim.api.nvim_buf_set_lines(0, header_position - 1, header_position, true, { nil })
+ end
+
-- Move cursor to file start and save position
local cursor_position = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_win_set_cursor(0, { 1, 0 })