From 19ddf67199fd5a6b0b3111230598c0b2567f792e Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:16:09 +1200 Subject: Move functions into autoload This means we require Vim 7.0. --- autoload/big_file_options.vim | 112 +++++++++++++++++++++++++++++++++++++++ doc/big_file_options.txt | 2 +- plugin/big_file_options.vim | 120 ++---------------------------------------- 3 files changed, 116 insertions(+), 118 deletions(-) create mode 100644 autoload/big_file_options.vim diff --git a/autoload/big_file_options.vim b/autoload/big_file_options.vim new file mode 100644 index 0000000..5c57343 --- /dev/null +++ b/autoload/big_file_options.vim @@ -0,0 +1,112 @@ +" Wrapper function to get the configured size limit, default to 10 MiB +function! s:Limit() + let limit = exists('g:big_file_options_limit') + \ ? 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) + + " 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 + +" These options can and should be set as early as possible +function! s:SetPre() + + " These are always set + setlocal noswapfile + setlocal undolevels=-1 + if has('persistent_undo') + setlocal noundofile + endif + + " Decide whether to set readonly options + let readonly = exists('g:big_file_options_readonly') + \ ? g:big_file_options_readonly + \ : 1 + if readonly + setlocal buftype=nowrite + setlocal nomodifiable + setlocal readonly + endif +endfunction + +" These options need to be set later, after the buffer has loaded +function! s:SetPost() + + " Force filetype off + setlocal filetype=NONE + + " Syntax features + if has('syntax') + + " Disable syntax highlighting if configured + let syntax = exists('g:big_file_options_syntax') + \ ? g:big_file_options_syntax + \ : 0 + if !syntax + setlocal syntax=OFF + endif + + " Force maximum syntax columns down if configured + if exists('+synmaxcol') + let synmaxcol = exists('g:big_file_options_synmaxcol') + \ ? g:big_file_options_synmaxcol + \ : 256 + if exists('+synmaxcol') && &synmaxcol > synmaxcol + let &l:synmaxcol = synmaxcol + endif + endif + + endif + + " Tell the user what we've done + echomsg 'Big file detected, set appropriate options' + +endfunction diff --git a/doc/big_file_options.txt b/doc/big_file_options.txt index 0603dc0..799f659 100644 --- a/doc/big_file_options.txt +++ b/doc/big_file_options.txt @@ -1,4 +1,4 @@ -*big_file_options.txt* For Vim version 6.0 Last change: 2018 July 7 +*big_file_options.txt* For Vim version 7.0 Last change: 2018 July 7 DESCRIPTION *big_file_options* diff --git a/plugin/big_file_options.vim b/plugin/big_file_options.vim index bc69e44..d78077a 100644 --- a/plugin/big_file_options.vim +++ b/plugin/big_file_options.vim @@ -8,132 +8,18 @@ if exists('loaded_big_file_options') || &compatible finish endif -if !has('autocmd') || v:version < 600 +if !has('autocmd') || v:version < 700 finish endif let loaded_big_file_options = 1 -" Wrapper function to get the configured size limit, default to 10 MiB -function! s:Limit() - let limit = exists('g:big_file_options_limit') - \ ? 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! s: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! s: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 - -" These options can and should be set as early as possible -function! s:SetPre() - - " These are always set - setlocal noswapfile - setlocal undolevels=-1 - if has('persistent_undo') - setlocal noundofile - endif - - " Decide whether to set readonly options - let readonly = exists('g:big_file_options_readonly') - \ ? g:big_file_options_readonly - \ : 1 - if readonly - setlocal buftype=nowrite - setlocal nomodifiable - setlocal readonly - endif - -endfunction - -" These options need to be set later, after the buffer has loaded -function! s:SetPost() - - " Force filetype off - setlocal filetype=NONE - - " Syntax features - if has('syntax') - - " Disable syntax highlighting if configured - let syntax = exists('g:big_file_options_syntax') - \ ? g:big_file_options_syntax - \ : 0 - if !syntax - setlocal syntax=OFF - endif - - " Force maximum syntax columns down if configured - if exists('+synmaxcol') - let synmaxcol = exists('g:big_file_options_synmaxcol') - \ ? g:big_file_options_synmaxcol - \ : 256 - if exists('+synmaxcol') && &synmaxcol > synmaxcol - let &l:synmaxcol = synmaxcol - endif - endif - - endif - - " Tell the user what we've done - echomsg 'Big file detected, set appropriate options' - -endfunction - " Define autocmd for calling to check filesize augroup big_file_options autocmd! autocmd BufReadPre,StdinReadPre \ * - \ call s:CheckPre(expand('')) + \ call big_file_options#CheckPre(expand('')) autocmd BufReadPost,StdinReadPost \ * - \ call s:CheckPost() + \ call big_file_options#CheckPost() augroup end -- cgit v1.2.3 From d9027054ca590da213b4a59d32891ad649c12d5b Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:17:16 +1200 Subject: Adjust autocmd definitions --- plugin/big_file_options.vim | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin/big_file_options.vim b/plugin/big_file_options.vim index d78077a..cea19ba 100644 --- a/plugin/big_file_options.vim +++ b/plugin/big_file_options.vim @@ -16,10 +16,8 @@ let loaded_big_file_options = 1 " Define autocmd for calling to check filesize augroup big_file_options autocmd! - autocmd BufReadPre,StdinReadPre - \ * + autocmd BufReadPre,StdinReadPre * \ call big_file_options#CheckPre(expand('')) - autocmd BufReadPost,StdinReadPost - \ * + autocmd BufReadPost,StdinReadPost * \ call big_file_options#CheckPost() augroup end -- cgit v1.2.3 From f405cd51f7ab3b569c1f4ce43797cd64ccbe88d1 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:19:03 +1200 Subject: Use get() in lieu of verbose pre-7.0 exists() --- autoload/big_file_options.vim | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/autoload/big_file_options.vim b/autoload/big_file_options.vim index 5c57343..f8b0671 100644 --- a/autoload/big_file_options.vim +++ b/autoload/big_file_options.vim @@ -1,8 +1,6 @@ " Wrapper function to get the configured size limit, default to 10 MiB function! s:Limit() - let limit = exists('g:big_file_options_limit') - \ ? g:big_file_options_limit - \ : 10 * 1024 * 1024 + let limit = get(g:, 'big_file_options_limit', 10 * 1024 * 1024) return limit endfunction @@ -67,9 +65,7 @@ function! s:SetPre() endif " Decide whether to set readonly options - let readonly = exists('g:big_file_options_readonly') - \ ? g:big_file_options_readonly - \ : 1 + let readonly = get(g:, 'big_file_options_readonly', 1) if readonly setlocal buftype=nowrite setlocal nomodifiable @@ -87,18 +83,14 @@ function! s:SetPost() if has('syntax') " Disable syntax highlighting if configured - let syntax = exists('g:big_file_options_syntax') - \ ? g:big_file_options_syntax - \ : 0 + let syntax = get(g:, 'big_file_options_syntax', 0) if !syntax setlocal syntax=OFF endif " Force maximum syntax columns down if configured if exists('+synmaxcol') - let synmaxcol = exists('g:big_file_options_synmaxcol') - \ ? g:big_file_options_synmaxcol - \ : 256 + let synmaxcol = get(g:, 'big_file_options_synmaxcol', 256) if exists('+synmaxcol') && &synmaxcol > synmaxcol let &l:synmaxcol = synmaxcol endif -- cgit v1.2.3 From b9bfb4df7fac946f52fe10c768e7d34dde160dfc Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:22:08 +1200 Subject: Reorder functions; put interfaces on the top --- autoload/big_file_options.vim | 62 ++++++++++++++++++++++++++++++++++++++----- 1 file 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 -- cgit v1.2.3 From 1c9df5c45f9d90f62c5364bf6c21ba547a70cbd0 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:22:28 +1200 Subject: Add "abort" attribute to functions --- autoload/big_file_options.vim | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/autoload/big_file_options.vim b/autoload/big_file_options.vim index e167dc2..0eca0be 100644 --- a/autoload/big_file_options.vim +++ b/autoload/big_file_options.vim @@ -1,5 +1,5 @@ " If we can use filesize to detect the big file early, we should -function! big_file_options#CheckPre(filename) +function! big_file_options#CheckPre(filename) abort " Try and get filesize, bail out if we can't let size = getfsize(a:filename) @@ -19,7 +19,7 @@ endfunction " If it's still indeterminate (stdin read?), try to check the buffer size " itself -function! big_file_options#CheckPost() +function! big_file_options#CheckPost() abort " The BufReadPre hook couldn't tell how big the file was; we'll examine it " now it's loaded into the buffer instead @@ -49,13 +49,13 @@ function! big_file_options#CheckPost() endfunction " Wrapper function to get the configured size limit, default to 10 MiB -function! s:Limit() +function! s:Limit() abort 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() +function! s:SetPre() abort " These are always set setlocal noswapfile @@ -74,7 +74,7 @@ function! s:SetPre() endfunction " These options need to be set later, after the buffer has loaded -function! s:SetPost() +function! s:SetPost() abort " Force filetype off setlocal filetype=NONE @@ -104,7 +104,7 @@ function! s:SetPost() endfunction " If we can use filesize to detect the big file early, we should -function! big_file_options#CheckPre(filename) +function! big_file_options#CheckPre(filename) abort " Try and get filesize, bail out if we can't let size = getfsize(a:filename) @@ -124,7 +124,7 @@ endfunction " If it's still indeterminate (stdin read?), try to check the buffer size " itself -function! big_file_options#CheckPost() +function! big_file_options#CheckPost() abort " The BufReadPre hook couldn't tell how big the file was; we'll examine it " now it's loaded into the buffer instead -- cgit v1.2.3 From f89394a347e96c6c69661611d14867d0d931a698 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:23:06 +1200 Subject: Remove redundant option existence check --- autoload/big_file_options.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/big_file_options.vim b/autoload/big_file_options.vim index 0eca0be..023c672 100644 --- a/autoload/big_file_options.vim +++ b/autoload/big_file_options.vim @@ -91,7 +91,7 @@ function! s:SetPost() abort " Force maximum syntax columns down if configured if exists('+synmaxcol') let synmaxcol = get(g:, 'big_file_options_synmaxcol', 256) - if exists('+synmaxcol') && &synmaxcol > synmaxcol + if &synmaxcol > synmaxcol let &l:synmaxcol = synmaxcol endif endif -- cgit v1.2.3 From a3d575f4db69123dbad6c59faf8ba6185e4b0091 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:25:08 +1200 Subject: Remove syntax feature check --- autoload/big_file_options.vim | 25 +++++++++---------------- doc/big_file_options.txt | 3 +-- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/autoload/big_file_options.vim b/autoload/big_file_options.vim index 023c672..4d65bd4 100644 --- a/autoload/big_file_options.vim +++ b/autoload/big_file_options.vim @@ -79,23 +79,16 @@ function! s:SetPost() abort " Force filetype off setlocal filetype=NONE - " Syntax features - if has('syntax') - - " Disable syntax highlighting if configured - let syntax = get(g:, 'big_file_options_syntax', 0) - if !syntax - setlocal syntax=OFF - endif - - " Force maximum syntax columns down if configured - if exists('+synmaxcol') - let synmaxcol = get(g:, 'big_file_options_synmaxcol', 256) - if &synmaxcol > synmaxcol - let &l:synmaxcol = synmaxcol - endif - endif + " Disable syntax highlighting if configured + let syntax = get(g:, 'big_file_options_syntax', 0) + if !syntax + setlocal syntax=OFF + endif + " Force maximum syntax columns down if configured + let synmaxcol = get(g:, 'big_file_options_synmaxcol', 256) + if &synmaxcol > synmaxcol + let &l:synmaxcol = synmaxcol endif " Tell the user what we've done diff --git a/doc/big_file_options.txt b/doc/big_file_options.txt index 799f659..713a6bc 100644 --- a/doc/big_file_options.txt +++ b/doc/big_file_options.txt @@ -40,8 +40,7 @@ to disable syntax highlighting completely on large files; this defaults to on. *g:big_file_options_synmaxcol* Set `g:big_file_options_synmaxcol` to the number of columns for which syntax highlighting should be done on big files, assuming |g:big_file_options_syntax| -is enabled. This defaults to 256, and only works if you have the |+synmaxcol| -feature. +is enabled. This defaults to 256. AUTHOR *big_file_options-author* -- cgit v1.2.3 From 6959f9508855d8f05932ebc17b0496d7949fd1b7 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 25 May 2019 17:32:15 +1200 Subject: Bump VERSION --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 9084fa2..227cea2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.0 +2.0.0 -- cgit v1.2.3