require("config.lazy")

-- visual mode  <space> = to line up "=" in selection
vim.keymap.set(
  'v', '<leader>=', ':Tabularize /=<CR>',
  { noremap = true, silent = true, desc = "Align = in selection" }
)

-- za to fold, zm and zR to (un)fold all
vim.cmd("set foldmethod=marker")

vim.opt.mouse = ''
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.rnu = true

-- length of an actual \t character:
vim.opt.tabstop=2
-- length to use when editing text (eg. TAB and BS keys)
-- (0 for 'tabstop', -1 for 'shiftwidth'):
vim.opt.softtabstop=-1
-- length to use when shifting text (eg. <<, >> and == commands)
-- (0 for 'tabstop'):
vim.opt.shiftwidth=0
-- round indentation to multiples of 'shiftwidth' when shifting text
-- (so that it behaves like Ctrl-D / Ctrl-T):
vim.opt.shiftround = true

-- if set, only insert spaces; otherwise insert \t and complete with spaces:
vim.opt.expandtab = true

-- reproduce the indentation of the previous line:
vim.opt.autoindent = true
-- keep indentation produced by 'autoindent' if leaving the line blank:
-- vim.opt.cpoptions+=I
-- try to be smart (increase the indenting level after '{',
-- decrease it after '}', and so on):
vim.opt.smartindent = true
-- a stricter alternative which works better for the C language:
vim.opt.cindent = true

-- keep cursor <lines> away from the top and bottom of the screen
vim.opt.scrolloff = 6

-- marker at line 80
vim.opt.colorcolumn = "80"

-- use language-specific plugins for indenting (better):
vim.cmd("filetype plugin indent on")
vim.cmd("syntax on")

vim.keymap.set('n', 'p', '<Cmd>pu<CR>')

vim.g.mapleader = " "
vim.keymap.set("n", "<leader>ll", vim.cmd.Ex)

-- Buffer switch
vim.keymap.set('n', '<TAB>', vim.cmd.bnext, { desc = 'Next buffer' })
vim.keymap.set('n', '<S-TAB>', vim.cmd.bprev, { desc = 'Previous buffer' })

-- Telescope
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.git_files, { desc  = 'Telescope find git files' })
vim.keymap.set("n", "<leader>bb", builtin.buffers, { desc    = 'Telescope choose buffer' })
vim.keymap.set('n', '<leader>fG', function()
  builtin.grep_string({ search = vim.fn.input("grep > ") } );
end, { desc = 'Telescope grep files' })

-- UndoTree
vim.keymap.set("n", "<leader>u", vim.cmd.UndotreeToggle)
vim.opt.undodir = os.getenv("HOME") .. "/.vim/undodir"
vim.opt.undofile = true

-- Reserve a space in the gutter
vim.opt.signcolumn = 'yes'

-- move paragraphs
vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv", { desc = 'Move selected paragraph down' })
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv", { desc = 'Move selected paragraph up' })

-- cut to system clipboard
vim.keymap.set("n", "<leader>y", "\"+y", { desc = 'Paste to system clipboard' })
vim.keymap.set("v", "<leader>y", "\"+y", { desc = 'Paste to system clipboard' })
vim.keymap.set("n", "<leader>Y", "\"+Y", { desc = 'Paste to system clipboard' })

--
-- Diagnostics
--
vim.diagnostic.config({

  -- Summary diagnostics at end of line for every error
  virtual_text = {
    virt_text_win_vol = 3,
  },

  -- All diagnostics for the current line only
  virtual_lines = {
    current_line = true
  }

})

vim.keymap.set("n", "]g", vim.diagnostic.goto_next)
vim.keymap.set("n", "[g", vim.diagnostic.goto_prev)
