vim.opt.tabstop = 8
vim.opt.softtabstop = 4
vim.opt.shiftwidth = 4
vim.opt.expandtab = false

vim.opt.number = true
vim.opt.relativenumber = true

vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.undofile = true

vim.opt.hlsearch = false
vim.opt.incsearch = true

vim.opt.termguicolors = true
vim.opt.cursorline = true
vim.opt.scrolloff = 8
vim.opt.updatetime = 50
vim.opt.clipboard = "unnamedplus"
vim.opt.linebreak = false
vim.cmd[[let &showbreak = "> "]]

if vim.fn.executable('rg') > 0 then
    vim.opt.grepprg = 'rg --vimgrep'
end

vim.opt.background = "light"
vim.cmd[[colorscheme nofrils-acme]]

function SafeCListNav(navdir)
  -- dir:
  --  1 is cnext (forwards)
  -- -1 is cprev (backwards)
  local dircmd = ''
  if navdir == 1 then
    dircmd = 'cnext'
  elseif navdir == -1 then
    dircmd = 'cprev'
  end
  local status, result = pcall(vim.cmd, dircmd)

  if status == false then
    return nil
  end
end

-- @section: key remaps
vim.keymap.set('n', "<space><space>x", "<cmd>source %<CR>")
vim.keymap.set('n', "<space>vs", "<cmd>vsplit<CR>")
vim.keymap.set('n', "<space>hs", "<cmd>split<CR>")
vim.keymap.set('n', "<space>pv", ":Ex <CR>")
vim.keymap.set('i', "<C-l>", "<Del>")

-- @block: c-list navigation
vim.keymap.set("n", "<A-n>", function() SafeCListNav(1) end)
vim.keymap.set("n", "<A-p>", function() SafeCListNav(-1) end)
vim.keymap.set("n", "<A-w>", vim.cmd.cw)

--[[ 
    @block: tab navigation
    @info helpful builtin tab shortcuts
    gt: Next tab
    gT: Previous tab
    ngt: Go to tab #n
--]]
vim.keymap.set("n", "<A-w><A-c>", ":tabnew ")
vim.keymap.set("n", "<A-m>", ":tabnext ")
vim.keymap.set("n", "<A-[>", ":tabprev<CR>")
vim.keymap.set("n", "<A-]>", ":tabnext<CR>")


-- @func: autoload_on_filechange
vim.opt.autoread = true
vim.api.nvim_create_autocmd("CursorHold", {
    desc = "Command to reload the file when changes",
    command = "silent! checktime",
    group = vim.api.nvim_create_augroup("FileChangeGroup", { clear = true }),
})

vim.api.nvim_create_autocmd("TextYankPost", {
  desc = "Command to highlight selection upon yanking",
  group = vim.api.nvim_create_augroup("VimYankGroup", { clear = true }),
  callback = function()
	  vim.highlight.on_yank()
  end,
})

-- @func: write lines using lua
function WriteText(toWrite)
    if not toWrite then
	return
    end
    local posYX = vim.api.nvim_win_get_cursor(0)
    vim.api.nvim_buf_set_text(0, posYX[1]-1, posYX[2], posYX[1]-1, posYX[2], { toWrite })
end