Skip to content

Commit 11c9534

Browse files
committed
Internal refactoring: Replace s:select_name() with xolox#session#prompt_for_name()
1 parent f454460 commit 11c9534

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

autoload/xolox/session.vim

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

6-
let g:xolox#session#version = '2.4.2'
6+
let g:xolox#session#version = '2.4.3'
77

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

@@ -449,7 +449,10 @@ function! s:prompt(msg, choices, option_name)
449449
endfunction
450450

451451
function! xolox#session#open_cmd(name, bang, command) abort " {{{2
452-
let name = s:select_name(s:unescape(a:name), 'restore')
452+
let name = s:unescape(a:name)
453+
if empty(name)
454+
let name = xolox#session#prompt_for_name('restore')
455+
endif
453456
if name != ''
454457
let starttime = xolox#misc#timer#start()
455458
let path = xolox#session#name_to_path(name)
@@ -477,12 +480,15 @@ function! xolox#session#open_cmd(name, bang, command) abort " {{{2
477480
endfunction
478481

479482
function! xolox#session#view_cmd(name) abort " {{{2
483+
" Name of session given as command argument?
480484
let name = s:unescape(a:name)
485+
" Default to the current session?
481486
if empty(name)
482487
let name = xolox#session#find_current_session()
483488
endif
489+
" Prompt the user to select a session.
484490
if empty(name)
485-
let name = s:select_name('', 'view')
491+
let name = xolox#session#prompt_for_name('view')
486492
endif
487493
if name != ''
488494
let path = xolox#session#name_to_path(name)
@@ -533,7 +539,10 @@ function! xolox#session#save_cmd(name, bang, command) abort " {{{2
533539
endfunction
534540

535541
function! xolox#session#delete_cmd(name, bang) " {{{2
536-
let name = s:select_name(s:unescape(a:name), 'delete')
542+
let name = s:unescape(a:name)
543+
if empty(name)
544+
let name = xolox#session#prompt_for_name('delete')
545+
endif
537546
if name != ''
538547
let path = xolox#session#name_to_path(name)
539548
if !filereadable(path)
@@ -699,26 +708,43 @@ function! s:unescape(s) " {{{2
699708
return s
700709
endfunction
701710

702-
function! s:select_name(name, action) " {{{2
703-
if a:name != ''
704-
return a:name
705-
endif
706-
let sessions = sort(xolox#session#get_names())
707-
if empty(sessions)
708-
return g:session_default_name
709-
elseif len(sessions) == 1
711+
function! xolox#session#prompt_for_name(action) " {{{2
712+
" Prompt the user to select one of the existing sessions. The first argument
713+
" is expected to be a string describing what will be done to the session
714+
" once it's been selected. Returns the name of the selected session as a
715+
" string (if no session is selected an empty string is returned). Here's
716+
" an example of what the prompt looks like:
717+
"
718+
" :call xolox#session#prompt_for_name('trash')
719+
"
720+
" Please select the session to trash:
721+
"
722+
" 1. first-session
723+
" 2. second-session
724+
" 3. third-session
725+
"
726+
" Type number and <Enter> or click with mouse (empty cancels):
727+
"
728+
" If only a single session exists there's nothing to choose from so the name
729+
" of that session will be returned directly, without prompting the user.
730+
let sessions = sort(xolox#session#get_names(), 1)
731+
if len(sessions) == 1
710732
return sessions[0]
733+
elseif !empty(sessions)
734+
let lines = copy(sessions)
735+
for i in range(len(sessions))
736+
let lines[i] = ' ' . (i + 1) . '. ' . lines[i]
737+
endfor
738+
redraw
739+
sleep 100 m
740+
echo "\nPlease select the session to " . a:action . ":"
741+
sleep 100 m
742+
let i = inputlist([''] + lines + [''])
743+
if i >= 1 && i <= len(sessions)
744+
return sessions[i - 1]
745+
endif
711746
endif
712-
let lines = copy(sessions)
713-
for i in range(len(sessions))
714-
let lines[i] = ' ' . (i + 1) . '. ' . lines[i]
715-
endfor
716-
redraw
717-
sleep 100 m
718-
echo "\nPlease select the session to " . a:action . ":"
719-
sleep 100 m
720-
let i = inputlist([''] + lines + [''])
721-
return i >= 1 && i <= len(sessions) ? sessions[i - 1] : ''
747+
return ''
722748
endfunction
723749

724750
function! xolox#session#name_to_path(name) " {{{2

0 commit comments

Comments
 (0)