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