167 lines
4.1 KiB
Lua
167 lines
4.1 KiB
Lua
local Bookmarks = {}
|
|
local H = {
|
|
bookmarks = {},
|
|
bookmarksFilePath = ".bookmarks",
|
|
}
|
|
|
|
function Bookmarks.setup()
|
|
_G.Bookmarks = Bookmarks
|
|
|
|
vim.opt.sessionoptions:append("globals")
|
|
|
|
-- Attach to mini.sessions
|
|
if MiniSessions then
|
|
local oldWriteFunction = MiniSessions.config.hooks.pre.write
|
|
MiniSessions.config.hooks.pre.write = function(data)
|
|
if oldWriteFunction then
|
|
oldWriteFunction(data)
|
|
end
|
|
Bookmarks.updateTabNames()
|
|
end
|
|
|
|
local oldReadFunction = MiniSessions.config.hooks.post.read
|
|
MiniSessions.config.hooks.post.read = function(data)
|
|
if oldReadFunction then
|
|
oldReadFunction(data)
|
|
end
|
|
Bookmarks.loadTabNames()
|
|
end
|
|
end
|
|
|
|
local augroup = vim.api.nvim_create_augroup("bookmark.nvim", {clear=true})
|
|
vim.api.nvim_create_autocmd(
|
|
{"DirChanged"},
|
|
{
|
|
pattern = {"global"},
|
|
callback = H.loadBookmarks,
|
|
group = augroup,
|
|
}
|
|
)
|
|
vim.api.nvim_create_autocmd(
|
|
{"DirChangedPre"},
|
|
{
|
|
pattern = {"global"},
|
|
callback = H.saveBookmarks,
|
|
group = augroup,
|
|
}
|
|
)
|
|
vim.api.nvim_create_autocmd(
|
|
{"SessionWritePost"},
|
|
{
|
|
callback = H.saveBookmarks,
|
|
group = augroup,
|
|
}
|
|
)
|
|
vim.api.nvim_create_autocmd(
|
|
{"VimLeavePre"},
|
|
{
|
|
pattern = {"*"},
|
|
callback = H.saveBookmarks,
|
|
group = augroup,
|
|
}
|
|
)
|
|
end
|
|
|
|
function H.loadBookmarks()
|
|
--clear current bookmarks
|
|
H.bookmarks = {}
|
|
H.bookmarksFilePath = ".bookmarks"
|
|
local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1]
|
|
if not path then
|
|
return
|
|
end
|
|
H.bookmarksFilePath = path
|
|
local file = io.open(H.bookmarksFilePath, "r")
|
|
if file then
|
|
local fileContent = file:read("*a")
|
|
file:close()
|
|
if fileContent and fileContent ~= "" then
|
|
H.bookmarks = vim.json.decode(fileContent)
|
|
end
|
|
else
|
|
vim.notify("Failed to load Bookmarks")
|
|
end
|
|
end
|
|
|
|
function H.saveBookmarks()
|
|
if #H.bookmarks > 0 or vim.fn.filereadable(H.bookmarksFilePath) == 1 then
|
|
local file = io.open(H.bookmarksFilePath, "w")
|
|
if file then
|
|
file:write(vim.json.encode(H.bookmarks))
|
|
file:flush()
|
|
file:close()
|
|
else
|
|
vim.notify("Failed to save Bookmarks")
|
|
end
|
|
end
|
|
end
|
|
|
|
function Bookmarks.addBookmark()
|
|
vim.ui.input({prompt= "Name of bookmark", default= vim.fn.bufname()}, function(input)
|
|
if input and input ~= "" then
|
|
local file = vim.api.nvim_buf_get_name(0)
|
|
local cursor = vim.api.nvim_win_get_cursor(0)
|
|
table.insert(H.bookmarks, {name = input, file = file, cursor = cursor})
|
|
vim.notify("Bookmark \"" .. input .. "\" added")
|
|
end
|
|
end)
|
|
end
|
|
|
|
function Bookmarks.selectBookmark(action, on_choice)
|
|
if H.bookmarks and #H.bookmarks > 0 then
|
|
vim.ui.select(H.bookmarks, {prompt = action .." Bookmark", format_item = function(item) return item.name end}, on_choice)
|
|
else
|
|
vim.notify("No bookmarks set or loaded")
|
|
end
|
|
end
|
|
|
|
function Bookmarks.deleteBookmark()
|
|
Bookmarks.selectBookmark("Delete", function(bookmark, idx)
|
|
if idx then
|
|
table.remove(H.bookmarks, idx)
|
|
vim.notify("Deleted bookmark \"".. bookmark.name .. "\"")
|
|
end
|
|
end)
|
|
end
|
|
|
|
function Bookmarks.jumpToBookmark()
|
|
Bookmarks.selectBookmark("Jump to",vim.schedule_wrap(function(item)
|
|
if item then
|
|
vim.cmd("edit " .. item.file)
|
|
vim.api.nvim_win_set_cursor(0, item.cursor)
|
|
--set jump in jumplist
|
|
vim.cmd("normal! m'")
|
|
end
|
|
end))
|
|
end
|
|
|
|
-- Tab naming
|
|
function Bookmarks.nameTab()
|
|
vim.ui.input({prompt = "Name this tab", default = vim.t.bookmark_tabname}, function(input)
|
|
if input and input ~= "" then
|
|
vim.t.bookmark_tabname = input
|
|
vim.cmd.redrawtabline()
|
|
end
|
|
end)
|
|
end
|
|
|
|
function Bookmarks.updateTabNames()
|
|
local names = {}
|
|
for i, tabid in ipairs(vim.api.nvim_list_tabpages()) do
|
|
names[i] = vim.t[tabid].bookmark_tabname
|
|
end
|
|
vim.g.Bookmark_tabnames = vim.json.encode(names)
|
|
end
|
|
|
|
function Bookmarks.loadTabNames()
|
|
local tabnames = vim.g.Bookmark_tabnames
|
|
if tabnames then
|
|
tabnames = vim.json.decode(tabnames)
|
|
for k,v in pairs(tabnames) do
|
|
vim.t[k].bookmark_tabname = v
|
|
end
|
|
end
|
|
end
|
|
|
|
return Bookmarks
|