diff options
Diffstat (limited to 'src/nvim/memline.c')
-rw-r--r-- | src/nvim/memline.c | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/src/nvim/memline.c b/src/nvim/memline.c index ec9996810f..e84b8d623d 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -3999,18 +3999,15 @@ void goto_byte(long cnt) /// Return 0 otherwise. int inc(pos_T *lp) { - char_u *p = ml_get_pos(lp); - - if (*p != NUL) { // still within line, move to next char (may be NUL) - if (has_mbyte) { - int l = (*mb_ptr2len)(p); + // when searching position may be set to end of a line + if (lp->col != MAXCOL) { + const char_u *const p = ml_get_pos(lp); + if (*p != NUL) { // still within line, move to next char (may be NUL) + const int l = utfc_ptr2len(p); lp->col += l; - return (p[l] != NUL) ? 0 : 2; + return ((p[l] != NUL) ? 0 : 2); } - lp->col++; - lp->coladd = 0; - return (p[1] != NUL) ? 0 : 2; } if (lp->lnum != curbuf->b_ml.ml_line_count) { // there is a next line lp->col = 0; @@ -4035,20 +4032,32 @@ int incl(pos_T *lp) int dec(pos_T *lp) { lp->coladd = 0; - if (lp->col > 0) { // still within line + if (lp->col == MAXCOL) { + // past end of line + char_u *p = ml_get(lp->lnum); + lp->col = (colnr_T)STRLEN(p); + lp->col -= utf_head_off(p, p + lp->col); + return 0; + } + + if (lp->col > 0) { + // still within line lp->col--; char_u *p = ml_get(lp->lnum); lp->col -= utf_head_off(p, p + lp->col); return 0; } - if (lp->lnum > 1) { // there is a prior line + if (lp->lnum > 1) { + // there is a prior line lp->lnum--; char_u *p = ml_get(lp->lnum); lp->col = (colnr_T)STRLEN(p); lp->col -= utf_head_off(p, p + lp->col); return 1; } - return -1; // at start of file + + // at start of file + return -1; } /// Same as dec(), but skip NUL at the end of non-empty lines. |