Summary Files Commits Branches @commit @diff @stats
path: root / lua / mappings.lua
blob: aa38bf9b85604d1ab7ba5abfffa12d1c9b45c520 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
--[=[ Helpers ]=]--

keys = {}
setmetatable(keys,
{
	__index = function (table, index)
		local mod, key = index:match("^(.*)_"), index:match("[^_]*$")
		if mod == nil then
			local upperkey = key:upper()
			if     upperkey == "ESCAPE" then key = "<Esc>"
			elseif #key > 1             then key = "<" .. key .. ">"
			end
			return key
		end
		mod = mod:upper()

		if     mod == "SHIFT"   then key = "<S-" .. key .. ">"
		elseif mod == "CONTROL" then key = "<C-" .. key .. ">"
		elseif mod == "ALT"     then key = "<M-" .. key .. ">"
		elseif mod == "SUPER"   then key = "<D-" .. key .. ">"
		elseif mod ~= nil       then error("Unknown key modifier \"" .. mod .. "\"!")
		end

		return key
	end
})
map = { keys = { lhs = {}, rhs = "" } }
setmetatable(map,
{
	__index = function (table, index)
		table.keys.lhs = {}
		table.keys.rhs = ""
		for i = 1, index:len() do
			table.keys.lhs[i] = index:sub(i, i)
		end
		return table.keys
	end
})
setmetatable(map.keys,
{
	__index = function (table, index)
		table.rhs = table.rhs .. keys[index]
		return table
	end,
	__call = function (table, ...)
		return vim.keymap.set(table.lhs, table.rhs, ...)
	end
})

local function icommand(command)
	return keys.Control_o .. command .. keys.Return
end

--[=[ Mappings ]=]--

-- Tab navigation
map.n.Alt_p("gt")
map.n.Alt_o("gT")

map.i.Alt_p(keys.Control_o .. "gt")
map.i.Alt_o(keys.Control_o .. "gT")

map.t.Alt_p("<C-\\><C-N>gt")
map.t.Alt_o("<C-\\><C-N>gT")

-- Split navigation
local function windownav(letter) return keys.Control_w .. letter .. keys.Control_w .. "=" end
map.nt.Control_l(windownav("l"))
map.nt.Control_h(windownav("h"))
map.nt.Control_j(windownav("j"))
map.nt.Control_k(windownav("k"))

-- Ctrl-S, Ctrl-A, Ctrl-C, Ctrol-X
map.i.Control_s(icommand(":w"))
map.i.Control_a(keys.Escape .. "ggVG")

map.n.Control_s(":w" .. keys.Enter)
map.n.Control_a("ggVG")

map.x.Control_c('"+y')
map.x.Control_d('"+d')

-- Undo/redo
map.i.Control_r(keys.Control_o .. keys.Control_r)
map.i.Control_u(keys.Control_o .. "u")

-- Backspace
map.ic.Control_h(keys.Control_w)
map.ic.Control_BS(keys.Control_w)

-- Move lines
map.n.Alt_j(":m+1" .. keys.Enter)
map.n.Alt_k(":m-2" .. keys.Enter)
map.x.Alt_j(":m '>+1<CR>gv=gv")
map.x.Alt_k(":m '<-2<CR>gv=gv")