@@ -195,17 +195,49 @@ export def IsLink(): dict<list<list<number>>>
195195 endif
196196enddef
197197
198+ def IsBinary (link: string ): bool
199+ # Check if a file is binary
200+ var is_binary = false
201+
202+ # Override if binary
203+ if executable (' file' ) && system ($ ' file --brief --mime {link}' ) !~ ' ^text/'
204+ is_binary = true
205+ # In case ' file' is not available, like in Windows, search for the NULL
206+ # byte
207+ elseif filereadable (link)
208+ && ! empty (readfile (link)- >filter (' v:val =~# "\\%u0000"' ))
209+ is_binary = true
210+ endif
211+
212+ return is_binary
213+ enddef
214+
198215export def OpenLink ()
199- norm! f [l
200- # Only work for [blabla][]
201- # var link_id = xxx- >matchstr (' \[*\]\s*\[\zs\d\+\ze' )
202-
203- b: markdown_extras_links = RefreshLinksDict ()
204- const link_id = utils.GetTextObject (' i[' ).text
205- const link = b: markdown_extras_links [link_id]
206- # TODO : filereadable () if not good if for example you have a .png it is
207- # still readable
208- if filereadable (link)
216+
217+ # Get link name depending of reference- style or inline link
218+ var symbol = ' '
219+ if searchpos (' [' , ' nW' ) == [0 , 0 ]
220+ symbol = ' ('
221+ elseif searchpos (' (' , ' nW' ) == [0 , 0 ]
222+ symbol = ' ['
223+ else
224+ symbol = utils.IsLess (searchpos (' [' , ' nW' ), searchpos (' (' , ' nW' ))
225+ ? ' ['
226+ : ' ('
227+ endif
228+
229+ exe $ " norm! f{symbol}l"
230+
231+ var link = ' '
232+ if symbol == ' ['
233+ b: markdown_extras_links = RefreshLinksDict ()
234+ const link_id = utils.GetTextObject (' i[' ).text
235+ link = b: markdown_extras_links [link_id]
236+ else
237+ link = utils.GetTextObject (' i(' ).text
238+ endif
239+
240+ if ! IsBinary (link)
209241 exe $ ' edit {link}'
210242 else
211243 exe $ " :Open {link}"
@@ -561,7 +593,6 @@ export def PreviewPopup()
561593 var saved_curpos = getcurpos ()
562594 norm! f [l
563595 var link_id = utils.GetTextObject (' i[' ).text
564- echom link_id
565596 var link_name = b: markdown_extras_links [link_id]
566597 # TODO At the moment only .md files have syntax highlight .
567598 var refFiletype = $ ' {fnamemodify(link_name, ":e")}' == ' md'
0 commit comments