aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/api/buffer.c
diff options
context:
space:
mode:
authorhashinclude <pulkitg10@gmail.com>2019-05-07 09:17:05 +0200
committerJustin M. Keyes <justinkz@gmail.com>2019-05-07 09:17:37 +0200
commit400ee59247eae62990baf30c7802bc8effb3704a (patch)
tree6ca5b263ddcac4e48d4e803cc226e83b4ceee798 /src/nvim/api/buffer.c
parentb3adfa03b70fd5ecbe485488c10ee952ec811d93 (diff)
downloadrneovim-400ee59247eae62990baf30c7802bc8effb3704a.tar.gz
rneovim-400ee59247eae62990baf30c7802bc8effb3704a.tar.bz2
rneovim-400ee59247eae62990baf30c7802bc8effb3704a.zip
API: fix cursor position when lines are added #9961
Restore code removed in #9674.
Diffstat (limited to 'src/nvim/api/buffer.c')
-rw-r--r--src/nvim/api/buffer.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index da4db60ad6..4a0b8d13d0 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -474,6 +474,7 @@ void nvim_buf_set_lines(uint64_t channel_id,
false);
changed_lines((linenr_T)start, 0, (linenr_T)end, (long)extra, true);
+ fix_cursor((linenr_T)start, (linenr_T)end, (linenr_T)extra);
end:
for (size_t i = 0; i < new_len; i++) {
@@ -1106,6 +1107,26 @@ free_exit:
return 0;
}
+// Check if deleting lines made the cursor position invalid.
+// Changed lines from `lo` to `hi`; added `extra` lines (negative if deleted).
+static void fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)
+{
+ if (curwin->w_cursor.lnum >= lo) {
+ // Adjust cursor position if it's in/after the changed lines.
+ if (curwin->w_cursor.lnum >= hi) {
+ curwin->w_cursor.lnum += extra;
+ check_cursor_col();
+ } else if (extra < 0) {
+ curwin->w_cursor.lnum = lo;
+ check_cursor();
+ } else {
+ check_cursor_col();
+ }
+ changed_cline_bef_curs();
+ }
+ invalidate_botline();
+}
+
// Normalizes 0-based indexes to buffer line numbers
static int64_t normalize_index(buf_T *buf, int64_t index, bool *oob)
{