-- vim: ft=lua:foldmethod=marker:foldmarker=[[[,]]]

local Path = require('plenary.path')

local g = vim.g
local o = vim.o
local wo = vim.wo
local bo = vim.bo
local cmd = vim.cmd
local api = vim.api

-- Line numbers [[[
  wo.number = true
  wo.relativenumber = true
-- ]]]

-- Indenting [[[
  bo.autoindent = true
  bo.smartindent = true
  o.smarttab = true
  bo.expandtab = true
  bo.tabstop = 2
  bo.shiftwidth = 2
-- ]]]

-- Backup and swap files [[[
  local cache_dir = Path:new(vim.env.HOME .. '/.cache/nvim/tmp')
  if not cache_dir:exists() then
    cache_dir:mkdir({mode = 493})
  end

  o.backupcopy = 'yes'
  g.backupdir = cache_dir:expand()
  o.updatetime = 300
  bo.swapfile = false
-- ]]]

-- Theme [[[
  -- cmd 'colorscheme monokai'
  -- o.termguicolors = true
  -- o.background = 'dark'
  cmd 'colorscheme catppuccin'
-- ]]]

-- Airline [[[
  -- g.airline_theme = 'molokai'
  g.airline_theme = 'catppuccin'
  g.airline_powerline_fonts = 1
-- ]]]

-- Undo stuff [[[
  if vim.fn.exists('+undofile') then
    local undo_dir = Path:new(vim.env.HOME .. '/.cache/nvim/undo')
    if not undo_dir:exists() then
      undo_dir:mkdir({mode = 493})
    end

    o.undofile = true
    o.undodir = undo_dir:expand()
    o.undolevels = 500
  end
-- ]]]

-- Misc [[[
  wo.signcolumn = 'auto'
  wo.colorcolumn = '+1'
  -- bo.textwidth = 120
  o.wildmenu = true
  o.hlsearch = true
  wo.foldmethod = 'marker'
  o.exrc = true
  o.secure = true
  wo.wrap = false
-- ]]]

-- netrw [[[
  g.netrw_banner = 1
  g.netrw_liststyle = 3
  g.netrw_altv = 1
  g.netrw_winsize = 20
  g.netrw_preview = 1
-- ]]]

-- Autocommands [[[
  function create_augroup(autocmds, name)
    cmd('augroup ' .. name)
    cmd('autocmd!')
    for _, autocmd in ipairs(autocmds) do
      cmd('autocmd ' .. table.concat(autocmd, ' '))
    end
    cmd('augroup END')
  end

  if not vim.fn.has('gui_running') then
    o.timeoutlen = 1000

    create_augroup({
      { 'InsertEnter', '*', 'set timeoutlen=0' },
      { 'InsertLeave', '*', 'set timeoutlen=1000' }
    }, 'fast_escape')
  end

  if vim.fn.has('autocmd') then
    create_augroup({
        { 'FileType', 'text', 'setlocal', 'textwidth=78' },
        { 'FileType', 'gitcommit', 'setlocal', 'spell' },
        { 'FileType', 'markdown', 'setlocal', 'spell' },
        { 'FileType', 'markdown', 'setlocal', 'wrap' },
        { 'BufNewFile,BufRead', '*.ex,*.eex', 'setf', 'elixir' },
        { 'BufNewFile,BufRead', '*.eex,*.leex,*.heex,*.sface', 'setf', 'eelixir' },
        { 'BufNewFile,BufRead', 'mix.lock', 'set', 'filetype=elixir' },
    }, 'filetypes')
  end
-- ]]]

-- Ignores [[[
  vim.opt.wildignore = {
    '*/.git/*',
    '*/tmp/*',
    '*.swp',
    '*/.DS_Store',
    '*/vendor',
    '*/env/*',
    '*/deps/*',
    '*/_build/*',
    '*/elixir_ls/*',
    '*/node_modules/*',
    '*/*.ghc',
    '*/*.o'
  }
-- ]]]