Compare commits
5 Commits
9c09a0ffcd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
699f021c43 | ||
|
|
7ec4a474d7 | ||
|
|
b97e491437 | ||
|
|
7514fa653f | ||
|
|
63cf649210 |
139
lua/bookmark.lua
139
lua/bookmark.lua
@@ -1,99 +1,56 @@
|
||||
local Bookmarks = {}
|
||||
local H = {
|
||||
bookmarks = {},
|
||||
bookmarksFilePath = ".bookmarks",
|
||||
state = {
|
||||
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,
|
||||
callback = function()
|
||||
H.loadBookmarks()
|
||||
end,
|
||||
group = augroup,
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
function H.getSavePath()
|
||||
local cwd = vim.fs.normalize(vim.fn.getcwd())
|
||||
local filename = string.gsub(cwd, "[/:]", "%%")
|
||||
local statePath = vim.fs.normalize(vim.fn.stdpath("state"))
|
||||
statePath = vim.fs.joinpath(statePath, "bookmark")
|
||||
vim.fn.mkdir(statePath, "p")
|
||||
local filePath = vim.fs.joinpath(statePath, filename)
|
||||
return filePath
|
||||
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
|
||||
H.state = {bookmarks = {}}
|
||||
local path = H.getSavePath()
|
||||
local file = io.open(path, "r")
|
||||
if not file 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
|
||||
local content = file:read("*a")
|
||||
file:close()
|
||||
H.state = vim.json.decode(content)
|
||||
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
|
||||
local path = H.getSavePath()
|
||||
local file = io.open(path, "w")
|
||||
if not file then
|
||||
return
|
||||
end
|
||||
file:write(vim.json.encode(H.state))
|
||||
file:close()
|
||||
end
|
||||
|
||||
function Bookmarks.addBookmark()
|
||||
@@ -101,15 +58,16 @@ function Bookmarks.addBookmark()
|
||||
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})
|
||||
table.insert(H.state.bookmarks, {name = input, file = file, cursor = cursor})
|
||||
H.saveBookmarks()
|
||||
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)
|
||||
if H.state.bookmarks and #H.state.bookmarks > 0 then
|
||||
vim.ui.select(H.state.bookmarks, {prompt = action .." Bookmark", format_item = function(item) return item.name end}, on_choice)
|
||||
else
|
||||
vim.notify("No bookmarks set or loaded")
|
||||
end
|
||||
@@ -118,7 +76,8 @@ end
|
||||
function Bookmarks.deleteBookmark()
|
||||
Bookmarks.selectBookmark("Delete", function(bookmark, idx)
|
||||
if idx then
|
||||
table.remove(H.bookmarks, idx)
|
||||
table.remove(H.state.bookmarks, idx)
|
||||
H.saveBookmarks()
|
||||
vim.notify("Deleted bookmark \"".. bookmark.name .. "\"")
|
||||
end
|
||||
end)
|
||||
@@ -135,32 +94,4 @@ function Bookmarks.jumpToBookmark()
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user