diff --git a/lua/bookmark.lua b/lua/bookmark.lua index 241ca15..845126d 100644 --- a/lua/bookmark.lua +++ b/lua/bookmark.lua @@ -1,6 +1,8 @@ local Bookmarks = {} local H = { - bookmarks = {}, + state = { + bookmarks = {}, + }, } function Bookmarks.setup() @@ -9,42 +11,49 @@ function Bookmarks.setup() --Ensure our data is saved in the session 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( - {"SessionLoadPost"}, + {"DirChanged"}, { pattern = {"*"}, - callback = H.loadBookmarks, + 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() - H.bookmarks = vim.json.decode(vim.g.Bookmarks) + H.state = {bookmarks = {}} + local path = H.getSavePath() + local file = io.open(path, "r") + if not file then + return + end + local content = file:read("*a") + file:close() + H.state = vim.json.decode(content) end function H.saveBookmarks() - vim.g.Bookmarks = vim.json.encode(H.bookmarks) + 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() @@ -52,7 +61,7 @@ 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 @@ -60,8 +69,8 @@ function Bookmarks.addBookmark() 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 @@ -70,7 +79,7 @@ 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.saveBookmark() vim.notify("Deleted bookmark \"".. bookmark.name .. "\"") end @@ -88,38 +97,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 - if vim.t[k] then - vim.t[k].bookmark_tabname = v - end - end - end -end - -function Bookmarks.showTabName() - vim.notify("Tabname: " .. vim.t.bookmark_tabname) -end - return Bookmarks