local opt = { noremap = true, silent = true }
local keymap = vim.api.nvim_set_keymap

keymap('n', 'gx', '<cmd>!xdg-open <c-r><c-a><cr>', opt)

keymap('n', '<c-,>', '<cmd>silent! NeoSwapPrev<cr>', opt)
keymap('n', '<c-.>', '<cmd>silent! NeoSwapNext<cr>', opt)
keymap('n', '<insert>', '<cmd>Lazy profile<cr>', opt)

-- resize {{{
keymap('n', '<c-up>', '<cmd>resize -2<cr>', opt)
keymap('n', '<c-down>', '<cmd>resize +2<cr>', opt)
keymap('n', '<c-left>', '<cmd>vertical resize -2<cr>', opt)
keymap('n', '<c-right>', '<cmd>vertical resize -2<cr>', opt)
-- }}}
-- indent {{{
keymap('v', '<', '<gv', opt)
keymap('v', '>', '>gv', opt)
vim.cmd([[
nnoremap M :m .+1<CR>==
nnoremap L :m .-2<CR>==
inoremap <c-M> <Esc>:m .+1<CR>==gi
inoremap <c-L> <Esc>:m .-2<CR>==gi
vnoremap M :m '>+1<CR>gv=gv
vnoremap L :m '<-2<CR>gv=gv
imap <c-s> <Esc>[s1z=`]a
]])
keymap('n', '<BS>', ':', { noremap = true })

-- search and replace {{{
-- keymap('n', '<leader>rs', ':s///gI<Left><Left><Left><Left><Left><Left>', { noremap = true })
-- keymap('n', '<leader>rr', ':%s///gI<Left><Left><Left><Left>', { noremap = true })
-- keymap('v', '<leader>r', ':s///gI<Left><Left><Left><Left>', { noremap = true })
-- }}}

function MapDHM()
	keymap('n', 'i', 'l', { noremap = true })
	keymap('n', 'n', 'j', { noremap = true })
	keymap('n', 'e', 'k', { noremap = true })
	keymap('n', 'l', 'i', { noremap = true })
	keymap('n', 'j', 'n', { noremap = true })
	keymap('n', 'J', 'N', { noremap = true })
	keymap('n', 'N', 'J', { noremap = true })
	keymap('x', 'l', 'i', { noremap = true })
	keymap('x', 'i', 'l', { noremap = true })
	keymap('x', 'n', 'j', { noremap = true })
	keymap('x', 'e', 'k', { noremap = true })
	keymap('n', '<nop>', 'I', { noremap = true })
end

function UnMapDHM()
	vim.api.nvim_del_keymap('n', 'l')
	vim.api.nvim_del_keymap('n', 'j')
	vim.api.nvim_del_keymap('v', 'l')
	vim.api.nvim_del_keymap('v', 'j')
	vim.api.nvim_del_keymap('v', 'k')
	vim.api.nvim_del_keymap('n', 'i')
	vim.api.nvim_del_keymap('n', 'J')
	vim.api.nvim_del_keymap('n', 'n')
	vim.api.nvim_del_keymap('n', 'N')
end

local function file_exists(name)
	local f = io.open(name, "r")
	if f ~= nil then
		io.close(f)
		return true
	else
		return false
	end
end

if file_exists('/tmp/colemak') then
	MapDHM()
end
