@@ -4,6 +4,7 @@ function T.run_in_native_terminal(config, is_windows, cmd, opts)
44 opts = opts or {}
55 local focus = (opts .focus ~= false )
66 local prev = vim .api .nvim_get_current_win ()
7+ local prev_mode = (vim .api .nvim_get_mode and vim .api .nvim_get_mode ().mode ) or ' n'
78 if config .terminal .open then
89 vim .cmd ' botright split | terminal'
910 vim .cmd (string.format (' resize %d' , config .terminal .height or 12 ))
@@ -16,6 +17,9 @@ function T.run_in_native_terminal(config, is_windows, cmd, opts)
1617 end
1718 if not focus then
1819 pcall (vim .api .nvim_set_current_win , prev )
20+ if prev_mode :sub (1 , 1 ) == ' n' then
21+ pcall (vim .cmd , ' stopinsert' )
22+ end
1923 end
2024 vim .defer_fn (function ()
2125 vim .fn .chansend (chan , cmd .. (is_windows () and ' \r ' or ' \n ' ))
@@ -36,11 +40,16 @@ function T.run_in_betterterm(config, is_windows, cmd, notify_warn, notify_err, o
3640 opts = opts or {}
3741 local want_focus = (opts .focus ~= false )
3842 local prev = vim .api .nvim_get_current_win ()
39- if open_first or focus then
43+ local prev_mode = (vim .api .nvim_get_mode and vim .api .nvim_get_mode ().mode ) or ' n'
44+ -- Only open immediately if we intend to focus now; otherwise avoid toggling UI.
45+ if (open_first or focus ) and want_focus then
4046 pcall (betterTerm .open , idx )
4147 end
4248 if not want_focus then
4349 pcall (vim .api .nvim_set_current_win , prev )
50+ if prev_mode :sub (1 , 1 ) == ' n' then
51+ pcall (vim .cmd , ' stopinsert' )
52+ end
4453 end
4554 vim .defer_fn (function ()
4655 local ok_send , err = pcall (betterTerm .send , cmd .. (is_windows () and ' \r ' or ' \n ' ), idx )
@@ -56,8 +65,11 @@ function T.run_in_betterterm(config, is_windows, cmd, notify_warn, notify_err, o
5665end
5766
5867function T .run_make_in_terminal (config , is_windows , cmdline , notify_warn , notify_err )
59- if not T .run_in_betterterm (config , is_windows , cmdline , notify_warn , notify_err , { focus = false }) then
60- if not T .run_in_native_terminal (config , is_windows , cmdline , { focus = false }) then
68+ -- Auto-focus terminal when none is currently open; otherwise do not steal focus.
69+ local open_terms = T .list_open_builtin_terminals ()
70+ local want_focus = (# open_terms == 0 )
71+ if not T .run_in_betterterm (config , is_windows , cmdline , notify_warn , notify_err , { focus = want_focus }) then
72+ if not T .run_in_native_terminal (config , is_windows , cmdline , { focus = want_focus }) then
6173 notify_err ' Unable to run make: cannot open terminal'
6274 end
6375 end
@@ -156,6 +168,16 @@ function T.select_or_run_in_terminal(config, is_windows, cmdline, notify_warn, n
156168 pcall (betterTerm .open , idx )
157169 end , 120 )
158170 end
171+ else
172+ -- Focus a builtin terminal window after sending, to honor explicit user choice
173+ vim .defer_fn (function ()
174+ local terms = T .list_open_builtin_terminals ()
175+ if terms and # terms > 0 then
176+ -- pick the last one (most recently opened)
177+ local last = terms [# terms ]
178+ pcall (open_builtin_terminal_window , config , last .bufnr )
179+ end
180+ end , 120 )
159181 end
160182 end
161183 else
0 commit comments