index : neolij.nvim
Seamless Neovim/Zellij integration with built-in navigation, pane/tab management, and Lua scripting.
about summary refs log tree commit diff
neolij.nvim / lua / zellij / init.lua
-rw-r--r-- blob a2c22e8842cf1f277288c78e81de024fcfc94da3 4548 bytes (plain)
1
local M = {}
2
3
local direction_translation = {
4
	up = "k",
5
	down = "j",
6
	left = "h",
7
	right = "l",
8
}
9
-- Run zellij action safely
10
function M.zellij_action(args)
11
	if not os.getenv("ZELLIJ") then
12
		return
13
	end
14
	vim.fn.system("zellij action " .. args)
15
	if vim.v.shell_error ~= 0 then
16
		error("zellij executable not found in PATH")
17
	end
18
end
19
20
-- Try vim window move, fallback to zellij
21
local function navigate(direction, vim_key, fallback_zellij, fallback_nvim)
22
	local before = vim.fn.winnr()
23
	vim.cmd("wincmd " .. vim_key)
24
	local after = vim.fn.winnr()
25
26
	-- If vim didn't move, fallback to zellij
27
	if before == after then
28
    if fallback_nvim then
29
      local current_tab = vim.fn.tabpagenr()
30
      local last_tab = vim.fn.tabpagenr("$")
31
32
      -- 2. Fallback to Tab switching based on direction
33
      -- We'll use 'l' (right) and 'h' (left) to trigger tab movement
34
      if direction == "right" and current_tab < last_tab then
35
        vim.cmd("tabnext")
36
        return -- Success
37
      elseif direction == "left" and current_tab > 1 then
38
        vim.cmd("tabprevious")
39
        return -- Success
40
      end
41
42
        -- If we reach here, neither window nor tab could move: escape to Zellij
43
        M.zellij_action(fallback_zellij .. " " .. direction)
44
    end
45
	end
46
end
47
48
-- Window navigation
49
function M.move(direction)
50
	navigate(direction, direction_translation[direction], "move-focus")
51
end
52
53
-- Window navigation or tab switch
54
function M.move_or_tab(direction, fallback_nvim)
55
	navigate(direction, direction_translation[direction], "move-focus-or-tab", fallback_nvim)
56
end
57
58
-- Tab actions
59
function M.new_tab(opts)
60
	if type(opts) == "table" then
61
		opts = opts.args
62
	end
63
64
	if opts ~= "" then
65
		M.zellij_action("new-tab " .. opts)
66
	else
67
		M.zellij_action("new-tab")
68
	end
69
end
70
71
function M.rename_tab(name)
72
	if type(name) == "table" then
73
		name = name.args
74
	end
75
76
	if name == "" or name == nil then
77
		name = vim.fn.input("Tab name: ")
78
	end
79
80
	if name == nil or name == "" then
81
		vim.notify("No tab name provided", vim.log.levels.ERROR)
82
		return
83
	end
84
85
	M.zellij_action("rename-tab " .. name)
86
end
87
88
function M.move_tab(direction)
89
	M.zellij_action("move-tab " .. direction)
90
end
91
92
-- Pane actions
93
function M.new_pane(opts)
94
	if type(opts) == "table" then
95
		opts = opts.args
96
	end
97
98
	if opts ~= "" then
99
		-- allow custom flags like -d right, -n name, etc
100
		M.zellij_action("new-pane " .. opts)
101
	else
102
		M.zellij_action("new-pane")
103
	end
104
end
105
106
function M.rename_pane(name)
107
	if type(name) == "table" then
108
		name = name.args
109
	end
110
111
	if name == "" or name == nil then
112
		name = vim.fn.input("Pane name: ")
113
	end
114
115
	if name == nil or name == "" then
116
		vim.notify("No pane name provided", vim.log.levels.ERROR)
117
		return
118
	end
119
120
	M.zellij_action("rename-pane " .. name)
121
end
122
123
function M.move_pane(direction)
124
	M.zellij_action("move-pane " .. direction)
125
end
126
127
-- Setup user commands
128
function M.setup()
129
	vim.api.nvim_create_user_command("ZellijUp", function()
130
		M.move("up")
131
	end, {})
132
	vim.api.nvim_create_user_command("ZellijDown", function()
133
		M.move("down")
134
	end, {})
135
	vim.api.nvim_create_user_command("ZellijLeft", function()
136
		M.move("left")
137
	end, {})
138
	vim.api.nvim_create_user_command("ZellijRight", function()
139
		M.move("right")
140
	end, {})
141
142
	vim.api.nvim_create_user_command("ZellijLeftTab", function()
143
		M.move_or_tab("left", false)
144
	end, {})
145
	vim.api.nvim_create_user_command("ZellijRightTab", function()
146
		M.move_or_tab("right", false)
147
	end, {})
148
149
	vim.api.nvim_create_user_command("ZellijLeftSmart", function()
150
		M.move_or_tab("left", true)
151
	end, {})
152
	vim.api.nvim_create_user_command("ZellijRightSmart", function()
153
		M.move_or_tab("right", true)
154
	end, {})
155
156
	vim.api.nvim_create_user_command("ZellijNewTab", M.new_tab, { nargs = "*" })
157
	vim.api.nvim_create_user_command("ZellijRenameTab", M.rename_tab, { nargs = "?" })
158
	vim.api.nvim_create_user_command("ZellijMoveTabRight", function()
159
		M.move_tab("right")
160
	end, {})
161
	vim.api.nvim_create_user_command("ZellijMoveTabLeft", function()
162
		M.move_tab("left")
163
	end, {})
164
165
	vim.api.nvim_create_user_command("ZellijNewPane", M.new_pane, { nargs = "*" })
166
	vim.api.nvim_create_user_command("ZellijRenamePane", M.rename_pane, { nargs = "?" })
167
	vim.api.nvim_create_user_command("ZellijMovePaneUp", function()
168
		M.move_pane("up")
169
	end, {})
170
	vim.api.nvim_create_user_command("ZellijMovePaneDown", function()
171
		M.move_pane("down")
172
	end, {})
173
	vim.api.nvim_create_user_command("ZellijMovePaneRight", function()
174
		M.move_pane("right")
175
	end, {})
176
	vim.api.nvim_create_user_command("ZellijMovePaneLeft", function()
177
		M.move_pane("left")
178
	end, {})
179
end
180
181
return M