~xenrox/srht-patches.nvim

a8709bbcae73d68d56895ba974af4ea5a6411049 — Thorben Günther 2 years ago
Add first version

Can only set APPLIED header.
2 files changed, 42 insertions(+), 0 deletions(-)

A lua/srht-patches/init.lua
A plugin/srht-patches.vim
A  => lua/srht-patches/init.lua +41 -0
@@ 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

A  => plugin/srht-patches.vim +1 -0
@@ 1,1 @@
command! PatchApplied lua require'srht-patches'.applied()