From 70b6f7aa789ce5ad4414c3afa0f7b42701399e4a Mon Sep 17 00:00:00 2001 From: Todd Blackman Date: Thu, 20 Mar 2014 07:58:19 -0400 Subject: [PATCH] Generic completion added to SaveSession. git feature branch completion included. --- autoload/xolox/save_default.vim | 15 +++++++++++++++ autoload/xolox/session.vim | 21 +++++++++++++++++---- doc/session.txt | 33 ++++++++++++++++++++++++++------- plugin/session.vim | 7 ++++++- 4 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 autoload/xolox/save_default.vim diff --git a/autoload/xolox/save_default.vim b/autoload/xolox/save_default.vim new file mode 100644 index 0000000..6de90ae --- /dev/null +++ b/autoload/xolox/save_default.vim @@ -0,0 +1,15 @@ +function! xolox#save_default#get_git_branch() " {{{2 + " Return current branch as a list, normalized to ascii word characters. + " Failure returns an empty list. + let branch = system('git rev-parse --abbrev-ref HEAD') + if v:shell_error != 0 + return [] + elseif branch =~ 'fatal: Not a git repository' + return [] + elseif branch =~ 'HEAD' + return [] + endif + let branch = substitute(branch , '\n$', '', '') + let branch = substitute(branch , '\W', '-', 'g') + return [branch] +endfunction diff --git a/autoload/xolox/session.vim b/autoload/xolox/session.vim index 07bd270..83d88bb 100644 --- a/autoload/xolox/session.vim +++ b/autoload/xolox/session.vim @@ -860,20 +860,33 @@ function! xolox#session#path_to_name(path) " {{{2 return xolox#misc#path#decode(fnamemodify(a:path, ':t:r')) endfunction -function! xolox#session#get_names() " {{{2 +function! xolox#session#get_names(list_mode) " {{{2 " Get the names of all available sessions. This scans the directory " configured with `g:session_directory` for files that end with the suffix " configured with `g:session_extension`, takes the base name of each file - " and decodes any URL encoded characters. Returns a list of strings. + " and decodes any URL encoded characters. Optionally prefixes file names + " from a user-supplied function if defined by the user. Returns a list of + " strings. let directory = xolox#misc#path#absolute(g:session_directory) let filenames = split(glob(xolox#misc#path#merge(directory, '*' . g:session_extension)), "\n") + if g:session_additional_names_function != 'none' && a:list_mode == 'save' + let AdditionalNames = function(g:session_additional_names_function) + let filenames = AdditionalNames() + filenames + endif return map(filenames, 'xolox#session#path_to_name(v:val)') endfunction function! xolox#session#complete_names(arg, line, pos) " {{{2 " Completion function for user defined Vim commands. Used by commands like - " `:OpenSession` and `:DeleteSession` to support user friendly completion. - let names = filter(xolox#session#get_names(), 'v:val =~ a:arg') + " `:OpenSession` and `:DeleteSession` (but not `:SaveSession`) to support + " user friendly completion. + let names = filter(xolox#session#get_names(''), 'v:val =~ a:arg') + return map(names, 'fnameescape(v:val)') +endfunction + +function! xolox#session#complete_save_names(arg, line, pos) " {{{2 + " Completion function for `:SaveSession` + let names = filter(xolox#session#get_names('save'), 'v:val =~ a:arg') return map(names, 'fnameescape(v:val)') endfunction diff --git a/doc/session.txt b/doc/session.txt index 0215fe0..0aeda85 100644 --- a/doc/session.txt +++ b/doc/session.txt @@ -33,6 +33,7 @@ Contents ~ 13. The |g:session_command_aliases| option 14. The |g:session_menu| option 15. The |g:loaded_session| option + 16. The |g:session_default_save_function| option 5. Compatibility with other plug-ins |session-compatibility-with-other-plug-ins| 6. Known issues |session-known-issues| 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 list: > Please select the session to restore: - + 1. vim-profile 2. session-plugin 3. etc. - + Type number and or click with mouse (empty cancels): < 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: > " If you only want to save the current tab page: set sessionoptions-=tabpages - + " If you don't want help windows to be restored: set sessionoptions-=help < @@ -456,6 +457,24 @@ session plug-in you can set this variable to any value in your |vimrc| script: > :let g:loaded_session = 1 < +------------------------------------------------------------------------------- +The *g:session_default_save_function* option + +This variable influences the first choice of the |:SaveSession| command to +allow users to specify their own defaults. +> + :let g:session_default_save_function = 'function_name' +< + +One use is to name sessions based on your current feature branch. Currently, +the following function is provided: + +A function to determine the current git branch in your current working +directory. +> + :let g:session_default_save_function = 'xolox#save_default#get_git_branch' +< + =============================================================================== *session-compatibility-with-other-plug-ins* Compatibility with other plug-ins ~ @@ -602,13 +621,13 @@ session is selected an empty string is returned. Here's an example of what the prompt looks like: > :call xolox#session#prompt_for_name('trash') - + Please select the session to trash: - + 1. first-session 2. second-session 3. third-session - + Type number and or click with mouse (empty cancels): < 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: " ~/.vim/sessions/example.vim: Vim session script. " Created by session.vim on 30 August 2010 at 05:26:28. " Open this file in Vim and run :source % to restore your session. - + set guioptions=aegit set guifont=Monaco\ 13 if exists('g:syntax_on') != 1 | syntax on | endif diff --git a/plugin/session.vim b/plugin/session.vim index 5e02ecd..7c5a157 100644 --- a/plugin/session.vim +++ b/plugin/session.vim @@ -27,6 +27,11 @@ endtry " Configuration defaults. {{{1 +" Setting to a function name (as a string) sets names used for SaveSession. +if !exists('g:session_additional_names_function') + let g:session_additional_names_function = 'none' +endif + " The name of the default session (without directory or filename extension). if !exists('g:session_default_name') let g:session_default_name = 'default' @@ -157,7 +162,7 @@ augroup END " one or more tab pages). command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names OpenSession call xolox#session#open_cmd(, , 'OpenSession') command! -bar -nargs=? -complete=customlist,xolox#session#complete_names ViewSession call xolox#session#view_cmd() -command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names SaveSession call xolox#session#save_cmd(, , 'SaveSession') +command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_save_names SaveSession call xolox#session#save_cmd(, , 'SaveSession') command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names DeleteSession call xolox#session#delete_cmd(, ) command! -bar -bang CloseSession call xolox#session#close_cmd(, 0, 1, 'CloseSession')