~xenrox/srht-patches.nvim

6a6f459a6d6d7a26b0793134b52f34ee9f615f7d — Thorben Günther 2 years ago 8423d8b
Handle already existing header

In that case it will simply be deleted.
1 files changed, 14 insertions(+), 2 deletions(-)

M lua/srht-patches/init.lua
M lua/srht-patches/init.lua => lua/srht-patches/init.lua +14 -2
@@ 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 })