@@ 1,41 @@
+local Patch = {}
+
+-- Check if email is a patch; email is a patch is the subject line contains PATCH
+local function check_patch()
+ local result = vim.fn.search("^Subject:.*PATCH.*", "nw")
+ if result == 0 then
+ return false
+ end
+ return true
+end
+
+local function set_header(status)
+ if not check_patch() then
+ return
+ 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 })
+
+ -- Search for first empty line and insert header
+ local line = vim.fn.search("^$")
+ vim.api.nvim_buf_set_lines(0, line - 1, line - 1, true, {
+ string.format("X-Sourcehut-Patchset-Update: %s", status),
+ })
+
+ -- If user was below inserted line we have to move cursor 1 line more
+ local offset = 0
+ if cursor_position[1] >= line then
+ offset = 1
+ end
+
+ -- Set cursor to old position
+ vim.api.nvim_win_set_cursor(0, { cursor_position[1] + offset, cursor_position[2] })
+end
+
+function Patch.applied()
+ set_header("APPLIED")
+end
+
+return Patch