aboutsummaryrefslogtreecommitdiffstats
path: root/lua/config/autocmds.lua
blob: ffb5d5700cfefa0224206d3529b5dc52d99bf362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
-- resize splits if window got resized
vim.api.nvim_create_autocmd({ "VimResized" }, {
  callback = function()
    vim.cmd "tabdo wincmd ="
  end,
})

-- close some filetypes with <q>
vim.api.nvim_create_autocmd("FileType", {
  pattern = {
    "qf",
    "help",
    "man",
    "notify",
    "lspinfo",
    "spectre_panel",
    "startuptime",
    "tsplayground",
    "PlenaryTestPopup",
  },
  callback = function(event)
    vim.bo[event.buf].buflisted = false
    vim.keymap.set("n", "q", "<cmd>close<cr>", { 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 = {
        ["<localleader>"] = {
          --- get from REMOTE
          ["1"] = { ":diffget LOCAL<CR>", "Get LOCAL" },
          --- get from BASE
          ["2"] = { ":diffget BASE<CR>", "Get BASE" },
          --- get from LOCAL
          ["3"] = { ":diffget REMOTE<CR>", "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,
})



-- 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,
-- })