Skip to content

Commit 90b0592

Browse files
committed
Added check on filesize
1 parent ce29b3e commit 90b0592

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

lib/links.vim

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ enddef
5656

5757
def GetFileSize(filename: string): number
5858
var filesize = ''
59+
# TODO: Using system() slow down significantly the opening and the file preview
5960
if filereadable(filename)
6061
if has('win32')
6162
filesize = system('powershell -NoProfile -ExecutionPolicy Bypass -Command '
6263
.. $'"(Get-Item \"{filename}\").length"')
64+
elseif has('unix') && system('uname') =~ 'Darwin'
65+
filesize = system($'stat -f %z {escape(filename, " ")}')
66+
elseif has('unix')
67+
filesize = system($'stat --format=%s {escape(filename, " ")}')
6368
else
64-
filesize = system($'stat --format=%s {filename}')
69+
utils.Echowarn($"Cannot determine the size of {filename}")
70+
filesize = "-1"
6571
endif
6672
else
6773
utils.Echoerr($"File {filename} is not readable")
@@ -614,13 +620,38 @@ export def PreviewPopup()
614620
if !empty(IsLink())
615621
# Search from the current cursor position to the end of line
616622
var saved_curpos = getcurpos()
617-
norm! f[l
618-
var link_id = utils.GetTextObject('i[').text
619-
var link_name = b:markdown_extras_links[link_id]
623+
624+
# Find the closest between [ and (
625+
var symbol = ''
626+
if searchpos('[', 'nW') == [0, 0]
627+
symbol = '('
628+
elseif searchpos('(', 'nW') == [0, 0]
629+
symbol = '['
630+
else
631+
symbol = utils.IsLess(searchpos('[', 'nW'), searchpos('(', 'nW'))
632+
? '['
633+
: '('
634+
endif
635+
636+
exe $"norm! f{symbol}l"
637+
638+
var link_name = ''
639+
var link_id = ''
640+
if symbol == '['
641+
b:markdown_extras_links = RefreshLinksDict()
642+
link_id = utils.GetTextObject('i[').text
643+
link_name = b:markdown_extras_links[link_id]
644+
else
645+
link_name = utils.GetTextObject('i(').text
646+
endif
647+
648+
#
649+
#
620650
# TODO At the moment only .md files have syntax highlight.
621651
var refFiletype = $'{fnamemodify(link_name, ":e")}' == 'md'
622652
? 'markdown'
623653
: 'text'
654+
# echom GetFileSize(link_name)
624655
if IsURL(link_name)
625656
|| (filereadable(link_name) && GetFileSize(link_name) > 1000000)
626657
previewText = [link_name]

lib/utils.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ vim9script
33
import autoload "./constants.vim"
44

55
export def Echoerr(msg: string)
6-
echohl ErrorMsg | echo $'[markdown_extras] {msg}' | echohl None
6+
echohl ErrorMsg | echom $'[markdown_extras] {msg}' | echohl None
77
enddef
88

99
export def Echowarn(msg: string)
10-
echohl WarningMsg | echo $'[markdown_extras] {msg}' | echohl None
10+
echohl WarningMsg | echom $'[markdown_extras] {msg}' | echohl None
1111
enddef
1212

1313
export def FormatWithoutMoving(a: number = 0, b: number = 0)

0 commit comments

Comments
 (0)