Skip to content

Commit

Permalink
Fix the bug that a redo reaching the latest change is taken as a new
Browse files Browse the repository at this point in the history
change by modifying the criterion on undo and redo, causing another
problem that ongoing changes in Insert mode and Replace mode are not
taken as new changes. To avoid this, the 'b:AutoCenter_MaxChangeNr'
variable is introduced to record the current max change number for
current buffer, which is updated only when not in the modes said above.
  • Loading branch information
Ace-Who committed Apr 12, 2017
1 parent 15fcfc4 commit edd1cce
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions autoload/AutoCenter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function! AutoCenter#On() "{{{
" text changes. So we don't center text in these cases.
autocmd TextChanged,TextChangedI *
\ if !s:isUndoRedo() | call s:center() | endif
autocmd BufEnter * call s:upMaxChangeNr()
augroup END
call s:upMaxChangeNr()
call s:saveMapping() " Save mappings for later restoring.
call s:addMapping()
call s:markState(1)
Expand All @@ -41,21 +43,22 @@ function! AutoCenter#Off() "{{{
call s:markState(0)
endfunction "}}}

function! s:isUndoRedo() "{{{
return changenr() <= b:AutoCenter_MaxChangeNr
endfunction "}}}

function! s:center() "{{{
" Remember the cursor position relative to current indent for restoring after
" centering the line.
let l:curpos = col('.') - indent('.')
center
" Put cursor back on the same char it is on before centering.
call cursor('.', l:curpos + indent('.'))
endfunction "}}}

function! s:isUndoRedo() "{{{
let l:undolist = split(execute('undolist'), "\n")
let l:maxChangeNr = split(l:undolist[-1], ' \+')[0]
" known bug: a redo reaching the lastest change makes changenr() equal to
" l:maxChangeNr.
return changenr() < l:maxChangeNr
" A change in Insert mode or Replace mode is in progress.
" Only update the max change number when a change is done.
if mode() !~ '^[iR]'
call s:upMaxChangeNr()
endif
endfunction "}}}

function! s:opCenter(type) "{{{
Expand Down Expand Up @@ -101,6 +104,13 @@ function! s:markState(state) "{{{
lockvar g:AutoCenter_On
endfunction "}}}

function! s:upMaxChangeNr() "{{{
unlockvar b:AutoCenter_MaxChangeNr
let l:undolist = split(execute('undolist'), "\n")
let b:AutoCenter_MaxChangeNr = split(l:undolist[-1], ' \+')[0]
lockvar b:AutoCenter_MaxChangeNr
endfunction "}}}

if !exists('g:AutoCenter_On') | call s:markState(0) | endif

" Restore 'cpoptions' setting {{{
Expand Down

0 comments on commit edd1cce

Please sign in to comment.