-
Notifications
You must be signed in to change notification settings - Fork 4
/
restore_buffer.vim
48 lines (40 loc) · 1.05 KB
/
restore_buffer.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let s:max_history_count = 10
let s:last_bufnr = 0
let s:histories = []
function! s:on_enter() abort
if s:last_bufnr == 0
return
endif
if !empty(&buftype)
return
endif
let l:info = get(getbufinfo(s:last_bufnr), 0, {})
if empty(l:info)
return
endif
if l:info.hidden || !l:info.loaded
let s:histories = ([s:last_bufnr] + s:histories)[0 : s:max_history_count - 1]
endif
endfunction
function! s:on_leave() abort
if !empty(bufname()) && empty(&buftype)
let s:last_bufnr = bufnr('')
endif
endfunction
function! s:restore() abort
let l:nr = get(s:histories, 0, 0)
let s:histories = s:histories[1:]
if l:nr > 0 && bufexists(l:nr)
exec 'buffer' l:nr
endif
endfunction
augroup plugin_restore_buffer
autocmd!
autocmd BufEnter * call <SID>on_enter()
autocmd BufLeave * call <SID>on_leave()
augroup END
nnoremap <silent> <Plug>(restore-buffer) <Cmd>call <SID>restore()<CR>
if !get(g:, 'restore_buffer_disable_default_mappings', v:false)
nmap <C-w>r <Plug>(restore-buffer)
nmap <C-w><C-r> <Plug>(restore-buffer)
endif