aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2019-05-25 17:22:08 +1200
committerTom Ryder <tom@sanctum.geek.nz>2019-05-25 17:22:19 +1200
commitb9bfb4df7fac946f52fe10c768e7d34dde160dfc (patch)
treeb53c03b9bd8d50f40ed7b83fe54bf5c55a1cdf95
parentUse get() in lieu of verbose pre-7.0 exists() (diff)
downloadvim-big-file-options-b9bfb4df7fac946f52fe10c768e7d34dde160dfc.tar.gz
vim-big-file-options-b9bfb4df7fac946f52fe10c768e7d34dde160dfc.zip
Reorder functions; put interfaces on the top
-rw-r--r--autoload/big_file_options.vim62
1 files changed, 56 insertions, 6 deletions
diff --git a/autoload/big_file_options.vim b/autoload/big_file_options.vim
index f8b0671..e167dc2 100644
--- a/autoload/big_file_options.vim
+++ b/autoload/big_file_options.vim
@@ -1,9 +1,3 @@
-" Wrapper function to get the configured size limit, default to 10 MiB
-function! s:Limit()
- let limit = get(g:, 'big_file_options_limit', 10 * 1024 * 1024)
- return limit
-endfunction
-
" If we can use filesize to detect the big file early, we should
function! big_file_options#CheckPre(filename)
@@ -54,6 +48,12 @@ function! big_file_options#CheckPost()
endfunction
+" Wrapper function to get the configured size limit, default to 10 MiB
+function! s:Limit()
+ let limit = get(g:, 'big_file_options_limit', 10 * 1024 * 1024)
+ return limit
+endfunction
+
" These options can and should be set as early as possible
function! s:SetPre()
@@ -102,3 +102,53 @@ function! s:SetPost()
echomsg 'Big file detected, set appropriate options'
endfunction
+
+" If we can use filesize to detect the big file early, we should
+function! big_file_options#CheckPre(filename)
+
+ " Try and get filesize, bail out if we can't
+ let size = getfsize(a:filename)
+ if size == -1
+ return
+ endif
+
+ " Set the buffer's big flag to whether the file is verifiably outsize
+ let b:big_file_options_big = size == -2 || size > s:Limit()
+
+ " If we found it's a big file, call the early options set
+ if b:big_file_options_big
+ call s:SetPre()
+ endif
+
+endfunction
+
+" If it's still indeterminate (stdin read?), try to check the buffer size
+" itself
+function! big_file_options#CheckPost()
+
+ " The BufReadPre hook couldn't tell how big the file was; we'll examine it
+ " now it's loaded into the buffer instead
+ if !exists('b:big_file_options_big')
+
+ " Test buffer size, bail if that doesn't work either
+ let size = line2byte(line('$') + 1)
+ if size == -1
+ return
+ endif
+
+ " Flag the buffer's oversize status, if it's positive, we'll catch up and
+ " run the early options set now
+ let b:big_file_options_big = size > s:Limit()
+ if b:big_file_options_big
+ call s:SetPre()
+ endif
+
+ endif
+
+ " If the buffer size is verifiably over the threshold, run the late options
+ " set
+ if b:big_file_options_big
+ call s:SetPost()
+ endif
+
+endfunction