aboutsummaryrefslogtreecommitdiff
path: root/lua/encrypt/setup-buffer.lua
blob: 8b950ec15c484606aeb9670bdb08bb7c9965a4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
local encrypt = require("encrypt.encrypt")

local function createWriteAutoCmd()
  if vim.b["encryptionAutoCmd"] ~= nil then
    return
  end

  local bufnr = vim.api.nvim_get_current_buf()
  vim.b["encryptionAutoCmd"] = vim.api.nvim_create_autocmd({ "BufWriteCmd" }, {
    buffer = bufnr,
    callback = function()
      if vim.b["encrypted"] ~= true then
        return
      end

      encrypt()
    end,
  })
end

local function setupBuffer()
  -- buftype="acwrite" means save using the BufWriteCmd command
  vim.bo.buftype = "acwrite"
  vim.bo.swapfile = false
  vim.bo.undofile = false
  vim.b["encrypted"] = true
  createWriteAutoCmd()
end

return setupBuffer