static void free_buffer(buf_T *);
static void free_buffer_stuff(buf_T *buf, int free_options);
static int bt_nofileread(buf_T *buf);
+static void no_write_message_buf(buf_T *buf);
#ifdef UNIX
# define dev_T dev_t
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
{
- dialog_changed(buf, FALSE);
- if (!bufref_valid(&bufref))
- // Autocommand deleted buffer, oops! It's not changed
- // now.
- return FAIL;
- // If it's still changed fail silently, the dialog already
- // mentioned why it fails.
- if (bufIsChanged(buf))
- return FAIL;
+# ifdef FEAT_TERMINAL
+ if (term_job_running(buf->b_term))
+ {
+ if (term_confirm_stop(buf) == FAIL)
+ return FAIL;
+ }
+ else
+# endif
+ {
+ dialog_changed(buf, FALSE);
+ if (!bufref_valid(&bufref))
+ // Autocommand deleted buffer, oops! It's not changed
+ // now.
+ return FAIL;
+ // If it's still changed fail silently, the dialog already
+ // mentioned why it fails.
+ if (bufIsChanged(buf))
+ return FAIL;
+ }
}
else
#endif
{
- semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
- buf->b_fnum);
+ no_write_message_buf(buf);
return FAIL;
}
}
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
{
- bufref_T bufref;
+# ifdef FEAT_TERMINAL
+ if (term_job_running(curbuf->b_term))
+ {
+ if (term_confirm_stop(curbuf) == FAIL)
+ return FAIL;
+ // Manually kill the terminal here because this command will
+ // hide it otherwise.
+ free_terminal(curbuf);
+ }
+ else
+# endif
+ {
+ bufref_T bufref;
- set_bufref(&bufref, buf);
- dialog_changed(curbuf, FALSE);
- if (!bufref_valid(&bufref))
- // Autocommand deleted buffer, oops!
- return FAIL;
+ set_bufref(&bufref, buf);
+ dialog_changed(curbuf, FALSE);
+ if (!bufref_valid(&bufref))
+ // Autocommand deleted buffer, oops!
+ return FAIL;
+
+ if (bufIsChanged(curbuf))
+ {
+ no_write_message();
+ return FAIL;
+ }
+ }
}
- if (bufIsChanged(curbuf))
+ else
#endif
{
no_write_message();
}
#endif
+ static void
+no_write_message_buf(buf_T *buf UNUSED)
+{
+#ifdef FEAT_TERMINAL
+ if (term_job_running(buf->b_term))
+ emsg(_(e_job_still_running_add_bang_to_end_the_job));
+ else
+#endif
+ semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
+ buf->b_fnum);
+}
+
void
no_write_message(void)
{
#endif
/*
- * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
- * buffer.
+ * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
+ * "popup" buffer.
*/
int
bt_dontwrite(buf_T *buf)
&& term->tl_job->jv_channel->ch_keep_open;
}
+//
+// Used to confirm whether we would like to kill a terminal.
+// Return OK when the user confirms to kill it.
+// Return FAIL if the user selects otherwise.
+//
+ int
+term_confirm_stop(buf_T *buf)
+{
+ char_u buff[DIALOG_MSG_SIZE];
+ int ret;
+
+ dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
+ ret = vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1);
+ if (ret == VIM_YES)
+ return OK;
+ else
+ return FAIL;
+}
+
/*
* Used when exiting: kill the job in "buf" if so desired.
* Return OK when the job finished.
if ((how == NULL || *how == NUL)
&& (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
{
- char_u buff[DIALOG_MSG_SIZE];
- int ret;
-
- dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
- ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
- if (ret == VIM_YES)
+ if (term_confirm_stop(buf) == OK)
how = "kill";
- else if (ret == VIM_CANCEL)
+ else
return FAIL;
}
#endif
func Test_terminal_wipe_buffer()
let buf = Run_shell_in_terminal({})
- call assert_fails(buf . 'bwipe', 'E89:')
+ call assert_fails(buf . 'bwipe', 'E948:')
exe buf . 'bwipe!'
call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
call assert_equal("", bufname(buf))
unlet g:job
endfunc
+" Test that using ':confirm bwipe' on terminal works
+func Test_terminal_confirm_wipe_buffer()
+ CheckUnix
+ CheckNotGui
+ CheckFeature dialog_con
+ let buf = Run_shell_in_terminal({})
+ call assert_fails(buf . 'bwipe', 'E948:')
+ call feedkeys('n', 'L')
+ call assert_fails('confirm ' .. buf .. 'bwipe', 'E517:')
+ call assert_equal(buf, bufnr())
+ call assert_equal(1, &modified)
+ call feedkeys('y', 'L')
+ exe 'confirm ' .. buf .. 'bwipe'
+ call assert_notequal(buf, bufnr())
+ call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
+ call assert_equal("", bufname(buf))
+
+ unlet g:job
+endfunc
+
+" Test that using :b! will hide the terminal
+func Test_terminal_goto_buffer()
+ let buf_mod = bufnr()
+ let buf_term = Run_shell_in_terminal({})
+ call assert_equal(buf_term, bufnr())
+ call assert_fails(buf_mod . 'b', 'E948:')
+ exe buf_mod . 'b!'
+ call assert_equal(buf_mod, bufnr())
+ call assert_equal('run', job_status(g:job))
+ call assert_notequal('', bufname(buf_term))
+ exec buf_mod .. 'bwipe!'
+ exec buf_term .. 'bwipe!'
+
+ unlet g:job
+endfunc
+
+" Test that using ':confirm :b' will kill terminal
+func Test_terminal_confirm_goto_buffer()
+ CheckUnix
+ CheckNotGui
+ CheckFeature dialog_con
+ let buf_mod = bufnr()
+ let buf_term = Run_shell_in_terminal({})
+ call feedkeys('n', 'L')
+ exe 'confirm ' .. buf_mod .. 'b'
+ call assert_equal(buf_term, bufnr())
+ call feedkeys('y', 'L')
+ exec 'confirm ' .. buf_mod .. 'b'
+ call assert_equal(buf_mod, bufnr())
+ call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
+ call assert_equal("", bufname(buf_term))
+ exec buf_mod .. 'bwipe!'
+
+ unlet g:job
+endfunc
+
+" Test that using :close! will hide the terminal
+func Test_terminal_close_win()
+ let buf = Run_shell_in_terminal({})
+ call assert_equal(buf, bufnr())
+ call assert_fails('close', 'E948:')
+ close!
+ call assert_notequal(buf, bufnr())
+ call assert_equal('run', job_status(g:job))
+ call assert_notequal('', bufname(buf))
+ exec buf .. 'bwipe!'
+
+ unlet g:job
+endfunc
+
+" Test that using ':confirm close' will kill terminal
+func Test_terminal_confirm_close_win()
+ CheckUnix
+ CheckNotGui
+ CheckFeature dialog_con
+ let buf = Run_shell_in_terminal({})
+ call feedkeys('n', 'L')
+ confirm close
+ call assert_equal(buf, bufnr())
+ call feedkeys('y', 'L')
+ confirm close
+ call assert_notequal(buf, bufnr())
+ call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
+ call assert_equal("", bufname(buf))
+
+ unlet g:job
+endfunc
+
+" Test that using :quit! will kill the terminal
+func Test_terminal_quit()
+ let buf = Run_shell_in_terminal({})
+ call assert_equal(buf, bufnr())
+ call assert_fails('quit', 'E948:')
+ quit!
+ call assert_notequal(buf, bufnr())
+ call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
+ exec buf .. 'bwipe!'
+
+ unlet g:job
+endfunc
+
+" Test that using ':confirm quit' will kill terminal
+func Test_terminal_confirm_quit()
+ CheckUnix
+ CheckNotGui
+ CheckFeature dialog_con
+ let buf = Run_shell_in_terminal({})
+ call feedkeys('n', 'L')
+ confirm quit
+ call assert_equal(buf, bufnr())
+ call feedkeys('y', 'L')
+ confirm quit
+ call assert_notequal(buf, bufnr())
+ call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
+
+ unlet g:job
+endfunc
+
+" Test :q or :next
+
func Test_terminal_split_quit()
let buf = Run_shell_in_terminal({})
split
func Test_terminal_list_args()
let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
- call assert_fails(buf . 'bwipe', 'E89:')
+ call assert_fails(buf . 'bwipe', 'E948:')
exe buf . 'bwipe!'
call assert_equal("", bufname(buf))
endfunction
" make Vim exit, it will prompt to kill the shell
call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
- call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
+ call WaitForAssert({-> assert_match('\[Y\]es, (N)o:', term_getline(buf, 20))})
call term_sendkeys(buf, "y")
call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})