diff options
author | Hinidu <hinidu@gmail.com> | 2014-05-02 15:34:53 +0300 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-05-28 10:42:06 -0400 |
commit | e62722922b671d6f529570af8c96c463878dd46d (patch) | |
tree | b7c0faf790b777dfc29caf595953f314253e43cb | |
parent | e4fe2dbd777a59a9a9b386d960eb9dddc459e84e (diff) | |
download | rneovim-e62722922b671d6f529570af8c96c463878dd46d.tar.gz rneovim-e62722922b671d6f529570af8c96c463878dd46d.tar.bz2 rneovim-e62722922b671d6f529570af8c96c463878dd46d.zip |
Extract cursor.h from misc{1,2}.h and memline.h
37 files changed, 548 insertions, 498 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index d2b4cafaac..332b7425d2 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -8,6 +8,7 @@ #include "nvim/api/private/defs.h" #include "nvim/vim.h" #include "nvim/buffer.h" +#include "nvim/cursor.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/misc1.h" diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 8bd8316477..60de6ea219 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -6,6 +6,7 @@ #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" #include "nvim/vim.h" +#include "nvim/cursor.h" #include "nvim/window.h" #include "nvim/screen.h" #include "nvim/misc2.h" diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 7c50b1721c..946053140d 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -30,6 +30,7 @@ #include "nvim/vim.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/digraph.h" #include "nvim/eval.h" 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; +} + diff --git a/src/nvim/cursor.h b/src/nvim/cursor.h new file mode 100644 index 0000000000..bbd20683bf --- /dev/null +++ b/src/nvim/cursor.h @@ -0,0 +1,27 @@ +#ifndef NVIM_CURSOR_H +#define NVIM_CURSOR_H + +#include "nvim/vim.h" +#include "nvim/misc2.h" + +int coladvance(colnr_T wcol); +int coladvance_force(colnr_T wcol); +int getvpos(pos_T *pos, colnr_T wcol); +int getviscol(void); +int getviscol2(colnr_T col, colnr_T coladd); +int inc_cursor(void); +int dec_cursor(void); +linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum); +void check_cursor_lnum(void); +void check_cursor_col(void); +void check_cursor_col_win(win_T *win); +void check_cursor(void); +void adjust_cursor_col(void); +int leftcol_changed(void); +int gchar_cursor(void); +void pchar_cursor(int c); +char_u *ml_get_curline(void); +char_u *ml_get_cursor(void); + +#endif // NVIM_CURSOR_H + diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 06416639bd..b01a7a934a 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -6,6 +6,7 @@ #include "nvim/diff.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" #include "nvim/ex_docmd.h" diff --git a/src/nvim/edit.c b/src/nvim/edit.c index bbb4ea0c03..5ce73e5dc8 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -16,6 +16,7 @@ #include "nvim/edit.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/digraph.h" #include "nvim/eval.h" #include "nvim/ex_docmd.h" diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 83bbd9a24c..bf82e9373e 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -16,6 +16,7 @@ #include "nvim/eval.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/ex_cmds.h" diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 6c297f399b..2fc882113a 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -18,6 +18,7 @@ #include "nvim/ex_cmds.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/digraph.h" #include "nvim/edit.h" diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index ae55c5b65a..a76f09f3bb 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -16,6 +16,7 @@ #include "nvim/ex_docmd.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/digraph.h" #include "nvim/edit.h" diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 278886cf5e..14687be426 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -18,6 +18,7 @@ #include "nvim/ex_getln.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/digraph.h" #include "nvim/edit.h" #include "nvim/eval.h" diff --git a/src/nvim/farsi.c b/src/nvim/farsi.c index 1f9dbf8985..edda8f3f23 100644 --- a/src/nvim/farsi.c +++ b/src/nvim/farsi.c @@ -4,6 +4,7 @@ /// +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/ex_docmd.h" #include "nvim/ex_eval.h" diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 027c7b07d5..290ff424cc 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -16,6 +16,7 @@ #include "nvim/fileio.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/eval.h" diff --git a/src/nvim/fold.c b/src/nvim/fold.c index 1215d5b24d..fe05ba76ac 100644 --- a/src/nvim/fold.c +++ b/src/nvim/fold.c @@ -16,6 +16,7 @@ #include "nvim/vim.h" #include "nvim/fold.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/eval.h" #include "nvim/ex_docmd.h" diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 52322244e1..9cd12a1d52 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -20,6 +20,7 @@ #include "nvim/vim.h" #include "nvim/getchar.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/ex_docmd.h" diff --git a/src/nvim/indent.c b/src/nvim/indent.c index 06273e98c2..fc59bdb50b 100644 --- a/src/nvim/indent.c +++ b/src/nvim/indent.c @@ -1,6 +1,7 @@ #include "nvim/indent.h" #include "nvim/eval.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/misc1.h" diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c index 8979635823..8db16efb80 100644 --- a/src/nvim/indent_c.c +++ b/src/nvim/indent_c.c @@ -1,6 +1,7 @@ #include "nvim/vim.h" #include "nvim/misc1.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/indent.h" #include "nvim/indent_c.h" diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index 10e94d7ced..ecc272fe00 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -82,6 +82,7 @@ #include "nvim/vim.h" #include "nvim/mbyte.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/fileio.h" #include "nvim/memline.h" #include "nvim/message.h" diff --git a/src/nvim/memline.c b/src/nvim/memline.c index dc6823c8fa..c831727a0a 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -46,6 +46,7 @@ #include "nvim/vim.h" #include "nvim/memline.h" #include "nvim/buffer.h" +#include "nvim/cursor.h" #include "nvim/eval.h" #include "nvim/fileio.h" #include "nvim/main.h" @@ -1777,23 +1778,6 @@ char_u *ml_get_pos(pos_T *pos) } /* - * 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; -} - -/* * Return a pointer to a line in a specific buffer * * "will_change": if TRUE mark the buffer dirty (chars in the line will be diff --git a/src/nvim/memline.h b/src/nvim/memline.h index 704a98b20e..831cf46a35 100644 --- a/src/nvim/memline.h +++ b/src/nvim/memline.h @@ -19,8 +19,6 @@ void ml_sync_all(int check_file, int check_char); void ml_preserve(buf_T *buf, int message); char_u *ml_get(linenr_T lnum); char_u *ml_get_pos(pos_T *pos); -char_u *ml_get_curline(void); -char_u *ml_get_cursor(void); char_u *ml_get_buf(buf_T *buf, linenr_T lnum, int will_change); int ml_line_alloced(void); int ml_append(linenr_T lnum, char_u *line, colnr_T len, int newfile); diff --git a/src/nvim/menu.c b/src/nvim/menu.c index d9b84eae71..f71c1a351d 100644 --- a/src/nvim/menu.c +++ b/src/nvim/menu.c @@ -16,6 +16,7 @@ #include "nvim/vim.h" #include "nvim/menu.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/eval.h" #include "nvim/ex_docmd.h" #include "nvim/getchar.h" diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c index d83c03c9fd..efe0ccff6a 100644 --- a/src/nvim/misc1.c +++ b/src/nvim/misc1.c @@ -16,6 +16,7 @@ #include "nvim/version_defs.h" #include "nvim/misc1.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/eval.h" @@ -1795,23 +1796,6 @@ int gchar_pos(pos_T *pos) return (int)*ptr; } -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; -} - /* * Skip to next part of an option argument: Skip space and comma. */ diff --git a/src/nvim/misc1.h b/src/nvim/misc1.h index df1d630621..5d9d2f084b 100644 --- a/src/nvim/misc1.h +++ b/src/nvim/misc1.h @@ -25,8 +25,6 @@ int del_bytes(long count, int fixpos_arg, int use_delcombine); void truncate_line(int fixpos); void del_lines(long nlines, int undo); int gchar_pos(pos_T *pos); -int gchar_cursor(void); -void pchar_cursor(int c); char_u *skip_to_option_part(char_u *p); void changed(void); void changed_int(void); diff --git a/src/nvim/misc2.c b/src/nvim/misc2.c index d49898815f..1db6c4193c 100644 --- a/src/nvim/misc2.c +++ b/src/nvim/misc2.c @@ -16,6 +16,7 @@ #include "nvim/file_search.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/eval.h" @@ -51,9 +52,6 @@ #include "nvim/os/os.h" #include "nvim/os/shell.h" -static int coladvance2(pos_T *pos, int addspaces, int finetune, - colnr_T wcol); - /* * Return TRUE if in the current mode we need to use virtual. */ @@ -70,256 +68,6 @@ int virtual_active(void) } /* - * 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; -} - -/* - * 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); -} - -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; -} - -/* - * Increment the cursor position. See inc() for return values. - */ -int inc_cursor(void) -{ - return inc(&curwin->w_cursor); -} - -/* * Increment the line pointer "lp" crossing line boundaries as necessary. * Return 1 when going to the next line. * Return 2 when moving forward onto a NUL at the end of the line). @@ -362,17 +110,6 @@ int incl(pos_T *lp) return r; } -/* - * 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); -} - int dec(pos_T *lp) { char_u *p; @@ -410,188 +147,6 @@ int decl(pos_T *lp) } /* - * 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; -} - -/* * Return TRUE when 'shell' has "csh" in the tail. */ int csh_like_shell(void) diff --git a/src/nvim/misc2.h b/src/nvim/misc2.h index 71a55bb94a..b94e35e258 100644 --- a/src/nvim/misc2.h +++ b/src/nvim/misc2.h @@ -6,24 +6,10 @@ /* misc2.c */ int virtual_active(void); -int getviscol(void); -int getviscol2(colnr_T col, colnr_T coladd); -int coladvance_force(colnr_T wcol); -int coladvance(colnr_T wcol); -int getvpos(pos_T *pos, colnr_T wcol); -int inc_cursor(void); int inc(pos_T *lp); int incl(pos_T *lp); -int dec_cursor(void); int dec(pos_T *lp); int decl(pos_T *lp); -linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum); -void check_cursor_lnum(void); -void check_cursor_col(void); -void check_cursor_col_win(win_T *win); -void check_cursor(void); -void adjust_cursor_col(void); -int leftcol_changed(void); int csh_like_shell(void); int copy_option_part(char_u **option, char_u *buf, int maxlen, char *sep_chars); diff --git a/src/nvim/move.c b/src/nvim/move.c index 758275578f..5ecdc93f51 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -19,6 +19,7 @@ #include "nvim/vim.h" #include "nvim/move.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/fold.h" diff --git a/src/nvim/normal.c b/src/nvim/normal.c index b505c349ae..63156d72fb 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -18,6 +18,7 @@ #include "nvim/normal.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/digraph.h" #include "nvim/edit.h" diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 05ca402d39..0483d931b5 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -17,6 +17,7 @@ #include "nvim/ops.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" diff --git a/src/nvim/option.c b/src/nvim/option.c index 33d146161b..5934f57dc8 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -39,6 +39,7 @@ #include "nvim/option.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/digraph.h" #include "nvim/eval.h" diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 269a33edcc..219a20d8b5 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -16,6 +16,7 @@ #include "nvim/quickfix.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" diff --git a/src/nvim/screen.c b/src/nvim/screen.c index f4616d3b8e..02b77ab1d9 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -93,6 +93,7 @@ #include "nvim/screen.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" diff --git a/src/nvim/search.c b/src/nvim/search.c index 9d37aa3339..750c789461 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -14,6 +14,7 @@ #include "nvim/vim.h" #include "nvim/search.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 1fe957d17c..44a42f0aaf 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -291,6 +291,7 @@ #include "nvim/spell.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" diff --git a/src/nvim/tag.c b/src/nvim/tag.c index a87210cbb8..3b1610682b 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -16,6 +16,7 @@ #include "nvim/tag.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/ex_cmds.h" diff --git a/src/nvim/ui.c b/src/nvim/ui.c index 6d27822bc0..ef61117126 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -19,6 +19,7 @@ #include "nvim/vim.h" #include "nvim/ui.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/ex_cmds2.h" #include "nvim/fold.h" diff --git a/src/nvim/undo.c b/src/nvim/undo.c index 805dfa547c..42804210d0 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -84,6 +84,7 @@ #include "nvim/vim.h" #include "nvim/undo.h" +#include "nvim/cursor.h" #include "nvim/edit.h" #include "nvim/eval.h" #include "nvim/fileio.h" diff --git a/src/nvim/window.c b/src/nvim/window.c index 534805b766..12840e502e 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -11,6 +11,7 @@ #include "nvim/window.h" #include "nvim/buffer.h" #include "nvim/charset.h" +#include "nvim/cursor.h" #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/eval.h" |