Skip to content

Commit b468f7a

Browse files
committed
:MDEIndices -> :MDEIndex
1 parent 8e0ebad commit b468f7a

File tree

5 files changed

+93
-35
lines changed

5 files changed

+93
-35
lines changed

autoload/mde_indices.vim

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,58 @@ vim9script
33
import autoload "./mde_links.vim" as links
44
import autoload "./mde_utils.vim" as utils
55

6+
var indices: any
67
var indices_id = -1
78

89
def 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
2037
enddef
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
5187
enddef

doc/markdown_extras.txt

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,8 @@ You could for example set the following:
162162
INDICES
163163

164164
As the plugin can be used for note-taking, it may be desirable to access
165-
different indices in an ergonomic way. This can be achieved with the
166-
|g:markdown_extras_indices| list in combination with the |:MDEIndices|
167-
command. Note that `g:markdown_extras_indices` is nothing, but a list (or a dict)
168-
of files. If it is a dict, then the keys is what is displayed in the popup
169-
menu and the values are the corresponding links.
165+
different indices in an ergonomic way. This can be achieved with the |:MDEIndex|
166+
command.
170167

171168
==============================================================================
172169
CONFIGURATION *markdown-extras-configuration* *g:markdown_extras_config*
@@ -223,8 +220,11 @@ filling it with he various keys.
223220

224221

225222
*g:markdown_extras_indices*
226-
g:markdown_extras_indices list<string> or dict<string>.
223+
g:markdown_extras_indices list<string>, list<list<string>> or dict<string>.
227224
If list<string>, contain the path of indices files.
225+
If list<string<string>> the first element of each
226+
item of the list is the alias, whereas the second
227+
is the link.
228228
If dict<string>, the values are the actual links
229229
whereas the keys are aliases. In the popup menu only
230230
the alias are displayed.
@@ -250,9 +250,27 @@ COMMANDS *markdown-extras-commands*
250250
*:MDEConvertLinks*
251251
:MDEConvertLinks Convert inline links into reference-style links.
252252

253-
*:MDEIndices*
254-
:MDEIndices Open a popup with all the filenames included in
255-
|g:markdown_extras_indices|.
253+
*:MDEIndex*
254+
:MDEIndex [{args}] Open a popup with all the URLs specified in {args}.
255+
The type of {args} can be list<string>, list<list<string>>
256+
or dict<string>. If no argument is passed, then
257+
|g:markdown_extras_indices| is considered.
258+
Example:
259+
>
260+
:MDEIndex # Uses g:markdown_extras_indices
261+
:MDEIndex ['path1', 'path2', 'path3]
262+
:MDEIndex {foo: 'path1', bar: 'path2', baz: 'path3}
263+
:MDEIndex [['foo', 'path1'], ['bar', 'path2'], ['baz', 'path3]]
264+
<
265+
Or you can use in a script, like for example:
266+
>
267+
const my_index = {
268+
foo: '/Users/ubaldot/home/Documents/foo.vim',
269+
bar: 'https://example.com',
270+
banana: '/mnt/c/files/bar.md'
271+
}
272+
execute $"MDEIndex {my_index}"
273+
<
256274

257275
*:MDEMake*
258276
:MDEMake Invoke |:make| and |:Open| the rendered file. It requires

doc/tags

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:MDEConvertLinks markdown_extras.txt /*:MDEConvertLinks*
2-
:MDEIndices markdown_extras.txt /*:MDEIndices*
2+
:MDEIndex markdown_extras.txt /*:MDEIndex*
33
:MDEMake markdown_extras.txt /*:MDEMake*
44
g:markdown_extras_config markdown_extras.txt /*g:markdown_extras_config*
55
g:markdown_extras_indices markdown_extras.txt /*g:markdown_extras_indices*

ftplugin/markdown.vim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ endfor
2222

2323
# Convert links inline links [mylink](blabla) to referenced links [mylink][3]
2424
command! -buffer -nargs=0 MDEConvertLinks links.ConvertLinks()
25-
command! -buffer -nargs=0 MDEIndices indices.ShowIndices()
2625

2726
# Jump back to the previous file
2827
nnoremap <buffer> <backspace> <ScriptCmd>funcs.GoToPrevVisitedBuffer()<cr>

plugin/markdown_extras.vim

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ vim9script noclear
66

77
import autoload './../autoload/mde_utils.vim' as utils
88
import autoload './../autoload/mde_funcs.vim' as funcs
9+
import autoload './../autoload/mde_indices.vim' as indices
910

1011
if has('win32') && !has("patch-9.1.1270")
1112
# Needs Vim version 9.0 and above
@@ -23,10 +24,8 @@ endif
2324
var release_notes =<< FOO
2425
## vim-markdown-extras: release notes
2526

26-
Links must have a valid URL format to keep consistency with
27-
the markdown requirements.
28-
29-
Hence, the following link:
27+
1. Links must have a valid URL format to keep consistency with
28+
the markdown requirements. Hence, the following link:
3029

3130
[1]: C:\User\John\My Documents\foo bar.txt
3231

@@ -40,6 +39,11 @@ Please, update the links in your markdown files.
4039
You can ask any LLM to convert the links for you.
4140
Typically, they are quite accurate.
4241

42+
2. `:MDEIndices` has been renamed to `:MDEIndex` and can take an argument, for
43+
example you can call as `:MDEIndices ['apple', 'banana', 'strawberry']`.
44+
Furthermore, such a command is no longer valid only for markdown files but
45+
it become global.
46+
4347
Press <Esc> to close this popup.
4448
FOO
4549

@@ -56,8 +60,6 @@ def ShowReleaseNotes()
5660
win_execute(popup_id, 'set conceallevel=2')
5761
enddef
5862

59-
command! -nargs=0 MDEReleaseNotes ShowReleaseNotes()
60-
6163
augroup MARKDOWN_EXTRAS_VISITED_BUFFERS
6264
autocmd!
6365
autocmd BufEnter * {
@@ -120,4 +122,7 @@ if use_pandoc && !executable('pandoc')
120122
endif
121123
endif
122124

123-
g:markdown_extras_loaded = true
125+
command! -nargs=0 MDEReleaseNotes ShowReleaseNotes()
126+
command! -nargs=? MDEIndex indices.ShowIndices(<f-args>)
127+
128+
g:markdown_extras_loaded = false

0 commit comments

Comments
 (0)