patch 9.0.0114: the command line takes up space even when not used

Problem:    The command line takes up space even when not used.
Solution:   Allow for 'cmdheight' to be set to zero. (Shougo Matsushita,
            closes #10675, closes #940)
diff --git a/src/window.c b/src/window.c
index 6be7369..15a4d50 100644
--- a/src/window.c
+++ b/src/window.c
@@ -1005,6 +1005,8 @@
 	needed = wmh1 + STATUS_HEIGHT;
 	if (flags & WSP_ROOM)
 	    needed += p_wh - wmh1;
+	if (p_ch == 0)
+	    needed += 1;  // Adjust for cmdheight=0.
 	if (flags & (WSP_BOT | WSP_TOP))
 	{
 	    minheight = frame_minheight(topframe, NOWIN) + need_status;
@@ -5668,6 +5670,8 @@
     if (full_screen && msg_scrolled == 0 && row < cmdline_row)
 	screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
     cmdline_row = row;
+    p_ch = MAX(Rows - cmdline_row, 0);
+    curtab->tp_ch_used = p_ch;
     msg_row = row;
     msg_col = 0;
 
@@ -5704,9 +5708,12 @@
 
     if (curfrp->fr_parent == NULL)
     {
-	// topframe: can only change the command line
 	if (height > ROWS_AVAIL)
-	    height = ROWS_AVAIL;
+	    // If height is greater than the available space, try to create
+	    // space for the frame by reducing 'cmdheight' if possible, while
+	    // making sure `cmdheight` doesn't go below 1.
+	    height = MIN((p_ch > 0 ? ROWS_AVAIL + (p_ch - 1)
+							: ROWS_AVAIL), height);
 	if (height > 0)
 	    frame_new_height(curfrp, height, FALSE, FALSE);
     }
@@ -6037,7 +6044,7 @@
     while (p_wmh > 0)
     {
 	room = Rows - p_ch;
-	needed = min_rows() - 1;  // 1 was added for the cmdline
+	needed = min_rows();
 	if (room >= needed)
 	    break;
 	--p_wmh;
@@ -6143,9 +6150,7 @@
 	 * Only dragging the last status line can reduce p_ch.
 	 */
 	room = Rows - cmdline_row;
-	if (curfr->fr_next == NULL)
-	    room -= 1;
-	else
+	if (curfr->fr_next != NULL)
 	    room -= p_ch;
 	if (room < 0)
 	    room = 0;
@@ -6196,9 +6201,7 @@
     row = win_comp_pos();
     screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
     cmdline_row = row;
-    p_ch = Rows - cmdline_row;
-    if (p_ch < 1)
-	p_ch = 1;
+    p_ch = MAX(Rows - cmdline_row, 0);
     curtab->tp_ch_used = p_ch;
     redraw_all_later(SOME_VALID);
     showmode();
@@ -6733,7 +6736,8 @@
 	    total = n;
     }
     total += tabline_height();
-    total += 1;		// count the room for the command line
+    if (p_ch > 0)
+	total += 1;		// count the room for the command line
     return total;
 }