local H=vim.api local P=H.nvim_create_augroup("\084\114\105\109\087\104\105\116\101\083\112\097\099\101\071\114\112",{clear=true})H.nvim_create_autocmd("\066\117\102\087\114\105\116\101\080\114\101",{command="\058\037\115\047\092\115\092\043\036\047\047\101";group=P})H.nvim_create_autocmd("\066\117\102\069\110\116\101\114",{command="\115\101\116 \102\111\114\109\097\116\111\112\116\105\111\110\115-\061\099\114\111"})H.nvim_create_autocmd("\070\105\108\101\116\121\112\101",{pattern="\109\097\105\108",callback=function()vim.opt.textwidth=0 vim.opt.wrapmargin=0 vim.opt.wrap=true vim.opt.linebreak=true vim.opt.columns=80 vim.opt.colorcolumn="\056\048"end})H.nvim_create_autocmd({"\066\117\102\082\101\097\100";"\066\117\102\078\101\119\070\105\108\101"},{pattern={"\042\046\116\121\112"};callback=function()vim.api.nvim_command("\115\101\116 \102\105\108\101\116\121\112\101\061\116\121\112\115\116")end})H.nvim_create_autocmd("\070\105\108\101\116\121\112\101",{pattern="\116\101\114\114\097\102\111\114\109-\118\097\114\115";callback=function()vim.api.nvim_command("\115\101\116 \102\105\108\101\116\121\112\101\061\104\099\108")end})H.nvim_create_autocmd("\084\101\120\116\089\097\110\107\080\111\115\116",{callback=function()vim.highlight.on_yank()end})H.nvim_create_autocmd("\066\117\102\082\101\097\100\080\111\115\116",{callback=function()local H=vim.api.nvim_buf_get_mark(0,"\"")local P=vim.api.nvim_buf_line_count(0)if H[1]>0 and H[1]<=P then pcall(vim.api.nvim_win_set_cursor,0,H)end end})H.nvim_create_autocmd("\070\105\108\101\084\121\112\101",{pattern={"\100\097\112-\102\108\111\097\116";"\102\117\103\105\116\105\118\101";"\104\101\108\112";"\109\097\110";"\110\111\116\105\102\121";"\113\102";"\080\108\101\110\097\114\121\084\101\115\116\080\111\112\117\112","\115\116\097\114\116\117\112\116\105\109\101","\116\115\112\108\097\121\103\114\111\117\110\100"};callback=function(H)vim.bo[H.buf].buflisted=false vim.keymap.set("\110","\113","\060\099\109\100\062\099\108\111\115\101\060\099\114\062",{buffer=H.buf;silent=true})end})H.nvim_create_autocmd("\070\105\108\101\084\121\112\101",{pattern="\109\097\110",command="\110\110\111\114\101\109\097\112 \060\098\117\102\102\101\114\062\060\115\105\108\101\110\116\062 \113 \058\113\117\105\116\060\067\082\062"})H.nvim_create_autocmd("\070\105\108\101\084\121\112\101",{pattern={"\078\101\111\071\105\116\083\116\097\116\117\115"};command="\115\101\116\108\111\099\097\108 \108\105\115\116\033"})local g=H.nvim_create_augroup("\067\117\114\115\111\114\076\105\110\101",{clear=true})H.nvim_create_autocmd({"\073\110\115\101\114\116\076\101\097\118\101";"\087\105\110\069\110\116\101\114"},{pattern="\042",command="\115\101\116 \099\117\114\115\111\114\108\105\110\101";group=g})H.nvim_create_autocmd({"\073\110\115\101\114\116\069\110\116\101\114","\087\105\110\076\101\097\118\101"},{pattern="\042";command="\115\101\116 \110\111\099\117\114\115\111\114\108\105\110\101";group=g})local B=H.nvim_create_augroup("\097\108\112\104\097\095\111\110\095\101\109\112\116\121",{clear=true})H.nvim_create_autocmd("\085\115\101\114",{pattern="\066\068\101\108\101\116\101\080\111\115\116\042";group=B;callback=function(H)local P=vim.api.nvim_buf_get_name(H.buf)local g=vim.api.nvim_buf_get_option(H.buf,"\102\105\108\101\116\121\112\101")local B=P==""and g==""if B then vim.api.nvim_command("\065\108\112\104\097")vim.api.nvim_command(H.buf.."\098\119\105\112\101\111\117\116")end end})H.nvim_create_autocmd({"\066\117\102\082\101\097\100";"\066\117\102\078\101\119\070\105\108\101"},{pattern={"\042\046\116\120\116";"\042\046\109\100","\042\046\116\101\120"};callback=function()vim.opt.spell=true vim.opt.spelllang="\101\110"end})
local api = vim.api

--- Remove all trailing whitespace on save
local TrimWhiteSpaceGrp = api.nvim_create_augroup('TrimWhiteSpaceGrp', { clear = true })
api.nvim_create_autocmd('BufWritePre', {
  command = [[:%s/\s\+$//e]],
  group = TrimWhiteSpaceGrp,
})

-- don't auto comment new line
api.nvim_create_autocmd('BufEnter', { command = [[set formatoptions-=cro]] })

-- wrap words "softly" (no carriage return) in mail buffer
api.nvim_create_autocmd('Filetype', {
  pattern = 'mail',
  callback = function()
    vim.opt.textwidth = 0
    vim.opt.wrapmargin = 0
    vim.opt.wrap = true
    vim.opt.linebreak = true
    vim.opt.columns = 80
    vim.opt.colorcolumn = '80'
  end,
})

-- detect typst filetype
api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
  pattern = { '*.typ' },
  callback = function()
    vim.api.nvim_command 'set filetype=typst'
  end,
})

-- detect terraform vars
api.nvim_create_autocmd('Filetype', {
  pattern = 'terraform-vars',
  callback = function()
    vim.api.nvim_command 'set filetype=hcl'
  end,
})

-- Highlight on yank
api.nvim_create_autocmd('TextYankPost', {
  callback = function()
    vim.highlight.on_yank()
  end,
})

-- go to last loc when opening a buffer
api.nvim_create_autocmd('BufReadPost', {
  callback = function()
    local mark = vim.api.nvim_buf_get_mark(0, '"')
    local lcount = vim.api.nvim_buf_line_count(0)
    if mark[1] > 0 and mark[1] <= lcount then
      pcall(vim.api.nvim_win_set_cursor, 0, mark)
    end
  end,
})

-- windows to close with "q"
api.nvim_create_autocmd('FileType', {
  pattern = {
    'dap-float',
    'fugitive',
    'help',
    'man',
    'notify',
    -- 'null-ls-info',
    'qf',
    'PlenaryTestPopup',
    'startuptime',
    'tsplayground',
  },
  callback = function(event)
    vim.bo[event.buf].buflisted = false
    vim.keymap.set('n', 'q', '<cmd>close<cr>', { buffer = event.buf, silent = true })
  end,
})
api.nvim_create_autocmd('FileType', { pattern = 'man', command = [[nnoremap <buffer><silent> q :quit<CR>]] })

-- disable list option in certain filetypes
api.nvim_create_autocmd('FileType', { pattern = { 'NeoGitStatus' }, command = [[setlocal list!]] })

-- show cursor line only in active window
local cursorGrp = api.nvim_create_augroup('CursorLine', { clear = true })
api.nvim_create_autocmd({ 'InsertLeave', 'WinEnter' }, {
  pattern = '*',
  command = 'set cursorline',
  group = cursorGrp,
})
api.nvim_create_autocmd({ 'InsertEnter', 'WinLeave' }, { pattern = '*', command = 'set nocursorline', group = cursorGrp })

-- when there is no buffer left show Alpha dashboard
-- requires "famiu/bufdelete.nvim" and "goolord/alpha-nvim"
local alpha_on_empty = api.nvim_create_augroup('alpha_on_empty', { clear = true })
api.nvim_create_autocmd('User', {
  pattern = 'BDeletePost*',
  group = alpha_on_empty,
  callback = function(event)
    local fallback_name = vim.api.nvim_buf_get_name(event.buf)
    local fallback_ft = vim.api.nvim_buf_get_option(event.buf, 'filetype')
    local fallback_on_empty = fallback_name == '' and fallback_ft == ''

    if fallback_on_empty then
      -- require("neo-tree").close_all()
      vim.api.nvim_command 'Alpha'
      vim.api.nvim_command(event.buf .. 'bwipeout')
    end
  end,
})

-- Enable spell checking for certain file types
api.nvim_create_autocmd(
  { 'BufRead', 'BufNewFile' },
  -- { pattern = { "*.txt", "*.md", "*.tex" }, command = [[setlocal spell<cr> setlocal spelllang=en,de<cr>]] }
  {
    pattern = { '*.txt', '*.md', '*.tex' },
    callback = function()
      vim.opt.spell = true
      vim.opt.spelllang = 'en' -- english only for now
    end,
  }
)
