Skip to content

Commit

Permalink
Move "session is empty?" check to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Mar 15, 2015
1 parent a5d564f commit 35b6742
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -270,8 +270,8 @@ Recently this plug-in switched from reimplementing [:mksession][mksession] to ac

<!-- Start of generated documentation -->

The documentation of the 38 functions below was extracted from
2 Vim scripts on March 15, 2015 at 10:15.
The documentation of the 39 functions below was extracted from
2 Vim scripts on March 15, 2015 at 10:22.

### Public API for the vim-session plug-in

Expand Down Expand Up @@ -356,6 +356,18 @@ Normally called by the [VimEnter] [] automatic command event.

[VimEnter]: http://vimdoc.sourceforge.net/htmldoc/autocmd.html#VimEnter

#### The `xolox#session#is_empty()` function

Check that the user has started Vim without editing any files. Used by
`xolox#session#auto_load()` to determine whether automatic session loading
should be performed. Currently checks the following conditions:

1. That the current buffer is either empty (contains no lines and is not
modified) or showing [vim-startify] [].
2. That the buffer list either empty or persistent.

[vim-startify]: https://github.com/mhinz/vim-startify/

#### The `xolox#session#auto_save()` function

Automatically save the current editing session when Vim is closed.
Expand Down
25 changes: 19 additions & 6 deletions autoload/xolox/session.vim
Expand Up @@ -4,7 +4,7 @@
" Last Change: March 15, 2015
" URL: http://peterodding.com/code/vim/session/

let g:xolox#session#version = '2.9.1'
let g:xolox#session#version = '2.9.2'

" Public API for session persistence. {{{1

Expand Down Expand Up @@ -386,11 +386,7 @@ function! xolox#session#auto_load() " {{{2
return
endif
" Check that the user has started Vim without editing any files.
let current_buffer_is_empty = (&modified == 0 && getline(1, '$') == [''])
let current_buffer_is_startify = (&filetype == 'startify')
let buffer_list_is_empty = (bufname('%') == '' && empty(filter(range(1, bufnr('$')), 'buflisted(v:val) && v:val != ' . bufnr(''))))
let buffer_list_is_persistent = (index(xolox#misc#option#split(&viminfo), '%') >= 0)
if (current_buffer_is_empty || current_buffer_is_startify) && (buffer_list_is_empty || buffer_list_is_persistent)
if xolox#session#is_empty()
" Check whether a session matching the user-specified server name exists.
if v:servername !~ '^\cgvim\d*$'
for session in xolox#session#get_names(0)
Expand Down Expand Up @@ -425,6 +421,23 @@ function! xolox#session#auto_load() " {{{2
endif
endfunction

function! xolox#session#is_empty() " {{{2
" Check that the user has started Vim without editing any files. Used by
" `xolox#session#auto_load()` to determine whether automatic session loading
" should be performed. Currently checks the following conditions:
"
" 1. That the current buffer is either empty (contains no lines and is not
" modified) or showing [vim-startify] [].
" 2. That the buffer list either empty or persistent.
"
" [vim-startify]: https://github.com/mhinz/vim-startify/
let current_buffer_is_empty = (&modified == 0 && getline(1, '$') == [''])
let current_buffer_is_startify = (&filetype == 'startify')
let buffer_list_is_empty = (bufname('%') == '' && empty(filter(range(1, bufnr('$')), 'buflisted(v:val) && v:val != ' . bufnr(''))))
let buffer_list_is_persistent = (index(xolox#misc#option#split(&viminfo), '%') >= 0)
return (current_buffer_is_empty || current_buffer_is_startify) && (buffer_list_is_empty || buffer_list_is_persistent)
endfunction

function! xolox#session#auto_save() " {{{2
" Automatically save the current editing session when Vim is closed.
" Normally called by the [VimLeavePre] [] automatic command event.
Expand Down
59 changes: 36 additions & 23 deletions doc/session.txt
Expand Up @@ -50,23 +50,24 @@ Contents ~
7. The |xolox#session#save_state()| function
8. The |xolox#session#save_special_windows()| function
9. The |xolox#session#auto_load()| function
10. The |xolox#session#auto_save()| function
11. The |xolox#session#auto_save_periodic()| function
12. The |xolox#session#auto_unlock()| function
13. The |xolox#session#prompt_for_name()| function
14. The |xolox#session#name_to_path()| function
15. The |xolox#session#path_to_name()| function
16. The |xolox#session#get_names()| function
17. The |xolox#session#complete_names()| function
18. The |xolox#session#complete_names_with_suggestions()| function
19. The |xolox#session#is_tab_scoped()| function
20. The |xolox#session#find_current_session()| function
21. The |xolox#session#get_label()| function
22. The |xolox#session#options_include()| function
23. The |xolox#session#include_tabs()| function
24. The |xolox#session#change_tab_options()| function
25. The |xolox#session#restore_tab_options()| function
26. The |xolox#session#locking_enabled()| function
10. The |xolox#session#is_empty()| function
11. The |xolox#session#auto_save()| function
12. The |xolox#session#auto_save_periodic()| function
13. The |xolox#session#auto_unlock()| function
14. The |xolox#session#prompt_for_name()| function
15. The |xolox#session#name_to_path()| function
16. The |xolox#session#path_to_name()| function
17. The |xolox#session#get_names()| function
18. The |xolox#session#complete_names()| function
19. The |xolox#session#complete_names_with_suggestions()| function
20. The |xolox#session#is_tab_scoped()| function
21. The |xolox#session#find_current_session()| function
22. The |xolox#session#get_label()| function
23. The |xolox#session#options_include()| function
24. The |xolox#session#include_tabs()| function
25. The |xolox#session#change_tab_options()| function
26. The |xolox#session#restore_tab_options()| function
27. The |xolox#session#locking_enabled()| function
2. Example function for session name suggestions |example-function-for-session-name-suggestions|
1. The |xolox#session#suggestions#vcs_feature_branch()| function
8. Contact |session-contact|
Expand Down Expand Up @@ -542,8 +543,8 @@ might take a while...)
*session-function-reference*
Function reference ~

The documentation of the 38 functions below was extracted from 2 Vim scripts on
March 15, 2015 at 10:15.
The documentation of the 39 functions below was extracted from 2 Vim scripts on
March 15, 2015 at 10:22.

-------------------------------------------------------------------------------
*public-api-for-vim-session-plug-in*
Expand Down Expand Up @@ -627,6 +628,17 @@ The *xolox#session#auto_load()* function
Automatically load the default or last used session when Vim starts. Normally
called by the |VimEnter| automatic command event.

-------------------------------------------------------------------------------
The *xolox#session#is_empty()* function

Check that the user has started Vim without editing any files. Used by
|xolox#session#auto_load()| to determine whether automatic session loading
should be performed. Currently checks the following conditions:

1. That the current buffer is either empty (contains no lines and is not
modified) or showing vim-startify [19].
2. That the buffer list either empty or persistent.

-------------------------------------------------------------------------------
The *xolox#session#auto_save()* function

Expand Down Expand Up @@ -785,13 +797,13 @@ Contact ~
If you have questions, bug reports, suggestions, etc. the author can be
contacted at peter@peterodding.com. The latest version is available at
http://peterodding.com/code/vim/session/ and http://github.com/xolox/vim-
session. If you like the script please vote for it on Vim Online [19].
session. If you like the script please vote for it on Vim Online [20].

===============================================================================
*session-license*
License ~

This software is licensed under the MIT license [20]. © 2015 Peter Odding
This software is licensed under the MIT license [21]. © 2015 Peter Odding
<peter@peterodding.com> and Ingo Karkat.

Thanks go out to everyone who has helped to improve the vim-session plug-in
Expand Down Expand Up @@ -919,7 +931,8 @@ References ~
[16] http://peterodding.com/code/vim/shell/
[17] http://peterodding.com/code/vim/session
[18] http://peterodding.com/code/vim/shell
[19] http://www.vim.org/scripts/script.php?script_id=3150
[20] http://en.wikipedia.org/wiki/MIT_License
[19] https://github.com/mhinz/vim-startify/
[20] http://www.vim.org/scripts/script.php?script_id=3150
[21] http://en.wikipedia.org/wiki/MIT_License

vim: ft=help

0 comments on commit 35b6742

Please sign in to comment.