vim.g.mapleader = " "
vim.keymap.set("n", "<leader>pv", vim.cmd.Ex)
--vim.keymap.set("n", "<leader>n", vim.cmd.Ex)
--vim.keymap.set("n", "<C-b>", vim.cmd.Ex)

--[[
vim.keymap.set("n", "<C-b>", ":NvimTreeToggle<CR>", {
	noremap = true,
})
vim.keymap.set("n", "<leader>n", ":NvimTreeToggle<CR>", {
	noremap = true,
})
--]]
vim.keymap.set("n", "<leader>n", "<CMD>Oil<CR>", { desc = "Open parent directory" })

-- barbar keymaps
-- Move to previous/next
vim.keymap.set("n", "<Tab>", "<Cmd>BufferPrevious<CR>")
vim.keymap.set("n", "<S-Tab>", "<Cmd>BufferNext<CR>")
-- Re-order to previous/next
vim.keymap.set("n", "<C-,>", "<Cmd>BufferMovePrevious<CR>")
vim.keymap.set("n", "<C-.>", "<Cmd>BufferMoveNext<CR>")
-- Close buffer
vim.keymap.set("n", "<C-w>", "<Cmd>BufferClose<CR>")
-- Pin/Unpin buffer
vim.keymap.set("n", "<C-p>", "<Cmd>BufferPin<CR>")
-- Goto buffer in position...
vim.keymap.set("n", "<C-1>", "<Cmd>BufferGoto 1<CR>")
vim.keymap.set("n", "<C-2>", "<Cmd>BufferGoto 2<CR>")
vim.keymap.set("n", "<C-3>", "<Cmd>BufferGoto 3<CR>")
vim.keymap.set("n", "<C-4>", "<Cmd>BufferGoto 4<CR>")
vim.keymap.set("n", "<C-5>", "<Cmd>BufferGoto 5<CR>")
vim.keymap.set("n", "<C-6>", "<Cmd>BufferGoto 6<CR>")
vim.keymap.set("n", "<C-7>", "<Cmd>BufferGoto 7<CR>")
vim.keymap.set("n", "<C-8>", "<Cmd>BufferGoto 8<CR>")
vim.keymap.set("n", "<C-9>", "<Cmd>BufferGoto 9<CR>")
vim.keymap.set("n", "<C-0>", "<Cmd>BufferLast<CR>")

vim.keymap.set("n", "<leader>a", ":lua vim.lsp.buf.code_action()<CR>")

local builtin = require("telescope.builtin")
vim.keymap.set("n", "<leader>ff", builtin.find_files, {})
vim.keymap.set("n", "<leader>fg", builtin.live_grep, {})
vim.keymap.set("n", "<leader>fb", builtin.buffers, {})
vim.keymap.set("n", "<leader>fh", builtin.help_tags, {})

vim.keymap.set("n", "<leader>pp", "<Cmd>Telescope projects<CR>", {})

function _ADD_CURR_DIR_TO_PROJECTS()
	local historyfile = require("project_nvim.utils.path").historyfile
	local curr_directory = vim.fn.expand("%:p:h")
	vim.cmd("!echo " .. curr_directory .. " >> " .. historyfile)
end

vim.cmd("command! ProjectAddManually lua _ADD_CURR_DIR_TO_PROJECTS()")

vim.keymap.set("n", "<leader>d", ":lua vim.lsp.buf.hover()<CR>")

-- Clear highlighting on escape in normal mode
vim.keymap.set("n", "<esc>", ":noh<return><esc>")
vim.keymap.set("n", "<esc>^[", "<esc>^[")
