@@ -11,35 +11,39 @@ let s:snippet = 'snippet'
1111let s: width = 0.9
1212let s: height = 0.6
1313
14- " TODO: remove
15- function ! s: SplitID (row)
16- return split (a: row , ' ' )[0 ]
17- endfunction
18-
19- " Use fzf to list all snippets, callback with arguments (selection).
20- function ! g: SphinxNotesSnippetList (callback, tags )
14+ " Use fzf to list all snippets, callback with argument id.
15+ function g: SphinxNotesSnippetList (tags , callback)
2116 let cmd = [s: snippet , ' list' ,
2217 \ ' --tags' , a: tags ,
2318 \ ' --width' , float2nr (&columns * s: width ) - 2 ,
2419 \ ]
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+
2529 " https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzfrun
2630 call fzf#run ({
2731 \ ' source' : join (cmd, ' ' ),
28- \ ' sink' : a: callback ,
32+ \ ' sink' : function ( ' List_CB ' ) ,
2933 \ ' options' : [' --with-nth' , ' 2..' , ' --no-hscroll' , ' --header-lines' , ' 1' ],
3034 \ ' window' : {' width' : s: width , ' height' : s: height },
3135 \ })
3236endfunction
3337
3438" Return the attribute value of specific snippet.
35- function ! g: SphinxNotesSnippetGet (id, attr)
39+ function g: SphinxNotesSnippetGet (id, attr)
3640 let cmd = [s: snippet , ' get' , a: id , ' --' . a: attr ]
3741 return systemlist (join (cmd, ' ' ))
3842endfunction
3943
4044" Use fzf to list all attr of specific snippet,
4145" callback with arguments (attr_name, attr_value).
42- function ! g: SphinxNotesSnippetListSnippetAttrs (id, callback)
46+ function g: SphinxNotesSnippetListSnippetAttrs (id, callback)
4347 " Display attr -> Identify attr (also used as CLI option)
4448 let attrs = {
4549 \ ' Source' : ' src' ,
@@ -55,20 +59,17 @@ function! g:SphinxNotesSnippetListSnippetAttrs(id, callback)
5559 call add (table, attrs[name] . delim . name)
5660 endfor
5761
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+ function ! ListSnippetAttrs_CB (selection ) closure
6263 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
64+ let val = g: SphinxNotesSnippetGet (a: id , opt )
65+ call a: callback (opt , val) " finally call user's cb
6566 endfunction
6667
6768 let preview_cmd = [s: snippet , ' get' , a: id , ' --$(echo {} | cut -d " " -f1)' ]
6869 let info_cmd = [' echo' , ' Index ID:' , a: id ]
6970 call fzf#run ({
7071 \ ' source' : table,
71- \ ' sink' : function (' s:SphinxNotesSnippetListSnippetAttrs_CB ' ),
72+ \ ' sink' : function (' ListSnippetAttrs_CB ' ),
7273 \ ' options' : [
7374 \ ' --header-lines' , ' 1' ,
7475 \ ' --with-nth' , ' 2..' ,
@@ -80,8 +81,8 @@ function! g:SphinxNotesSnippetListSnippetAttrs(id, callback)
8081 \ })
8182endfunction
8283
83- function ! g: SphinxNotesSnippetInput (id)
84- function ! s: SphinxNotesSnippetInput_CB (attr, val)
84+ function g: SphinxNotesSnippetInput (id)
85+ function ! Input_CB (attr, val) " TODO: became g:func.
8586 if a: attr == ' docname'
8687 " Create doc reference.
8788 let content = ' :doc:`/' . a: val [0 ] . ' `'
@@ -91,20 +92,18 @@ function! g:SphinxNotesSnippetInput(id)
9192 else
9293 let content = join (a: val , ' <CR>' )
9394 endif
94-
9595 execute ' normal! i' . content
9696 endfunction
9797
98- call g: SphinxNotesSnippetListSnippetAttrs (a: id , function (' s:SphinxNotesSnippetInput_CB ' ))
98+ call g: SphinxNotesSnippetListSnippetAttrs (a: id , function (' Input_CB ' ))
9999endfunction
100100
101- function ! g: SphinxNotesSnippetListAndInput ()
102- function ! s: SphinxNotesSnippetListAndInput_CB (selection )
103- let id = s: SplitID (a: selection )
104- call g: SphinxNotesSnippetInput (id)
101+ function g: SphinxNotesSnippetListAndInput ()
102+ function ! ListAndInput_CB (id)
103+ call g: SphinxNotesSnippetInput (a: id )
105104 endfunction
106105
107- call g: SphinxNotesSnippetList (function ( ' s:SphinxNotesSnippetListAndInput_CB ' ), ' "*" ' )
106+ call g: SphinxNotesSnippetList (' "*" ' , function ( ' ListAndInput_CB ' ) )
108107endfunction
109108
110109 " vim: set shiftwidth = 2 :
0 commit comments