local autocmds = {
	{ -- Handles the automatic line numeration changes
		{ "BufEnter", "FocusGained", "InsertLeave", "WinEnter" },
		{
			pattern = "*",
			command = 'if &nu && mode() != "i" | set rnu | endif',
		},
	},
	{ -- Handles the automatic line numeration changes
		{ "BufLeave", "FocusLost", "InsertEnter", "WinLeave" },
		{
			pattern = "*",
			command = "if &nu | set nornu | endif",
		},
	},
	{ -- Use 'q' to quit from common plugins
		"FileType",
		{
			pattern = { "help", "man" },
			callback = function()
				vim.cmd([[
                  nnoremap <silent> <buffer> q :close<CR> 
                  set nobuflisted 
                ]])
			end,
		},
	},
	{
		"Filetype",
		{
			pattern = { "gitcommit", "markdown" },
			callback = function()
				vim.opt_local.spell = true
			end,
		},
	},
	{ -- Fix auto comment
		"BufWinEnter",
		{
			command = "set formatoptions-=cro",
		},
	},
	{ -- Highlight yanked text
		"TextYankPost",
		{
			callback = function()
				vim.highlight.on_yank({ higroup = "Visual", timeout = 200 })
			end,
		},
	},
	{ -- Trigger shortcuts script
		"BufWritePost",
		{
            pattern = { "directories", "files" },
            command = "!shortcuts"
		},
	},
	{ -- Trigger xrdb
		"BufWritePost",
		{
            pattern = { "xresources" },
            command = "!xrdb %"
		},
	},
}

vim.api.nvim_create_augroup("user_config", { clear = true })
for _, entry in ipairs(autocmds) do
	local event = entry[1]
	local opts = entry[2]
	if type(opts.group) == "string" and opts.group ~= "" then
		local exists, _ = pcall(vim.api.nvim_get_autocmds, { group = opts.group })
		if not exists then
			vim.api.nvim_create_augroup(opts.group, {})
		end
	end
	vim.api.nvim_create_autocmd(event, opts)
end