From e62722922b671d6f529570af8c96c463878dd46d Mon Sep 17 00:00:00 2001 From: Hinidu Date: Fri, 2 May 2014 15:34:53 +0300 Subject: Extract cursor.h from misc{1,2}.h and memline.h --- src/nvim/cursor.c | 489 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 489 insertions(+) create mode 100644 src/nvim/cursor.c (limited to 'src/nvim/cursor.c') diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c new file mode 100644 index 0000000000..661516b52d --- /dev/null +++ b/src/nvim/cursor.c @@ -0,0 +1,489 @@ +#include "nvim/cursor.h" +#include "nvim/charset.h" +#include "nvim/fold.h" +#include "nvim/memline.h" +#include "nvim/memory.h" +#include "nvim/misc1.h" +#include "nvim/move.h" +#include "nvim/screen.h" +#include "nvim/vim.h" + +static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol); + +/* + * Get the screen position of the cursor. + */ +int getviscol(void) +{ + colnr_T x; + + getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL); + return (int)x; +} + +/* + * Get the screen position of character col with a coladd in the cursor line. + */ +int getviscol2(colnr_T col, colnr_T coladd) +{ + colnr_T x; + pos_T pos; + + pos.lnum = curwin->w_cursor.lnum; + pos.col = col; + pos.coladd = coladd; + getvvcol(curwin, &pos, &x, NULL, NULL); + return (int)x; +} + +/* + * Go to column "wcol", and add/insert white space as necessary to get the + * cursor in that column. + * The caller must have saved the cursor line for undo! + */ +int coladvance_force(colnr_T wcol) +{ + int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol); + + if (wcol == MAXCOL) + curwin->w_valid &= ~VALID_VIRTCOL; + else { + /* Virtcol is valid */ + curwin->w_valid |= VALID_VIRTCOL; + curwin->w_virtcol = wcol; + } + return rc; +} + +/* + * Try to advance the Cursor to the specified screen column. + * If virtual editing: fine tune the cursor position. + * Note that all virtual positions off the end of a line should share + * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)), + * beginning at coladd 0. + * + * return OK if desired column is reached, FAIL if not + */ +int coladvance(colnr_T wcol) +{ + int rc = getvpos(&curwin->w_cursor, wcol); + + if (wcol == MAXCOL || rc == FAIL) + curwin->w_valid &= ~VALID_VIRTCOL; + else if (*ml_get_cursor() != TAB) { + /* Virtcol is valid when not on a TAB */ + curwin->w_valid |= VALID_VIRTCOL; + curwin->w_virtcol = wcol; + } + return rc; +} + +static int +coladvance2 ( + pos_T *pos, + int addspaces, /* change the text to achieve our goal? */ + int finetune, /* change char offset for the exact column */ + colnr_T wcol /* column to move to */ +) +{ + int idx; + char_u *ptr; + char_u *line; + colnr_T col = 0; + int csize = 0; + int one_more; + int head = 0; + + one_more = (State & INSERT) + || restart_edit != NUL + || (VIsual_active && *p_sel != 'o') + || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL) + ; + line = ml_get_buf(curbuf, pos->lnum, FALSE); + + if (wcol >= MAXCOL) { + idx = (int)STRLEN(line) - 1 + one_more; + col = wcol; + + if ((addspaces || finetune) && !VIsual_active) { + curwin->w_curswant = linetabsize(line) + one_more; + if (curwin->w_curswant > 0) + --curwin->w_curswant; + } + } else { + int width = W_WIDTH(curwin) - win_col_off(curwin); + + if (finetune + && curwin->w_p_wrap + && curwin->w_width != 0 + && wcol >= (colnr_T)width) { + csize = linetabsize(line); + if (csize > 0) + csize--; + + if (wcol / width > (colnr_T)csize / width + && ((State & INSERT) == 0 || (int)wcol > csize + 1)) { + /* In case of line wrapping don't move the cursor beyond the + * right screen edge. In Insert mode allow going just beyond + * the last character (like what happens when typing and + * reaching the right window edge). */ + wcol = (csize / width + 1) * width - 1; + } + } + + ptr = line; + while (col <= wcol && *ptr != NUL) { + /* Count a tab for what it's worth (if list mode not on) */ + csize = win_lbr_chartabsize(curwin, ptr, col, &head); + mb_ptr_adv(ptr); + col += csize; + } + idx = (int)(ptr - line); + /* + * Handle all the special cases. The virtual_active() check + * is needed to ensure that a virtual position off the end of + * a line has the correct indexing. The one_more comparison + * replaces an explicit add of one_more later on. + */ + if (col > wcol || (!virtual_active() && one_more == 0)) { + idx -= 1; + /* Don't count the chars from 'showbreak'. */ + csize -= head; + col -= csize; + } + + if (virtual_active() + && addspaces + && ((col != wcol && col != wcol + 1) || csize > 1)) { + /* 'virtualedit' is set: The difference between wcol and col is + * filled with spaces. */ + + if (line[idx] == NUL) { + /* Append spaces */ + int correct = wcol - col; + char_u *newline = xmalloc(idx + correct + 1); + int t; + + for (t = 0; t < idx; ++t) + newline[t] = line[t]; + + for (t = 0; t < correct; ++t) + newline[t + idx] = ' '; + + newline[idx + correct] = NUL; + + ml_replace(pos->lnum, newline, FALSE); + changed_bytes(pos->lnum, (colnr_T)idx); + idx += correct; + col = wcol; + } else { + /* Break a tab */ + int linelen = (int)STRLEN(line); + int correct = wcol - col - csize + 1; /* negative!! */ + char_u *newline; + int t, s = 0; + int v; + + if (-correct > csize) + return FAIL; + + newline = xmalloc(linelen + csize); + + for (t = 0; t < linelen; t++) { + if (t != idx) + newline[s++] = line[t]; + else + for (v = 0; v < csize; v++) + newline[s++] = ' '; + } + + newline[linelen + csize - 1] = NUL; + + ml_replace(pos->lnum, newline, FALSE); + changed_bytes(pos->lnum, idx); + idx += (csize - 1 + correct); + col += correct; + } + } + } + + if (idx < 0) + pos->col = 0; + else + pos->col = idx; + + pos->coladd = 0; + + if (finetune) { + if (wcol == MAXCOL) { + /* The width of the last character is used to set coladd. */ + if (!one_more) { + colnr_T scol, ecol; + + getvcol(curwin, pos, &scol, NULL, &ecol); + pos->coladd = ecol - scol; + } + } else { + int b = (int)wcol - (int)col; + + /* The difference between wcol and col is used to set coladd. */ + if (b > 0 && b < (MAXCOL - 2 * W_WIDTH(curwin))) + pos->coladd = b; + + col += b; + } + } + + /* prevent from moving onto a trail byte */ + if (has_mbyte) + mb_adjustpos(curbuf, pos); + + if (col < wcol) + return FAIL; + return OK; +} + +/* + * Return in "pos" the position of the cursor advanced to screen column "wcol". + * return OK if desired column is reached, FAIL if not + */ +int getvpos(pos_T *pos, colnr_T wcol) +{ + return coladvance2(pos, FALSE, virtual_active(), wcol); +} + +/* + * Increment the cursor position. See inc() for return values. + */ +int inc_cursor(void) +{ + return inc(&curwin->w_cursor); +} + +/* + * dec(p) + * + * Decrement the line pointer 'p' crossing line boundaries as necessary. + * Return 1 when crossing a line, -1 when at start of file, 0 otherwise. + */ +int dec_cursor(void) +{ + return dec(&curwin->w_cursor); +} + +/* + * Get the line number relative to the current cursor position, i.e. the + * difference between line number and cursor position. Only look for lines that + * can be visible, folded lines don't count. + */ +linenr_T +get_cursor_rel_lnum ( + win_T *wp, + linenr_T lnum /* line number to get the result for */ +) +{ + linenr_T cursor = wp->w_cursor.lnum; + linenr_T retval = 0; + + if (hasAnyFolding(wp)) { + if (lnum > cursor) { + while (lnum > cursor) { + (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL); + /* if lnum and cursor are in the same fold, + * now lnum <= cursor */ + if (lnum > cursor) + retval++; + lnum--; + } + } else if (lnum < cursor) { + while (lnum < cursor) { + (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL); + /* if lnum and cursor are in the same fold, + * now lnum >= cursor */ + if (lnum < cursor) + retval--; + lnum++; + } + } + /* else if (lnum == cursor) + * retval = 0; + */ + } else + retval = lnum - cursor; + + return retval; +} + +/* + * Make sure curwin->w_cursor.lnum is valid. + */ +void check_cursor_lnum(void) +{ + if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { + /* If there is a closed fold at the end of the file, put the cursor in + * its first line. Otherwise in the last line. */ + if (!hasFolding(curbuf->b_ml.ml_line_count, + &curwin->w_cursor.lnum, NULL)) + curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; + } + if (curwin->w_cursor.lnum <= 0) + curwin->w_cursor.lnum = 1; +} + +/* + * Make sure curwin->w_cursor.col is valid. + */ +void check_cursor_col(void) +{ + check_cursor_col_win(curwin); +} + +/* + * Make sure win->w_cursor.col is valid. + */ +void check_cursor_col_win(win_T *win) +{ + colnr_T len; + colnr_T oldcol = win->w_cursor.col; + colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd; + + len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE)); + if (len == 0) + win->w_cursor.col = 0; + else if (win->w_cursor.col >= len) { + /* Allow cursor past end-of-line when: + * - in Insert mode or restarting Insert mode + * - in Visual mode and 'selection' isn't "old" + * - 'virtualedit' is set */ + if ((State & INSERT) || restart_edit + || (VIsual_active && *p_sel != 'o') + || (ve_flags & VE_ONEMORE) + || virtual_active()) + 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); + } + } 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) + win->w_cursor.coladd = 0; + 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 */ + win->w_cursor.coladd = 0; + } +} + +/* + * make sure curwin->w_cursor in on a valid character + */ +void check_cursor(void) +{ + check_cursor_lnum(); + check_cursor_col(); +} + +/* + * Make sure curwin->w_cursor is not on the NUL at the end of the line. + * Allow it when in Visual mode and 'selection' is not "old". + */ +void adjust_cursor_col(void) +{ + if (curwin->w_cursor.col > 0 + && (!VIsual_active || *p_sel == 'o') + && gchar_cursor() == NUL) + --curwin->w_cursor.col; +} + +/* + * When curwin->w_leftcol has changed, adjust the cursor position. + * Return TRUE if the cursor was moved. + */ +int leftcol_changed(void) +{ + long lastcol; + colnr_T s, e; + int retval = FALSE; + + changed_cline_bef_curs(); + lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1; + validate_virtcol(); + + /* + * If the cursor is right or left of the screen, move it to last or first + * character. + */ + if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso)) { + retval = TRUE; + coladvance((colnr_T)(lastcol - p_siso)); + } else if (curwin->w_virtcol < curwin->w_leftcol + p_siso) { + retval = TRUE; + (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso)); + } + + /* + * If the start of the character under the cursor is not on the screen, + * advance the cursor one more char. If this fails (last char of the + * line) adjust the scrolling. + */ + getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e); + if (e > (colnr_T)lastcol) { + retval = TRUE; + coladvance(s - 1); + } else if (s < curwin->w_leftcol) { + retval = TRUE; + if (coladvance(e + 1) == FAIL) { /* there isn't another character */ + curwin->w_leftcol = s; /* adjust w_leftcol instead */ + changed_cline_bef_curs(); + } + } + + if (retval) + curwin->w_set_curswant = TRUE; + redraw_later(NOT_VALID); + return retval; +} + +int gchar_cursor(void) +{ + if (has_mbyte) + return (*mb_ptr2char)(ml_get_cursor()); + return (int)*ml_get_cursor(); +} + +/* + * Write a character at the current cursor position. + * It is directly written into the block. + */ +void pchar_cursor(int c) +{ + *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) + + curwin->w_cursor.col) = c; +} + +/* + * Return pointer to cursor line. + */ +char_u *ml_get_curline(void) +{ + return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE); +} + +/* + * Return pointer to cursor position. + */ +char_u *ml_get_cursor(void) +{ + return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE) + + curwin->w_cursor.col; +} + -- cgit From 2a154ef71de506e64fbbc2d8e613b5a696f8cb60 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Thu, 15 May 2014 16:49:58 +0300 Subject: Enable -Wconversion on cursor.c --- src/nvim/cursor.c | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'src/nvim/cursor.c') diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index 661516b52d..e90422bf6d 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -161,16 +161,9 @@ coladvance2 ( if (line[idx] == NUL) { /* Append spaces */ int correct = wcol - col; - char_u *newline = xmalloc(idx + correct + 1); - int t; - - for (t = 0; t < idx; ++t) - newline[t] = line[t]; - - for (t = 0; t < correct; ++t) - newline[t + idx] = ' '; - - newline[idx + correct] = NUL; + char_u *newline = xmallocz((size_t)(idx + correct)); + memcpy(newline, line, (size_t)idx); + memset(newline + idx, ' ', (size_t)correct); ml_replace(pos->lnum, newline, FALSE); changed_bytes(pos->lnum, (colnr_T)idx); @@ -181,23 +174,18 @@ coladvance2 ( int linelen = (int)STRLEN(line); int correct = wcol - col - csize + 1; /* negative!! */ char_u *newline; - int t, s = 0; - int v; if (-correct > csize) return FAIL; - newline = xmalloc(linelen + csize); - - for (t = 0; t < linelen; t++) { - if (t != idx) - newline[s++] = line[t]; - else - for (v = 0; v < csize; v++) - newline[s++] = ' '; - } - - newline[linelen + csize - 1] = NUL; + newline = xmallocz((size_t)(linelen - 1 + csize)); + // 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)); ml_replace(pos->lnum, newline, FALSE); changed_bytes(pos->lnum, idx); @@ -464,7 +452,7 @@ int gchar_cursor(void) * Write a character at the current cursor position. * It is directly written into the block. */ -void pchar_cursor(int c) +void pchar_cursor(char_u c) { *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) + curwin->w_cursor.col) = c; -- cgit From baaa4287851ecf478b40054efd7b786501eb70ef Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Fri, 16 May 2014 11:59:00 +0300 Subject: Remove ml_ prefix from cursor.h functions s/ml_get_curline/get_cursor_line_ptr s/ml_get_cursor/get_cursor_pos_ptr --- src/nvim/cursor.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/nvim/cursor.c') diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index e90422bf6d..8427f2a0a2 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -70,7 +70,7 @@ int coladvance(colnr_T wcol) if (wcol == MAXCOL || rc == FAIL) curwin->w_valid &= ~VALID_VIRTCOL; - else if (*ml_get_cursor() != TAB) { + else if (*get_cursor_pos_ptr() != TAB) { /* Virtcol is valid when not on a TAB */ curwin->w_valid |= VALID_VIRTCOL; curwin->w_virtcol = wcol; @@ -444,8 +444,8 @@ int leftcol_changed(void) int gchar_cursor(void) { if (has_mbyte) - return (*mb_ptr2char)(ml_get_cursor()); - return (int)*ml_get_cursor(); + return (*mb_ptr2char)(get_cursor_pos_ptr()); + return (int)*get_cursor_pos_ptr(); } /* @@ -461,7 +461,7 @@ void pchar_cursor(char_u c) /* * Return pointer to cursor line. */ -char_u *ml_get_curline(void) +char_u *get_cursor_line_ptr(void) { return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE); } @@ -469,7 +469,7 @@ char_u *ml_get_curline(void) /* * Return pointer to cursor position. */ -char_u *ml_get_cursor(void) +char_u *get_cursor_pos_ptr(void) { return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE) + curwin->w_cursor.col; -- cgit From a01f7948bc28df20b5d96ed53f8d25b2db12a48d Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Fri, 16 May 2014 13:40:40 +0300 Subject: Add cursor.{c,h} to clint-files.txt --- src/nvim/cursor.c | 79 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) (limited to 'src/nvim/cursor.c') diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index 8427f2a0a2..308f09f264 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -1,3 +1,5 @@ +#include + #include "nvim/cursor.h" #include "nvim/charset.h" #include "nvim/fold.h" @@ -8,7 +10,7 @@ #include "nvim/screen.h" #include "nvim/vim.h" -static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol); +static int coladvance2(pos_T *pos, bool addspaces, bool finetune, colnr_T wcol); /* * Get the screen position of the cursor. @@ -43,11 +45,11 @@ int getviscol2(colnr_T col, colnr_T coladd) */ int coladvance_force(colnr_T wcol) { - int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol); + int rc = coladvance2(&curwin->w_cursor, true, false, wcol); - if (wcol == MAXCOL) + if (wcol == MAXCOL) { curwin->w_valid &= ~VALID_VIRTCOL; - else { + } else { /* Virtcol is valid */ curwin->w_valid |= VALID_VIRTCOL; curwin->w_virtcol = wcol; @@ -78,11 +80,10 @@ int coladvance(colnr_T wcol) return rc; } -static int -coladvance2 ( +static int coladvance2( pos_T *pos, - int addspaces, /* change the text to achieve our goal? */ - int finetune, /* change char offset for the exact column */ + bool addspaces, /* change the text to achieve our goal? */ + bool finetune, /* change char offset for the exact column */ colnr_T wcol /* column to move to */ ) { @@ -97,9 +98,8 @@ coladvance2 ( one_more = (State & INSERT) || restart_edit != NUL || (VIsual_active && *p_sel != 'o') - || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL) - ; - line = ml_get_buf(curbuf, pos->lnum, FALSE); + || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL); + line = ml_get_buf(curbuf, pos->lnum, false); if (wcol >= MAXCOL) { idx = (int)STRLEN(line) - 1 + one_more; @@ -165,7 +165,7 @@ coladvance2 ( memcpy(newline, line, (size_t)idx); memset(newline + idx, ' ', (size_t)correct); - ml_replace(pos->lnum, newline, FALSE); + ml_replace(pos->lnum, newline, false); changed_bytes(pos->lnum, (colnr_T)idx); idx += correct; col = wcol; @@ -187,7 +187,7 @@ coladvance2 ( memcpy(newline + idx + csize, line + idx + 1, (size_t)(linelen - idx - 1)); - ml_replace(pos->lnum, newline, FALSE); + ml_replace(pos->lnum, newline, false); changed_bytes(pos->lnum, idx); idx += (csize - 1 + correct); col += correct; @@ -237,7 +237,7 @@ coladvance2 ( */ int getvpos(pos_T *pos, colnr_T wcol) { - return coladvance2(pos, FALSE, virtual_active(), wcol); + return coladvance2(pos, false, virtual_active(), wcol); } /* @@ -264,8 +264,7 @@ int dec_cursor(void) * difference between line number and cursor position. Only look for lines that * can be visible, folded lines don't count. */ -linenr_T -get_cursor_rel_lnum ( +linenr_T get_cursor_rel_lnum( win_T *wp, linenr_T lnum /* line number to get the result for */ ) @@ -276,7 +275,7 @@ get_cursor_rel_lnum ( if (hasAnyFolding(wp)) { if (lnum > cursor) { while (lnum > cursor) { - (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL); + (void)hasFoldingWin(wp, lnum, &lnum, NULL, true, NULL); /* if lnum and cursor are in the same fold, * now lnum <= cursor */ if (lnum > cursor) @@ -285,7 +284,7 @@ get_cursor_rel_lnum ( } } else if (lnum < cursor) { while (lnum < cursor) { - (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL); + (void)hasFoldingWin(wp, lnum, NULL, &lnum, true, NULL); /* if lnum and cursor are in the same fold, * now lnum >= cursor */ if (lnum < cursor) @@ -296,8 +295,9 @@ get_cursor_rel_lnum ( /* else if (lnum == cursor) * retval = 0; */ - } else + } else { retval = lnum - cursor; + } return retval; } @@ -335,10 +335,10 @@ void check_cursor_col_win(win_T *win) colnr_T oldcol = win->w_cursor.col; colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd; - len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE)); - if (len == 0) + len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, false)); + if (len == 0) { win->w_cursor.col = 0; - else if (win->w_cursor.col >= len) { + } else if (win->w_cursor.col >= len) { /* Allow cursor past end-of-line when: * - in Insert mode or restarting Insert mode * - in Visual mode and 'selection' isn't "old" @@ -346,16 +346,17 @@ void check_cursor_col_win(win_T *win) if ((State & INSERT) || restart_edit || (VIsual_active && *p_sel != 'o') || (ve_flags & VE_ONEMORE) - || virtual_active()) + || virtual_active()) { win->w_cursor.col = len; - else { + } 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); } - } else if (win->w_cursor.col < 0) + } 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 @@ -394,13 +395,15 @@ void adjust_cursor_col(void) /* * When curwin->w_leftcol has changed, adjust the cursor position. - * Return TRUE if the cursor was moved. + * Return true if the cursor was moved. */ -int leftcol_changed(void) +bool leftcol_changed(void) { - long lastcol; + // TODO(hinidu): I think it should be colnr_T or int, but p_siso is long. + // Perhaps we can change p_siso to int. + int64_t lastcol; colnr_T s, e; - int retval = FALSE; + bool retval = false; changed_cline_bef_curs(); lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1; @@ -411,11 +414,11 @@ int leftcol_changed(void) * character. */ if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso)) { - retval = TRUE; + retval = true; coladvance((colnr_T)(lastcol - p_siso)); } else if (curwin->w_virtcol < curwin->w_leftcol + p_siso) { - retval = TRUE; - (void)coladvance((colnr_T)(curwin->w_leftcol + p_siso)); + retval = true; + coladvance((colnr_T)(curwin->w_leftcol + p_siso)); } /* @@ -425,10 +428,10 @@ int leftcol_changed(void) */ getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e); if (e > (colnr_T)lastcol) { - retval = TRUE; + retval = true; coladvance(s - 1); } else if (s < curwin->w_leftcol) { - retval = TRUE; + retval = true; if (coladvance(e + 1) == FAIL) { /* there isn't another character */ curwin->w_leftcol = s; /* adjust w_leftcol instead */ changed_cline_bef_curs(); @@ -436,7 +439,7 @@ int leftcol_changed(void) } if (retval) - curwin->w_set_curswant = TRUE; + curwin->w_set_curswant = true; redraw_later(NOT_VALID); return retval; } @@ -454,7 +457,7 @@ int gchar_cursor(void) */ void pchar_cursor(char_u c) { - *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) + *(ml_get_buf(curbuf, curwin->w_cursor.lnum, true) + curwin->w_cursor.col) = c; } @@ -463,7 +466,7 @@ void pchar_cursor(char_u c) */ char_u *get_cursor_line_ptr(void) { - return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE); + return ml_get_buf(curbuf, curwin->w_cursor.lnum, false); } /* @@ -471,7 +474,7 @@ char_u *get_cursor_line_ptr(void) */ char_u *get_cursor_pos_ptr(void) { - return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE) + + return ml_get_buf(curbuf, curwin->w_cursor.lnum, false) + curwin->w_cursor.col; } -- cgit From 7e3681c32e4fd1e19c01ffe7286ea29aafb2efc4 Mon Sep 17 00:00:00 2001 From: Pavel Platto Date: Tue, 20 May 2014 11:41:49 +0300 Subject: Remove code duplication in get_cursor_rel_lnum --- src/nvim/cursor.c | 57 +++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) (limited to 'src/nvim/cursor.c') diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index 308f09f264..8f8bc60510 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -259,47 +259,34 @@ int dec_cursor(void) return dec(&curwin->w_cursor); } -/* - * Get the line number relative to the current cursor position, i.e. the - * difference between line number and cursor position. Only look for lines that - * can be visible, folded lines don't count. - */ -linenr_T get_cursor_rel_lnum( - win_T *wp, - linenr_T lnum /* line number to get the result for */ -) +/// Get the line number relative to the current cursor position, i.e. the +/// difference between line number and cursor position. Only look for lines that +/// can be visible, folded lines don't count. +/// +/// @param lnum line number to get the result for +linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum) { linenr_T cursor = wp->w_cursor.lnum; + if (lnum == cursor || !hasAnyFolding(wp)) { + return lnum - cursor; + } + + linenr_T from_line = lnum < cursor ? lnum : cursor; + linenr_T to_line = lnum > cursor ? lnum : cursor; linenr_T retval = 0; - if (hasAnyFolding(wp)) { - if (lnum > cursor) { - while (lnum > cursor) { - (void)hasFoldingWin(wp, lnum, &lnum, NULL, true, NULL); - /* if lnum and cursor are in the same fold, - * now lnum <= cursor */ - if (lnum > cursor) - retval++; - lnum--; - } - } else if (lnum < cursor) { - while (lnum < cursor) { - (void)hasFoldingWin(wp, lnum, NULL, &lnum, true, NULL); - /* if lnum and cursor are in the same fold, - * now lnum >= cursor */ - if (lnum < cursor) - retval--; - lnum++; - } - } - /* else if (lnum == cursor) - * retval = 0; - */ - } else { - retval = lnum - cursor; + // Loop until we reach to_line, skipping folds. + for (; from_line < to_line; from_line++, retval++) { + // If from_line is in a fold, set it to the last line of that fold. + hasFoldingWin(wp, from_line, NULL, &from_line, true, NULL); } - return retval; + // If to_line is in a closed fold, the line count is off by +1. Correct it. + if (from_line > to_line) { + retval--; + } + + return (lnum < cursor) ? -retval : retval; } /* -- cgit