local augroup = vim.api.nvim_create_augroup('plex_generic_autocmds', {})

-- enable text wrapping for text filetypes
vim.api.nvim_create_autocmd('FileType', {
  group = augroup,
  pattern = {
    'tex',
    'text',
    'markdown',
    'help',
    'typst',
  },
  callback = function()
    vim.opt_local.wrap = true
  end,
})

-- use html for htmldjango
vim.api.nvim_create_autocmd('FileType', {
  group = augroup,
  pattern = {
    'html',
  },
  callback = function()
    vim.bo.filetype = 'html'
  end,
})

-- always use the tex filetype for latex things, as those are most supported by
-- latex language servers.
vim.api.nvim_create_autocmd('FileType', {
  group = augroup,
  pattern = {
    'latex',
    'plaintex',
  },
  callback = function()
    vim.bo.filetype = 'tex'
  end,
})

-- conceallevel is what converts things like **bold** to be displayed as bold
-- without the stars. This is useful, but we might not want it for some
-- filetypes.
vim.api.nvim_create_autocmd('FileType', {
  group = augroup,
  pattern = {
    'latex',
    'markdown',
    'plaintex',
    'tex',
    'typst',
  },
  callback = function()
    vim.opt_local.conceallevel = 0
    vim.opt_local.concealcursor = ''
  end,
})

-- Enable spellcheck for some types
vim.api.nvim_create_autocmd('FileType', {
  group = augroup,
  pattern = {
    'latex',
    'markdown',
    'typst',
    'plaintex',
    'tex',
  },
  callback = function()
    vim.cmd [[set spell]]
  end,
})

-- disable auto formatting for some filetypes
vim.api.nvim_create_autocmd('FileType', {
  group = augroup,
  pattern = {
    'TelescopePrompt',
  },
  callback = function()
    vim.opt_local.formatoptions = ''
  end,
})

-- tweak colorschemes and highlights after loading a new colorscheme
vim.api.nvim_create_autocmd('ColorScheme', {
  group = augroup,
  callback = function()
    vim.cmd [[highlight Comment gui=NONE]] -- no italics for comments
  end,
})
