Skip to content

Commit f13514a

Browse files
committed
Merge pull request #88: User defined session name completion suggestions
2 parents 407ce08 + 70b6f7a commit f13514a

File tree

5 files changed

+148
-23
lines changed

5 files changed

+148
-23
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ By default the plug-in installs a top level menu. If you don't like this you can
227227

228228
:let g:session_menu = 0
229229

230+
### The `g:session_name_suggestion_function` option
231+
232+
The default completion of the `:SaveSession` command is based on the names of the existing sessions. You can add your own suggestions using this option by setting the option to the name of a Vim script function. By default this option is set to an example function that suggests the name of the current git or Mercurial feature branch (when you're working in a version control repository).
233+
230234
### The `g:loaded_session` option
231235

232236
This variable isn't really an option but if you want to avoid loading the vim-session plug-in you can set this variable to any value in your [vimrc script] [vimrc]:
@@ -251,8 +255,8 @@ Recently this plug-in switched from reimplementing [:mksession][mksession] to ac
251255

252256
<!-- Start of generated documentation -->
253257

254-
The documentation of the 34 functions below was extracted from
255-
1 Vim scripts on July 6, 2014 at 21:01.
258+
The documentation of the 37 functions below was extracted from
259+
2 Vim scripts on July 6, 2014 at 22:23.
256260

257261
### Public API for the vim-session plug-in
258262

@@ -405,10 +409,20 @@ configured with `g:session_directory` for files that end with the suffix
405409
configured with `g:session_extension`, takes the base name of each file
406410
and decodes any URL encoded characters. Returns a list of strings.
407411

412+
If the first argument is true (1) then the user defined function
413+
configured with `g:session_name_suggestion_function` is called to find
414+
suggested session names, which are prefixed to the list of available
415+
sessions, otherwise the argument should be false (0).
416+
408417
#### The `xolox#session#complete_names()` function
409418

410419
Completion function for user defined Vim commands. Used by commands like
411-
`:OpenSession` and `:DeleteSession` to support user friendly completion.
420+
`:OpenSession` and `:DeleteSession` (but not `:SaveSession`) to support
421+
user friendly completion.
422+
423+
#### The `xolox#session#complete_names_with_suggestions()` function
424+
425+
Completion function for the Vim command `:SaveSession`.
412426

413427
#### The `xolox#session#is_tab_scoped()` function
414428

@@ -449,6 +463,16 @@ scoped session. Saves a copy of the original value to be restored later.
449463

450464
Restore the original value of Vim's [sessionoptions] [] option.
451465

466+
### Example function for session name suggestions
467+
468+
#### The `xolox#session#suggestions#vcs_feature_branch()` function
469+
470+
This function implements an example of a function that can be used with
471+
the `g:session_name_suggestion_function` option. It finds the name of the
472+
current git or Mercurial feature branch (if any) and suggests this name as
473+
the name for the session that is being saved with :SaveSession. Returns a
474+
list with one string on success and an empty list on failure.
475+
452476
<!-- End of generated documentation -->
453477

454478
## Contact

autoload/xolox/session.vim

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
" Last Change: July 6, 2014
55
" URL: http://peterodding.com/code/vim/session/
66

7-
let g:xolox#session#version = '2.5'
7+
let g:xolox#session#version = '2.6'
88

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

@@ -403,7 +403,7 @@ function! xolox#session#auto_load() " {{{2
403403
if current_buffer_is_empty && (buffer_list_is_empty || buffer_list_is_persistent)
404404
" Check whether a session matching the user-specified server name exists.
405405
if v:servername !~ '^\cgvim\d*$'
406-
for session in xolox#session#get_names()
406+
for session in xolox#session#get_names(0)
407407
if v:servername ==? session
408408
call xolox#session#open_cmd(session, '', 'OpenSession')
409409
return
@@ -452,7 +452,7 @@ function! xolox#session#auto_save() " {{{2
452452
let name = xolox#session#find_current_session()
453453
" If no session is active and the user doesn't have any sessions yet, help
454454
" them get started by suggesting to create the default session.
455-
if empty(name) && (empty(xolox#session#get_names()) || g:session_default_overwrite)
455+
if empty(name) && (empty(xolox#session#get_names(0)) || g:session_default_overwrite)
456456
let name = g:session_default_name
457457
endif
458458
" Prompt the user to save the active/first/default session?
@@ -837,7 +837,7 @@ function! xolox#session#prompt_for_name(action) " {{{2
837837
"
838838
" If only a single session exists there's nothing to choose from so the name
839839
" of that session will be returned directly, without prompting the user.
840-
let sessions = sort(xolox#session#get_names(), 1)
840+
let sessions = sort(xolox#session#get_names(0), 1)
841841
if len(sessions) == 1
842842
return sessions[0]
843843
elseif !empty(sessions)
@@ -878,20 +878,37 @@ function! xolox#session#path_to_name(path) " {{{2
878878
return xolox#misc#path#decode(fnamemodify(a:path, ':t:r'))
879879
endfunction
880880

881-
function! xolox#session#get_names() " {{{2
881+
function! xolox#session#get_names(include_suggestions) " {{{2
882882
" Get the names of all available sessions. This scans the directory
883883
" configured with `g:session_directory` for files that end with the suffix
884884
" configured with `g:session_extension`, takes the base name of each file
885885
" and decodes any URL encoded characters. Returns a list of strings.
886+
"
887+
" If the first argument is true (1) then the user defined function
888+
" configured with `g:session_name_suggestion_function` is called to find
889+
" suggested session names, which are prefixed to the list of available
890+
" sessions, otherwise the argument should be false (0).
886891
let directory = xolox#misc#path#absolute(g:session_directory)
887892
let filenames = split(glob(xolox#misc#path#merge(directory, '*' . g:session_extension)), "\n")
888-
return map(filenames, 'xolox#session#path_to_name(v:val)')
893+
call map(filenames, 'xolox#session#path_to_name(v:val)')
894+
if a:include_suggestions && !empty(g:session_name_suggestion_function)
895+
let suggested_names = call(g:session_name_suggestion_function, [])
896+
let filenames = suggested_names + filenames
897+
endif
898+
return filenames
889899
endfunction
890900

891901
function! xolox#session#complete_names(arg, line, pos) " {{{2
892902
" Completion function for user defined Vim commands. Used by commands like
893-
" `:OpenSession` and `:DeleteSession` to support user friendly completion.
894-
let names = filter(xolox#session#get_names(), 'v:val =~ a:arg')
903+
" `:OpenSession` and `:DeleteSession` (but not `:SaveSession`) to support
904+
" user friendly completion.
905+
let names = filter(xolox#session#get_names(0), 'v:val =~ a:arg')
906+
return map(names, 'fnameescape(v:val)')
907+
endfunction
908+
909+
function! xolox#session#complete_names_with_suggestions(arg, line, pos) " {{{2
910+
" Completion function for the Vim command `:SaveSession`.
911+
let names = filter(xolox#session#get_names(1), 'v:val =~ a:arg')
895912
return map(names, 'fnameescape(v:val)')
896913
endfunction
897914

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
" Example function for session name suggestions.
2+
"
3+
" Author: Peter Odding
4+
" Last Change: July 6, 2014
5+
" URL: http://peterodding.com/code/vim/session/
6+
7+
function! xolox#session#suggestions#vcs_feature_branch() " {{{1
8+
" This function implements an example of a function that can be used with
9+
" the `g:session_name_suggestion_function` option. It finds the name of the
10+
" current git or Mercurial feature branch (if any) and suggests this name as
11+
" the name for the session that is being saved with :SaveSession. Returns a
12+
" list with one string on success and an empty list on failure.
13+
let [kind, directory] = xolox#session#suggestions#find_vcs_repository()
14+
if kind == 'git'
15+
let command = 'git rev-parse --abbrev-ref HEAD'
16+
let names_to_ignore = ['master']
17+
elseif kind == 'hg'
18+
let command = 'hg branch'
19+
let names_to_ignore = ['default']
20+
else
21+
return []
22+
endif
23+
let result = xolox#misc#os#exec({'command': command, 'check': 0})
24+
if result['exit_code'] == 0 && !empty(result['stdout'])
25+
let branch_name = xolox#misc#str#trim(result['stdout'][0])
26+
if !empty(branch_name) && index(names_to_ignore, branch_name) == -1
27+
return [xolox#misc#str#slug(branch_name)]
28+
endif
29+
endif
30+
return []
31+
endfunction
32+
33+
function! xolox#session#suggestions#find_vcs_repository()
34+
for name in ['git', 'hg']
35+
let match = finddir('.' . name, '.;')
36+
if !empty(match)
37+
let directory = fnamemodify(match, ':h')
38+
return [name, directory]
39+
endif
40+
endfor
41+
return ['', '']
42+
endfunction

doc/session.txt

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Contents ~
3333
13. The |g:session_restart_environment| option
3434
14. The |g:session_command_aliases| option
3535
15. The |g:session_menu| option
36-
16. The |g:loaded_session| option
36+
16. The |g:session_name_suggestion_function| option
37+
17. The |g:loaded_session| option
3738
5. Compatibility with other plug-ins |session-compatibility-with-other-plug-ins|
3839
6. Known issues |session-known-issues|
3940
7. Function reference |session-function-reference|
@@ -55,13 +56,16 @@ Contents ~
5556
15. The |xolox#session#path_to_name()| function
5657
16. The |xolox#session#get_names()| function
5758
17. The |xolox#session#complete_names()| function
58-
18. The |xolox#session#is_tab_scoped()| function
59-
19. The |xolox#session#find_current_session()| function
60-
20. The |xolox#session#get_label()| function
61-
21. The |xolox#session#options_include()| function
62-
22. The |xolox#session#include_tabs()| function
63-
23. The |xolox#session#change_tab_options()| function
64-
24. The |xolox#session#restore_tab_options()| function
59+
18. The |xolox#session#complete_names_with_suggestions()| function
60+
19. The |xolox#session#is_tab_scoped()| function
61+
20. The |xolox#session#find_current_session()| function
62+
21. The |xolox#session#get_label()| function
63+
22. The |xolox#session#options_include()| function
64+
23. The |xolox#session#include_tabs()| function
65+
24. The |xolox#session#change_tab_options()| function
66+
25. The |xolox#session#restore_tab_options()| function
67+
2. Example function for session name suggestions |example-function-for-session-name-suggestions|
68+
1. The |xolox#session#suggestions#vcs_feature_branch()| function
6569
8. Contact |session-contact|
6670
9. License |session-license|
6771
10. Sample session script |sample-session-script|
@@ -458,6 +462,15 @@ can disable it by adding the following line to your |vimrc| script:
458462
>
459463
:let g:session_menu = 0
460464
<
465+
-------------------------------------------------------------------------------
466+
The *g:session_name_suggestion_function* option
467+
468+
The default completion of the |:SaveSession| command is based on the names of
469+
the existing sessions. You can add your own suggestions using this option by
470+
setting the option to the name of a Vim script function. By default this option
471+
is set to an example function that suggests the name of the current git or
472+
Mercurial feature branch (when you're working in a version control repository).
473+
461474
-------------------------------------------------------------------------------
462475
The *g:loaded_session* option
463476

@@ -499,8 +512,8 @@ might take a while...)
499512
*session-function-reference*
500513
Function reference ~
501514

502-
The documentation of the 34 functions below was extracted from 1 Vim scripts on
503-
July 6, 2014 at 21:01.
515+
The documentation of the 37 functions below was extracted from 2 Vim scripts on
516+
July 6, 2014 at 22:23.
504517

505518
-------------------------------------------------------------------------------
506519
*public-api-for-vim-session-plug-in*
@@ -650,11 +663,22 @@ with |g:session_directory| for files that end with the suffix configured with
650663
|g:session_extension|, takes the base name of each file and decodes any URL
651664
encoded characters. Returns a list of strings.
652665

666+
If the first argument is true (1) then the user defined function configured
667+
with |g:session_name_suggestion_function| is called to find suggested session
668+
names, which are prefixed to the list of available sessions, otherwise the
669+
argument should be false (0).
670+
653671
-------------------------------------------------------------------------------
654672
The *xolox#session#complete_names()* function
655673

656674
Completion function for user defined Vim commands. Used by commands like
657-
|:OpenSession| and |:DeleteSession| to support user friendly completion.
675+
|:OpenSession| and |:DeleteSession| (but not |:SaveSession|) to support user
676+
friendly completion.
677+
678+
-------------------------------------------------------------------------------
679+
The *xolox#session#complete_names_with_suggestions()* function
680+
681+
Completion function for the Vim command |:SaveSession|.
658682

659683
-------------------------------------------------------------------------------
660684
The *xolox#session#is_tab_scoped()* function
@@ -702,6 +726,19 @@ The *xolox#session#restore_tab_options()* function
702726
Restore the original value of Vim's sessionoptions (see |'sessionoptions'|)
703727
option.
704728

729+
-------------------------------------------------------------------------------
730+
*example-function-for-session-name-suggestions*
731+
Example function for session name suggestions ~
732+
733+
-------------------------------------------------------------------------------
734+
The *xolox#session#suggestions#vcs_feature_branch()* function
735+
736+
This function implements an example of a function that can be used with the
737+
|g:session_name_suggestion_function| option. It finds the name of the current
738+
git or Mercurial feature branch (if any) and suggests this name as the name for
739+
the session that is being saved with :SaveSession. Returns a list with one
740+
string on success and an empty list on failure.
741+
705742
===============================================================================
706743
*session-contact*
707744
Contact ~

plugin/session.vim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ if !exists('g:session_persist_colors')
114114
let g:session_persist_colors = 1
115115
endif
116116

117+
" Enable user defined session name completion suggestions for :SaveSession.
118+
if !exists('g:session_name_suggestion_function')
119+
let g:session_name_suggestion_function = 'xolox#session#suggestions#vcs_feature_branch'
120+
endif
121+
117122
" Make sure the sessions directory exists and is writable. {{{1
118123

119124
let s:directory = fnamemodify(g:session_directory, ':p')
@@ -163,7 +168,7 @@ call xolox#misc#cursorhold#register({'function': 'xolox#session#auto_save_period
163168
" one or more tab pages).
164169
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names OpenSession call xolox#session#open_cmd(<q-args>, <q-bang>, 'OpenSession')
165170
command! -bar -nargs=? -complete=customlist,xolox#session#complete_names ViewSession call xolox#session#view_cmd(<q-args>)
166-
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names SaveSession call xolox#session#save_cmd(<q-args>, <q-bang>, 'SaveSession')
171+
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names_with_suggestions SaveSession call xolox#session#save_cmd(<q-args>, <q-bang>, 'SaveSession')
167172
command! -bar -bang -nargs=? -complete=customlist,xolox#session#complete_names DeleteSession call xolox#session#delete_cmd(<q-args>, <q-bang>)
168173
command! -bar -bang CloseSession call xolox#session#close_cmd(<q-bang>, 0, 1, 'CloseSession')
169174

0 commit comments

Comments
 (0)