Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions autoload/fuzzyy/files.vim
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ def Preview(wid: number, opts: dict<any>)
return
endif
var preview_bufnr = winbufnr(preview_wid)
var fileraw = readfile(path, '', 1000)
noautocmd call popup_settext(preview_wid, fileraw)
if selector.IsBinary(path)
noautocmd call popup_settext(preview_wid, 'Cannot preview binary file')
else
noautocmd call popup_settext(preview_wid, readfile(path, '', 1000))
win_execute(preview_wid, 'silent! doautocmd filetypedetect BufNewFile ' .. path)
endif
win_execute(preview_wid, 'norm gg')
win_execute(preview_wid, 'silent! doautocmd filetypedetect BufNewFile ' .. path)
noautocmd win_execute(preview_wid, 'silent! setlocal nospell nolist')
enddef

Expand Down
3 changes: 1 addition & 2 deletions autoload/fuzzyy/grep.vim
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ def Preview(wid: number, opts: dict<any>)

if path != last_path
var preview_bufnr = winbufnr(preview_wid)
var fileraw = readfile(path)
noautocmd call popup_settext(preview_wid, fileraw)
noautocmd call popup_settext(preview_wid, readfile(path))
win_execute(preview_wid, 'silent! doautocmd filetypedetect BufNewFile ' .. path)
noautocmd win_execute(preview_wid, 'silent! setlocal nospell nolist')
endif
Expand Down
9 changes: 6 additions & 3 deletions autoload/fuzzyy/mru.vim
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ def Preview(wid: number, opts: dict<any>)
return
endif
var preview_bufnr = winbufnr(preview_wid)
var fileraw = readfile(path)
noautocmd call popup_settext(preview_wid, fileraw)
if selector.IsBinary(path)
noautocmd call popup_settext(preview_wid, 'Cannot preview binary file')
else
noautocmd call popup_settext(preview_wid, readfile(path))
win_execute(preview_wid, 'silent! doautocmd filetypedetect BufNewFile ' .. path)
endif
win_execute(preview_wid, 'norm gg')
win_execute(preview_wid, 'silent! doautocmd filetypedetect BufNewFile ' .. path)
noautocmd win_execute(preview_wid, 'silent! setlocal nospell nolist')
enddef

Expand Down
12 changes: 12 additions & 0 deletions autoload/fuzzyy/utils/selector.vim
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ export def GetRootDir(): string
return getcwd()
enddef

export def IsBinary(path: string): bool
# NUL byte check for binary files, used to avoid showing preview
# Assumes a file encoding that does not allow NUL bytes, so will
# generate false positives for UTF-16 and UTF-32, but the preview
# window doesn't work for these encodings anyway, even with a BOM
var bytes = readblob(path, 0, 128)
for byte in bytes
if byte == 0 | return true | endif
endfor
return false
enddef

# Search pattern @pattern in a list of strings @li
# if pattern is empty, return [li, []]
# params:
Expand Down