rebase on 2015
[Vimrc.git] / bundle / tlib_vim-1.12 / samples / tlib / input / tlib_input_list.vim
blob4d668ffaf15958df719cc8744f41914bf0405882
1 " The following variable configures the way |tlib#input#ListD()| works. 
2 " In this example, we allow selection of multiple items (we could also 
3 " allow only a single choice and make |tlib#input#ListD()| work on the 
4 " indices, not the items).
6 " We also set a prompt that will be displayed in the command area.
8 " By default, |tlib#input#ListD()| will automatically select an item if 
9 " there is only one item left matching the filter. In this example, we 
10 " disable this feature.
12 " For demonstration purposes, we also define a key handler that prints 
13 " the selected items.
14 let s:state = {
15             \ 'type': 'm',
16             \ 'query': 'Select lines for command output',
17             \ 'pick_last_item': 0,
18             \ 'key_handlers': [
19                 \ {'key': 16, 'agent': 'PrintMe', 'key_name': '<c-p>', 'help': 'Print line'},
20             \ ],
21             \ }
23 " A key handler takes two arguments: the current state of the list 
24 " display and a list of selected items/indices (depending on the type 
25 " parameter).
26 function! PrintMe(state, items) "{{{3
27     echom "You selected:"
28     for i in a:items
29         echom i
30     endfor
31     call input("Press ENTER to continue")
32     let a:state.state = 'redisplay'
33     return a:state
34 endf
36 " In this example, we evaluate an ex-command with |:execute| and display 
37 " the command's output as list. The user can select certain lines by 
38 " typing some pattern or by pressing <a-NUMBER> to select an item by 
39 " number. The user can then press <c-p> to print the lines (see above) 
40 " or <cr> to pick the selected lines.
41 function! SelectOutput(ex) "{{{3
42     redir => lines
43     silent exec a:ex
44     redir END
45     let state = copy(s:state)
46     let state.base = split(lines, '\n')
47     let picked = tlib#input#ListD(state)
48     echom "You picked: ". join(picked, ', ')
49 endf