aboutsummaryrefslogtreecommitdiff
path: root/lua/encrypt/setup-buffer.lua
blob: e03f71c9a7c3a27a0b34e5a61a4cf0cf536da8f2 (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
31
32
33
34
35
36
37
local encrypt = require("encrypt.encrypt")
local helpers = require("encrypt.helpers")

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

---@param buftype buftype
local function setupBuffer(buftype)
  -- buftype="acwrite" means save using the BufWriteCmd command
  vim.bo.buftype = "acwrite"
  vim.bo.swapfile = false
  vim.bo.undofile = false
  vim.b["encrypted"] = true
  -- Lock the encrypted file to read-only mode to avoid corruption
  -- caused by unintended modifications.
  if buftype == helpers.BUFTYPE.encrypted then
    vim.bo.modifiable = false
  end
  createWriteAutoCmd()
end

return setupBuffer