patch 8.2.0158: triggering CompleteDone earlier is not backwards compatible

Problem:    Triggering CompleteDone earlier is not backwards compatible.
            (Daniel Hahler)
Solution:   Add CompleteDonePre instead.
diff --git a/src/autocmd.c b/src/autocmd.c
index 13aa065..dce23b8 100644
--- a/src/autocmd.c
+++ b/src/autocmd.c
@@ -114,6 +114,7 @@
     {"ColorSchemePre",	EVENT_COLORSCHEMEPRE},
     {"CompleteChanged",	EVENT_COMPLETECHANGED},
     {"CompleteDone",	EVENT_COMPLETEDONE},
+    {"CompleteDonePre",	EVENT_COMPLETEDONEPRE},
     {"CursorHold",	EVENT_CURSORHOLD},
     {"CursorHoldI",	EVENT_CURSORHOLDI},
     {"CursorMoved",	EVENT_CURSORMOVED},
diff --git a/src/insexpand.c b/src/insexpand.c
index e3c811a..0be2009 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -2061,12 +2061,11 @@
 
 	    auto_format(FALSE, TRUE);
 
-	    // Trigger the CompleteDone event to give scripts a chance to
-	    // act upon the completion.  Do this before clearing the info,
-	    // and restore ctrl_x_mode, so that complete_info() can be
-	    // used.
+	    // Trigger the CompleteDonePre event to give scripts a chance to
+	    // act upon the completion before clearing the info, and restore
+	    // ctrl_x_mode, so that complete_info() can be used.
 	    ctrl_x_mode = prev_mode;
-	    ins_apply_autocmds(EVENT_COMPLETEDONE);
+	    ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
 
 	    ins_compl_free();
 	    compl_started = FALSE;
@@ -2092,6 +2091,9 @@
 	    if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
 		do_c_expr_indent();
 #endif
+	    // Trigger the CompleteDone event to give scripts a chance to act
+	    // upon the end of completion.
+	    ins_apply_autocmds(EVENT_COMPLETEDONE);
 	}
     }
     else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
diff --git a/src/testdir/test_ins_complete.vim b/src/testdir/test_ins_complete.vim
index ef5ac72..77045f4 100644
--- a/src/testdir/test_ins_complete.vim
+++ b/src/testdir/test_ins_complete.vim
@@ -175,7 +175,7 @@
   let s:called_completedone = 1
 endfunc
 
-func s:CompleteDone_CheckCompletedItemDict()
+func s:CompleteDone_CheckCompletedItemDict(pre)
   call assert_equal( 'aword',          v:completed_item[ 'word' ] )
   call assert_equal( 'wrd',            v:completed_item[ 'abbr' ] )
   call assert_equal( 'extra text',     v:completed_item[ 'menu' ] )
@@ -183,7 +183,9 @@
   call assert_equal( 'W',              v:completed_item[ 'kind' ] )
   call assert_equal( 'test',           v:completed_item[ 'user_data' ] )
 
-  call assert_equal('function', complete_info().mode)
+  if a:pre
+    call assert_equal('function', complete_info().mode)
+  endif
 
   let s:called_completedone = 1
 endfunc
@@ -205,7 +207,8 @@
 endfunc
 
 func Test_CompleteDoneDict()
-  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict()
+  au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
+  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
 
   set completefunc=<SID>CompleteDone_CompleteFuncDict
   execute "normal a\<C-X>\<C-U>\<C-Y>"
diff --git a/src/version.c b/src/version.c
index 739169f..7447918 100644
--- a/src/version.c
+++ b/src/version.c
@@ -743,6 +743,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    158,
+/**/
     157,
 /**/
     156,
diff --git a/src/vim.h b/src/vim.h
index 9402c4e..23e5374 100644
--- a/src/vim.h
+++ b/src/vim.h
@@ -1303,6 +1303,7 @@
     EVENT_COLORSCHEMEPRE,	// before loading a colorscheme
     EVENT_COMPLETECHANGED,	// after completion popup menu changed
     EVENT_COMPLETEDONE,		// after finishing insert complete
+    EVENT_COMPLETEDONEPRE,	// idem, before clearing info
     EVENT_CURSORHOLD,		// cursor in same position for a while
     EVENT_CURSORHOLDI,		// idem, in Insert mode
     EVENT_CURSORMOVED,		// cursor was moved