diff --git a/lua/core/autocmds.lua b/lua/core/autocmds.lua index ec36a675..cbde0b66 100644 --- a/lua/core/autocmds.lua +++ b/lua/core/autocmds.lua @@ -10,4 +10,40 @@ vim.api.nvim_create_autocmd('TextYankPost', { callback = function() vim.hl.on_yank() end, -}) \ No newline at end of file +}) + +-- Warn when trying to quit a shared tmux session +local function is_shared_session() + local tmux = vim.env.TMUX + if not tmux or tmux == '' then + return false + end + + -- Check if session name contains "-nvim-shared" + local handle = io.popen "tmux display-message -p '#{session_name}' 2>/dev/null" + if handle then + local session_name = handle:read('*a'):gsub('\n', '') + handle:close() + return session_name:match '%-nvim%-shared$' ~= nil + end + return false +end + +-- Override quit commands for shared sessions +vim.api.nvim_create_autocmd('VimLeavePre', { + group = augroup 'shared_session_warning', + callback = function() + if is_shared_session() then + vim.ui.input({ + prompt = '⚠️ Are you sure you want to close a shared session? ', + }, function(input) + if not input or input:lower() ~= 'y' then + -- Cancel the quit + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', false) + error 'Quit cancelled' + end + end) + end + end, +}) + diff --git a/lua/core/options.lua b/lua/core/options.lua index 8325e768..2047b71f 100644 --- a/lua/core/options.lua +++ b/lua/core/options.lua @@ -66,53 +66,3 @@ vim.opt.cursorline = true -- Minimal number of screen lines to keep above and below the cursor vim.opt.scrolloff = 10 - --- Function to check if running in a shared tmux session -local function is_shared_tmux_session() - if not vim.env.TMUX then - return false - end - - local handle = io.popen('tmux list-sessions -F "#{session_name}:#{session_attached}" 2>/dev/null') - if not handle then - return false - end - - local current_session = vim.fn.system('tmux display-message -p "#S"'):gsub('\n', '') - local output = handle:read('*a') - handle:close() - - -- Check if session name contains "shared" (case insensitive) - if current_session:lower():find('shared') then - return true - end - - -- Also check if multiple users are attached - for line in output:gmatch('[^\r\n]+') do - local session_name, attached_count = line:match('([^:]+):(%d+)') - if session_name == current_session and tonumber(attached_count) > 1 then - return true - end - end - - return false -end - --- Warn before quitting if in a shared tmux session -vim.api.nvim_create_autocmd('VimLeavePre', { - callback = function() - if is_shared_tmux_session() then - local choice = vim.fn.confirm( - 'You are in a shared tmux session. Other users may be affected.\nDo you really want to quit?', - '&Yes\n&No', - 2 -- Default to No - ) - -- vim.fn.confirm returns 1 for Yes, 2 for No, 0 for Esc - -- The & makes Y/y and N/n work as shortcuts (case-insensitive) - if choice ~= 1 then - return -- Prevent quit unless explicitly choosing Yes - end - end - end, -}) -