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 / neolij / init.lua
-rw-r--r-- blob 72e3cc03fce7fdf0e69f126f3045065e2d0aaf61 4988 bytes (plain)
1
local M = {}
2
3
local direction_translation = {
4
	up = "k",
5
	down = "j",
6
	left = "h",
7
	right = "l",
8
}
9
10
local default_config = {
11
  -- if use vim-zellij-navigator plugin set this to true.
12
  vim_zellij_navigator = false
13
}
14
15
M.config = {}
16
17
-- Run zellij action safely
18
function M.zellij_action(args)
19
	if not os.getenv("ZELLIJ") then
20
		return
21
	end
22
	vim.fn.system("zellij action " .. args)
23
	if vim.v.shell_error ~= 0 then
24
		error("zellij executable not found in PATH")
25
	end
26
end
27
28
-- Try vim window move, fallback to zellij
29
local function navigate(direction, vim_key, fallback_zellij, fallback_nvim)
30
	local before = vim.fn.winnr()
31
	vim.cmd("wincmd " .. vim_key)
32
	local after = vim.fn.winnr()
33
34
	-- If vim didn't move, fallback to zellij
35
	if before == after then
36
    if fallback_nvim then
37
      local current_tab = vim.fn.tabpagenr()
38
      local last_tab = vim.fn.tabpagenr("$")
39
40
      -- 2. Fallback to Tab switching based on direction
41
      -- We'll use 'l' (right) and 'h' (left) to trigger tab movement
42
      if direction == "right" and current_tab < last_tab then
43
        vim.cmd("tabnext")
44
        return
45
      elseif direction == "left" and current_tab > 1 then
46
        vim.cmd("tabprevious")
47
        return
48
      end
49
50
        -- If we reach here, neither window nor tab could move: escape to Zellij
51
        M.zellij_action(fallback_zellij .. " " .. direction)
52
    end
53
	end
54
end
55
56
-- Window navigation
57
function M.move(direction)
58
	navigate(direction, direction_translation[direction], "move-focus")
59
end
60
61
-- Window navigation or tab switch
62
function M.move_or_tab(direction, fallback_nvim)
63
	navigate(direction, direction_translation[direction], "move-focus-or-tab", fallback_nvim)
64
end
65
66
-- Tab actions
67
function M.new_tab(opts)
68
	if type(opts) == "table" then
69
		opts = opts.args
70
	end
71
72
	if opts ~= "" then
73
		M.zellij_action("new-tab " .. opts)
74
	else
75
		M.zellij_action("new-tab")
76
	end
77
end
78
79
function M.rename_tab(name)
80
	if type(name) == "table" then
81
		name = name.args
82
	end
83
84
	if name == "" or name == nil then
85
		name = vim.fn.input("Tab name: ")
86
	end
87
88
	if name == nil or name == "" then
89
		vim.notify("No tab name provided", vim.log.levels.ERROR)
90
		return
91
	end
92
93
	M.zellij_action("rename-tab " .. name)
94
end
95
96
function M.move_tab(direction)
97
	M.zellij_action("move-tab " .. direction)
98
end
99
100
-- Pane actions
101
function M.new_pane(opts)
102
	if type(opts) == "table" then
103
		opts = opts.args
104
	end
105
106
	if opts ~= "" then
107
		-- allow custom flags like -d right, -n name, etc
108
		M.zellij_action("new-pane " .. opts)
109
	else
110
		M.zellij_action("new-pane")
111
	end
112
end
113
114
function M.rename_pane(name)
115
	if type(name) == "table" then
116
		name = name.args
117
	end
118
119
	if name == "" or name == nil then
120
		name = vim.fn.input("Pane name: ")
121
	end
122
123
	if name == nil or name == "" then
124
		vim.notify("No pane name provided", vim.log.levels.ERROR)
125
		return
126
	end
127
128
	M.zellij_action("rename-pane " .. name)
129
end
130
131
function M.move_pane(direction)
132
	M.zellij_action("move-pane " .. direction)
133
end
134
135
-- Setup user commands
136
function M.setup(user_config)
137
  M.config = vim.tbl_deep_extend("force", default_config, user_config or {})
138
  if M.config.vim_zellij_navigator then
139
    vim.fn.jobstart('zellij pipe -n "nvim_hook" "open"')
140
    vim.api.nvim_create_autocmd({"VimLeavePre"}, {
141
        pattern = {"*"},
142
        command = 'zellij pipe -n "nvim_hook" "close"'
143
    })
144
  end
145
	vim.api.nvim_create_user_command("NeolijUp", function()
146
		M.move("up")
147
	end, {})
148
	vim.api.nvim_create_user_command("NeolijDown", function()
149
		M.move("down")
150
	end, {})
151
	vim.api.nvim_create_user_command("NeolijLeft", function()
152
		M.move("left")
153
	end, {})
154
	vim.api.nvim_create_user_command("NeolijRight", function()
155
		M.move("right")
156
	end, {})
157
158
	vim.api.nvim_create_user_command("NeolijLeftTab", function()
159
		M.move_or_tab("left", false)
160
	end, {})
161
	vim.api.nvim_create_user_command("NeolijRightTab", function()
162
		M.move_or_tab("right", false)
163
	end, {})
164
165
	vim.api.nvim_create_user_command("NeolijLeftSmart", function()
166
		M.move_or_tab("left", true)
167
	end, {})
168
	vim.api.nvim_create_user_command("NeolijRightSmart", function()
169
		M.move_or_tab("right", true)
170
	end, {})
171
172
	vim.api.nvim_create_user_command("NeolijNewTab", M.new_tab, { nargs = "*" })
173
	vim.api.nvim_create_user_command("NeolijRenameTab", M.rename_tab, { nargs = "?" })
174
	vim.api.nvim_create_user_command("NeolijMoveTabRight", function()
175
		M.move_tab("right")
176
	end, {})
177
	vim.api.nvim_create_user_command("NeolijMoveTabLeft", function()
178
		M.move_tab("left")
179
	end, {})
180
181
	vim.api.nvim_create_user_command("NeolijNewPane", M.new_pane, { nargs = "*" })
182
	vim.api.nvim_create_user_command("NeolijRenamePane", M.rename_pane, { nargs = "?" })
183
	vim.api.nvim_create_user_command("NeolijMovePaneUp", function()
184
		M.move_pane("up")
185
	end, {})
186
	vim.api.nvim_create_user_command("NeolijMovePaneDown", function()
187
		M.move_pane("down")
188
	end, {})
189
	vim.api.nvim_create_user_command("NeolijMovePaneRight", function()
190
		M.move_pane("right")
191
	end, {})
192
	vim.api.nvim_create_user_command("NeolijMovePaneLeft", function()
193
		M.move_pane("left")
194
	end, {})
195
end
196
197
return M