Make chat windows read only

This commit is contained in:
Jonas Widen 2025-03-16 20:55:21 +01:00
parent f8afeef891
commit 4db184f29b

View File

@ -29,7 +29,15 @@ local function setup_buffer_options(bufnr)
swapfile = false, swapfile = false,
bufhidden = 'wipe', bufhidden = 'wipe',
modifiable = false, modifiable = false,
readonly = true, -- Add readonly option readonly = true,
undolevels = -1, -- Disable undo
list = false, -- Disable list mode
number = false, -- Disable line numbers
relativenumber = false,
modifiable = false,
modified = false,
foldmethod = 'manual',
foldlevel = 99,
} }
for option, value in pairs(options) do for option, value in pairs(options) do
@ -40,8 +48,15 @@ end
local function setup_buffer_autocmds(bufnr) local function setup_buffer_autocmds(bufnr)
local augroup = vim.api.nvim_create_augroup('GeminiChatBuffer', { clear = true }) local augroup = vim.api.nvim_create_augroup('GeminiChatBuffer', { clear = true })
-- Block all file operations -- Block ALL file operations and buffer modifications
vim.api.nvim_create_autocmd({'BufReadCmd', 'FileReadCmd', 'BufWriteCmd', 'FileWriteCmd'}, { local blocked_events = {
'BufReadCmd', 'FileReadCmd', 'BufWriteCmd', 'FileWriteCmd',
'BufReadPre', 'BufReadPost', 'BufWrite', 'BufWritePre', 'BufWritePost',
'FileReadPre', 'FileReadPost', 'FileWritePre', 'FileWritePost',
'BufAdd', 'BufCreate', 'BufDelete', 'BufWipeout'
}
vim.api.nvim_create_autocmd(blocked_events, {
group = augroup, group = augroup,
buffer = bufnr, buffer = bufnr,
callback = function() callback = function()
@ -57,6 +72,10 @@ local function setup_buffer_autocmds(bufnr)
callback = function() callback = function()
vim.opt_local.modifiable = false vim.opt_local.modifiable = false
vim.opt_local.readonly = true vim.opt_local.readonly = true
-- Disable file-related options
vim.opt_local.swapfile = false
vim.opt_local.buftype = 'nofile'
vim.opt_local.buflisted = false
end end
}) })
end end
@ -64,27 +83,44 @@ end
local function setup_buffer_keymaps(bufnr) local function setup_buffer_keymaps(bufnr)
local opts = { buffer = bufnr, nowait = true, silent = true } local opts = { buffer = bufnr, nowait = true, silent = true }
-- Disable ALL file operations -- Block ALL commands that could manipulate files or buffers
local operations = { local blocked_cmds = {
'e', 'edit', 'w', 'write', 'sp', 'split', 'vs', 'vsplit', 'e', 'edit', 'ene', 'enew', 'fin', 'find',
'new', 'vnew', 'read', 'update', 'saveas', 'f', 'file', 'w', 'write', 'wa', 'wall',
'wq', 'wqa', 'wa', 'wall', 'q', 'quit', 'qa', 'qall' 'sp', 'split', 'vs', 'vsplit',
'new', 'vnew', 'tabnew',
'read', 'r', 'file', 'f',
'saveas', 'w', 'wq', 'wqa',
'q', 'quit', 'qa', 'qall',
'bn', 'bnext', 'bp', 'bprevious',
'bd', 'bdelete', 'bw', 'bwipeout',
'o', 'open', 'browse'
} }
for _, op in ipairs(operations) do -- Block all command variations
vim.keymap.set('n', ':' .. op, function() for _, cmd in ipairs(blocked_cmds) do
vim.notify('Operation not allowed in chat window', vim.log.levels.WARN) vim.keymap.set('n', ':' .. cmd, function()
vim.notify('Command not allowed in chat window', vim.log.levels.WARN)
end, opts)
vim.keymap.set('n', ':' .. cmd .. '!', function()
vim.notify('Command not allowed in chat window', vim.log.levels.WARN)
end, opts) end, opts)
end end
-- Disable command mode completely -- Disable command-line completely
vim.keymap.set('n', ':', function() vim.keymap.set('n', ':', function()
vim.notify('Command mode disabled in chat window', vim.log.levels.WARN) vim.notify('Command mode disabled in chat window', vim.log.levels.WARN)
end, opts) end, opts)
-- Block common file operation key combinations -- Block common file operation key combinations
vim.keymap.set('n', 'ZZ', '<Nop>', opts) local blocked_keys = {
vim.keymap.set('n', 'ZQ', '<Nop>', opts) 'ZZ', 'ZQ', 'gf', '<C-w>f', '<C-w>gf',
'<C-w>v', '<C-w>s', '<C-w>n'
}
for _, key in ipairs(blocked_keys) do
vim.keymap.set('n', key, '<Nop>', opts)
end
-- Set chat-specific keymaps -- Set chat-specific keymaps
local mappings = config.options.mappings local mappings = config.options.mappings