Skip to content

Commit 46d8732

Browse files
committed
Add option to disable session locking (issue #112)
Please refer to issue #112 on GitHub: #112
1 parent f2dab5d commit 46d8732

File tree

3 files changed

+87
-30
lines changed

3 files changed

+87
-30
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ The vim-session plug-in uses lock files to prevent double loading of sessions. T
135135
2. If the directory `/var/lock` exists and is writable that is used as a sane default.
136136
3. As a sane fall back for platforms where `/var/lock` is not available the directory that stores the session scripts themselves is used.
137137

138+
### The `g:session_lock_enabled` option
139+
140+
Depending on your workflow locking of editing sessions can get annoying at times, so if you don't care about opening a session more than once and potentially "losing a version of your session" then you can use this option to completely disable session locking as follows:
141+
142+
" Disable all session locking - I know what I'm doing :-).
143+
let g:session_lock_enabled = 0
144+
138145
### The `g:session_default_name` option
139146

140147
The name of the default session without directory or filename extension (you'll never guess what the default is).
@@ -263,8 +270,8 @@ Recently this plug-in switched from reimplementing [:mksession][mksession] to ac
263270

264271
<!-- Start of generated documentation -->
265272

266-
The documentation of the 37 functions below was extracted from
267-
2 Vim scripts on February 13, 2015 at 13:25.
273+
The documentation of the 38 functions below was extracted from
274+
2 Vim scripts on February 18, 2015 at 22:56.
268275

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

@@ -471,6 +478,14 @@ scoped session. Saves a copy of the original value to be restored later.
471478

472479
Restore the original value of Vim's [sessionoptions] [] option.
473480

481+
#### The `xolox#session#locking_enabled()` function
482+
483+
Check whether session locking is enabled. Returns true (1) when locking is
484+
enabled, false (0) otherwise.
485+
486+
By default session locking is enabled but users can opt-out by setting
487+
`g:session_lock_enabled` to false (0).
488+
474489
### Example function for session name suggestions
475490

476491
#### The `xolox#session#suggestions#vcs_feature_branch()` function

autoload/xolox/session.vim

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
" Public API for the vim-session plug-in.
22
"
33
" Author: Peter Odding
4-
" Last Change: February 13, 2015
4+
" Last Change: February 18, 2015
55
" URL: http://peterodding.com/code/vim/session/
66

7-
let g:xolox#session#version = '2.8'
7+
let g:xolox#session#version = '2.9'
88

99
" Public API for session persistence. {{{1
1010

@@ -499,15 +499,17 @@ function! xolox#session#auto_unlock() " {{{2
499499
" [VimLeavePre] [] automatic command event.
500500
"
501501
" [VimLeavePre]: http://vimdoc.sourceforge.net/htmldoc/autocmd.html#VimLeavePre
502-
let i = 0
503-
while i < len(s:lock_files)
504-
let lock_file = s:lock_files[i]
505-
if delete(lock_file) == 0
506-
call remove(s:lock_files, i)
507-
else
508-
let i += 1
509-
endif
510-
endwhile
502+
if xolox#session#locking_enabled()
503+
let i = 0
504+
while i < len(s:lock_files)
505+
let lock_file = s:lock_files[i]
506+
if delete(lock_file) == 0
507+
call remove(s:lock_files, i)
508+
else
509+
let i += 1
510+
endif
511+
endwhile
512+
endif
511513
endfunction
512514

513515
" Commands that enable users to manage multiple sessions. {{{1
@@ -1007,6 +1009,15 @@ if !exists('s:lock_files')
10071009
let s:lock_files = []
10081010
endif
10091011

1012+
function! xolox#session#locking_enabled()
1013+
" Check whether session locking is enabled. Returns true (1) when locking is
1014+
" enabled, false (0) otherwise.
1015+
"
1016+
" By default session locking is enabled but users can opt-out by setting
1017+
" `g:session_lock_enabled` to false (0).
1018+
return xolox#misc#option#get('session_lock_enabled', 1)
1019+
endfunction
1020+
10101021
function! s:vim_instance_id()
10111022
let id = {'pid': getpid()}
10121023
if !empty(v:servername)
@@ -1045,6 +1056,9 @@ function! s:lock_file_path(session_path)
10451056
endfunction
10461057

10471058
function! s:lock_session(session_path)
1059+
if !xolox#session#locking_enabled()
1060+
return 1
1061+
endif
10481062
let lock_file = s:lock_file_path(a:session_path)
10491063
if xolox#misc#persist#save(lock_file, s:vim_instance_id())
10501064
if index(s:lock_files, lock_file) == -1
@@ -1055,6 +1069,9 @@ function! s:lock_session(session_path)
10551069
endfunction
10561070

10571071
function! s:unlock_session(session_path)
1072+
if !xolox#session#locking_enabled()
1073+
return 1
1074+
endif
10581075
let lock_file = s:lock_file_path(a:session_path)
10591076
if delete(lock_file) == 0
10601077
let idx = index(s:lock_files, lock_file)
@@ -1066,6 +1083,9 @@ function! s:unlock_session(session_path)
10661083
endfunction
10671084

10681085
function! s:session_is_locked(session_name, command)
1086+
if !xolox#session#locking_enabled()
1087+
return 0
1088+
endif
10691089
let session_path = xolox#session#name_to_path(a:session_name)
10701090
let lock_file = s:lock_file_path(session_path)
10711091
if filereadable(lock_file)

doc/session.txt

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@ Contents ~
2121
1. The |sessionoptions| setting
2222
2. The |g:session_directory| option
2323
3. The |g:session_lock_directory| option
24-
4. The |g:session_default_name| option
25-
5. The |g:session_default_overwrite| option
26-
6. The |g:session_extension| option
27-
7. The |g:session_autoload| option
28-
8. The |g:session_autosave| option
29-
9. The |g:session_autosave_periodic| option
30-
10. The |g:session_verbose_messages| option
31-
11. The |g:session_default_to_last| option
32-
12. The |g:session_persist_colors| option
33-
13. The |g:session_persist_globals| option
34-
14. The |g:session_restart_environment| option
35-
15. The |g:session_command_aliases| option
36-
16. The |g:session_menu| option
37-
17. The |g:session_name_suggestion_function| option
38-
18. The |g:loaded_session| option
24+
4. The |g:session_lock_enabled| option
25+
5. The |g:session_default_name| option
26+
6. The |g:session_default_overwrite| option
27+
7. The |g:session_extension| option
28+
8. The |g:session_autoload| option
29+
9. The |g:session_autosave| option
30+
10. The |g:session_autosave_periodic| option
31+
11. The |g:session_verbose_messages| option
32+
12. The |g:session_default_to_last| option
33+
13. The |g:session_persist_colors| option
34+
14. The |g:session_persist_globals| option
35+
15. The |g:session_restart_environment| option
36+
16. The |g:session_command_aliases| option
37+
17. The |g:session_menu| option
38+
18. The |g:session_name_suggestion_function| option
39+
19. The |g:loaded_session| option
3940
5. Compatibility with other plug-ins |session-compatibility-with-other-plug-ins|
4041
6. Known issues |session-known-issues|
4142
7. Function reference |session-function-reference|
@@ -65,6 +66,7 @@ Contents ~
6566
23. The |xolox#session#include_tabs()| function
6667
24. The |xolox#session#change_tab_options()| function
6768
25. The |xolox#session#restore_tab_options()| function
69+
26. The |xolox#session#locking_enabled()| function
6870
2. Example function for session name suggestions |example-function-for-session-name-suggestions|
6971
1. The |xolox#session#suggestions#vcs_feature_branch()| function
7072
8. Contact |session-contact|
@@ -322,6 +324,17 @@ factors:
322324
3. As a sane fall back for platforms where '/var/lock' is not available the
323325
directory that stores the session scripts themselves is used.
324326

327+
-------------------------------------------------------------------------------
328+
The *g:session_lock_enabled* option
329+
330+
Depending on your workflow locking of editing sessions can get annoying at
331+
times, so if you don't care about opening a session more than once and
332+
potentially "losing a version of your session" then you can use this option to
333+
completely disable session locking as follows:
334+
>
335+
" Disable all session locking - I know what I'm doing :-).
336+
let g:session_lock_enabled = 0
337+
<
325338
-------------------------------------------------------------------------------
326339
The *g:session_default_name* option
327340

@@ -529,8 +542,8 @@ might take a while...)
529542
*session-function-reference*
530543
Function reference ~
531544

532-
The documentation of the 37 functions below was extracted from 2 Vim scripts on
533-
February 13, 2015 at 13:25.
545+
The documentation of the 38 functions below was extracted from 2 Vim scripts on
546+
February 18, 2015 at 22:56.
534547

535548
-------------------------------------------------------------------------------
536549
*public-api-for-vim-session-plug-in*
@@ -743,6 +756,15 @@ The *xolox#session#restore_tab_options()* function
743756
Restore the original value of Vim's sessionoptions (see |'sessionoptions'|)
744757
option.
745758

759+
-------------------------------------------------------------------------------
760+
The *xolox#session#locking_enabled()* function
761+
762+
Check whether session locking is enabled. Returns true (1) when locking is
763+
enabled, false (0) otherwise.
764+
765+
By default session locking is enabled but users can opt-out by setting
766+
|g:session_lock_enabled| to false (0).
767+
746768
-------------------------------------------------------------------------------
747769
*example-function-for-session-name-suggestions*
748770
Example function for session name suggestions ~

0 commit comments

Comments
 (0)