Pārlūkot izejas kodu

Make persistence work with normal sessions

Marco Hinz 11 gadi atpakaļ
vecāks
revīzija
19da8f9ee8
3 mainītis faili ar 40 papildinājumiem un 37 dzēšanām
  1. 29 37
      autoload/startify.vim
  2. 4 0
      doc/startify.txt
  3. 7 0
      plugin/startify.vim

+ 29 - 37
autoload/startify.vim

@@ -37,14 +37,6 @@ endif
 let s:secoff = type(s:lists[0]) == 3 ? (len(s:lists[0]) + 1) : 0
 let s:section_header_lines = []
 
-" Init: autocmds {{{1
-if get(g:, 'startify_session_persistence')
-  autocmd startify VimLeave *
-        \ if exists('v:this_session') && filewritable(v:this_session) |
-        \   call s:session_write(fnameescape(v:this_session)) |
-        \ endif
-endif
-
 " Function: #get_separator {{{1
 function! startify#get_separator() abort
   return !exists('+shellslash') || &shellslash ? '/' : '\'
@@ -197,20 +189,47 @@ function! startify#session_save(...) abort
 
   let spath = s:session_dir . s:sep . sname
   if !filereadable(spath)
-    call s:session_write(fnameescape(spath))
+    call startify#session_write(fnameescape(spath))
     echo 'Session saved under: '. spath
     return
   endif
 
   echo 'Session already exists. Overwrite?  [y/n]' | redraw
   if nr2char(getchar()) == 'y'
-    call s:session_write(fnameescape(spath))
+    call startify#session_write(fnameescape(spath))
     echo 'Session saved under: '. spath
   else
     echo 'Did NOT save the session!'
   endif
 endfunction
 
+" Function: #session_write {{{1
+function! startify#session_write(spath)
+  let ssop = &sessionoptions
+  try
+    set sessionoptions-=options
+    execute 'mksession!' a:spath
+  catch
+    execute 'echoerr' string(v:exception)
+  finally
+    let &sessionoptions = ssop
+  endtry
+
+  if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
+    execute 'split' a:spath
+
+    " put existing variables from savevars into session file
+    call append(line('$')-3, map(filter(get(g:, 'startify_session_savevars', []), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
+
+    " put commands from savecmds into session file
+    call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
+
+    setlocal bufhidden=delete
+    silent update
+    silent hide
+  endif
+endfunction
+
 " Function: #session_delete {{{1
 function! startify#session_delete(...) abort
   if !isdirectory(s:session_dir)
@@ -599,33 +618,6 @@ function! s:restore_position() abort
   endif
 endfunction
 
-" Function: s:session_write {{{1
-function! s:session_write(spath)
-  let ssop = &sessionoptions
-  try
-    set sessionoptions-=options
-    execute 'mksession!' a:spath
-  catch
-    execute 'echoerr' string(v:exception)
-  finally
-    let &sessionoptions = ssop
-  endtry
-
-  if exists('g:startify_session_savevars') || exists('g:startify_session_savecmds')
-    execute 'split' a:spath
-
-    " put existing variables from savevars into session file
-    call append(line('$')-3, map(filter(get(g:, 'startify_session_savevars', []), 'exists(v:val)'), '"let ". v:val ." = ". strtrans(string(eval(v:val)))'))
-
-    " put commands from savecmds into session file
-    call append(line('$')-3, get(g:, 'startify_session_savecmds', []))
-
-    setlocal bufhidden=delete
-    silent update
-    silent hide
-  endif
-endfunction
-
 " Function: s:print_section_header {{{1
 function! s:print_section_header() abort
   $

+ 4 - 0
doc/startify.txt

@@ -215,6 +215,10 @@ Great way to create a portable project folder.
 <
 Automatically update sessions before exiting Vim.
 
+This also works for sessions started with:
+>
+    vim -S mysession.vim
+<
 ------------------------------------------------------------------------------
                                              *g:startify_session_delete_buffers*
 >

+ 7 - 0
plugin/startify.vim

@@ -18,6 +18,13 @@ augroup startify
           \ endif |
           \ autocmd! startify VimEnter
   endif
+
+  if get(g:, 'startify_session_persistence')
+    autocmd startify VimLeave *
+          \ if exists('v:this_session') && filewritable(v:this_session) |
+          \   call startify#session_write(fnameescape(v:this_session)) |
+          \ endif
+  endif
 augroup END
 
 command! -nargs=? -bar -complete=customlist,startify#session_list SSave   call startify#session_save(<f-args>)