diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2019-05-25 17:16:09 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2019-05-25 17:16:09 +1200 |
commit | 19ddf67199fd5a6b0b3111230598c0b2567f792e (patch) | |
tree | 903d21c4631c161d5ae35c9ad74bcc017866943a | |
parent | Merge branch 'release/v1.1.0' into develop (diff) | |
download | vim-big-file-options-19ddf67199fd5a6b0b3111230598c0b2567f792e.tar.gz vim-big-file-options-19ddf67199fd5a6b0b3111230598c0b2567f792e.zip |
Move functions into autoload
This means we require Vim 7.0.
-rw-r--r-- | autoload/big_file_options.vim | 112 | ||||
-rw-r--r-- | doc/big_file_options.txt | 2 | ||||
-rw-r--r-- | plugin/big_file_options.vim | 120 |
3 files changed, 116 insertions, 118 deletions
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('<afile>')) + \ call big_file_options#CheckPre(expand('<afile>')) autocmd BufReadPost,StdinReadPost \ * - \ call s:CheckPost() + \ call big_file_options#CheckPost() augroup end |