diff options
| author | zeertzjq <zeertzjq@outlook.com> | 2022-07-28 12:22:00 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-28 12:22:00 +0800 |
| commit | fe254fb7e7a8c143be581aa2f696770423595343 (patch) | |
| tree | e5d2abb911d65b1eb2761e4cb5c2109867ebbcbf /src/nvim/window.c | |
| parent | bdbf843031b91db7ab0d4ad925c8652947d38b70 (diff) | |
| parent | 0134a2cb3eaf03cbf845a5f9c13a153e4ef9b6b6 (diff) | |
| download | rneovim-fe254fb7e7a8c143be581aa2f696770423595343.tar.gz rneovim-fe254fb7e7a8c143be581aa2f696770423595343.tar.bz2 rneovim-fe254fb7e7a8c143be581aa2f696770423595343.zip | |
Merge pull request #19556 from zeertzjq/vim-9.0.0061
vim-patch:9.0.{0061,partial:0077,0094}
Diffstat (limited to 'src/nvim/window.c')
| -rw-r--r-- | src/nvim/window.c | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/src/nvim/window.c b/src/nvim/window.c index 98e1dbafd2..b2812189d9 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -6855,43 +6855,67 @@ bool only_one_window(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT return count <= 1; } -/// Correct the cursor line number in other windows. Used after changing the -/// current buffer, and before applying autocommands. -/// -/// @param do_curwin when true, also check current window. -void check_lnums(bool do_curwin) +/// Implementation of check_lnums() and check_lnums_nested(). +static void check_lnums_both(bool do_curwin, bool nested) { FOR_ALL_TAB_WINDOWS(tp, wp) { if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf) { - // save the original cursor position and topline - wp->w_save_cursor.w_cursor_save = wp->w_cursor; - wp->w_save_cursor.w_topline_save = wp->w_topline; + if (!nested) { + // save the original cursor position and topline + wp->w_save_cursor.w_cursor_save = wp->w_cursor; + wp->w_save_cursor.w_topline_save = wp->w_topline; + } - if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count) { + bool need_adjust = wp->w_cursor.lnum > curbuf->b_ml.ml_line_count; + if (need_adjust) { wp->w_cursor.lnum = curbuf->b_ml.ml_line_count; } - if (wp->w_topline > curbuf->b_ml.ml_line_count) { - wp->w_topline = curbuf->b_ml.ml_line_count; + if (need_adjust || !nested) { + // save the (corrected) cursor position + wp->w_save_cursor.w_cursor_corr = wp->w_cursor; } - // save the corrected cursor position and topline - wp->w_save_cursor.w_cursor_corr = wp->w_cursor; - wp->w_save_cursor.w_topline_corr = wp->w_topline; + need_adjust = wp->w_topline > curbuf->b_ml.ml_line_count; + if (need_adjust) { + wp->w_topline = curbuf->b_ml.ml_line_count; + } + if (need_adjust || !nested) { + // save the (corrected) topline + wp->w_save_cursor.w_topline_corr = wp->w_topline; + } } } } +/// Correct the cursor line number in other windows. Used after changing the +/// current buffer, and before applying autocommands. +/// +/// @param do_curwin when true, also check current window. +void check_lnums(bool do_curwin) +{ + check_lnums_both(do_curwin, false); +} + +/// Like check_lnums() but for when check_lnums() was already called. +void check_lnums_nested(bool do_curwin) +{ + check_lnums_both(do_curwin, true); +} + /// Reset cursor and topline to its stored values from check_lnums(). /// check_lnums() must have been called first! void reset_lnums(void) { FOR_ALL_TAB_WINDOWS(tp, wp) { if (wp->w_buffer == curbuf) { - // Restore the value if the autocommand didn't change it. - if (equalpos(wp->w_save_cursor.w_cursor_corr, wp->w_cursor)) { + // Restore the value if the autocommand didn't change it and it was + // set. + if (equalpos(wp->w_save_cursor.w_cursor_corr, wp->w_cursor) + && wp->w_save_cursor.w_cursor_save.lnum != 0) { wp->w_cursor = wp->w_save_cursor.w_cursor_save; } - if (wp->w_save_cursor.w_topline_corr == wp->w_topline) { + if (wp->w_save_cursor.w_topline_corr == wp->w_topline + && wp->w_save_cursor.w_topline_save != 0) { wp->w_topline = wp->w_save_cursor.w_topline_save; } } |