Skip to content

Commit e4d71c7

Browse files
authored
Merge pull request #341 from easymotion/fix-undo
fix undo restore failing when empty undo history
2 parents d55e7bf + 3f844bc commit e4d71c7

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

autoload/EasyMotion.vim

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,6 @@ function! s:GetVisualStartPosition(c_pos, v_start, v_end, search_direction) "{{{
739739
endfunction "}}}
740740

741741
" -- Others ------------------------------
742-
function! s:is_cmdwin() "{{{
743-
return bufname('%') ==# '[Command Line]'
744-
endfunction "}}}
745-
function! s:should_use_wundo() "{{{
746-
" wundu cannot use in command-line window and
747-
" unless undolist is not empty
748-
return ! s:is_cmdwin() && undotree().seq_last != 0
749-
endfunction "}}}
750742
function! s:handleEmpty(input, visualmode) "{{{
751743
" if empty, reselect and return 1
752744
if empty(a:input)
@@ -1104,12 +1096,8 @@ function! s:PromptUser(groups) "{{{
11041096
" }}}
11051097

11061098
" -- Put labels on targets & Get User Input & Restore all {{{
1107-
" Save undo tree {{{
1108-
let s:undo_file = tempname()
1109-
if s:should_use_wundo()
1110-
execute "wundo" s:undo_file
1111-
endif
1112-
"}}}
1099+
" Save undo tree
1100+
let undo_lock = EasyMotion#undo#save()
11131101
try
11141102
" Set lines with markers {{{
11151103
call s:SetLines(lines_items, 'marker')
@@ -1156,21 +1144,8 @@ function! s:PromptUser(groups) "{{{
11561144
\ )
11571145
" }}}
11581146

1159-
" Restore undo tree {{{
1160-
if s:should_use_wundo() && filereadable(s:undo_file)
1161-
silent execute "rundo" s:undo_file
1162-
call delete(s:undo_file)
1163-
unlet s:undo_file
1164-
else
1165-
" Break undo history (undobreak)
1166-
let old_undolevels = &undolevels
1167-
set undolevels=-1
1168-
keepjumps call setline('.', getline('.'))
1169-
let &undolevels = old_undolevels
1170-
unlet old_undolevels
1171-
" FIXME: Error occur by GundoToggle for undo number 2 is empty
1172-
keepjumps call setline('.', getline('.'))
1173-
endif "}}}
1147+
" Restore undo tree
1148+
call undo_lock.restore()
11741149

11751150
redraw
11761151
endtry "}}}

autoload/EasyMotion/undo.vim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
let s:Buffer = vital#easymotion#import('Vim.Buffer')
2+
3+
function! EasyMotion#undo#save() abort
4+
return s:undo_lock.save()
5+
endfunction
6+
7+
let s:undo_lock = {}
8+
9+
function! s:undo_lock.save() abort
10+
let undo = deepcopy(self)
11+
call undo._save()
12+
return undo
13+
endfunction
14+
15+
function! s:undo_lock._save() abort
16+
if undotree().seq_last == 0
17+
" if there are no undo history, disable undo feature by setting
18+
" 'undolevels' to -1 and restore it.
19+
let self.save_undolevels = &l:undolevels
20+
let &l:undolevels = -1
21+
elseif !s:Buffer.is_cmdwin()
22+
" command line window doesn't support :wundo.
23+
let self.undofile = tempname()
24+
execute 'wundo!' self.undofile
25+
else
26+
let self.is_cmdwin = s:TRUE
27+
endif
28+
endfunction
29+
30+
function! s:undo_lock.restore() abort
31+
if has_key(self, 'save_undolevels')
32+
let &l:undolevels = self.save_undolevels
33+
endif
34+
if has_key(self, 'undofile') && filereadable(self.undofile)
35+
silent execute 'rundo' self.undofile
36+
call delete(self.undofile)
37+
endif
38+
if has_key(self, 'is_cmdwin')
39+
" XXX: it breaks undo history. AFAIK, there are no way to save and restore
40+
" undo history in commandline window.
41+
call self.undobreak()
42+
endif
43+
endfunction
44+
45+
function! s:undo_lock.undobreak() abort
46+
let old_undolevels = &l:undolevels
47+
setlocal undolevels=-1
48+
keepjumps call setline('.', getline('.'))
49+
let &l:undolevels = old_undolevels
50+
endfunction

0 commit comments

Comments
 (0)