From a6a8f01ebf016c52a9b64765dbc37c2a4a748b6b Mon Sep 17 00:00:00 2001 From: psychhim Date: Wed, 15 Oct 2025 02:10:11 +0530 Subject: [PATCH] Fix: alpha window closing. Patches to open files with telescope and neotree on the same window as alpha. Autocmds to load global and local settigs to every window after opening from telescope/neovim from alpha window. --- init.lua | 3 ++ lua/plugins/custom/alpha.lua | 56 +++++++++++++++++++++++++-- lua/plugins/custom/filetree.lua | 8 +++- lua/plugins/custom/telescope.lua | 7 +++- lua/settings_autoload_after_alpha.lua | 44 +++++++++++++++++++++ 5 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 lua/settings_autoload_after_alpha.lua diff --git a/init.lua b/init.lua index f0201c8..4886df5 100644 --- a/init.lua +++ b/init.lua @@ -343,6 +343,9 @@ vim.api.nvim_create_autocmd('TextYankPost', { pattern = '*', }) +-- Auto-apply settings to all normal buffers +require('settings_autoload_after_alpha').setup() + -- Theme require('kanagawa').setup { transparent = true } vim.cmd [[colorscheme kanagawa]] diff --git a/lua/plugins/custom/alpha.lua b/lua/plugins/custom/alpha.lua index bade2a1..ff579d1 100644 --- a/lua/plugins/custom/alpha.lua +++ b/lua/plugins/custom/alpha.lua @@ -15,13 +15,61 @@ return { -- Setup buttons dashboard.section.buttons.val = { -- New file - dashboard.button('i', ' New File', 'ene startinsert'), + dashboard.button('i', ' New File', function() + local alpha_buf = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_delete(alpha_buf, { force = true }) + vim.cmd 'enew' + vim.cmd 'startinsert' + end), -- Open Neo-tree in starting directory dashboard.button('n', ' Open Neo-tree', 'cd ' .. vim.fn.fnameescape(start_dir) .. ' | Neotree toggle float'), - -- Quit Neovim using custom function - dashboard.button('qa', ' Quit', 'lua close_nvim_with_prompt()'), + -- Close Alpha window + dashboard.button('q', ' Close this window', function() + local current_buf = vim.api.nvim_get_current_buf() + -- Count listed buffers + local listed_count = 0 + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if vim.api.nvim_buf_get_option(buf, 'buflisted') then + listed_count = listed_count + 1 + end + end + -- If only 1 listed buffer left, force quit Neovim + if listed_count == 1 then + vim.cmd 'qa!' + else + -- Delete current buffer + if vim.api.nvim_buf_is_valid(current_buf) then + vim.api.nvim_buf_delete(current_buf, { force = true }) + end + -- Delete one listed, unmodified, no-name buffer + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if vim.api.nvim_buf_is_valid(buf) then + local listed = vim.api.nvim_buf_get_option(buf, 'buflisted') + local modified = vim.api.nvim_buf_get_option(buf, 'modified') + local name = vim.api.nvim_buf_get_name(buf) + -- target buffers that are listed, have no modifications, and no name + if listed and not modified and name == '' then + vim.api.nvim_buf_delete(buf, { force = true }) + break -- delete only one buffer + end + end + end + end + end), } - alpha.setup(dashboard.opts) + -- Ensure Alpha buffer is a scratch buffer so closing it doesn’t leave [No Name] + local opts = dashboard.opts + opts.hide = true -- hide it from buffer list + alpha.setup(opts) + + -- Open Alpha in a dedicated scratch buffer if no file is loaded + if vim.fn.bufnr '$' == 1 and vim.fn.bufname(0) == '' then + vim.cmd 'enew' -- create a proper empty buffer + vim.cmd 'Alpha' -- open Alpha in that buffer + vim.bo.buflisted = false -- mark it unlisted so it doesn’t pollute buffer pickers + vim.bo.buftype = 'nofile' -- make it a scratch buffer + -- vim.bo.bufhidden = 'wipe' -- critical: buffer is automatically deleted when hidden + end end, } diff --git a/lua/plugins/custom/filetree.lua b/lua/plugins/custom/filetree.lua index ea0b1a2..d640709 100644 --- a/lua/plugins/custom/filetree.lua +++ b/lua/plugins/custom/filetree.lua @@ -135,10 +135,14 @@ return { local buftype = vim.api.nvim_buf_get_option(buf, 'buftype') local modified = vim.api.nvim_buf_get_option(buf, 'modified') -- PATCH: Treat Alpha dashboard as empty - if (bufname == '' and buftype == '' and not modified) or is_alpha_buffer(buf) then + if is_alpha_buffer(buf) or (bufname == '' and buftype == '' and not modified) then empty_buf = buf vim.api.nvim_set_current_win(win) - break + if is_alpha_buffer(buf) then + vim.api.nvim_buf_delete(buf, { force = true }) + end + vim.cmd('edit ' .. vim.fn.fnameescape(path)) + return end end end diff --git a/lua/plugins/custom/telescope.lua b/lua/plugins/custom/telescope.lua index 0a472fd..f90267f 100644 --- a/lua/plugins/custom/telescope.lua +++ b/lua/plugins/custom/telescope.lua @@ -93,11 +93,16 @@ return { local name = vim.api.nvim_buf_get_name(buf) local buftype = vim.api.nvim_buf_get_option(buf, 'buftype') local modified = vim.api.nvim_buf_get_option(buf, 'modified') - -- PATCH: also treat Alpha dashboard buffer as empty if (name == '' and buftype == '' and not modified) or is_alpha_buffer(buf) then vim.api.nvim_set_current_win(win) + -- DELETE Alpha buffer if it's in this window + if is_alpha_buffer(buf) then + vim.api.nvim_buf_delete(buf, { force = true }) + end + -- Now open the file in this window vim.cmd('edit ' .. vim.fn.fnameescape(path)) + -- Stop further processing return end end diff --git a/lua/settings_autoload_after_alpha.lua b/lua/settings_autoload_after_alpha.lua new file mode 100644 index 0000000..13756f6 --- /dev/null +++ b/lua/settings_autoload_after_alpha.lua @@ -0,0 +1,44 @@ +-- Auto-apply all preferred settings to every normal buffer +local M = {} + +function M.setup() + vim.api.nvim_create_autocmd({ 'BufWinEnter', 'WinEnter', 'BufEnter' }, { + callback = function() + local ft = vim.bo.filetype + local bt = vim.bo.buftype + + -- Skip floating or special buffers + if ft == 'neo-tree' or ft == 'TelescopePrompt' or bt == 'terminal' or bt == 'nofile' then + return + end + + -- Window-local options + vim.wo.number = true + vim.wo.relativenumber = true + vim.wo.signcolumn = 'yes' + vim.wo.scrolloff = 8 + + -- Global options + vim.o.hlsearch = false + vim.o.mouse = '' -- disable mouse + vim.o.breakindent = true + vim.o.undofile = true + vim.o.ignorecase = true + vim.o.smartcase = true + vim.o.updatetime = 250 + vim.o.timeoutlen = 300 + vim.o.completeopt = 'menuone,noselect' + vim.o.termguicolors = true + vim.o.showtabline = 1 + + -- Tabs and indentation + vim.o.expandtab = false + vim.o.shiftwidth = 4 + vim.o.softtabstop = 0 + vim.o.tabstop = 4 + vim.o.smartindent = true + end, + }) +end + +return M