Skip to content

autocmd BufReadPost doesnt work #88

Description

@Ampnbsp

I tried to adapt this plugin to mappings.lua file but a read_undo_file callback is not executed when I open a file.

Can you help me with this?

-- UNDODIR_TREE

local function get_undo_file(filepath)
  local filename = vim.fn.undofile(filepath)
  filename = filename:gsub("%%", "/")
  filename = filename:gsub("//", "/")

  return filename
end

local function write_undo_file()

  local filepath = vim.fn.expand("%:p")
  if filepath == "" or vim.bo.buftype ~= "" or vim.bo.readonly then
    return
  end

  local undo_file = get_undo_file(filepath)
    if vim.fn.filereadable(undo_file) ~= 1 then
        vim.notify("Undo file not readable (write_undo_file): " .. undo_file, vim.log.levels.WARN)
    end

  local dir = vim.fn.fnamemodify(undo_file, ":h")

  if vim.fn.isdirectory(dir) == 0 then
    vim.fn.mkdir(dir, "p")
  end

  if vim.fn.isdirectory(dir) == 1 then
    vim.cmd("silent! wundo " .. vim.fn.fnameescape(undo_file))
  else
    vim.notify("Failed to create undo dir: " .. dir, vim.log.levels.ERROR)
  end
end

local function read_undo_file()

  local filepath = vim.fn.expand("%:p")

  if filepath == "" or vim.bo.buftype ~= "" then
    return
  end
  local undo_file = get_undo_file(filepath)

    if vim.fn.filereadable(undo_file) ~= 1 then
        vim.notify("Undo file not readable: " .. undo_file, vim.log.levels.WARN)
    end
  if vim.fn.filereadable(undo_file) == 1 then
    vim.cmd("silent! rundo " .. vim.fn.fnameescape(undo_file))
  end
end

autocmd("BufReadPost", {
    callback =  read_undo_file
})

autocmd("BufWritePre", {
    callback = function()
        vim.bo.undofile = false
    end,
})
autocmd("BufWritePost", {
    callback = write_undo_file
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions