@@ -54,6 +54,22 @@ def InitScriptLocalVars()
5454 }
5555enddef
5656
57+ def GetFileSize (filename: string ): number
58+ var filesize = ' '
59+ if filereadable (filename)
60+ if has (' win32' )
61+ filesize = system (' powershell -NoProfile -ExecutionPolicy Bypass -Command '
62+ .. $ ' "(Get-Item \"{filename}\").length"' )
63+ else
64+ filesize = system ($ ' stat --format=%s {filename}' )
65+ endif
66+ else
67+ utils.Echoerr ($ " File {filename} is not readable" )
68+ filesize = " -1"
69+ endif
70+ return filesize- >substitute (' \n' , ' ' , ' g' )- >str2nr ()
71+ enddef
72+
5773export def RefreshLinksDict (): dict <string>
5874 # Generate the b: markdown_extras_links by parsing the # References section,
5975 # but it requires that there is a # Reference section at the end
@@ -199,14 +215,18 @@ def IsBinary(link: string): bool
199215 # Check if a file is binary
200216 var is_binary = false
201217
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
218+ # Override if binary and not too large
219+ if filereadable (link)
220+ # Large file : open in a new Vim instance if
221+ if executable (' file' ) && system ($ ' file --brief --mime {link}' ) !~ ' ^text/'
222+ is_binary = true
223+ # In case ' file' is not available, like in Windows, search for the NULL
224+ # byte. Guard if the file is too large
225+ elseif ! empty (readfile (link)- >filter (' v:val =~# "\\%u0000"' ))
226+ is_binary = true
227+ endif
228+ else
229+ utils.Echoerr ($ " File {link} is not readable" )
210230 endif
211231
212232 return is_binary
@@ -237,7 +257,10 @@ export def OpenLink()
237257 link = utils.GetTextObject (' i(' ).text
238258 endif
239259
240- if ! IsBinary (link)
260+ # TODO : files less than 1 MB are opened in the same Vim instance
261+ if ! IsURL (link)
262+ && (0 < GetFileSize (link) && GetFileSize (link) < 1000000 )
263+ && ! IsBinary (link)
241264 exe $ ' edit {link}'
242265 else
243266 exe $ " :Open {link}"
@@ -599,6 +622,7 @@ export def PreviewPopup()
599622 ? ' markdown'
600623 : ' text'
601624 if IsURL (link_name)
625+ || (filereadable (link_name) && GetFileSize (link_name) > 1000000 )
602626 previewText = [link_name]
603627 refFiletype = ' text'
604628 else
0 commit comments