-- resize splits if window got resized vim.api.nvim_create_autocmd({ "VimResized" }, { callback = function() vim.cmd "tabdo wincmd =" end, }) -- close some filetypes with vim.api.nvim_create_autocmd("FileType", { pattern = { "qf", "help", "man", "notify", "lspinfo", "spectre_panel", "startuptime", "PlenaryTestPopup", }, callback = function(event) vim.bo[event.buf].buflisted = false vim.keymap.set("n", "q", "close", { buffer = event.buf, silent = true }) end, }) vim.api.nvim_create_autocmd("FileType", { pattern = { "gitcommit", "markdown", "NeogitCommitMessage", "norg" }, callback = function() vim.opt_local.wrap = true vim.opt_local.spell = true end, }) -- vim.api.nvim_create_autocmd({ "BufWritePre" }, { -- callback = function() -- vim.cmd [[%s/\s\+$//e]] -- end, -- }) -- Auto-format C/C++ files with clang-format on save -- vim.api.nvim_create_autocmd("BufWritePre", { -- pattern = { "*.c", "*.h", "*.cpp", "*.hpp", "*.cc", "*.cxx" }, -- callback = function() -- vim.lsp.buf.format { async = false } -- end, -- }) -- Función para alternar el ajuste de línea function ToggleWrap() local wrap = vim.wo.wrap vim.wo.wrap = not wrap if vim.wo.wrap then print "Ajuste de línea: Habilitado" else print "Ajuste de línea: Deshabilitado" end end vim.api.nvim_create_autocmd("BufEnter", { pattern = "*", callback = function() if vim.opt.diff:get() then local wk = require "which-key" local opts = { mode = "n", -- NORMAL mode buffer = vim.api.nvim_get_current_buf(), -- Specify a buffer number for buffer local mappings } local mappings = { [""] = { --- get from REMOTE ["1"] = { ":diffget LOCAL", "Get LOCAL" }, --- get from BASE ["2"] = { ":diffget BASE", "Get BASE" }, --- get from LOCAL ["3"] = { ":diffget REMOTE", "Get REMOTE" }, }, } wk.register(mappings, opts) end end, }) -- Autocmds are automatically loaded on the VeryLazy event -- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua -- Add any additional autocmds here -- local function augroup(name) return vim.api.nvim_create_augroup("my_lazyvim_" .. name, { clear = true }) end vim.api.nvim_create_autocmd("FileType", { group = augroup "disable_treesitter_special_buffers", pattern = { "oil", "snacks_notif" }, callback = function(event) pcall(vim.treesitter.stop, event.buf) end, }) -- Auto-ajustar la altura del quickfix a su contenido (reemplaza nvim-bqf auto_resize_height) vim.api.nvim_create_autocmd("FileType", { group = augroup "qf_autoresize", pattern = "qf", callback = function() local height = math.max(1, math.min(10, vim.fn.line "$")) vim.cmd("resize " .. height) end, }) -- Set filetype for .env and .env.* files vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { group = augroup "env_filetype", pattern = { "*.env", ".env.*" }, callback = function() vim.opt_local.filetype = "sh" end, }) -- Set filetype for .hurl files vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { group = augroup "hurl_filetype", pattern = { "*.hurl" }, callback = function() vim.opt_local.filetype = "hurl" end, }) -- Set filetype for .toml files vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { group = augroup "toml_filetype", pattern = { "*.tomg-config*" }, callback = function() vim.opt_local.filetype = "toml" end, }) -- Set filetype for .ejs files vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, { group = augroup "ejs_filetype", pattern = { "*.ejs", "*.ejs.t" }, callback = function() vim.opt_local.filetype = "embedded_template" end, }) vim.api.nvim_create_autocmd("User", { pattern = { "CodeCompanionToolApprovalRequested", "CodeCompanionChatDone", }, callback = function(args) local event = args.match local messages = { CodeCompanionToolApprovalRequested = "Aprobación requerida", CodeCompanionChatDone = "Respuesta lista", } local msg = messages[event] or event vim.fn.jobstart({ "notify-send", "-a", "CodeCompanion", "CodeCompanion", msg }) end, }) -- Progreso LSP como notificación de snacks.notifier (reemplaza noice lsp.progress) ---@type table local lsp_progress = vim.defaulttable() vim.api.nvim_create_autocmd("LspProgress", { group = augroup "lsp_progress_notify", ---@param ev {data: {client_id: integer, params: lsp.ProgressParams}} callback = function(ev) local client = vim.lsp.get_client_by_id(ev.data.client_id) local value = ev.data.params.value --[[@as {percentage?: number, title?: string, message?: string, kind: "begin" | "report" | "end"}]] if not client or type(value) ~= "table" then return end local p = lsp_progress[client.id] for i = 1, #p + 1 do if i == #p + 1 or p[i].token == ev.data.params.token then p[i] = { token = ev.data.params.token, msg = ("[%3d%%] %s%s"):format( value.kind == "end" and 100 or value.percentage or 100, value.title or "", value.message and (" **%s**"):format(value.message) or "" ), done = value.kind == "end", } break end end local msg = {} ---@type string[] lsp_progress[client.id] = vim.tbl_filter(function(v) return table.insert(msg, v.msg) or not v.done end, p) local spinner = { "⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏" } vim.notify(table.concat(msg, "\n"), vim.log.levels.INFO, { id = "lsp_progress", title = client.name, opts = function(notif) notif.icon = #lsp_progress[client.id] == 0 and " " or spinner[math.floor(vim.uv.hrtime() / (1e6 * 80)) % #spinner + 1] end, }) end, }) -- Formatear/minificar JSON con jq (reemplaza json-nvim) vim.api.nvim_create_autocmd("FileType", { group = augroup "json_jq", pattern = "json", callback = function(event) vim.keymap.set("n", "jff", ":%!jq .", { buffer = event.buf, desc = "Formatear JSON con jq" }) vim.keymap.set("n", "jmf", ":%!jq -c .", { buffer = event.buf, desc = "Minificar JSON con jq" }) end, }) -- Cerrar buffers inactivos tras 120 min (reemplaza nvim-early-retirement) local retirement_age_mins = 120 local buf_last_used = {} vim.api.nvim_create_autocmd("BufEnter", { group = augroup "early_retirement", callback = function(event) buf_last_used[event.buf] = os.time() end, }) local retirement_timer = assert(vim.uv.new_timer()) retirement_timer:start( 60000, 60000, vim.schedule_wrap(function() local now = os.time() for _, buf in ipairs(vim.api.nvim_list_bufs()) do if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buflisted and not vim.bo[buf].modified and #vim.fn.win_findbuf(buf) == 0 then local last = buf_last_used[buf] if last == nil then buf_last_used[buf] = now elseif now - last > retirement_age_mins * 60 then pcall(vim.api.nvim_buf_delete, buf, {}) buf_last_used[buf] = nil end end end end) ) -- Detección simple de indentación (reemplaza indent-o-matic); editorconfig nativo tiene prioridad vim.api.nvim_create_autocmd("BufReadPost", { group = augroup "detect_indent", callback = function(event) local ec = vim.b[event.buf].editorconfig if ec and (ec.indent_style or ec.indent_size) then return end local tabs, spaces, min_spaces = 0, 0, math.huge for _, line in ipairs(vim.api.nvim_buf_get_lines(event.buf, 0, 200, false)) do local ws = line:match "^%s+" if ws then if ws:find "\t" then tabs = tabs + 1 else spaces = spaces + 1 if #ws > 1 and #ws < min_spaces then min_spaces = #ws end end end end if tabs > spaces then vim.bo[event.buf].expandtab = false elseif spaces > 0 and min_spaces ~= math.huge then vim.bo[event.buf].expandtab = true vim.bo[event.buf].shiftwidth = min_spaces vim.bo[event.buf].softtabstop = min_spaces end end, }) -- vim.api.nvim_create_autocmd("User", { -- pattern = "CodeCompanion*", -- callback = function(args) -- local event = args.match -- local line = os.date("%H:%M:%S") .. " " .. event .. "\n" -- -- local f = io.open("/tmp/codecompanionEvents.txt", "a") -- if f then -- f:write(line) -- f:close() -- end -- end, -- }) -- WezTerm no implementa el borrado de imágenes del protocolo kitty (snacks.image -- usa el modo fallback sin placeholders), así que al salir de un buffer de imagen -- quedan restos en pantalla. Forzar un repintado completo los limpia. if vim.env.TERM_PROGRAM == "WezTerm" then vim.api.nvim_create_autocmd("BufWinLeave", { group = vim.api.nvim_create_augroup("wezterm_image_cleanup", { clear = true }), callback = function(ev) if vim.bo[ev.buf].filetype == "image" then vim.schedule(function() vim.cmd "mode" end) end end, }) end