From 9c09a0ffcd2c58cdc68e6e04684ec33dd7ab1656 Mon Sep 17 00:00:00 2001 From: Stefan Rakel Date: Wed, 26 Nov 2025 13:52:21 +0100 Subject: [PATCH] Add naming of tabs --- Readme.md | 2 ++ lua/bookmark.lua | 83 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/Readme.md b/Readme.md index 1c5ffaa..8255512 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,5 @@ # Bookmark.nvim Small plugin to create and delete as well as navigate to bookmarks + +Also Naming of tabs is implemented. Names are saved in `vim.t.bookmark_tabname` diff --git a/lua/bookmark.lua b/lua/bookmark.lua index 705cf89..e9fa24b 100644 --- a/lua/bookmark.lua +++ b/lua/bookmark.lua @@ -1,10 +1,33 @@ -local bookmarksFilePath = ".bookmarks" -local M = {} +local Bookmarks = {} local H = { bookmarks = {}, + bookmarksFilePath = ".bookmarks", } -function M.setup() +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"}, @@ -42,13 +65,13 @@ end function H.loadBookmarks() --clear current bookmarks H.bookmarks = {} - bookmarksFilePath = ".bookmarks" + H.bookmarksFilePath = ".bookmarks" local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1] if not path then return end - bookmarksFilePath = path - local file = io.open(bookmarksFilePath, "r") + H.bookmarksFilePath = path + local file = io.open(H.bookmarksFilePath, "r") if file then local fileContent = file:read("*a") file:close() @@ -61,19 +84,19 @@ function H.loadBookmarks() end function H.saveBookmarks() - if #H.bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) == 1 then - local file = io.open(bookmarksFilePath, "w") + 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 load Bookmarks") + vim.notify("Failed to save Bookmarks") end end end -function M.addBookmark() +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) @@ -84,7 +107,7 @@ function M.addBookmark() end) end -function M.selectBookmark(action, on_choice) +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 @@ -92,8 +115,8 @@ function M.selectBookmark(action, on_choice) end end -function M.deleteBookmark() - M.selectBookmark("Delete", function(bookmark, idx) +function Bookmarks.deleteBookmark() + Bookmarks.selectBookmark("Delete", function(bookmark, idx) if idx then table.remove(H.bookmarks, idx) vim.notify("Deleted bookmark \"".. bookmark.name .. "\"") @@ -101,8 +124,8 @@ function M.deleteBookmark() end) end -function M.jumpToBookmark() - M.selectBookmark("Jump to",vim.schedule_wrap(function(item) +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) @@ -112,4 +135,32 @@ function M.jumpToBookmark() end)) end -return M +-- 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