cursor_down(
    long	n,
    int		upd_topline)	    // When TRUE: update topline
{
    linenr_T	lnum;

    if (n > 0)
    {
	lnum = curwin->w_cursor.lnum;
#ifdef FEAT_FOLDING
	// Move to last line of fold, will fail if it's the end-of-file.
	(void)hasFolding(lnum, NULL, &lnum);
#endif
	// This fails if the cursor is already in the last line or would move
	// beyond the last line and '-' is in 'cpoptions'
	if (lnum >= curbuf->b_ml.ml_line_count
		|| (lnum + n > curbuf->b_ml.ml_line_count
		    && vim_strchr(p_cpo, CPO_MINUS) != NULL))
	    return FAIL;
	if (lnum + n >= curbuf->b_ml.ml_line_count)
	    lnum = curbuf->b_ml.ml_line_count;
	else
#ifdef FEAT_FOLDING
	if (hasAnyFolding(curwin))
	{
	    linenr_T	last;

	    // count each sequence of folded lines as one logical line
	    while (n--)
	    {
		if (hasFolding(lnum, NULL, &last))
		    lnum = last + 1;
		else
		    ++lnum;
		if (lnum >= curbuf->b_ml.ml_line_count)
		    break;
	    }
	    if (lnum > curbuf->b_ml.ml_line_count)
		lnum = curbuf->b_ml.ml_line_count;
	}
	else
#endif
	    lnum += n;
	curwin->w_cursor.lnum = lnum;
    }

    // try to advance to the column we want to be at
    coladvance(curwin->w_curswant);

    if (upd_topline)
	update_topline();	// make sure curwin->w_topline is valid

    return OK;
}
cursor_down(
    long	n,
    int		upd_topline)	     
{
    linenr_T	lnum;

    if (n > 0)
    {
	lnum = curwin->w_cursor.lnum;
#ifdef FEAT_FOLDING
	 
	(void)hasFolding(lnum, NULL, &lnum);
#endif
	 
	 
	if (lnum >= curbuf->b_ml.ml_line_count
		|| (lnum + n > curbuf->b_ml.ml_line_count
		    && vim_strchr(p_cpo, CPO_MINUS) != NULL))
	    return FAIL;
	if (lnum + n >= curbuf->b_ml.ml_line_count)
	    lnum = curbuf->b_ml.ml_line_count;
	else
#ifdef FEAT_FOLDING
	if (hasAnyFolding(curwin))
	{
	    linenr_T	last;

	     
	    while (n--)
	    {
		if (hasFolding(lnum, NULL, &last))
		    lnum = last + 1;
		else
		    ++lnum;
		if (lnum >= curbuf->b_ml.ml_line_count)
		    break;
	    }
	    if (lnum > curbuf->b_ml.ml_line_count)
		lnum = curbuf->b_ml.ml_line_count;
	}
	else
#endif
	    lnum += n;
	curwin->w_cursor.lnum = lnum;
    }

     
    coladvance(curwin->w_curswant);

    if (upd_topline)
	update_topline();	 

    return OK;
}