File: tlib_input_list.vim

package info (click to toggle)
vim-tlib 1.28-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 752 kB
  • sloc: ruby: 61; makefile: 5
file content (50 lines) | stat: -rw-r--r-- 1,817 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
" The following variable configures the way |tlib#input#ListD()| works. 
" In this example, we allow selection of multiple items (we could also 
" allow only a single choice and make |tlib#input#ListD()| work on the 
" indices, not the items).
"
" We also set a prompt that will be displayed in the command area.
"
" By default, |tlib#input#ListD()| will automatically select an item if 
" there is only one item left matching the filter. In this example, we 
" disable this feature.
"
" For demonstration purposes, we also define a key handler that prints 
" the selected items.
let s:state = {
            \ 'type': 'm',
            \ 'query': 'Select lines for command output',
            \ 'pick_last_item': 0,
            \ 'key_handlers': [
                \ {'key': 16, 'agent': 'PrintMe', 'key_name': '<c-p>', 'help': 'Print line'},
            \ ],
            \ }

" A key handler takes two arguments: the current state of the list 
" display and a list of selected items/indices (depending on the type 
" parameter).
function! PrintMe(state, items) "{{{3
    echom "You selected:"
    for i in a:items
        echom i
    endfor
    call input("Press ENTER to continue")
    let a:state.state = 'redisplay'
    return a:state
endf

" In this example, we evaluate an ex-command with |:execute| and display 
" the command's output as list. The user can select certain lines by 
" typing some pattern or by pressing <a-NUMBER> to select an item by 
" number. The user can then press <c-p> to print the lines (see above) 
" or <cr> to pick the selected lines.
function! SelectOutput(ex) "{{{3
    redir => lines
    silent exec a:ex
    redir END
    let state = copy(s:state)
    let state.base = split(lines, '\n')
    let picked = tlib#input#ListD(state)
    echom "You picked: ". join(picked, ', ')
endf