1
0

Add naming of tabs

This commit is contained in:
Stefan Rakel
2025-11-26 13:52:21 +01:00
parent 2ee6717235
commit 9c09a0ffcd
2 changed files with 69 additions and 16 deletions

View File

@@ -1,3 +1,5 @@
# Bookmark.nvim # Bookmark.nvim
Small plugin to create and delete as well as navigate to bookmarks 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`

View File

@@ -1,10 +1,33 @@
local bookmarksFilePath = ".bookmarks" local Bookmarks = {}
local M = {}
local H = { local H = {
bookmarks = {}, 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}) local augroup = vim.api.nvim_create_augroup("bookmark.nvim", {clear=true})
vim.api.nvim_create_autocmd( vim.api.nvim_create_autocmd(
{"DirChanged"}, {"DirChanged"},
@@ -42,13 +65,13 @@ end
function H.loadBookmarks() function H.loadBookmarks()
--clear current bookmarks --clear current bookmarks
H.bookmarks = {} H.bookmarks = {}
bookmarksFilePath = ".bookmarks" H.bookmarksFilePath = ".bookmarks"
local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1] local path = vim.fs.find(".bookmarks", {upward = true, type="file"})[1]
if not path then if not path then
return return
end end
bookmarksFilePath = path H.bookmarksFilePath = path
local file = io.open(bookmarksFilePath, "r") local file = io.open(H.bookmarksFilePath, "r")
if file then if file then
local fileContent = file:read("*a") local fileContent = file:read("*a")
file:close() file:close()
@@ -61,19 +84,19 @@ function H.loadBookmarks()
end end
function H.saveBookmarks() function H.saveBookmarks()
if #H.bookmarks > 0 or vim.fn.filereadable(bookmarksFilePath) == 1 then if #H.bookmarks > 0 or vim.fn.filereadable(H.bookmarksFilePath) == 1 then
local file = io.open(bookmarksFilePath, "w") local file = io.open(H.bookmarksFilePath, "w")
if file then if file then
file:write(vim.json.encode(H.bookmarks)) file:write(vim.json.encode(H.bookmarks))
file:flush() file:flush()
file:close() file:close()
else else
vim.notify("Failed to load Bookmarks") vim.notify("Failed to save Bookmarks")
end end
end end
end end
function M.addBookmark() function Bookmarks.addBookmark()
vim.ui.input({prompt= "Name of bookmark", default= vim.fn.bufname()}, function(input) vim.ui.input({prompt= "Name of bookmark", default= vim.fn.bufname()}, function(input)
if input and input ~= "" then if input and input ~= "" then
local file = vim.api.nvim_buf_get_name(0) local file = vim.api.nvim_buf_get_name(0)
@@ -84,7 +107,7 @@ function M.addBookmark()
end) end)
end end
function M.selectBookmark(action, on_choice) function Bookmarks.selectBookmark(action, on_choice)
if H.bookmarks and #H.bookmarks > 0 then 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) vim.ui.select(H.bookmarks, {prompt = action .." Bookmark", format_item = function(item) return item.name end}, on_choice)
else else
@@ -92,8 +115,8 @@ function M.selectBookmark(action, on_choice)
end end
end end
function M.deleteBookmark() function Bookmarks.deleteBookmark()
M.selectBookmark("Delete", function(bookmark, idx) Bookmarks.selectBookmark("Delete", function(bookmark, idx)
if idx then if idx then
table.remove(H.bookmarks, idx) table.remove(H.bookmarks, idx)
vim.notify("Deleted bookmark \"".. bookmark.name .. "\"") vim.notify("Deleted bookmark \"".. bookmark.name .. "\"")
@@ -101,8 +124,8 @@ function M.deleteBookmark()
end) end)
end end
function M.jumpToBookmark() function Bookmarks.jumpToBookmark()
M.selectBookmark("Jump to",vim.schedule_wrap(function(item) Bookmarks.selectBookmark("Jump to",vim.schedule_wrap(function(item)
if item then if item then
vim.cmd("edit " .. item.file) vim.cmd("edit " .. item.file)
vim.api.nvim_win_set_cursor(0, item.cursor) vim.api.nvim_win_set_cursor(0, item.cursor)
@@ -112,4 +135,32 @@ function M.jumpToBookmark()
end)) end))
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