Skip to content

Commit

Permalink
Make it possible to forget the last used session (pull request #33)
Browse files Browse the repository at this point in the history
Pull request #33 on GitHub by Ingo Karkat:
#33
  • Loading branch information
xolox committed May 2, 2013
1 parent c9fa3e2 commit 037d0ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -2,11 +2,11 @@

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.

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:
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:

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

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:
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:

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

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

### The `:SaveSession` command

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.
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.

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.

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

### The `g:session_autoload` option

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'`.
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'`.

### The `g:session_autosave` option

Expand Down
60 changes: 41 additions & 19 deletions autoload/xolox/session.vim
Expand Up @@ -3,7 +3,7 @@
" Last Change: May 2, 2013
" URL: http://peterodding.com/code/vim/session/

let g:xolox#session#version = '1.6.4'
let g:xolox#session#version = '1.7'

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

Expand Down Expand Up @@ -261,13 +261,25 @@ function! xolox#session#auto_load() " {{{2
endfor
endif
" Default to the last used session or the default session?
let session = s:last_session_recall()
let [has_last_session, session] = s:get_last_or_default_session()
let path = xolox#session#name_to_path(session)
if filereadable(path) && !s:session_is_locked(path)
let msg = "Do you want to restore your %s editing session?"
let label = session != g:session_default_name ? 'last used' : 'default'
if s:prompt(printf(msg, label), 'g:session_autoload')
if (g:session_default_to_last == 0 || has_last_session) && filereadable(path) && !s:session_is_locked(path)
" 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?",
\ is_default_session ? 'default' : 'last used',
\ is_default_session ? '' : printf(' (%s)', session))
" Prepare the list of choices.
let choices = ['&Yes', '&No']
if !is_default_session
call add(choices, '&Forget')
endif
" Prompt the user (if not configured otherwise).
let choice = s:prompt(msg, choices, 'g:session_autoload')
if choice == 1
call xolox#session#open_cmd(session, '')
elseif choice == 3
call s:last_session_forget()
endif
endif
endif
Expand All @@ -278,7 +290,7 @@ function! xolox#session#auto_save() " {{{2
let name = s:get_name('', 0)
if name != ''
let msg = "Do you want to save your editing session before quitting Vim?"
if s:prompt(msg, 'g:session_autosave')
if s:prompt(msg, ['&Yes', '&No'], 'g:session_autosave') == 1
call xolox#session#save_cmd(name, '')
endif
endif
Expand All @@ -299,18 +311,20 @@ endfunction

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

function! s:prompt(msg, var)
let value = eval(a:var)
if value == 'yes' || (type(value) != type('') && value)
function! s:prompt(msg, choices, option_name)
let option_value = eval(a:option_name)
if option_value == 'yes'
return 1
elseif option_value == 'no'
return 0
else
if g:session_verbose_messages
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'"
let prompt = printf(format, a:msg, xolox#misc#os#is_win() ? '~\_vimrc' : '~/.vimrc', a:var)
else
let prompt = a:msg
endif
return confirm(prompt, "&Yes\n&No", 1, 'Question') == 1
return confirm(prompt, join(a:choices, "\n"), 1, 'Question')
endif
endfunction

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

function! s:last_session_recall()
if g:session_default_to_last
let fname = s:last_session_file()
if filereadable(fname)
return readfile(fname)[0]
endif
function! s:last_session_forget()
let last_session_file = s:last_session_file()
if filereadable(last_session_file) && delete(last_session_file) != 0
call xolox#misc#msg#warn("session.vim %s: Failed to delete name of last used session!", g:xolox#session#version)
endif
endfunction

function! s:get_last_or_default_session()
let last_session_file = s:last_session_file()
let has_last_session = filereadable(last_session_file)
if g:session_default_to_last && has_last_session
let lines = readfile(last_session_file)
return [has_last_session, lines[0]]
else
return [has_last_session, g:session_default_name]
endif
return g:session_default_name
endfunction

" Lock file management: {{{2
Expand Down
22 changes: 11 additions & 11 deletions doc/session.txt
Expand Up @@ -43,16 +43,16 @@ your current settings and the arrangement of tab pages and/or split windows
and the files they contain.

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:
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:

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

When you start Vim without editing any files and the 'default' session exists,
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:

Screenshot of auto-open prompt, see reference [2]
Expand Down Expand Up @@ -95,9 +95,9 @@ The *:SaveSession* command
This command saves your current editing session just like Vim's built-in
|: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.
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.

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
Expand Down Expand Up @@ -221,7 +221,7 @@ separates the basename from the extension. Defaults to '.vim'.
The *g:session_autoload* option

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
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
Expand Down

0 comments on commit 037d0ba

Please sign in to comment.