Skip to content

Commit

Permalink
Split out coladvance and update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
luukvbaal committed Sep 26, 2022
1 parent 2b76bb4 commit 3963476
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
36 changes: 26 additions & 10 deletions src/edit.c
Expand Up @@ -2797,16 +2797,24 @@ cursor_up(
wp->w_cursor.lnum = lnum;
}

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

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

return wp->w_cursor.lnum;
}

linenr_T
cursor_up_adv(
win_T *wp,
long n,
int upd_topline) // When TRUE: update topline
{
n = cursor_up(wp, n, upd_topline);
coladvance(wp->w_curswant);

return n;
}

/*
* Cursor down a number of logical lines.
*/
Expand Down Expand Up @@ -2858,16 +2866,24 @@ cursor_down(
wp->w_cursor.lnum = lnum;
}

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

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

return wp->w_cursor.lnum;
}

linenr_T
cursor_down_adv(
win_T *wp,
long n,
int upd_topline) // When TRUE: update topline
{
n = cursor_down(wp, n, upd_topline);
coladvance(wp->w_curswant);

return n;
}

/*
* Stuff the last inserted text in the read buffer.
* Last_insert actually is a copy of the redo buffer, so we
Expand Down Expand Up @@ -4704,7 +4720,7 @@ ins_up(

undisplay_dollar();
tpos = curwin->w_cursor;
if (cursor_up(curwin, 1L, TRUE))
if (cursor_up_adv(curwin, 1L, TRUE))
{
if (startcol)
coladvance(getvcol_nolist(&Insstart));
Expand Down Expand Up @@ -4761,7 +4777,7 @@ ins_down(

undisplay_dollar();
tpos = curwin->w_cursor;
if (cursor_down(curwin, 1L, TRUE))
if (cursor_down_adv(curwin, 1L, TRUE))
{
if (startcol)
coladvance(getvcol_nolist(&Insstart));
Expand Down
21 changes: 11 additions & 10 deletions src/normal.c
Expand Up @@ -2499,14 +2499,14 @@ scroll_redraw(int up, long count)
if (up)
{
if (curwin->w_cursor.lnum > prev_lnum
|| cursor_down(curwin, 1L, FALSE) == FAIL)
|| cursor_down_adv(curwin, 1L, FALSE) == FAIL)
break;
}
else
{
if (curwin->w_cursor.lnum < prev_lnum
|| prev_topline == 1L
|| cursor_up(curwin, 1L, FALSE) == FAIL)
|| cursor_up_adv(curwin, 1L, FALSE) == FAIL)
break;
}
// Mark w_topline as valid, otherwise the screen jumps back at the
Expand Down Expand Up @@ -3999,7 +3999,8 @@ nv_up(cmdarg_T *cap)
else
{
cap->oap->motion_type = MLINE;
if (cursor_up(curwin, cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
if (cursor_up_adv(curwin, cap->count1, cap->oap->op_type == OP_NOP)
== FAIL)
clearopbeep(cap->oap);
else if (cap->arg)
beginline(BL_WHITE | BL_FIX);
Expand Down Expand Up @@ -4045,7 +4046,7 @@ nv_down(cmdarg_T *cap)
#endif
{
cap->oap->motion_type = MLINE;
if (cursor_down(curwin, cap->count1,
if (cursor_down_adv(curwin, cap->count1,
cap->oap->op_type == OP_NOP) == FAIL)
clearopbeep(cap->oap);
else if (cap->arg)
Expand Down Expand Up @@ -4126,7 +4127,7 @@ nv_dollar(cmdarg_T *cap)
if (!virtual_active() || gchar_cursor() != NUL
|| cap->oap->op_type == OP_NOP)
curwin->w_curswant = MAXCOL; // so we stay at the end
if (cursor_down(curwin, (long)(cap->count1 - 1),
if (cursor_down_adv(curwin, (long)(cap->count1 - 1),
cap->oap->op_type == OP_NOP) == FAIL)
clearopbeep(cap->oap);
#ifdef FEAT_FOLDING
Expand Down Expand Up @@ -5780,7 +5781,7 @@ nv_g_underscore_cmd(cmdarg_T *cap)
cap->oap->motion_type = MCHAR;
cap->oap->inclusive = TRUE;
curwin->w_curswant = MAXCOL;
if (cursor_down(curwin, (long)(cap->count1 - 1),
if (cursor_down_adv(curwin, (long)(cap->count1 - 1),
cap->oap->op_type == OP_NOP) == FAIL)
{
clearopbeep(cap->oap);
Expand Down Expand Up @@ -5856,7 +5857,7 @@ nv_g_dollar_cmd(cmdarg_T *cap)
{
if (cap->count1 > 1)
// if it fails, let the cursor still move to the last char
(void)cursor_down(curwin, cap->count1 - 1, FALSE);
(void)cursor_down_adv(curwin, cap->count1 - 1, FALSE);

i = curwin->w_leftcol + curwin->w_width - col_off - 1;
coladvance((colnr_T)i);
Expand Down Expand Up @@ -5989,7 +5990,7 @@ nv_g_cmd(cmdarg_T *cap)
if (!curwin->w_p_wrap)
{
oap->motion_type = MLINE;
i = cursor_down(curwin, cap->count1, oap->op_type == OP_NOP);
i = cursor_down_adv(curwin, cap->count1, oap->op_type == OP_NOP);
}
else
i = nv_screengo(oap, FORWARD, cap->count1);
Expand All @@ -6003,7 +6004,7 @@ nv_g_cmd(cmdarg_T *cap)
if (!curwin->w_p_wrap)
{
oap->motion_type = MLINE;
i = cursor_up(curwin, cap->count1, oap->op_type == OP_NOP);
i = cursor_up_adv(curwin, cap->count1, oap->op_type == OP_NOP);
}
else
i = nv_screengo(oap, BACKWARD, cap->count1);
Expand Down Expand Up @@ -6448,7 +6449,7 @@ set_op_var(int optype)
nv_lineop(cmdarg_T *cap)
{
cap->oap->motion_type = MLINE;
if (cursor_down(curwin, cap->count1 - 1L,
if (cursor_down_adv(curwin, cap->count1 - 1L,
cap->oap->op_type == OP_NOP) == FAIL)
clearopbeep(cap->oap);
else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Expand Down
35 changes: 18 additions & 17 deletions src/window.c
Expand Up @@ -6349,30 +6349,32 @@ set_fraction(win_T *wp)
/*
* Handle scroll position for 'nosplitscroll'. Replaces scroll_to_fraction()
* call from win_new_height(). Instead we iterate over all windows in a
* tabpage and calculate the new scroll/cursor position.
* tabpage and calculate the new scroll position.
* TODO: Ensure this also works with wrapped lines.
* Requires topline to be able to be set to a bufferline with some
* offset(row-wise scrolling/smoothscroll).
*/
static void
win_fix_scroll(int resize)
{
win_T *wp;
linenr_T lnum;
int diff;
win_T *wp;
linenr_T lnum;

skip_update_topline = TRUE; // avoid scrolling in curs_columns()
FOR_ALL_WINDOWS(wp)
{
// Skip when window height has not changed.
if (wp->w_height != wp->w_prev_height)
{
// Determine botline needed to avoid scrolling and set cursor.
// If window has moved update botline to keep the same screenlines.
if (wp->w_winrow != wp->w_prev_winrow)
{
lnum = wp->w_cursor.lnum;
int diff = (wp->w_winrow - wp->w_prev_winrow)
+ (wp->w_height - wp->w_prev_height);
diff = (wp->w_winrow - wp->w_prev_winrow)
+ (wp->w_height - wp->w_prev_height);
wp->w_cursor.lnum = wp->w_botline - 1;
// Add difference in height and row to botline.
if (diff > 0)
cursor_down(wp, diff, FALSE);
else
Expand Down Expand Up @@ -6407,35 +6409,34 @@ win_fix_scroll(int resize)
static void
win_fix_cursor(int normal)
{
win_T *wp = curwin;
long so = get_scrolloff_value();
linenr_T nlnum = 0;
linenr_T lnum = wp->w_cursor.lnum;
linenr_T top;
linenr_T bot;
long so = get_scrolloff_value();
win_T *wp = curwin;
linenr_T nlnum = 0;
linenr_T lnum = wp->w_cursor.lnum;
linenr_T bot;
linenr_T top;

if (wp->w_buffer->b_ml.ml_line_count < wp->w_height)
return;
#ifdef FEAT_CMDWIN
if (skip_win_fix_cursor)
return;
#endif

// Determine valid cursor range.
so = MIN(wp->w_height / 2, so);
wp->w_cursor.lnum = wp->w_topline;
top = cursor_down(wp, so, FALSE);
wp->w_cursor.lnum = wp->w_botline - 1;
bot = cursor_up(wp, so, FALSE);

// Check if cursor position is above or below visible range.
// Check if cursor position is above or below valid cursor range.
if (lnum > bot && (wp->w_botline - wp->w_buffer->b_ml.ml_line_count) != 1)
nlnum = bot;
else if (lnum < top && wp->w_topline != 1)
nlnum = (so == wp->w_height / 2) ? bot : top;

wp->w_cursor.lnum = lnum;
// Cursor is invalid for current scroll position.
if (nlnum)

if (nlnum) // Cursor is invalid for current scroll position.
{
if (normal) // Save to jumplist and set cursor to avoid scrolling.
{
Expand Down

0 comments on commit 3963476

Please sign in to comment.