-- move selection in visual mode
vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv")
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv")

-- move to line start/end
vim.keymap.set('i', "<C-e>", "<C-o>$")
vim.keymap.set('i', "<C-a>", "<C-o>^")

vim.keymap.set({ 'n', 'v' }, "<C-e>", '$')
vim.keymap.set({ 'n', 'v' }, "<C-a>", '^')

-- yank to clipboard
vim.keymap.set("n", "<leader>y", "\"+y")
vim.keymap.set("v", "<leader>y", "\"+y")
vim.keymap.set("n", "<leader>Y", "\"+Y")

-- paste without copy
vim.keymap.set("x", "<leader>p", "\"_dP")

-- delete without copy
vim.keymap.set("n", "<leader>d", "\"_d");
vim.keymap.set("v", "<leader>d", "\"_d");

-- search and replace hovered word
vim.keymap.set("n", "<leader>s", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]])

-- make file executable
vim.keymap.set("n", "<leader>x", "<cmd>!chmod +x %<CR>", {silent = true})

-- insert shebang on top of file
vim.keymap.set("n", "<leader>sb", function ()
    local filetype = vim.bo.filetype
    local mapping = {
        jl = "/usr/bin/env julia",
        julia = "/usr/bin/env julia",
        sh = "bash",
        zsh = "zsh",
        py = "/usr/bin/env python",
        python = "/usr/bin/env python",
    }
    local command = mapping[filetype]
    if (command == nil) then return end
    -- local handle = assert(io.popen(string.format("/bin/zsh -c \"where %s\"", command)))
    -- local result = (handle:lines())()
    -- handle:close()
    local buf = vim.api.nvim_get_current_buf()
    -- print(result)
    vim.api.nvim_buf_set_lines(buf, 0, 0, true, { "#!" .. command })
    vim.api.nvim_command('write')
    local filename = vim.api.nvim_buf_get_name(0)
    assert(io.popen(string.format("/usr/bin/chmod +x \"%s\"", filename)))
end)

-- window splitting
vim.keymap.set("n", "<C-/>", "<cmd>sp<CR>")
vim.keymap.set("n", "<C-\\>", "<cmd>vs<CR>")

-- window switching
vim.keymap.set("n", "<C-h>", "<cmd>wincmd h<CR>")
vim.keymap.set("n", "<C-j>", "<cmd>wincmd j<CR>")
vim.keymap.set("n", "<C-k>", "<cmd>wincmd k<CR>")
vim.keymap.set("n", "<C-l>", "<cmd>wincmd l<CR>")

-- window movement
vim.keymap.set("n", "<C-Left>", "<cmd>wincmd H<CR>")
vim.keymap.set("n", "<C-Right>", "<cmd>wincmd L<CR>")
vim.keymap.set("n", "<C-Up>", "<cmd>wincmd K<CR>")
vim.keymap.set("n", "<C-Down>", "<cmd>wincmd J<CR>")
vim.keymap.set("n", "<C->", "<cmd>wincmd r<CR>")
vim.keymap.set("n", "<C-A-Right>", "<cmd>vert res +1<CR>")
vim.keymap.set("n", "<C-A-Left>", "<cmd>vert res -1<CR>")
vim.keymap.set("n", "<C-A-Up>", "<cmd>res +1<CR>")
vim.keymap.set("n", "<C-A-Down>", "<cmd>res -1<CR>")

-- lsp
vim.keymap.set("n", "<F4>", vim.lsp.buf.code_action)
vim.keymap.set("n", "<F3>", vim.lsp.buf.format)
vim.keymap.set("n", "<leader>gD", vim.lsp.buf.declaration)
vim.keymap.set("n", "<leader>gd", vim.lsp.buf.definition)
vim.keymap.set("n", "<leader>gt", vim.lsp.buf.type_definition)
vim.keymap.set("n", "K", vim.lsp.buf.hover)
vim.keymap.set("n", "<leader>ls", vim.lsp.buf.document_symbol)
vim.keymap.set("n", "<leader>li", vim.lsp.buf.implementation)
vim.keymap.set("n", "<leader>lr", vim.lsp.buf.references)
vim.keymap.set("n", "<F2>", vim.lsp.buf.rename)
vim.keymap.set("n", "<leader>sh", vim.lsp.buf.signature_help)
vim.keymap.set("n", "<leader>do", vim.diagnostic.open_float)
vim.keymap.set("n", "<leader>dn", function() vim.diagnostic.jump({count=1, float=true}) end)
vim.keymap.set("n", "<leader>dp", function() vim.diagnostic.jump({count=-1, float=true}) end)

