Skip to content

Commit 037d0ba

Browse files
committed
Make it possible to forget the last used session (pull request #33)
Pull request #33 on GitHub by Ingo Karkat: #33
1 parent c9fa3e2 commit 037d0ba

File tree

3 files changed

+56
-34
lines changed

3 files changed

+56
-34
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
The `session.vim` plug-in improves upon [Vim](http://www.vim.org/)'s built-in [:mksession][mksession] command by enabling you to easily and (if you want) automatically persist and restore your Vim editing sessions. It works by generating a [Vim script](http://vimdoc.sourceforge.net/htmldoc/usr_41.html#script) that restores your current settings and the arrangement of tab pages and/or split windows and the files they contain.
44

5-
To persist your current editing session you can execute the `:SaveSession` command. If you don't provide a name for the session 'default' is used. You're free to use whatever characters you like in session names. When you want to restore your session simply execute `:OpenSession`. Again the name 'default' is used if you don't provide one. When a session is active, has been changed and you quit Vim you'll be prompted whether you want to save the open session before quitting Vim:
5+
To persist your current editing session you can execute the `:SaveSession` command. If you don't provide a name for the session 'default' is used (you can change this name with an option). You're free to use whatever characters you like in session names. When you want to restore your session simply execute `:OpenSession`. Again the name 'default' is used if you don't provide one. When a session is active, has been changed and you quit Vim you'll be prompted whether you want to save the open session before quitting Vim:
66

77
![Screenshot of auto-save prompt](http://peterodding.com/code/vim/session/autosave.png)
88

9-
When you start Vim without editing any files and the 'default' session exists, you'll be prompted whether you want to restore the default session:
9+
When you start Vim without editing any files and the default session exists, you'll be prompted whether you want to restore the default session:
1010

1111
![Screenshot of auto-open prompt](http://peterodding.com/code/vim/session/autoopen.png)
1212

@@ -28,7 +28,7 @@ Note that environment variables inside command arguments are expanded by the plu
2828

2929
### The `:SaveSession` command
3030

31-
This command saves your current editing session just like Vim's built-in [:mksession][mksession] command does. The difference is that you don't pass a full pathname as argument but just a name, any name really. Press `<Tab>` to get completion of existing session names. If you don't provide an argument the name 'default' is used, unless an existing session is open in which case the name of that session will be used.
31+
This command saves your current editing session just like Vim's built-in [:mksession][mksession] command does. The difference is that you don't pass a full pathname as argument but just a name, any name really. Press `<Tab>` to get completion of existing session names. If you don't provide an argument the default session name is used, unless an existing session is open in which case the name of that session will be used.
3232

3333
If the session you're trying to save is already active in another Vim instance you'll get a warning and nothing happens. You can use a bang (!) as in `:SaveSession! ...` to ignore the warning and save the session anyway.
3434

@@ -103,7 +103,7 @@ The filename extension of session scripts. This should include the dot that sepa
103103

104104
### The `g:session_autoload` option
105105

106-
By default this option is set to `'prompt'`. This means that when you start Vim without opening any files and the `default` session script exists, the session plug-in will ask whether you want to restore your default session. When you set this option to `'yes'` and you start Vim without opening any files the default session will be restored without a prompt. To completely disable automatic loading you can set this option to `'no'`.
106+
By default this option is set to `'prompt'`. This means that when you start Vim without opening any files and the default session script exists, the session plug-in will ask whether you want to restore your default session. When you set this option to `'yes'` and you start Vim without opening any files the default session will be restored without a prompt. To completely disable automatic loading you can set this option to `'no'`.
107107

108108
### The `g:session_autosave` option
109109

autoload/xolox/session.vim

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Last Change: May 2, 2013
44
" URL: http://peterodding.com/code/vim/session/
55

6-
let g:xolox#session#version = '1.6.4'
6+
let g:xolox#session#version = '1.7'
77

88
call xolox#misc#compat#check('session', 2)
99

@@ -261,13 +261,25 @@ function! xolox#session#auto_load() " {{{2
261261
endfor
262262
endif
263263
" Default to the last used session or the default session?
264-
let session = s:last_session_recall()
264+
let [has_last_session, session] = s:get_last_or_default_session()
265265
let path = xolox#session#name_to_path(session)
266-
if filereadable(path) && !s:session_is_locked(path)
267-
let msg = "Do you want to restore your %s editing session?"
268-
let label = session != g:session_default_name ? 'last used' : 'default'
269-
if s:prompt(printf(msg, label), 'g:session_autoload')
266+
if (g:session_default_to_last == 0 || has_last_session) && filereadable(path) && !s:session_is_locked(path)
267+
" Compose the message for the prompt.
268+
let is_default_session = (session == g:session_default_name)
269+
let msg = printf("Do you want to restore your %s editing session%s?",
270+
\ is_default_session ? 'default' : 'last used',
271+
\ is_default_session ? '' : printf(' (%s)', session))
272+
" Prepare the list of choices.
273+
let choices = ['&Yes', '&No']
274+
if !is_default_session
275+
call add(choices, '&Forget')
276+
endif
277+
" Prompt the user (if not configured otherwise).
278+
let choice = s:prompt(msg, choices, 'g:session_autoload')
279+
if choice == 1
270280
call xolox#session#open_cmd(session, '')
281+
elseif choice == 3
282+
call s:last_session_forget()
271283
endif
272284
endif
273285
endif
@@ -278,7 +290,7 @@ function! xolox#session#auto_save() " {{{2
278290
let name = s:get_name('', 0)
279291
if name != ''
280292
let msg = "Do you want to save your editing session before quitting Vim?"
281-
if s:prompt(msg, 'g:session_autosave')
293+
if s:prompt(msg, ['&Yes', '&No'], 'g:session_autosave') == 1
282294
call xolox#session#save_cmd(name, '')
283295
endif
284296
endif
@@ -299,18 +311,20 @@ endfunction
299311

300312
" Commands that enable users to manage multiple sessions. {{{1
301313

302-
function! s:prompt(msg, var)
303-
let value = eval(a:var)
304-
if value == 'yes' || (type(value) != type('') && value)
314+
function! s:prompt(msg, choices, option_name)
315+
let option_value = eval(a:option_name)
316+
if option_value == 'yes'
305317
return 1
318+
elseif option_value == 'no'
319+
return 0
306320
else
307321
if g:session_verbose_messages
308322
let format = "%s Note that you can permanently disable this dialog by adding the following line to your %s script:\n\n\t:let %s = 'no'"
309323
let prompt = printf(format, a:msg, xolox#misc#os#is_win() ? '~\_vimrc' : '~/.vimrc', a:var)
310324
else
311325
let prompt = a:msg
312326
endif
313-
return confirm(prompt, "&Yes\n&No", 1, 'Question') == 1
327+
return confirm(prompt, join(a:choices, "\n"), 1, 'Question')
314328
endif
315329
endfunction
316330

@@ -398,7 +412,7 @@ function! xolox#session#close_cmd(bang, silent, save_allowed) abort " {{{2
398412
if name != ''
399413
if a:save_allowed
400414
let msg = "Do you want to save your current editing session before closing it?"
401-
if s:prompt(msg, 'g:session_autosave')
415+
if s:prompt(msg, ['&Yes', '&No'], 'g:session_autosave') == 1
402416
call xolox#session#save_cmd(name, a:bang)
403417
endif
404418
else
@@ -573,14 +587,22 @@ function! s:last_session_persist(name)
573587
endif
574588
endfunction
575589

576-
function! s:last_session_recall()
577-
if g:session_default_to_last
578-
let fname = s:last_session_file()
579-
if filereadable(fname)
580-
return readfile(fname)[0]
581-
endif
590+
function! s:last_session_forget()
591+
let last_session_file = s:last_session_file()
592+
if filereadable(last_session_file) && delete(last_session_file) != 0
593+
call xolox#misc#msg#warn("session.vim %s: Failed to delete name of last used session!", g:xolox#session#version)
594+
endif
595+
endfunction
596+
597+
function! s:get_last_or_default_session()
598+
let last_session_file = s:last_session_file()
599+
let has_last_session = filereadable(last_session_file)
600+
if g:session_default_to_last && has_last_session
601+
let lines = readfile(last_session_file)
602+
return [has_last_session, lines[0]]
603+
else
604+
return [has_last_session, g:session_default_name]
582605
endif
583-
return g:session_default_name
584606
endfunction
585607

586608
" Lock file management: {{{2

doc/session.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ your current settings and the arrangement of tab pages and/or split windows
4343
and the files they contain.
4444

4545
To persist your current editing session you can execute the |:SaveSession|
46-
command. If you don't provide a name for the session 'default' is used. You're
47-
free to use whatever characters you like in session names. When you want to
48-
restore your session simply execute |:OpenSession|. Again the name 'default'
49-
is used if you don't provide one. When a session is active, has been changed
50-
and you quit Vim you'll be prompted whether you want to save the open session
51-
before quitting Vim:
46+
command. If you don't provide a name for the session 'default' is used (you
47+
can change this name with an option). You're free to use whatever characters
48+
you like in session names. When you want to restore your session simply
49+
execute |:OpenSession|. Again the name 'default' is used if you don't provide
50+
one. When a session is active, has been changed and you quit Vim you'll be
51+
prompted whether you want to save the open session before quitting Vim:
5252

5353
Screenshot of auto-save prompt, see reference [1]
5454

55-
When you start Vim without editing any files and the 'default' session exists,
55+
When you start Vim without editing any files and the default session exists,
5656
you'll be prompted whether you want to restore the default session:
5757

5858
Screenshot of auto-open prompt, see reference [2]
@@ -95,9 +95,9 @@ The *:SaveSession* command
9595
This command saves your current editing session just like Vim's built-in
9696
|:mksession| command does. The difference is that you don't pass a full pathname
9797
as argument but just a name, any name really. Press '<Tab>' to get completion
98-
of existing session names. If you don't provide an argument the name 'default'
99-
is used, unless an existing session is open in which case the name of that
100-
session will be used.
98+
of existing session names. If you don't provide an argument the default
99+
session name is used, unless an existing session is open in which case the
100+
name of that session will be used.
101101

102102
If the session you're trying to save is already active in another Vim instance
103103
you'll get a warning and nothing happens. You can use a bang (!) as in
@@ -221,7 +221,7 @@ separates the basename from the extension. Defaults to '.vim'.
221221
The *g:session_autoload* option
222222

223223
By default this option is set to 'prompt'. This means that when you start Vim
224-
without opening any files and the 'default' session script exists, the session
224+
without opening any files and the default session script exists, the session
225225
plug-in will ask whether you want to restore your default session. When you
226226
set this option to 'yes' and you start Vim without opening any files the
227227
default session will be restored without a prompt. To completely disable

0 commit comments

Comments
 (0)