diff --git a/init.lua b/init.lua index 97a96c7..7b8cb98 100644 --- a/init.lua +++ b/init.lua @@ -388,28 +388,8 @@ require 'buffer_deleted' -- [[ Pastebin ]] require('pastebin').setup() --- [[ Autocmd to refresh Alpha when switching to a tab with Alpha buffer ]] -vim.api.nvim_create_autocmd('TabEnter', { - pattern = '*', - callback = function() - if just_created_tab then - just_created_tab = false - return - end - local buf = vim.api.nvim_get_current_buf() - local buftype = vim.api.nvim_buf_get_option(buf, 'filetype') - if buftype == 'alpha' then - vim.schedule(function() - local alpha_loaded, alpha = pcall(require, 'alpha') - if not alpha_loaded then - return - end - vim.cmd 'enew' - alpha.start(true) - end) - end - end, -}) +-- [[ Refresh Alpha when switching to a tab to replace breakage ]] +require 'alpha_fix' -- The line beneath this is called `modeline`. See `:help modeline` -- vim: ts=2 sts=2 sw=2 et diff --git a/lua/alpha_fix.lua b/lua/alpha_fix.lua new file mode 100644 index 0000000..5dede03 --- /dev/null +++ b/lua/alpha_fix.lua @@ -0,0 +1,75 @@ +-- Manages Alpha refresh when switching and preserves ASCII art + +_G.alpha_tab_ascii = {} + +local last_tab_count = #vim.api.nvim_list_tabpages() + +-- Save ASCII art +vim.api.nvim_create_autocmd('TabLeave', { + pattern = '*', + callback = function() + local buf = vim.api.nvim_get_current_buf() + local buftype = vim.api.nvim_buf_get_option(buf, 'filetype') + if buftype == 'alpha' then + local dashboard_loaded, dashboard = pcall(require, 'alpha.themes.dashboard') + if dashboard_loaded then + local tab = vim.api.nvim_get_current_tabpage() + -- Save the current ASCII for this tab + _G.alpha_tab_ascii[tab] = vim.deepcopy(dashboard.section.header.val) + end + end + end, +}) + +-- Refresh Alpha when switching to a tab with Alpha buffer +vim.api.nvim_create_autocmd('TabEnter', { + pattern = '*', + callback = function() + local current_tab_count = #vim.api.nvim_list_tabpages() + + if current_tab_count > last_tab_count then + last_tab_count = current_tab_count + return + end + + last_tab_count = current_tab_count + + local buf = vim.api.nvim_get_current_buf() + local buftype = vim.api.nvim_buf_get_option(buf, 'filetype') + if buftype == 'alpha' then + vim.schedule(function() + local alpha_loaded, alpha = pcall(require, 'alpha') + if not alpha_loaded then + return + end + + local dashboard_loaded, dashboard = pcall(require, 'alpha.themes.dashboard') + if not dashboard_loaded then + vim.cmd 'enew' + alpha.start(true) + return + end + + local tab = vim.api.nvim_get_current_tabpage() + + if _G.alpha_tab_ascii[tab] then + dashboard.section.header.val = _G.alpha_tab_ascii[tab] + end + + vim.cmd 'enew' + alpha.start(true) + end) + end + end, +}) + +-- Clean +vim.api.nvim_create_autocmd('TabClosed', { + pattern = '*', + callback = function() + local closed_tab = tonumber(vim.fn.expand '') + if closed_tab then + _G.alpha_tab_ascii[closed_tab] = nil + end + end, +}) diff --git a/lua/plugins/custom/alpha.lua b/lua/plugins/custom/alpha.lua index 7bac5dd..b5c8a8b 100644 --- a/lua/plugins/custom/alpha.lua +++ b/lua/plugins/custom/alpha.lua @@ -104,7 +104,13 @@ return { vim.api.nvim_create_autocmd('User', { pattern = 'AlphaReady', callback = function() - refresh_header() + local tab = vim.api.nvim_get_current_tabpage() + -- Only refresh if this tab doesn't have stored ASCII + if not _G.alpha_tab_ascii[tab] then + refresh_header() + local dashboard = require 'alpha.themes.dashboard' + _G.alpha_tab_ascii[tab] = vim.deepcopy(dashboard.section.header.val) + end end, }) end,