vimwiki

Personal wiki for vim
git clone https://github.com/vimwiki/vimwiki.git
Log | Files | Refs | README | LICENSE

commit d265df42f59973512d457a1d3c2d9906f5206661
parent 8231433bad541d4879a453c6d961c1864a22a8d3
Author: Ivan Tishchenko <ivan.tishchenko@dsr-company.com>
Date:   Fri, 27 Nov 2015 03:05:39 +0300

Prevent cursor moving up/down when TOC changes.

getpos()/setpos() combination does not account for changes of lines
count.  So if you do getpos, then remove lines _above_ it, then do
setpos -- it's going to be off.

The fix calculates the "diff", and adjusts saved position for that diff
(if any).

Diffstat:
Mautoload/vimwiki/base.vim | 19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/autoload/vimwiki/base.vim b/autoload/vimwiki/base.vim @@ -1144,6 +1144,10 @@ function! vimwiki#base#update_listing_in_buffer(strings, start_header, endif let old_cursor_pos = getpos('.') + let cursor_line = old_cursor_pos[1] + let is_cursor_after_listing = 0 + + let lines_diff = 0 if already_there " delete the old listing @@ -1152,9 +1156,19 @@ function! vimwiki#base#update_listing_in_buffer(strings, start_header, while end_lnum <= line('$') && getline(end_lnum) =~# a:content_regex let end_lnum += 1 endwhile + let is_cursor_after_listing = ( cursor_line >= end_lnum ) + " We'll be removing a range. But, apparently, if folds are enabled, Vim + " won't let you remove a range that overlaps with closed fold -- the entire + " fold gets deleted. So we temporarily disable folds, and then reenable + " them right back. + let foldenable_save = &l:foldenable + setlo nofoldenable silent exe start_lnum.','.string(end_lnum - 1).'delete _' + let &l:foldenable = foldenable_save + let lines_diff = 0 - (end_lnum - start_lnum) else let start_lnum = a:default_lnum + let is_cursor_after_listing = ( cursor_line > a:default_lnum ) let whitespaces_in_first_line = '' endif @@ -1164,6 +1178,7 @@ function! vimwiki#base#update_listing_in_buffer(strings, start_header, \ '__Header__', '\='."'".a:start_header."'", '') call append(start_lnum - 1, new_header) let start_lnum += 1 + let lines_diff += 1 + len(a:strings) for string in a:strings call append(start_lnum - 1, string) let start_lnum += 1 @@ -1171,8 +1186,12 @@ function! vimwiki#base#update_listing_in_buffer(strings, start_header, " append an empty line if there is not one if start_lnum <= line('$') && getline(start_lnum) !~# '\m^\s*$' call append(start_lnum - 1, '') + let lines_diff += 1 endif + if is_cursor_after_listing + let old_cursor_pos[1] += lines_diff + endif call setpos('.', old_cursor_pos) endfunction "}}}