Skip to content

Commit

Permalink
Try to improve user feedback when sessions are locked
Browse files Browse the repository at this point in the history
See also: #87
  • Loading branch information
xolox committed Jul 6, 2014
1 parent f13514a commit d833471
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -256,7 +256,7 @@ Recently this plug-in switched from reimplementing [:mksession][mksession] to ac
<!-- Start of generated documentation -->

The documentation of the 37 functions below was extracted from
2 Vim scripts on July 6, 2014 at 22:23.
2 Vim scripts on July 7, 2014 at 01:17.

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

Expand Down
31 changes: 15 additions & 16 deletions autoload/xolox/session.vim
@@ -1,10 +1,10 @@
" Public API for the vim-session plug-in.
"
" Author: Peter Odding
" Last Change: July 6, 2014
" Last Change: July 7, 2014
" URL: http://peterodding.com/code/vim/session/

let g:xolox#session#version = '2.6'
let g:xolox#session#version = '2.6.1'

" Public API for session persistence. {{{1

Expand Down Expand Up @@ -413,7 +413,7 @@ function! xolox#session#auto_load() " {{{2
" Default to the last used session or the default session?
let [has_last_session, session] = s:get_last_or_default_session()
let path = xolox#session#name_to_path(session)
if (g:session_default_to_last == 0 || has_last_session) && filereadable(path) && !s:session_is_locked(path)
if (g:session_default_to_last == 0 || has_last_session) && filereadable(path) && !s:session_is_locked(session, 'OpenSession')
" Compose the message for the prompt.
let is_default_session = (session == g:session_default_name)
let msg = printf("Do you want to restore your %s editing session%s?",
Expand Down Expand Up @@ -554,7 +554,7 @@ function! xolox#session#open_cmd(name, bang, command) abort " {{{2
let msg = "session.vim %s: The %s session at %s doesn't exist!"
call xolox#misc#msg#warn(msg, g:xolox#session#version, string(name), fnamemodify(path, ':~'))
return 0
elseif a:bang == '!' || !s:session_is_locked(path, a:command)
elseif a:bang == '!' || !s:session_is_locked(name, a:command)
let oldcwd = s:nerdtree_persist()
call xolox#session#close_cmd(a:bang, 1, name != xolox#session#find_current_session(), a:command)
call s:lock_session(path)
Expand Down Expand Up @@ -612,7 +612,7 @@ function! xolox#session#save_cmd(name, bang, command) abort " {{{2
endif
let path = xolox#session#name_to_path(name)
let friendly_path = fnamemodify(path, ':~')
if a:bang == '!' || !s:session_is_locked(path, a:command)
if a:bang == '!' || !s:session_is_locked(name, a:command)
let lines = []
call xolox#session#save_session(lines, friendly_path)
if xolox#misc#os#is_win() && !xolox#session#options_include('unix')
Expand Down Expand Up @@ -647,7 +647,7 @@ function! xolox#session#delete_cmd(name, bang) " {{{2
if !filereadable(path)
let msg = "session.vim %s: The %s session at %s doesn't exist!"
call xolox#misc#msg#warn(msg, g:xolox#session#version, string(name), fnamemodify(path, ':~'))
elseif a:bang == '!' || !s:session_is_locked(path, 'DeleteSession')
elseif a:bang == '!' || !s:session_is_locked(name, 'DeleteSession')
if delete(path) != 0
let msg = "session.vim %s: Failed to delete %s session at %s!"
call xolox#misc#msg#warn(msg, g:xolox#session#version, string(name), fnamemodify(path, ':~'))
Expand Down Expand Up @@ -1030,7 +1030,7 @@ endfunction

function! s:lock_session(session_path)
let lock_file = a:session_path . '.lock'
if writefile([string(s:vim_instance_id())], lock_file) == 0
if xolox#misc#persist#save(lock_file, s:vim_instance_id())
if index(s:lock_files, lock_file) == -1
call add(s:lock_files, lock_file)
endif
Expand All @@ -1049,13 +1049,13 @@ function! s:unlock_session(session_path)
endif
endfunction

function! s:session_is_locked(session_path, ...)
let lock_file = a:session_path . '.lock'
function! s:session_is_locked(session_name, command)
let session_path = xolox#session#name_to_path(a:session_name)
let lock_file = session_path . '.lock'
if filereadable(lock_file)
let this_instance = s:vim_instance_id()
let other_instance = eval(get(readfile(lock_file), 0, '{}'))
let name = string(fnamemodify(a:session_path, ':t:r'))
let arguments = [g:xolox#session#version, name]
let other_instance = xolox#misc#persist#load(lock_file)
let arguments = [g:xolox#session#version, string(a:session_name)]
if this_instance == other_instance
" Session belongs to current Vim instance and tab page.
return 0
Expand All @@ -1073,11 +1073,10 @@ function! s:session_is_locked(session_path, ...)
else
call add(arguments, 'with PID ' . other_instance['pid'])
endif
let msg .= " If that doesn't seem right maybe you forcefully closed Vim or it crashed?"
endif
if exists('a:1')
let msg .= " Use :%s! to override."
call add(arguments, a:1)
endif
let msg .= " Use the command ':%s! %s' to override."
call extend(arguments, [a:command, a:session_name])
call call('xolox#misc#msg#warn', [msg] + arguments)
return 1
endif
Expand Down
2 changes: 1 addition & 1 deletion doc/session.txt
Expand Up @@ -513,7 +513,7 @@ might take a while...)
Function reference ~

The documentation of the 37 functions below was extracted from 2 Vim scripts on
July 6, 2014 at 22:23.
July 7, 2014 at 01:17.

-------------------------------------------------------------------------------
*public-api-for-vim-session-plug-in*
Expand Down

0 comments on commit d833471

Please sign in to comment.