-- set leader key
vim.g.mapleader = " "
vim.g.maplocalleader = " "

-- disable the spacebar key's default behaviour in normal and visual modes
vim.keymap.set({ "n", "v" }, "<Space>", "<Nop>", { silent = true })

-- command options
local opts = { noremap = true, silent = true }

-- save & quit file
vim.keymap.set("n", "<C-s>", "<cmd> w <CR>", opts)
vim.keymap.set("n", "<C-q>", "<cmd> q <CR>", opts)

-- Allow moving the cursor through wrapped lines with j, k
vim.keymap.set("n", "k", "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
vim.keymap.set("n", "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })

-- clear highlights
vim.keymap.set("n", "<Esc>", ":noh<CR>", opts)

-- delete single character without copying into register
-- also allows to delete selection without copying into register
vim.keymap.set({ "n", "v" }, "x", '"_x', opts)

-- Vertical scroll and center
vim.keymap.set("n", "<C-d>", "<C-d>zz", opts)
vim.keymap.set("n", "<C-u>", "<C-u>zz", opts)

-- Find and center
vim.keymap.set("n", "n", "nzzzv", opts)
vim.keymap.set("n", "N", "Nzzzv", opts)

-- Resize with arrows
vim.keymap.set("n", "<S-Up>", ":resize -1<CR>", opts)
vim.keymap.set("n", "<S-Down>", ":resize +1<CR>", opts)
vim.keymap.set("n", "<S-Left>", ":vertical resize -1<CR>", opts)
vim.keymap.set("n", "<S-Right>", ":vertical resize +1<CR>", opts)

-- Buffers
-- vim.keymap.set("n", "<C-S-f>", ":bnext<CR>", opts)
-- vim.keymap.set("n", "<C-S-d>", ":bprevious<CR>", opts)
vim.keymap.set("n", "<leader>x", ":bdelete!<CR>", opts) -- close buffer
vim.keymap.set("n", "<leader>b", "<cmd> enew <CR>", opts) -- new buffer

-- Increment/decrement numbers
vim.keymap.set("n", "<leader>+", "<C-a>", opts) -- increment
vim.keymap.set("n", "<leader>-", "<C-x>", opts) -- decrement

-- Window management
vim.keymap.set("n", "<leader>v", "<C-w>v", opts) -- split window vertically
vim.keymap.set("n", "<leader>h", "<C-w>s", opts) -- split window horizontally
vim.keymap.set("n", "<leader>se", "<C-w>=", opts) -- make split windows equal width & height

-- Navigate between splits
vim.keymap.set("n", "<C-k>", ":wincmd k<CR>", opts)
vim.keymap.set("n", "<C-j>", ":wincmd j<CR>", opts)
vim.keymap.set("n", "<C-h>", ":wincmd h<CR>", opts)
vim.keymap.set("n", "<C-l>", ":wincmd l<CR>", opts)

-- Toggle line wrapping
vim.keymap.set("n", "<leader>lw", "<cmd>set wrap!<CR>", opts)

-- Press jk fast to exit insert mode
vim.keymap.set("i", "jk", "<ESC>", opts)
vim.keymap.set("i", "kj", "<ESC>", opts)

-- Move text up and down
vim.keymap.set({ "n", "v" }, "<A-j>", ":m .+1<CR>==", opts)
vim.keymap.set({ "n", "v" }, "<A-k>", ":m .-1<CR>==", opts)

-- Stay in indent mode
vim.keymap.set("v", "<", "<gv", opts)
vim.keymap.set("v", ">", ">gv", opts)

-- Keep last yanked when pasting
vim.keymap.set("v", "p", '"_dP', opts)

-- Tabs
vim.keymap.set("n", "<leader>tn", ":tabnew<CR>", opts) -- open new tab
vim.keymap.set("n", "<leader>tw", ":tabclose<CR>", opts) -- close current tab
