aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-07-07 18:05:41 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-07-07 18:05:41 +1200
commit10e23336ae2fb213f44affc8c8b37b0d9bc23056 (patch)
tree0a1761025405b93e244959c72ed182e7fd47858d
parentBump VERSION (diff)
parentBump VERSION (diff)
downloadvim-big-file-options-10e23336ae2fb213f44affc8c8b37b0d9bc23056.tar.gz
vim-big-file-options-10e23336ae2fb213f44affc8c8b37b0d9bc23056.zip
Merge branch 'release/v1.0.0'v1.0.0
* release/v1.0.0: Bump VERSION Overhaul for new version Use full plugin name in options prefix Simply 'synmaxcol' setting Allow setting options any time
-rw-r--r--README.md4
-rw-r--r--VERSION2
-rw-r--r--doc/big_file_options.txt35
-rw-r--r--plugin/big_file_options.vim136
4 files changed, 129 insertions, 48 deletions
diff --git a/README.md b/README.md
index 0bb2cba..c3e2ec9 100644
--- a/README.md
+++ b/README.md
@@ -3,8 +3,8 @@ big\_file\_options.vim
This plugin adds an `autocmd` hook to check the file size of an incoming
buffer, and if it's over a certain threshold, disables certain options in order
-to make the file a bit easier to edit. It disables backups, swap files, undo
-files, and by default syntax highlighting.
+to make the file a bit easier to edit. It makes the buffer read-only, and
+disables filetypes, swap files, undo files, and syntax highlighting.
License
-------
diff --git a/VERSION b/VERSION
index 0c62199..3eefcb9 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.2.1
+1.0.0
diff --git a/doc/big_file_options.txt b/doc/big_file_options.txt
index bc133ae..532d2d0 100644
--- a/doc/big_file_options.txt
+++ b/doc/big_file_options.txt
@@ -1,11 +1,11 @@
-*big_file_options.txt* For Vim version 6.0 Last change: 2018 June 17
+*big_file_options.txt* For Vim version 6.0 Last change: 2018 July 7
DESCRIPTION *big_file_options*
This plugin adds an |autocmd| hook to check the file size of an incoming
-buffer, and if it's over a certain threshold, disables certain options in
-order to make the file a bit easier to edit. It disables backups, swap files,
-undo files, and by default syntax highlighting.
+buffer, and if it's over a certain threshold, disables certain options in order
+to make the file a bit easier to edit. It makes the buffer read-only, and
+disables filetypes, swap files, undo files, and syntax highlighting.
It's similar to the much older and more sophisticated LargeFile plugin by
Charles Campbell, which is based on VimTip #611:
@@ -22,20 +22,25 @@ This plugin is only available if 'compatible' is not set. It also requires the
OPTIONS *big_file_options-options*
-There are a few options you can set in your |vimrc| before loading the plugin:
+There are a few options you can set at any time before loading big files; it's
+probably best to put them in your |vimrc|.
- *g:big_file_size*
-Set `g:big_file_size` to the threshold in bytes beyond which a file should be
-considered "big"; this defaults to 10 MiB.
+ *g:big_file_options_limit*
+Set `g:big_file_options_limit` to the threshold in bytes beyond which a file
+should be considered "big"; this defaults to 10 MiB.
- *g:big_file_syntax*
-Set `g:big_file_syntax` to either 1 or 0 depending on whether you want to
-disable syntax highlighting completely on large files.
+ *g:big_file_options_readonly*
+Set `g:big_file_options_readonly` to either 1 or 0 depending on whether you
+want to make the buffer read-only; this defaults to on.
- *g:big_file_synmaxcol*
-Set `g:big_file_synmaxcol` to the number of columns for which syntax
-highlighting should be done on big files, assuming |g:big_file_syntax| is
-enabled. It defaults to 256 and only works if you have the |+synmaxcol|
+ *g:big_file_options_syntax*
+Set `g:big_file_options_syntax` to either 1 or 0 depending on whether you want
+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.
AUTHOR *big_file_options-author*
diff --git a/plugin/big_file_options.vim b/plugin/big_file_options.vim
index 067d15c..a2df77b 100644
--- a/plugin/big_file_options.vim
+++ b/plugin/big_file_options.vim
@@ -13,51 +13,127 @@ if !has('autocmd') || v:version < 600
endif
let g:loaded_big_file_options = 1
-" Default threshold is 10 MiB
-let s:size = exists('g:big_file_size')
- \ ? g:big_file_size
- \ : 10 * 1024 * 1024
-
-" Default to leaving syntax highlighting off
-let s:syntax = exists('g:big_file_syntax')
- \ ? g:big_file_syntax
- \ : 0
-
-" Cut 'synmaxcol' down to this or smaller for big files
-let s:synmaxcol = exists('g:big_file_synmaxcol')
- \ ? g:big_file_synmaxcol
- \ : 256
-
-" Declare function for turning off slow options
-function! s:BigFileOptions()
-
- " Don't do anything if the buffer size is under the threshold
- if line2byte(line('$') + 1) <= s:size
+" Wrapper function to get the configured size limit, default to 10 MiB
+function! s:Limit()
+ let l:limit = exists('g:big_file_options_limit')
+ \ ? g:big_file_options_limit
+ \ : 10 * 1024 * 1024
+ return l: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 l:size = getfsize(a:filename)
+ if l:size == -1
return
endif
- " Turn off backups, swap files, and undo files
- setlocal nobackup
- setlocal nowritebackup
+ " Set the buffer's big flag to whether the file is verifiably outsize
+ let b:big_file_options_big = l:size == -2 || l: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 l:size = line2byte(line('$') + 1)
+ if l: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 = l: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
- " Limit the number of columns of syntax highlighting
- if exists('+synmaxcol') && &synmaxcol > s:synmaxcol
- execute 'setlocal synmaxcol=' . s:synmaxcol
+ " Decide whether to set readonly options
+ let l:readonly = exists('g:big_file_options_readonly')
+ \ ? g:big_file_options_readonly
+ \ : 1
+ if l:readonly
+ setlocal buftype=nowrite
+ setlocal nomodifiable
+ setlocal readonly
endif
- " Disable syntax highlighting if configured to do so
- if !s:syntax
- setlocal syntax=OFF
+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 l:syntax = exists('g:big_file_options_syntax')
+ \ ? g:big_file_options_syntax
+ \ : 0
+ if !l:syntax
+ setlocal syntax=OFF
+ endif
+
+ " Force maximum syntax columns down if configured
+ if exists('+synmaxcol')
+ let l:synmaxcol = exists('g:big_file_options_synmaxcol')
+ \ ? g:big_file_options_synmaxcol
+ \ : 256
+ if exists('+synmaxcol') && &synmaxcol > l:synmaxcol
+ let &l:synmaxcol = l: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 BufReadPost * call s:BigFileOptions()
+ autocmd BufReadPre,StdinReadPre
+ \ *
+ \ call s:CheckPre(expand('<afile>'))
+ autocmd BufReadPost,StdinReadPost
+ \ *
+ \ call s:CheckPost()
augroup end