Skip to content

Commit 100118c

Browse files
committed
patch 8.2.2128: there is no way to do something on CTRL-Z
Problem: There is no way to do something on CTRL-Z. Solution: Add VimSuspend and VimResume autocommand events. (closes #7450)
1 parent 1b884a0 commit 100118c

7 files changed

Lines changed: 67 additions & 1 deletion

File tree

runtime/doc/autocmd.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ Name triggered by ~
306306
|VimLeavePre| before exiting Vim, before writing the viminfo file
307307
|VimLeave| before exiting Vim, after writing the viminfo file
308308

309+
|VimSuspend| when suspending Vim
310+
|VimResume| when Vim is resumed after being suspended
311+
309312
Terminal
310313
|TerminalOpen| after a terminal buffer was created
311314
|TerminalWinOpen| after a terminal buffer was created in a new window
@@ -1231,6 +1234,17 @@ VimLeavePre Before exiting Vim, just before writing the
12311234
VimResized After the Vim window was resized, thus 'lines'
12321235
and/or 'columns' changed. Not when starting
12331236
up though.
1237+
*VimResume*
1238+
VimResume When the Vim instance is resumed after being
1239+
suspended and |VimSuspend| was triggered.
1240+
Useful for triggering |:checktime| and ensure
1241+
the buffers content did not change while Vim
1242+
was suspended: >
1243+
:autocmd VimResume * checktime
1244+
< *VimSuspend*
1245+
VimSuspend When the Vim instance is suspended. Only when
1246+
CTRL-Z was typed inside Vim, not when the
1247+
SIGSTOP or SIGTSTP signal was sent to Vim.
12341248
*WinEnter*
12351249
WinEnter After entering another window. Not done for
12361250
the first window, when Vim has just started.

src/autocmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ static struct event_name
191191
{"WinLeave", EVENT_WINLEAVE},
192192
{"VimResized", EVENT_VIMRESIZED},
193193
{"TextYankPost", EVENT_TEXTYANKPOST},
194+
{"VimSuspend", EVENT_VIMSUSPEND},
195+
{"VimResume", EVENT_VIMRESUME},
194196
{NULL, (event_T)0}
195197
};
196198

src/ex_docmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5864,6 +5864,7 @@ ex_stop(exarg_T *eap)
58645864
{
58655865
if (!eap->forceit)
58665866
autowrite_all();
5867+
apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
58675868
windgoto((int)Rows - 1, 0);
58685869
out_char('\n');
58695870
out_flush();
@@ -5881,6 +5882,7 @@ ex_stop(exarg_T *eap)
58815882
scroll_start(); // scroll screen before redrawing
58825883
redraw_later_clear();
58835884
shell_resized(); // may have resized window
5885+
apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
58845886
}
58855887
}
58865888

src/normal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5787,7 +5787,7 @@ nv_suspend(cmdarg_T *cap)
57875787
clearop(cap->oap);
57885788
if (VIsual_active)
57895789
end_visual_mode(); // stop Visual mode
5790-
do_cmdline_cmd((char_u *)"st");
5790+
do_cmdline_cmd((char_u *)"stop");
57915791
}
57925792

57935793
/*

src/testdir/test_suspend.vim

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,48 @@ func Test_suspend()
6161
call delete('Xfoo')
6262
endfunc
6363

64+
func Test_suspend_autocmd()
65+
CheckFeature terminal
66+
CheckExecutable /bin/sh
67+
68+
let buf = term_start('/bin/sh', #{term_rows: 6})
69+
" Wait for shell prompt.
70+
call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
71+
72+
call term_sendkeys(buf, v:progpath
73+
\ . " --clean -X"
74+
\ . " -c 'set nu'"
75+
\ . " -c 'let g:count = 0'"
76+
\ . " -c 'au VimSuspend * let g:count += 1'"
77+
\ . " -c 'au VimResume * let g:count += 1'"
78+
\ . " -c 'call setline(1, \"foo\")'"
79+
\ . " Xfoo\<CR>")
80+
" Cursor in terminal buffer should be on first line in spawned vim.
81+
call WaitForAssert({-> assert_equal(' 1 foo', term_getline(buf, '.'))})
82+
83+
for suspend_cmd in [":suspend\<CR>",
84+
\ ":stop\<CR>",
85+
\ ":suspend!\<CR>",
86+
\ ":stop!\<CR>",
87+
\ "\<C-Z>"]
88+
" Suspend and wait for shell prompt. Then "fg" will restore Vim.
89+
call term_sendkeys(buf, suspend_cmd)
90+
call CheckSuspended(buf, 0)
91+
endfor
92+
93+
call term_sendkeys(buf, ":echo g:count\<CR>")
94+
call TermWait(buf)
95+
call WaitForAssert({-> assert_match('^10', term_getline(buf, 6))})
96+
97+
" Quit gracefully to dump coverage information.
98+
call term_sendkeys(buf, ":qall!\<CR>")
99+
call TermWait(buf)
100+
" Wait until Vim actually exited and shell shows a prompt
101+
call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
102+
call StopShellInTerminal(buf)
103+
104+
exe buf . 'bwipe!'
105+
call delete('Xfoo')
106+
endfunc
107+
64108
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2128,
753755
/**/
754756
2127,
755757
/**/

src/vim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,8 @@ enum auto_event
13441344
EVENT_WINENTER, // after entering a window
13451345
EVENT_WINLEAVE, // before leaving a window
13461346
EVENT_WINNEW, // when entering a new window
1347+
EVENT_VIMSUSPEND, // before Vim is suspended
1348+
EVENT_VIMRESUME, // after Vim is resumed
13471349

13481350
NUM_EVENTS // MUST be the last one
13491351
};

0 commit comments

Comments
 (0)