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.

This commit is contained in:
psychhim
2025-10-15 02:10:11 +05:30
parent 036a8d766c
commit a6a8f01ebf
5 changed files with 111 additions and 7 deletions

View File

@@ -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]]

View File

@@ -15,13 +15,61 @@ return {
-- Setup buttons
dashboard.section.buttons.val = {
-- New file
dashboard.button('i', ' New File', '<Cmd>ene <Bar> startinsert<CR>'),
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('<leader>n', ' Open Neo-tree', '<Cmd>cd ' .. vim.fn.fnameescape(start_dir) .. ' | Neotree toggle float<CR>'),
-- Quit Neovim using custom function
dashboard.button('<leader>qa', ' Quit', '<Cmd>lua close_nvim_with_prompt()<CR>'),
-- Close Alpha window
dashboard.button('<leader>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 doesnt 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 doesnt 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,
}

View File

@@ -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

View File

@@ -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

View File

@@ -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