Skip to content

Instantly share code, notes, and snippets.

@igrep
Last active January 17, 2016 01:29
Show Gist options
  • Select an option

  • Save igrep/ad5acc8ca4b0e51bd122 to your computer and use it in GitHub Desktop.

Select an option

Save igrep/ad5acc8ca4b0e51bd122 to your computer and use it in GitHub Desktop.
Save file names your vim opened, on a bat file to restore. Alternative to http://vim.wikia.com/wiki/Automatic_session_restore_in_git_directories
" Alternative to http://vim.wikia.com/wiki/Automatic_session_restore_in_git_directories for Windows.
" I created this because automatic session restore didn't work On my windows environment.
"
" Save a bat file to reopen the last opened files on exit.
" I consulted unite.vim\plugin\unite\buffer.vim for which events to autocmd and how to handle them.
if !( has('win32') || has('win64') )
finish
end
" Disable by default to prevent from unintentionally overwriting s:OUT_FILE_NAME
" Usually this feature is enabled when launching vim by s:OUT_FILE_NAME
if !(exists("g:auto_save_opening_files_bat"))
let g:auto_save_opening_files_bat = 0
endif
augroup auto-save-opening-files-bat
autocmd!
autocmd BufEnter,BufWinEnter,BufFilePost * call s:append_opening_buffer()
autocmd BufDelete,BufWipeout * call s:remove_unloading_buffer()
autocmd VimLeave * call s:save_opening_files_on_bat()
augroup END
let s:loaded_buffers_dictionary = {}
let s:OUT_FILE_NAME = '_vim-open-recent-used-files.bat'
function! s:save_opening_files_on_bat()
if !(g:auto_save_opening_files_bat)
return
endif
let buffers = s:get_loaded_buffers()
if buffers != [] && filewritable( s:OUT_FILE_NAME )
let output = 'start ' . v:progname . ' -c :EnableAutoSaveOpeningFilesBat ' . join( buffers, ' ' )
call writefile( [output], s:OUT_FILE_NAME )
endif
endfunction
function! s:append_opening_buffer()
if !(g:auto_save_opening_files_bat)
return
endif
let path = expand('<amatch>:.')
let buffer_number = bufnr('%')
if bufname(buffer_number) == '' || buffer_number != expand('<abuf>')
return
endif
let s:loaded_buffers_dictionary[buffer_number] = path
endfunction
function! s:remove_unloading_buffer()
if !(g:auto_save_opening_files_bat)
return
endif
let buffer_number = bufnr('%')
if bufname(buffer_number) == '' || buffer_number != expand('<abuf>')
return
endif
if has_key( s:loaded_buffers_dictionary, buffer_number )
call remove( s:loaded_buffers_dictionary, buffer_number )
endif
endfunction
function! s:get_loaded_buffers()
call s:purge_buffers_dictionary( s:loaded_buffers_dictionary )
return values( s:loaded_buffers_dictionary )
endfunction
function! s:purge_buffers_dictionary( buffers_dictionary )
call filter( a:buffers_dictionary,
\ 'buflisted( str2nr( v:key ) ) && filereadable( a:buffers_dictionary[v:key] )' )
endfunction
function! s:InstallOpenRecentUsedFilesBat()
if ! filewritable( s:OUT_FILE_NAME )
call writefile( [], s:OUT_FILE_NAME )
call s:EnableAutoSaveOpeningFilesBat()
endif
endfunction
command! InstallOpenRecentUsedFilesBat :call s:InstallOpenRecentUsedFilesBat()
function! s:EnableAutoSaveOpeningFilesBat() abort
let g:auto_save_opening_files_bat = 1
endfunction
command! EnableAutoSaveOpeningFilesBat :call s:EnableAutoSaveOpeningFilesBat()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment