|
8 | 8 | " NOTE: junegunn/fzf.vim is required |
9 | 9 |
|
10 | 10 | let s:snippet = 'snippet' |
| 11 | +let s:width = 0.9 |
| 12 | +let s:height = 0.6 |
11 | 13 |
|
12 | | -function! s:SplitID(row) |
13 | | - return split(a:row, ' ')[0] |
14 | | -endfunction |
15 | | - |
16 | | -" TODO: extra opts |
17 | | -function! g:SphinxNotesSnippetList(callback, tags) |
18 | | - let l:width = 0.9 |
| 14 | +" Use fzf to list all snippets, callback with argument id. |
| 15 | +function g:SphinxNotesSnippetList(tags, callback) |
19 | 16 | let cmd = [s:snippet, 'list', |
20 | 17 | \ '--tags', a:tags, |
21 | | - \ '--width', float2nr(&columns * l:width) - 2, |
| 18 | + \ '--width', float2nr(&columns * s:width) - 2, |
22 | 19 | \ ] |
| 20 | + |
| 21 | + " Use closure keyword so that inner function can access outer one's |
| 22 | + " localvars (l:) and arguments (a:). |
| 23 | + " https://vi.stackexchange.com/a/21807 |
| 24 | + function! List_CB(selection) closure |
| 25 | + let id = split(a:selection, ' ')[0] |
| 26 | + call a:callback(id) |
| 27 | + endfunction |
| 28 | + |
23 | 29 | " https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzfrun |
24 | 30 | call fzf#run({ |
25 | 31 | \ 'source': join(cmd, ' '), |
26 | | - \ 'sink': a:callback, |
| 32 | + \ 'sink': function('List_CB'), |
27 | 33 | \ 'options': ['--with-nth', '2..', '--no-hscroll', '--header-lines', '1'], |
28 | | - \ 'window': {'width': l:width, 'height': 0.6}, |
| 34 | + \ 'window': {'width': s:width, 'height': s:height}, |
29 | 35 | \ }) |
30 | 36 | endfunction |
31 | 37 |
|
32 | | -" vim: set shiftwidth=2: |
| 38 | +" Return the attribute value of specific snippet. |
| 39 | +function g:SphinxNotesSnippetGet(id, attr) |
| 40 | + let cmd = [s:snippet, 'get', a:id, '--' . a:attr] |
| 41 | + return systemlist(join(cmd, ' ')) |
| 42 | +endfunction |
| 43 | + |
| 44 | +" Use fzf to list all attr of specific snippet, |
| 45 | +" callback with arguments (attr_name, attr_value). |
| 46 | +function g:SphinxNotesSnippetListSnippetAttrs(id, callback) |
| 47 | + " Display attr -> Identify attr (also used as CLI option) |
| 48 | + let attrs = { |
| 49 | + \ 'Source': 'src', |
| 50 | + \ 'URL': 'url', |
| 51 | + \ 'Docname': 'docname', |
| 52 | + \ 'Dependent files': 'deps', |
| 53 | + \ 'Text': 'text', |
| 54 | + \ 'Title': 'title', |
| 55 | + \ } |
| 56 | + let delim = ' ' |
| 57 | + let table = ['OPTION' . delim . 'ATTRIBUTE'] |
| 58 | + for name in keys(attrs) |
| 59 | + call add(table, attrs[name] . delim . name) |
| 60 | + endfor |
| 61 | + |
| 62 | + function! ListSnippetAttrs_CB(selection) closure |
| 63 | + let opt = split(a:selection, ' ')[0] |
| 64 | + let val = g:SphinxNotesSnippetGet(a:id, opt) |
| 65 | + call a:callback(opt, val) " finally call user's cb |
| 66 | + endfunction |
| 67 | + |
| 68 | + let preview_cmd = [s:snippet, 'get', a:id, '--$(echo {} | cut -d " " -f1)'] |
| 69 | + let info_cmd = ['echo', 'Index ID:', a:id] |
| 70 | + call fzf#run({ |
| 71 | + \ 'source': table, |
| 72 | + \ 'sink': function('ListSnippetAttrs_CB'), |
| 73 | + \ 'options': [ |
| 74 | + \ '--header-lines', '1', |
| 75 | + \ '--with-nth', '2..', |
| 76 | + \ '--preview', join(preview_cmd, ' '), |
| 77 | + \ '--preview-window', ',wrap', |
| 78 | + \ '--info-command', join(info_cmd, ' '), |
| 79 | + \ ], |
| 80 | + \ 'window': {'width': s:width, 'height': s:height}, |
| 81 | + \ }) |
| 82 | +endfunction |
| 83 | + |
| 84 | +function g:SphinxNotesSnippetInput(id) |
| 85 | + function! Input_CB(attr, val) " TODO: became g:func. |
| 86 | + if a:attr == 'docname' |
| 87 | + " Create doc reference. |
| 88 | + let content = ':doc:`/' . a:val[0] . '`' |
| 89 | + elseif a:attr == 'title' |
| 90 | + " Create local section reference. |
| 91 | + let content = '`' . a:val[0] . '`_' |
| 92 | + else |
| 93 | + let content = join(a:val, '<CR>') |
| 94 | + endif |
| 95 | + execute 'normal! i' . content |
| 96 | + endfunction |
| 97 | + |
| 98 | + call g:SphinxNotesSnippetListSnippetAttrs(a:id, function('Input_CB')) |
| 99 | +endfunction |
| 100 | + |
| 101 | +function g:SphinxNotesSnippetListAndInput() |
| 102 | + function! ListAndInput_CB(id) |
| 103 | + call g:SphinxNotesSnippetInput(a:id) |
| 104 | + endfunction |
| 105 | + |
| 106 | + call g:SphinxNotesSnippetList('"*"', function('ListAndInput_CB')) |
| 107 | +endfunction |
| 108 | + |
| 109 | + " vim: set shiftwidth=2: |
0 commit comments