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)
diff --git a/src/fileio.c b/src/fileio.c
index 8331c08..05c3df5 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -7773,6 +7773,7 @@
     {"TabEnter",	EVENT_TABENTER},
     {"TabLeave",	EVENT_TABLEAVE},
     {"TermChanged",	EVENT_TERMCHANGED},
+    {"TerminalOpen",	EVENT_TERMINALOPEN},
     {"TermResponse",	EVENT_TERMRESPONSE},
     {"TextChanged",	EVENT_TEXTCHANGED},
     {"TextChangedI",	EVENT_TEXTCHANGEDI},
diff --git a/src/terminal.c b/src/terminal.c
index cc83055..7a80636 100644
--- a/src/terminal.c
+++ b/src/terminal.c
@@ -38,45 +38,36 @@
  * in tl_scrollback are no longer used.
  *
  * TODO:
- * - Add a flag to kill the job when Vim is exiting.  Useful when it's showing
- *   a logfile.  Or send keys there to make it quit: "exit\r" for a shell.
+ * - if the job in the terminal does not support the mouse, we can use the
+ *   mouse in the Terminal window for copy/paste and scrolling.
  * - When using 'termguicolors' still use the 16 ANSI colors as-is.  Helps for
+ * - In the GUI use a terminal emulator for :!cmd.  Make the height the same as
+ *   the window and position it higher up when it gets filled, so it looks like
+ *   the text scrolls up.
+ * - implement term_setsize()
+ * - Copy text in the vterm to the Vim buffer once in a while, so that
+ *   completion works.
  * - Adding WinBar to terminal window doesn't display, text isn't shifted down.
  *   a job that uses 16 colors while Vim is using > 256.
  * - in GUI vertical split causes problems.  Cursor is flickering. (Hirohito
  *   Higashi, 2017 Sep 19)
- * - Trigger TerminalOpen event?  #2422  patch in #2484
  * - after resizing windows overlap. (Boris Staletic, #2164)
  * - Redirecting output does not work on MS-Windows, Test_terminal_redir_file()
  *   is disabled.
- * - if the job in the terminal does not support the mouse, we can use the
- *   mouse in the Terminal window for copy/paste and scrolling.
  * - cursor blinks in terminal on widows with a timer. (xtal8, #2142)
- * - When closing gvim with an active terminal buffer, the dialog suggests
- *   saving the buffer.  Should say something else. (Manas Thakur, #2215)
- *   Also: #2223
  * - Termdebug does not work when Vim build with mzscheme.  gdb hangs.
  * - MS-Windows GUI: WinBar has  tearoff item
  * - MS-Windows GUI: still need to type a key after shell exits?  #1924
  * - After executing a shell command the status line isn't redraw.
- * - implement term_setsize()
  * - add test for giving error for invalid 'termsize' value.
  * - support minimal size when 'termsize' is "rows*cols".
  * - support minimal size when 'termsize' is empty?
  * - GUI: when using tabs, focus in terminal, click on tab does not work.
- * - GUI: when 'confirm' is set and trying to exit Vim, dialog offers to save
- *   changes to "!shell".
- *   (justrajdeep, 2017 Aug 22)
  * - Redrawing is slow with Athena and Motif.  Also other GUI? (Ramel Eshed)
  * - For the GUI fill termios with default values, perhaps like pangoterm:
  *   http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
  * - when 'encoding' is not utf-8, or the job is using another encoding, setup
  *   conversions.
- * - In the GUI use a terminal emulator for :!cmd.  Make the height the same as
- *   the window and position it higher up when it gets filled, so it looks like
- *   the text scrolls up.
- * - Copy text in the vterm to the Vim buffer once in a while, so that
- *   completion works.
  * - add an optional limit for the scrollback size.  When reaching it remove
  *   10% at the start.
  */
@@ -582,6 +573,8 @@
 	term_close_buffer(curbuf, old_curbuf);
 	return NULL;
     }
+
+    apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, curbuf);
     return newbuf;
 }
 
diff --git a/src/testdir/test_terminal.vim b/src/testdir/test_terminal.vim
index 0178e3b..d8e40d1 100644
--- a/src/testdir/test_terminal.vim
+++ b/src/testdir/test_terminal.vim
@@ -908,3 +908,37 @@
   " close the terminal window where Vim was running
   quit
 endfunc
+
+func Test_terminalopen_autocmd()
+  augroup repro
+    au!
+    au TerminalOpen * let s:called += 1
+  augroup END
+
+  let s:called = 0
+
+  " Open a terminal window with :terminal
+  terminal
+  call assert_equal(1, s:called)
+  bwipe!
+
+  " Open a terminal window with term_start()
+  call term_start(&shell)
+  call assert_equal(2, s:called)
+  bwipe!
+
+  " Open a hidden terminal buffer with :terminal
+  terminal ++hidden
+  call assert_equal(3, s:called)
+  for buf in term_list()
+    exe buf . "bwipe!"
+  endfor
+
+  " Open a hidden terminal buffer with term_start()
+  let buf = term_start(&shell, {'hidden': 1})
+  call assert_equal(4, s:called)
+  exe buf . "bwipe!"
+
+  unlet s:called
+  au! repro
+endfunction
diff --git a/src/version.c b/src/version.c
index 4f1c001..a6ca132 100644
--- a/src/version.c
+++ b/src/version.c
@@ -767,6 +767,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1596,
+/**/
     1595,
 /**/
     1594,
diff --git a/src/vim.h b/src/vim.h
index 3b458a0..86948b0 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -1346,6 +1346,7 @@
     EVENT_CMDUNDEFINED,		/* command undefined */
     EVENT_OPTIONSET,		/* option was set */
     EVENT_TEXTYANKPOST,		/* after some text was yanked */
+    EVENT_TERMINALOPEN,		/* after a terminal buffer was created */
     NUM_EVENTS			/* MUST be the last one */
 };