-
Notifications
You must be signed in to change notification settings - Fork 0
/
single-file-version.lua
129 lines (112 loc) · 3.24 KB
/
single-file-version.lua
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
vim.g.tmux_navigater_enabled = true
vim.g.tmux_navigator_pane_nowrap = false
vim.g.tmux_navigater_cross_win = false
local vim_to_tmux_directions = {
h = "L",
j = "D",
k = "U",
l = "R",
}
local locations = {
h = "#{pane_at_left}",
l = "#{pane_at_right}",
j = "#{pane_at_bottom}",
k = "#{pane_at_top}",
}
--- function wrapping navigation in vim
--- @param direction string
local function vim_navigate(direction)
local win = vim.fn.winnr()
if vim_to_tmux_directions[direction] ~= nil then
vim.cmd("wincmd " .. direction)
return win == vim.fn.winnr()
end
end
local function tmux_is_zoom()
return vim.fn.system("tmux display -pF '#{window_zoomed_flag}'") == "1\n"
end
--- function to send tmux command
--- @param direction string
--- @param navi_opts table | nil
local function tmux_aware_navigate(direction, navi_opts)
navi_opts = navi_opts
or {
pane_nowrap = vim.g.tmux_navigator_pane_nowrap,
cross_win = vim.g.tmux_navigater_cross_win,
}
-- NOTE: after calling navigate, we are still in the same window
-- meaning that we should turn to tmux now
local should_go_tmux = vim_navigate(direction)
should_go_tmux = should_go_tmux and vim.g.tmux_navigater_enabled and not tmux_is_zoom()
if not should_go_tmux then
return
end
-- NOTE: it makes no sense to call tmux command if
-- we are not in tmux
if vim.env["TMUX"] == nil or vim.env["TMUX_PANE"] == nil then
return
end
local cmd_list = {
"tmux",
}
local s = {
"select-pane",
"-t",
vim.env["TMUX_PANE"],
"-" .. vim_to_tmux_directions[direction],
}
if navi_opts.pane_nowrap then
local c = '""'
if navi_opts.cross_win then
if direction == "h" then
c = '"previous-window"'
elseif direction == "l" then
c = '"next-window"'
end
end
vim.list_extend(cmd_list, {
"if",
"-F",
"'" .. locations[direction] .. "'",
c,
})
table.insert(cmd_list, '"' .. vim.fn.join(s, " ") .. '"')
else
vim.list_extend(cmd_list, s)
end
local cmd = vim.fn.join(cmd_list, " ")
vim.fn.system(cmd)
end
local function nvim_tmux_navigate_left(opts)
tmux_aware_navigate("h", opts)
end
local function nvim_tmux_navigate_right(opts)
tmux_aware_navigate("l", opts)
end
local function nvim_tmux_navigate_up(opts)
tmux_aware_navigate("k", opts)
end
local function nvim_tmux_navigate_down(opts)
tmux_aware_navigate("j", opts)
end
local default_opts = {
use_default_keymap = true,
}
-- TODO: navigate command can receive args
vim.api.nvim_create_user_command("NTmuxLeft", nvim_tmux_navigate_left, {})
vim.api.nvim_create_user_command("NTmuxRight", nvim_tmux_navigate_right, {})
vim.api.nvim_create_user_command("NTmuxUp", nvim_tmux_navigate_up, {})
vim.api.nvim_create_user_command("NTmuxDown", nvim_tmux_navigate_down, {})
vim.api.nvim_create_user_command("NTmuxToggle", function()
vim.g.tmux_navigater_enabled = not vim.g.tmux_navigater_enabled
end, {})
vim.api.nvim_create_user_command("NTmuxDisable", function()
vim.g.tmux_navigater_enabled = false
end, {})
vim.api.nvim_create_user_command("NTmuxEnable", function()
vim.g.tmux_navigater_enabled = true
end, {})
vim.keymap.set("n", "<c-h>", nvim_tmux_navigate_left)
vim.keymap.set("n", "<c-l>", nvim_tmux_navigate_right)
vim.keymap.set("n", "<c-j>", nvim_tmux_navigate_down)
vim.keymap.set("n", "<c-k>", nvim_tmux_navigate_up)