diff options
Diffstat (limited to 'src/nvim/cursor.c')
-rw-r--r-- | src/nvim/cursor.c | 106 |
1 files changed, 75 insertions, 31 deletions
diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index afaa6022c9..6c1bd01ff5 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -1,6 +1,10 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + #include <stdbool.h> #include <inttypes.h> +#include "nvim/assert.h" #include "nvim/cursor.h" #include "nvim/charset.h" #include "nvim/fold.h" @@ -9,8 +13,10 @@ #include "nvim/misc1.h" #include "nvim/move.h" #include "nvim/screen.h" +#include "nvim/state.h" #include "nvim/vim.h" #include "nvim/ascii.h" +#include "nvim/mark.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "cursor.c.generated.h" @@ -115,11 +121,11 @@ static int coladvance2( --curwin->w_curswant; } } else { - int width = curwin->w_width - win_col_off(curwin); + int width = curwin->w_grid.Columns - win_col_off(curwin); if (finetune && curwin->w_p_wrap - && curwin->w_width != 0 + && curwin->w_grid.Columns != 0 && wcol >= (colnr_T)width) { csize = linetabsize(line); if (csize > 0) @@ -139,7 +145,7 @@ static int coladvance2( while (col <= wcol && *ptr != NUL) { /* Count a tab for what it's worth (if list mode not on) */ csize = win_lbr_chartabsize(curwin, line, ptr, col, &head); - mb_ptr_adv(ptr); + MB_PTR_ADV(ptr); col += csize; } idx = (int)(ptr - line); @@ -165,7 +171,9 @@ static int coladvance2( if (line[idx] == NUL) { /* Append spaces */ int correct = wcol - col; - char_u *newline = xmallocz((size_t)(idx + correct)); + size_t newline_size; + STRICT_ADD(idx, correct, &newline_size, size_t); + char_u *newline = xmallocz(newline_size); memcpy(newline, line, (size_t)idx); memset(newline + idx, ' ', (size_t)correct); @@ -182,14 +190,17 @@ static int coladvance2( if (-correct > csize) return FAIL; - newline = xmallocz((size_t)(linelen - 1 + csize)); + size_t n; + STRICT_ADD(linelen - 1, csize, &n, size_t); + newline = xmallocz(n); // Copy first idx chars memcpy(newline, line, (size_t)idx); // Replace idx'th char with csize spaces memset(newline + idx, ' ', (size_t)csize); // Copy the rest of the line - memcpy(newline + idx + csize, line + idx + 1, - (size_t)(linelen - idx - 1)); + STRICT_SUB(linelen, idx, &n, size_t); + STRICT_SUB(n, 1, &n, size_t); + memcpy(newline + idx + csize, line + idx + 1, n); ml_replace(pos->lnum, newline, false); changed_bytes(pos->lnum, idx); @@ -218,17 +229,19 @@ static int coladvance2( } else { int b = (int)wcol - (int)col; - /* The difference between wcol and col is used to set coladd. */ - if (b > 0 && b < (MAXCOL - 2 * curwin->w_width)) + // The difference between wcol and col is used to set coladd. + if (b > 0 && b < (MAXCOL - 2 * curwin->w_grid.Columns)) { pos->coladd = b; + } col += b; } } - /* prevent from moving onto a trail byte */ - if (has_mbyte) - mb_adjustpos(curbuf, pos); + // Prevent from moving onto a trail byte. + if (has_mbyte) { + mark_mb_adjustpos(curbuf, pos); + } if (col < wcol) return FAIL; @@ -293,6 +306,26 @@ linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum) return (lnum < cursor) ? -retval : retval; } +// Make sure "pos.lnum" and "pos.col" are valid in "buf". +// This allows for the col to be on the NUL byte. +void check_pos(buf_T *buf, pos_T *pos) +{ + char_u *line; + colnr_T len; + + if (pos->lnum > buf->b_ml.ml_line_count) { + pos->lnum = buf->b_ml.ml_line_count; + } + + if (pos->col > 0) { + line = ml_get_buf(buf, pos->lnum, false); + len = (colnr_T)STRLEN(line); + if (pos->col > len) { + pos->col = len; + } + } +} + /* * Make sure curwin->w_cursor.lnum is valid. */ @@ -317,9 +350,8 @@ void check_cursor_col(void) check_cursor_col_win(curwin); } -/* - * Make sure win->w_cursor.col is valid. - */ +/// Make sure win->w_cursor.col is valid. Special handling of insert-mode. +/// @see mb_check_adjust_col void check_cursor_col_win(win_T *win) { colnr_T len; @@ -341,25 +373,40 @@ void check_cursor_col_win(win_T *win) win->w_cursor.col = len; } else { win->w_cursor.col = len - 1; - /* Move the cursor to the head byte. */ - if (has_mbyte) - mb_adjustpos(win->w_buffer, &win->w_cursor); + // Move the cursor to the head byte. + if (has_mbyte) { + mark_mb_adjustpos(win->w_buffer, &win->w_cursor); + } } } else if (win->w_cursor.col < 0) { win->w_cursor.col = 0; } - /* If virtual editing is on, we can leave the cursor on the old position, - * only we must set it to virtual. But don't do it when at the end of the - * line. */ - if (oldcol == MAXCOL) + // If virtual editing is on, we can leave the cursor on the old position, + // only we must set it to virtual. But don't do it when at the end of the + // line. + if (oldcol == MAXCOL) { win->w_cursor.coladd = 0; - else if (ve_flags == VE_ALL) { - if (oldcoladd > win->w_cursor.col) + } else if (ve_flags == VE_ALL) { + if (oldcoladd > win->w_cursor.col) { win->w_cursor.coladd = oldcoladd - win->w_cursor.col; - else - /* avoid weird number when there is a miscalculation or overflow */ + + // Make sure that coladd is not more than the char width. + // Not for the last character, coladd is then used when the cursor + // is actually after the last character. + if (win->w_cursor.col + 1 < len) { + assert(win->w_cursor.coladd > 0); + int cs, ce; + + getvcol(win, &win->w_cursor, &cs, NULL, &ce); + if (win->w_cursor.coladd > ce - cs) { + win->w_cursor.coladd = ce - cs; + } + } + } else { + // avoid weird number when there is a miscalculation or overflow win->w_cursor.coladd = 0; + } } } @@ -397,7 +444,7 @@ bool leftcol_changed(void) bool retval = false; changed_cline_bef_curs(); - lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1; + lastcol = curwin->w_leftcol + curwin->w_grid.Columns - curwin_col_off() - 1; validate_virtcol(); /* @@ -437,9 +484,7 @@ bool leftcol_changed(void) int gchar_cursor(void) { - if (has_mbyte) - return (*mb_ptr2char)(get_cursor_pos_ptr()); - return (int)*get_cursor_pos_ptr(); + return utf_ptr2char(get_cursor_pos_ptr()); } /* @@ -468,4 +513,3 @@ char_u *get_cursor_pos_ptr(void) return ml_get_buf(curbuf, curwin->w_cursor.lnum, false) + curwin->w_cursor.col; } - |