@@ -3,26 +3,58 @@ vim9script
33import autoload " ./mde_links.vim" as links
44import autoload " ./mde_utils.vim" as utils
55
6+ var indices: any
67var indices_id = -1
78
89def IndicesCallback (id: number , idx: number )
910 if idx > 0
1011
11- if typename (g: markdown_extras_indices ) == " list<string>"
12- var selection = getbufline (winbufnr (id), idx)[0 ]
13- exe $ ' edit {selection}'
14- elseif typename (g: markdown_extras_indices ) == " dict<string>"
15- var selection = getbufline (winbufnr (id), idx)[0 ]
16- exe $ ' edit {g:markdown_extras_indices[selection]}'
12+ var selection = ' '
13+ if typename (indices) == " list<string>"
14+ selection = getbufline (winbufnr (id), idx)[0 ]
15+ elseif typename (indices) == " list<list<string>>"
16+ selection = getbufline (winbufnr (id), idx)[0 ]
17+ var indices_names = indices- >mapnew ((_, val) = > val[0 ])
18+ var ii = index (indices_names, selection )
19+ selection = indices[ii][1 ]
20+ elseif typename (indices) == " dict<string>"
21+ var selection_key = getbufline (winbufnr (id), idx)[0 ]
22+ selection = indices[selection_key]
1723 endif
24+
25+ if ! empty (selection )
26+ if links.IsURL (selection ) && selection = ~ ' ^file://'
27+ exe $ ' edit {fnameescape(links.URLToPath(selection))}'
28+ elseif links.IsURL (selection )
29+ exe $ ' Open {selection}'
30+ else
31+ exe $ ' edit {fnameescape(selection)}'
32+ endif
33+ endif
34+
1835 indices_id = -1
1936 endif
2037enddef
2138
22- export def ShowIndices ()
23- if exists (' g:markdown_extras_indices' ) != 0 && ! empty (' g:markdown_extras_indices' )
39+ export def ShowIndices (passed_indices: string = ' ' )
40+ var indices_found = false
41+ echom typename (passed_indices)
42+
43+ if ! empty (passed_indices)
44+ # TODO : remove the eval () with something better
45+ indices = eval (passed_indices)
46+ indices_found = true
47+ elseif exists (' g:markdown_extras_indices' ) != 0
48+ && ! empty (' g:markdown_extras_indices' )
49+ indices = g: markdown_extras_indices
50+ indices_found = true
51+ else
52+ utils.Echoerr (" Cannot find indices" )
53+ endif
54+
55+ if indices_found
2456 const popup_width = (&columns * 2 ) / 3
25- const popup_height = min ([len (g: markdown_extras_indices ), &lines / 2 ])
57+ const popup_height = min ([len (indices ), &lines / 2 ])
2658 var opts = {
2759 pos: ' center' ,
2860 border: [1 , 1 , 1 , 1 ],
@@ -38,14 +70,18 @@ export def ShowIndices()
3870 wrap : 0 ,
3971 drag: 0 ,
4072 }
41- if typename (g: markdown_extras_indices ) == " list<string>"
42- indices_id = popup_create (g: markdown_extras_indices , opts)
43- links.ShowPromptPopup (indices_id, g: markdown_extras_indices , " indices: " )
44- elseif typename (g: markdown_extras_indices ) == " dict<string>"
45- indices_id = popup_create (keys (g: markdown_extras_indices ), opts)
46- links.ShowPromptPopup (indices_id, keys (g: markdown_extras_indices ), " indices: " )
73+ if typename (indices) == " list<string>"
74+ indices_id = popup_create (indices, opts)
75+ links.ShowPromptPopup (indices_id, indices, " indices: " )
76+ elseif typename (indices) == " list<list<string>>"
77+ var indices_names = indices- >mapnew ((_, val) = > val[0 ])
78+ indices_id = popup_create (indices_names, opts)
79+ links.ShowPromptPopup (indices_id, indices_names, " indices: " )
80+ elseif typename (indices) == " dict<string>"
81+ indices_id = popup_create (keys (indices), opts)
82+ links.ShowPromptPopup (indices_id, keys (indices), " indices: " )
83+ else
84+ utils.Echoerr ($ " Wrong argument type to ':MDEIndex' ({typename(indices)})" )
4785 endif
48- else
49- utils.Echoerr (" 'g:markdown_extras_indices' not set" )
5086 endif
5187enddef
0 commit comments