-- vim: fdm=marker

-- Enable modelines
vim.o.modeline = true

-- Enable hybrid line numbers
vim.o.number = true
vim.o.relativenumber = true

-- Indent with two spaces by default
vim.o.tabstop = 2
vim.o.shiftwidth = 2
vim.o.expandtab = true

-- Enable smart indentation
vim.o.smartindent = true

-- Disable line wrapping
vim.o.wrap = false

-- Highlight the current line
vim.o.cursorline = true

-- Enable 24-bit RGB colors
vim.o.termguicolors = true

-- Enable syntax highlighting and filetype plugins
vim.cmd('syntax enable')
vim.cmd('filetype plugin indent on')

-- Set leader key to space
vim.g.mapleader = ' '

-- Set local leader to backslash
vim.g.maplocalleader = '\\'

-- Save/quit with <Leader-[wq]>
vim.api.nvim_set_keymap('n', '<Leader>w', ':w<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<Leader>q', ':q<CR>', { noremap = true, silent = true })

-- Navigate between splts with <C-[hjkl]>
vim.api.nvim_set_keymap('n', '<C-h>', '<C-w>h', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-j>', '<C-w>j', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-k>', '<C-w>k', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-l>', '<C-w>l', { noremap = true, silent = true })

-- Packages {{{

-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  local lazyrepo = "https://github.com/folke/lazy.nvim.git"
  local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
  if vim.v.shell_error ~= 0 then
    vim.api.nvim_echo({
      { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
      { out, "WarningMsg" },
      { "\nPress any key to exit..." },
    }, true, {})
    vim.fn.getchar()
    os.exit(1)
  end
end
vim.opt.rtp:prepend(lazypath)

-- Setup lazy.nvim
require("lazy").setup({
  spec = {
    {
      'nvim-telescope/telescope.nvim', tag = '0.1.8',
      dependencies = { { 'nvim-lua/plenary.nvim' }, { 'BurntSushi/ripgrep'}, { 'nvim-tree/nvim-web-devicons' } },
      config = function()
        require('telescope').setup({})
        local builtin = require('telescope.builtin')
        vim.keymap.set('n', '<Leader>ff', builtin.find_files, { desc = 'Telescope: Find files' })
        vim.keymap.set('n', '<Leader>fg', builtin.live_grep, { desc = 'Telescope: Live grep' })
        vim.keymap.set('n', '<Leader>fb', builtin.buffers, { desc = 'Telescope: Buffers' })
        vim.keymap.set('n', '<Leader>fh', builtin.help_tags, { desc = 'Telescope: Help tags' })
      end,
    },

    {
      'folke/TokyoNight.nvim',
      config = function()
        vim.cmd('colorscheme tokyonight-storm')
      end
    },

    {
      'hrsh7th/nvim-cmp',
      event = 'InsertEnter',
      dependencies = { { 'hrsh7th/cmp-buffer' }, { 'hrsh7th/cmp-path' }, { 'delphinus/cmp-ctags' } },
      config = function()
        local cmp = require('cmp')
        cmp.setup({
          mapping = cmp.mapping.preset.insert({
            ['<C-Space>'] = cmp.mapping.complete(),
            ['<CR>'] = cmp.mapping.confirm({ select = true }),
            ['<C-e>'] = cmp.mapping.abort(),
            ['<Tab>'] = cmp.mapping.select_next_item(),
            ['<S-Tab>'] = cmp.mapping.select_prev_item(),
          }),
          sources = {
            { name = 'buffer' },
            { name = 'path' },
            { name = 'ctags' },
          }
        })
      end
    },

    { 'ludovicchabant/vim-gutentags' },
  },

  -- Configure any other settings here. See the documentation for more details.
  -- colorscheme that will be used when installing plugins.
  install = { colorscheme = { "habamax" } },
  -- automatically check for plugin updates
  checker = { enabled = true },
})

-- }}}
