-- unset space so it can be used as leader
vim.keymap.set("n", " ", "", { silent = true, remap = false })

-- source config
vim.keymap.set("n", "<leader>o", ":source $HOME/.config/nvim/init.lua<CR>")

-- move around windows
vim.keymap.set("n", "<C-h>", "<C-w><C-h>", { desc = "focus to l window" })
vim.keymap.set("n", "<C-l>", "<C-w><C-l>", { desc = "focus to r window" })
vim.keymap.set("n", "<C-j>", "<C-w><C-j>", { desc = "focus to dwn window" })
vim.keymap.set("n", "<C-k>", "<C-w><C-k>", { desc = "focus to up window" })

-- move lines up and down
vim.keymap.set("n", "<A-j>", ":m .+1<CR>==")
vim.keymap.set("n", "<A-k>", ":m .-2<CR>==")
vim.keymap.set("v", "<A-j>", ":m '>+1<CR>gv=gv")
vim.keymap.set("v", "<A-k>", ":m '<-2<CR>gv=gv")

-- quote around highlighted words
vim.keymap.set('v', "'", "c''<Esc>P")
vim.keymap.set('v', '"', 'c""<Esc>P')

-- non-floating terminal
vim.keymap.set('n', 'T', ":belowright split | terminal<CR> | :resize 8<CR>i")

-- toggle spellcheck
vim.keymap.set('n', '<leader>st', ':set spell!<CR>')

-- toggle nvim tree
vim.keymap.set("n", "<leader>e", ":Lex<CR>")

-- mini pick
vim.keymap.set("n", "<leader>p", ":Pick files<CR>")


-- barbar
vim.keymap.set("n", "<leader>x", ":BufferClose<CR>")
-- (I need both options, one for windows, one for linux)
vim.keymap.set("n", "<A-h>", ":BufferPrevious<CR>")
vim.keymap.set("n", "<A-l>", ":BufferNext<CR>")
vim.keymap.set("n", "<A-u>", "<Cmd>BufferPrevious<CR>")
vim.keymap.set("n", "<A-i>", "<Cmd>BufferNext<CR>")
vim.keymap.set("n", "<A-U>", "<Cmd>BufferPrevious<CR>")
vim.keymap.set("n", "<A-I>", "<Cmd>BufferNext<CR>")
vim.keymap.set("n", "<A-H>", "<Cmd>BufferMovePrevious<CR>")
vim.keymap.set("n", "<A-L>", "<Cmd>BufferMoveNext<CR>")
vim.keymap.set("n", "<A-X>", "<Cmd>BufferCloseAllButCurrent<CR>")

-- format
vim.keymap.set("n", "<leader>f", ":lua vim.lsp.buf.format()<CR>")

-- diagnostics
vim.keymap.set("n", "<leader>ds", ":lua vim.diagnostic.open_float()<CR>")
vim.keymap.set("n", "<leader>dn", function() vim.diagnostic.jump({ count = 1 }) end)
vim.keymap.set("n", "<leader>dp", function() vim.diagnostic.jump({ count = -1 }) end)

-- markdown autocommands
vim.api.nvim_create_autocmd('FileType', {
  pattern = 'markdown',
  callback = function()
		vim.keymap.set("n", "<leader>[", "i- [ ] <CR><Esc>")
		vim.keymap.set("n", "m", "^3<right>rx")
		vim.keymap.set("n", "M", "^3<right>r ")
	end
})
