~gpanders/dotfiles

ref: 5629911cfe9a2fc4e566bab14373aec451244000 dotfiles/vim/.vim/autoload/fzy.vim -rw-r--r-- 1.5 KiB
5629911cGreg Anders Fix bash syntax error in 'note' command 1 year, 9 months ago
                                                                                
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
function! s:completed(winid, filename, action, ...) abort
    bdelete!
    call win_gotoid(a:winid)
    if filereadable(a:filename)
        let lines = readfile(a:filename)
        if !empty(lines)
            if type(a:action) ==# type('')
                exe a:action . ' ' . lines[0]
            elseif type(a:action) ==# type({->0})
                call a:action(lines[0])
            endif
        endif
        call delete(a:filename)
    endif
endfunction

function! s:tags(line) abort
    let [tag, _, filename] = split(a:line)[0:2]
    let index = index(map(taglist('^' . tag . '$'), 'v:val.filename'), filename) + 1
    execute index . 'tag ' . tag
endfunction

function! s:fzy(cmd, action, title) abort
    let file = tempname()
    let winid = win_getid()
    let cmd = split(&shell) + split(&shellcmdflag) + [a:cmd . ' | fzy > ' . file]
    let F = function('s:completed', [winid, file, a:action])
    botright 10 new
    if has('nvim')
        call termopen(cmd, {'on_exit': F})
    else
        call term_start(cmd, {'exit_cb': F, 'curwin': 1, 'term_kill': 'quit'})
    endif
    exe 'file ' . a:title
    startinsert
endfunction

function! fzy#files()
    call s:fzy(get(b:, 'fzy_find_files_cmd', g:fzy_find_files_cmd), 'e', 'files')
endfunction

function! fzy#tags()
    let tags = map(taglist('.'), {_, v -> printf('%-40s %-12s %s', v.name, v.kind, fnamemodify(v.filename, ':.'))})
    let file = tempname()
    call writefile(tags, file)
    call s:fzy('cat ' . file, {t -> s:tags(t) && delete(file)}, 'tags')
endfunction