neovim-config/lua/autocmds.lua
2025-10-22 06:57:49 +02:00

117 lines
3.8 KiB
Lua
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Ouvrir lz terminal dans une fenetre flottante pour le make
vim.cmd([[
autocmd FileType cpp setlocal nobuflisted
let g:makeprg="make | tee /tmp/make_output" " Ne pas bloquer après make
command! Make silent execute 'belowright terminal make'
]])
-- Redimensionne les fenêtres lors du redimensionnement du terminal
vim.api.nvim_create_autocmd("VimResized", {
group = vim.api.nvim_create_augroup("win_autoresize", { clear = true }),
desc = "Auto-resize windows on resizing operation",
command = "wincmd =",
})
-- Désactive le highlight après la recherche
vim.api.nvim_create_autocmd("InsertEnter", {
callback = function()
vim.cmd("nohlsearch")
end,
})
-- Activer ZenMode pour les fichiers Markdown
vim.api.nvim_create_autocmd({"BufEnter","BufWinEnter"}, {
pattern = { "*.md", "*.wiki" },
callback = function()
vim.defer_fn(function ()
if not require("zen-mode.view").is_open() then
vim.cmd("ZenMode")
end
end, 100)
end
})
-- Désactiver ZenMode lors du changement de buffer
vim.api.nvim_create_autocmd("BufLeave", {
pattern = "*",
callback = function()
if require("zen-mode.view").is_open() then
vim.cmd("ZenMode")
end
end
})
local function format_and_restore()
vim.cmd("normal! mz") -- Place une marque 'z' à la position actuelle
vim.cmd([[%s/\s\+$//e]]) -- Supprime les espaces inutiles en fin de ligne
--vim.cmd("normal! gg=G") -- Formate tout le fichier
vim.cmd("normal! `z") -- Retourne à la marque 'z'
end
-- Configure l'autocommand pour le formatage à la sauvegarde
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*", -- Applique à tous les fichiers
callback = function()
local ft = vim.bo.filetype -- Récupère le type de fichier
if ft == "cpp" or ft == "c" then
-- Utilise LSP pour formater le C et C++
vim.lsp.buf.format({ async = false })
elseif ft == "python" then
-- Utilise Black pour formater le Python
vim.cmd("Black")
elseif ft ~= "markdown" then
format_and_restore()
end
end,
})
-- Vérifie si tmux est installé
local function is_tmux_installed()
return vim.fn.executable("tmux") == 1
end
-- Vérifie si on est dans une session tmux
local function is_in_tmux()
return os.getenv("TMUX") ~= nil
end
-- Récupère la couleur d'un groupe de highlight
local function get_hl_color(hl, attr)
local ok, hl_data = pcall(vim.api.nvim_get_hl, 0, { name = hl })
if ok and hl_data[attr] then
return string.format("#%06x", hl_data[attr])
else
return "#444444" -- Gris par défaut si rien trouvé
end
end
-- 📡 Synchronise les couleurs avec Tmux selon le mode
local function set_tmux_status(mode)
if not (is_tmux_installed() and is_in_tmux()) then
return -- Ne fait rien si tmux n'est pas dispo
end
local mode_colors = {
n = get_hl_color("MiniStatuslineModeNormal", "bg"),
i = get_hl_color("MiniStatuslineModeInsert", "bg"),
v = get_hl_color("MiniStatuslineModeVisual", "bg"),
V = get_hl_color("MiniStatuslineModeVisual", "bg"),
[""] = get_hl_color("MiniStatuslineModeVisual", "bg"),
R = get_hl_color("MiniStatuslineModeReplace", "bg"),
c = get_hl_color("MiniStatuslineModeCommand", "bg"),
}
local color = mode_colors[mode] or get_hl_color("MiniStatuslineModeNormal", "bg")
os.execute(string.format("tmux set-option -g status-bg '%s'", color))
end
-- Active la synchronisation Tmux ↔ Neovim
if is_tmux_installed() and is_in_tmux() then
vim.api.nvim_create_autocmd("ModeChanged", {
pattern = "*",
callback = function()
local mode = vim.fn.mode()
set_tmux_status(mode)
end,
})
end