patch 9.0.0907: restoring window after WinScrolled may fail

Problem:    Restoring window after WinScrolled may fail.
Solution:   Lock the window layout when triggering WinScrolled.
diff --git a/src/window.c b/src/window.c
index 996a8aa..a64a52e 100644
--- a/src/window.c
+++ b/src/window.c
@@ -84,6 +84,48 @@
 // autocommands mess up the window structure.
 static int split_disallowed = 0;
 
+// When non-zero closing a window is forbidden.  Used to avoid that nasty
+// autocommands mess up the window structure.
+static int close_disallowed = 0;
+
+/*
+ * Disallow changing the window layout (split window, close window, move
+ * window).  Resizing is still allowed.
+ * Used for autocommands that temporarily use another window and need to
+ * make sure the previously selected window is still there.
+ * Must be matched with exactly one call to window_layout_unlock()!
+ */
+    static void
+window_layout_lock(void)
+{
+    ++split_disallowed;
+    ++close_disallowed;
+}
+
+    static void
+window_layout_unlock(void)
+{
+    --split_disallowed;
+    --close_disallowed;
+}
+
+/*
+ * When the window layout cannot be changed give an error and return TRUE.
+ */
+    int
+window_layout_locked(void)
+{
+    if (split_disallowed > 0 || close_disallowed > 0)
+    {
+	if (close_disallowed == 0)
+	    emsg(_(e_cannot_split_window_when_closing_buffer));
+	else
+	    emsg(_(e_not_allowed_to_change_window_layout_in_this_autocmd));
+	return TRUE;
+    }
+    return FALSE;
+}
+
 // #define WIN_DEBUG
 #ifdef WIN_DEBUG
 /*
@@ -2531,6 +2573,8 @@
 	emsg(_(e_cannot_close_last_window));
 	return FAIL;
     }
+    if (window_layout_locked())
+	return FAIL;
 
     if (win->w_closing || (win->w_buffer != NULL
 					       && win->w_buffer->b_locked > 0))
@@ -2802,24 +2846,28 @@
     void
 may_trigger_winscrolled(void)
 {
-    win_T	    *wp = curwin;
     static int	    recursive = FALSE;
-    char_u	    winid[NUMBUFLEN];
 
     if (recursive || !has_winscrolled())
 	return;
 
+    win_T *wp = curwin;
     if (wp->w_last_topline != wp->w_topline
 	    || wp->w_last_leftcol != wp->w_leftcol
 	    || wp->w_last_skipcol != wp->w_skipcol
 	    || wp->w_last_width != wp->w_width
 	    || wp->w_last_height != wp->w_height)
     {
-	vim_snprintf((char *)winid, sizeof(winid), "%d", wp->w_id);
+	// "curwin" may be different from the actual current window, make sure
+	// it can be restored.
+	window_layout_lock();
 
 	recursive = TRUE;
+	char_u winid[NUMBUFLEN];
+	vim_snprintf((char *)winid, sizeof(winid), "%d", wp->w_id);
 	apply_autocmds(EVENT_WINSCROLLED, winid, winid, FALSE, wp->w_buffer);
 	recursive = FALSE;
+	window_layout_unlock();
 
 	// an autocmd may close the window, "wp" may be invalid now
 	if (win_valid_any_tab(wp))
@@ -4014,6 +4062,8 @@
 	emsg(_(e_invalid_in_cmdline_window));
 	return FAIL;
     }
+    if (window_layout_locked())
+	return FAIL;
 
     newtp = alloc_tabpage();
     if (newtp == NULL)