Skip to content

Commit b852c3e

Browse files
committed
patch 8.0.1596: no autocommand specifically for opening a terminal window
Problem: No autocommand specifically for opening a terminal window. Solution: Add TerminalOpen. (?, closes #2484)
1 parent 12a96de commit b852c3e

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

runtime/doc/autocmd.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ Name triggered by ~
257257
|BufCreate| just after adding a buffer to the buffer list
258258
|BufDelete| before deleting a buffer from the buffer list
259259
|BufWipeout| before completely deleting a buffer
260+
|TerminalOpen| after a terminal buffer was created
260261

261262
|BufFilePre| before changing the name of the current buffer
262263
|BufFilePost| after changing the name of the current buffer
@@ -328,6 +329,10 @@ Name triggered by ~
328329
|CmdlineEnter| after the cursor moves to the command line
329330
|CmdlineLeave| before the cursor leaves the command line
330331

332+
|CmdlineChanged| after a change was made to the command-line text
333+
|CmdlineEnter| after the cursor moves to the command line
334+
|CmdlineLeave| before the cursor leaves the command line
335+
331336
|InsertEnter| starting Insert mode
332337
|InsertChange| when typing <Insert> while in Insert or Replace mode
333338
|InsertLeave| when leaving Insert mode
@@ -968,6 +973,11 @@ TermChanged After the value of 'term' has changed. Useful
968973
for re-loading the syntax file to update the
969974
colors, fonts and other terminal-dependent
970975
settings. Executed for all loaded buffers.
976+
*TerminalOpen*
977+
TerminalOpen Just after a terminal buffer was created, with
978+
`:terminal` or |term_start()|. This event is
979+
triggered even if the buffer is created
980+
without a window, with the ++hidden option.
971981
*TermResponse*
972982
TermResponse After the response to |t_RV| is received from
973983
the terminal. The value of |v:termresponse|

src/fileio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7773,6 +7773,7 @@ static struct event_name
77737773
{"TabEnter", EVENT_TABENTER},
77747774
{"TabLeave", EVENT_TABLEAVE},
77757775
{"TermChanged", EVENT_TERMCHANGED},
7776+
{"TerminalOpen", EVENT_TERMINALOPEN},
77767777
{"TermResponse", EVENT_TERMRESPONSE},
77777778
{"TextChanged", EVENT_TEXTCHANGED},
77787779
{"TextChangedI", EVENT_TEXTCHANGEDI},

src/terminal.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,36 @@
3838
* in tl_scrollback are no longer used.
3939
*
4040
* TODO:
41-
* - Add a flag to kill the job when Vim is exiting. Useful when it's showing
42-
* a logfile. Or send keys there to make it quit: "exit\r" for a shell.
41+
* - if the job in the terminal does not support the mouse, we can use the
42+
* mouse in the Terminal window for copy/paste and scrolling.
4343
* - When using 'termguicolors' still use the 16 ANSI colors as-is. Helps for
44+
* - In the GUI use a terminal emulator for :!cmd. Make the height the same as
45+
* the window and position it higher up when it gets filled, so it looks like
46+
* the text scrolls up.
47+
* - implement term_setsize()
48+
* - Copy text in the vterm to the Vim buffer once in a while, so that
49+
* completion works.
4450
* - Adding WinBar to terminal window doesn't display, text isn't shifted down.
4551
* a job that uses 16 colors while Vim is using > 256.
4652
* - in GUI vertical split causes problems. Cursor is flickering. (Hirohito
4753
* Higashi, 2017 Sep 19)
48-
* - Trigger TerminalOpen event? #2422 patch in #2484
4954
* - after resizing windows overlap. (Boris Staletic, #2164)
5055
* - Redirecting output does not work on MS-Windows, Test_terminal_redir_file()
5156
* is disabled.
52-
* - if the job in the terminal does not support the mouse, we can use the
53-
* mouse in the Terminal window for copy/paste and scrolling.
5457
* - cursor blinks in terminal on widows with a timer. (xtal8, #2142)
55-
* - When closing gvim with an active terminal buffer, the dialog suggests
56-
* saving the buffer. Should say something else. (Manas Thakur, #2215)
57-
* Also: #2223
5858
* - Termdebug does not work when Vim build with mzscheme. gdb hangs.
5959
* - MS-Windows GUI: WinBar has tearoff item
6060
* - MS-Windows GUI: still need to type a key after shell exits? #1924
6161
* - After executing a shell command the status line isn't redraw.
62-
* - implement term_setsize()
6362
* - add test for giving error for invalid 'termsize' value.
6463
* - support minimal size when 'termsize' is "rows*cols".
6564
* - support minimal size when 'termsize' is empty?
6665
* - GUI: when using tabs, focus in terminal, click on tab does not work.
67-
* - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
68-
* changes to "!shell".
69-
* (justrajdeep, 2017 Aug 22)
7066
* - Redrawing is slow with Athena and Motif. Also other GUI? (Ramel Eshed)
7167
* - For the GUI fill termios with default values, perhaps like pangoterm:
7268
* http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
7369
* - when 'encoding' is not utf-8, or the job is using another encoding, setup
7470
* conversions.
75-
* - In the GUI use a terminal emulator for :!cmd. Make the height the same as
76-
* the window and position it higher up when it gets filled, so it looks like
77-
* the text scrolls up.
78-
* - Copy text in the vterm to the Vim buffer once in a while, so that
79-
* completion works.
8071
* - add an optional limit for the scrollback size. When reaching it remove
8172
* 10% at the start.
8273
*/
@@ -582,6 +573,8 @@ term_start(typval_T *argvar, jobopt_T *opt, int without_job, int forceit)
582573
term_close_buffer(curbuf, old_curbuf);
583574
return NULL;
584575
}
576+
577+
apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, curbuf);
585578
return newbuf;
586579
}
587580

src/testdir/test_terminal.vim

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,3 +908,37 @@ func Test_terminal_qall_prompt()
908908
" close the terminal window where Vim was running
909909
quit
910910
endfunc
911+
912+
func Test_terminalopen_autocmd()
913+
augroup repro
914+
au!
915+
au TerminalOpen * let s:called += 1
916+
augroup END
917+
918+
let s:called = 0
919+
920+
" Open a terminal window with :terminal
921+
terminal
922+
call assert_equal(1, s:called)
923+
bwipe!
924+
925+
" Open a terminal window with term_start()
926+
call term_start(&shell)
927+
call assert_equal(2, s:called)
928+
bwipe!
929+
930+
" Open a hidden terminal buffer with :terminal
931+
terminal ++hidden
932+
call assert_equal(3, s:called)
933+
for buf in term_list()
934+
exe buf . "bwipe!"
935+
endfor
936+
937+
" Open a hidden terminal buffer with term_start()
938+
let buf = term_start(&shell, {'hidden': 1})
939+
call assert_equal(4, s:called)
940+
exe buf . "bwipe!"
941+
942+
unlet s:called
943+
au! repro
944+
endfunction

src/version.c

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

767767
static int included_patches[] =
768768
{ /* Add new patch number below this line */
769+
/**/
770+
1596,
769771
/**/
770772
1595,
771773
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ enum auto_event
13461346
EVENT_CMDUNDEFINED, /* command undefined */
13471347
EVENT_OPTIONSET, /* option was set */
13481348
EVENT_TEXTYANKPOST, /* after some text was yanked */
1349+
EVENT_TERMINALOPEN, /* after a terminal buffer was created */
13491350
NUM_EVENTS /* MUST be the last one */
13501351
};
13511352

0 commit comments

Comments
 (0)