1414
1515-- confirm tmux is installed
1616local function is_tmux_installed ()
17- local output = vim .fn .system (" which tmux 2>/dev/null" )
18- return output ~= " " and not string.match (output , " tmux not found" )
17+ return vim .fn .executable (" tmux" ) == 1
1918end
2019
2120-- check if session is in tmux
2221local function is_tmux_running ()
23- local output = vim .fn .system (" tmux info 2>/dev/null" )
24- return output ~= " " and not string.match (output , " no client running" )
22+ return vim .env .TMUX ~= nil
2523end
2624
2725
3028-- get the file extension
3129local function get_file_extension ()
3230 local filename = vim .api .nvim_buf_get_name (0 )
33- if filename == " " then return nil end
3431 return filename :match (" ^.+(%..+)$" ):sub (2 )
3532end
3633
3734-- get build and run commands based on file extension
3835local function get_commands_for_extension (extension )
39- local fileExtension = get_file_extension ()
40- if fileExtension then
36+ if extension then
4137 for _ , cfg in ipairs (M .config ) do
4238 if vim .tbl_contains (cfg .extension , extension ) then
4339 return cfg .build , cfg .run
4440 end
4541 end
46- print (" No build and run commands found for this extension" )
42+ print (" Error: No build and run commands found for this extension" )
4743 else
48- print (" No file extension found" )
44+ print (" Error: No file extension found" )
4945 end
5046 return nil , nil
5147end
5248
5349-- check if a tmux window with the given name exists
5450local function tmux_window_exists (window_name )
55- local handle = io.popen (" tmux list-windows | grep -w " .. window_name )
56- local result = handle :read (" *a" )
57- handle :close ()
51+ local result = vim .fn .system (" tmux list-windows | grep -w " .. window_name )
5852 return result ~= " "
5953end
6054
6155
6256-- # CALL FUNCTIONS #------------------------------------------------------------
6357
64- -- function to run the command in a new or existing tmux window
65- function M .new_window (cmd )
66- if cmd then
67- local window_name = " build"
68-
69- if tmux_window_exists (window_name ) then
70- -- ensure build is in same directory as the project
71- local proj_dir = vim .fn .system (" pwd" )
72- proj_dir = vim .fn .trim (proj_dir )
73-
74- local win_dir = vim .fn .system (" tmux display -p -t 'build' '#{pane_current_path}'" )
75- win_dir = vim .fn .trim (win_dir )
58+ -- run command in a new or existing tmux window
59+ local function new_window (cmd , new_cmd )
60+ local window_name = " build"
7661
77- if ( win_dir ~= proj_dir ) then
78- cmd = " cd " .. proj_dir .. " ; " .. cmd
79- end
62+ if tmux_window_exists ( window_name ) then
63+ local proj_dir = vim . fn . trim ( vim . fn . system ( " pwd " ))
64+ local win_dir = vim . fn . trim ( vim . fn . system ( " tmux display -p -t " .. window_name .. " '#{pane_current_path}' " ))
8065
81- -- run the command
82- local cmd_head = " silent !tmux select-window -t " .. window_name
83- vim .cmd (cmd_head .. " \\ ; send-keys '" .. cmd .. " ' C-m" )
84- else
85- local cmd_head = " silent !tmux new-window -n " .. window_name
86- vim .cmd (cmd_head .. " '" .. cmd .. " ; zsh'" )
66+ if win_dir ~= proj_dir then
67+ cmd = " cd " .. proj_dir .. " ; " .. cmd
8768 end
69+
70+ local cmd_head = " silent !tmux select-window -t " .. window_name
71+ vim .cmd (cmd_head .. " \\ ; send-keys '" .. cmd .. " ' C-m" )
8872 else
89- print (" No run command found for this extension" )
73+ local cmd_head = " silent !tmux new-window -n " .. window_name
74+ vim .cmd (cmd_head .. " '" .. cmd .. " ; zsh'" )
9075 end
9176end
9277
93- function M .same_window (cmd , side )
78+ -- run command in same window on a new pane
79+ local function split_window (cmd , side )
9480 if cmd then
95- local cmd_head = " silent !tmux split-window "
96- vim .cmd (cmd_head .. side .. " -v '" .. cmd .. " ; exec zsh'" )
81+ local cmd_head = " silent !tmux split-window " .. side
82+ vim .cmd (cmd_head .. " '" .. cmd .. " ; exec zsh'" )
9783 else
98- print (" No run command found for this extension" )
84+ print (" Error: No run command found for this extension" )
9985 end
10086end
10187
10490
10591-- call the appropriate function based on the option
10692function M .dispatch (option )
107-
108- -- confirm tmux is installed
10993 if not is_tmux_installed () then
11094 print (" Error: install TMUX to use the plugin" )
11195 return 1
11296 end
11397
114- -- check if tmux running, else print error
11598 if not is_tmux_running () then
11699 print (" Error: run session in TMUX" )
117100 return 1
118101 end
119102
120- local make , run = get_commands_for_extension (get_file_extension ())
103+ local extension = get_file_extension ()
104+ local make , run = get_commands_for_extension (extension )
121105
122106 if option == " RunBG" then
123- M . new_window (run )
107+ new_window (run , true )
124108 elseif option == " RunV" then
125- M . same_window (run , " -v" )
109+ split_window (run , " -v" )
126110 elseif option == " RunH" then
127- M . same_window (run , " -h" )
111+ split_window (run , " -h" )
128112 elseif option == " MakeV" then
129- M . same_window (make , " -v" )
113+ split_window (make , " -v" )
130114 elseif option == " MakeH" then
131- M . same_window (make , " -h" )
115+ split_window (make , " -h" )
132116 elseif option == " Make" then
133- M . new_window (make )
117+ new_window (make , true )
134118 else
135- print (" Invalid option. Please use one of: RunBG, RunV, RunH, Make" )
119+ print (" Error: Invalid option. Please use one of: RunBG, RunV, RunH, Make" )
136120 end
137121end
138122
@@ -141,7 +125,7 @@ vim.api.nvim_create_user_command('TMUXcompile', function(args)
141125 M .dispatch (args .args )
142126end , {
143127 nargs = 1 ,
144- complete = function (arglead , cmdline , cursorpos )
128+ complete = function ()
145129 return { " RunBG" , " RunV" , " RunH" , " Make" }
146130 end ,
147131})
0 commit comments