Skip to content

Commit 70b6f7a

Browse files
committed
Generic completion added to SaveSession.
git feature branch completion included.
1 parent a55318b commit 70b6f7a

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

autoload/xolox/save_default.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function! xolox#save_default#get_git_branch() " {{{2
2+
" Return current branch as a list, normalized to ascii word characters.
3+
" Failure returns an empty list.
4+
let branch = system('git rev-parse --abbrev-ref HEAD')
5+
if v:shell_error != 0
6+
return []
7+
elseif branch =~ 'fatal: Not a git repository'
8+
return []
9+
elseif branch =~ 'HEAD'
10+
return []
11+
endif
12+
let branch = substitute(branch , '\n$', '', '')
13+
let branch = substitute(branch , '\W', '-', 'g')
14+
return [branch]
15+
endfunction

autoload/xolox/session.vim

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,20 +860,33 @@ function! xolox#session#path_to_name(path) " {{{2
860860
return xolox#misc#path#decode(fnamemodify(a:path, ':t:r'))
861861
endfunction
862862

863-
function! xolox#session#get_names() " {{{2
863+
function! xolox#session#get_names(list_mode) " {{{2
864864
" Get the names of all available sessions. This scans the directory
865865
" configured with `g:session_directory` for files that end with the suffix
866866
" configured with `g:session_extension`, takes the base name of each file
867-
" and decodes any URL encoded characters. Returns a list of strings.
867+
" and decodes any URL encoded characters. Optionally prefixes file names
868+
" from a user-supplied function if defined by the user. Returns a list of
869+
" strings.
868870
let directory = xolox#misc#path#absolute(g:session_directory)
869871
let filenames = split(glob(xolox#misc#path#merge(directory, '*' . g:session_extension)), "\n")
872+
if g:session_additional_names_function != 'none' && a:list_mode == 'save'
873+
let AdditionalNames = function(g:session_additional_names_function)
874+
let filenames = AdditionalNames() + filenames
875+
endif
870876
return map(filenames, 'xolox#session#path_to_name(v:val)')
871877
endfunction
872878

873879
function! xolox#session#complete_names(arg, line, pos) " {{{2
874880
" Completion function for user defined Vim commands. Used by commands like
875-
" `:OpenSession` and `:DeleteSession` to support user friendly completion.
876-
let names = filter(xolox#session#get_names(), 'v:val =~ a:arg')
881+
" `:OpenSession` and `:DeleteSession` (but not `:SaveSession`) to support
882+
" user friendly completion.
883+
let names = filter(xolox#session#get_names(''), 'v:val =~ a:arg')
884+
return map(names, 'fnameescape(v:val)')
885+
endfunction
886+
887+
function! xolox#session#complete_save_names(arg, line, pos) " {{{2
888+
" Completion function for `:SaveSession`
889+
let names = filter(xolox#session#get_names('save'), 'v:val =~ a:arg')
877890
return map(names, 'fnameescape(v:val)')
878891
endfunction
879892

doc/session.txt

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Contents ~
3333
13. The |g:session_command_aliases| option
3434
14. The |g:session_menu| option
3535
15. The |g:loaded_session| option
36+
16. The |g:session_default_save_function| option
3637
5. Compatibility with other plug-ins |session-compatibility-with-other-plug-ins|
3738
6. Known issues |session-known-issues|
3839
7. Function reference |session-function-reference|
@@ -167,11 +168,11 @@ that session is opened, otherwise the plug-in will ask you to select one from a
167168
list:
168169
>
169170
Please select the session to restore:
170-
171+
171172
1. vim-profile
172173
2. session-plugin
173174
3. etc.
174-
175+
175176
Type number and <Enter> or click with mouse (empty cancels):
176177
<
177178
If the session you're trying to open is already active in another Vim instance
@@ -281,7 +282,7 @@ how it works by setting |'sessionoptions'| in your |vimrc| script, for example:
281282
>
282283
" If you only want to save the current tab page:
283284
set sessionoptions-=tabpages
284-
285+
285286
" If you don't want help windows to be restored:
286287
set sessionoptions-=help
287288
<
@@ -456,6 +457,24 @@ session plug-in you can set this variable to any value in your |vimrc| script:
456457
>
457458
:let g:loaded_session = 1
458459
<
460+
-------------------------------------------------------------------------------
461+
The *g:session_default_save_function* option
462+
463+
This variable influences the first choice of the |:SaveSession| command to
464+
allow users to specify their own defaults.
465+
>
466+
:let g:session_default_save_function = 'function_name'
467+
<
468+
469+
One use is to name sessions based on your current feature branch. Currently,
470+
the following function is provided:
471+
472+
A function to determine the current git branch in your current working
473+
directory.
474+
>
475+
:let g:session_default_save_function = 'xolox#save_default#get_git_branch'
476+
<
477+
459478
===============================================================================
460479
*session-compatibility-with-other-plug-ins*
461480
Compatibility with other plug-ins ~
@@ -602,13 +621,13 @@ session is selected an empty string is returned. Here's an example of what the
602621
prompt looks like:
603622
>
604623
:call xolox#session#prompt_for_name('trash')
605-
624+
606625
Please select the session to trash:
607-
626+
608627
1. first-session
609628
2. second-session
610629
3. third-session
611-
630+
612631
Type number and <Enter> or click with mouse (empty cancels):
613632
<
614633
If only a single session exists there's nothing to choose from so the name of
@@ -718,7 +737,7 @@ was editing the plug-in itself in Vim:
718737
" ~/.vim/sessions/example.vim: Vim session script.
719738
" Created by session.vim on 30 August 2010 at 05:26:28.
720739
" Open this file in Vim and run :source % to restore your session.
721-
740+
722741
set guioptions=aegit
723742
set guifont=Monaco\ 13
724743
if exists('g:syntax_on') != 1 | syntax on | endif

plugin/session.vim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ endtry
2727

2828
" Configuration defaults. {{{1
2929

30+
" Setting to a function name (as a string) sets names used for SaveSession.
31+
if !exists('g:session_additional_names_function')
32+
let g:session_additional_names_function = 'none'
33+
endif
34+
3035
" The name of the default session (without directory or filename extension).
3136
if !exists('g:session_default_name')
3237
let g:session_default_name = 'default'
@@ -157,7 +162,7 @@ augroup END
157162
" one or more tab pages).
158163
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names OpenSession call xolox#session#open_cmd(<q-args>, <q-bang>, 'OpenSession')
159164
command! -bar -nargs=? -complete=customlist,xolox#session#complete_names ViewSession call xolox#session#view_cmd(<q-args>)
160-
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names SaveSession call xolox#session#save_cmd(<q-args>, <q-bang>, 'SaveSession')
165+
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_save_names SaveSession call xolox#session#save_cmd(<q-args>, <q-bang>, 'SaveSession')
161166
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names DeleteSession call xolox#session#delete_cmd(<q-args>, <q-bang>)
162167
command! -bar -bang CloseSession call xolox#session#close_cmd(<q-bang>, 0, 1, 'CloseSession')
163168

0 commit comments

Comments
 (0)