wordcount: Simplify update strategy

Checking for wordcount changes now uses a b:changedtick comparison in
the statusline funcref. The autocommand strategy that used to do this is
removed, simplifying the code.
This commit is contained in:
Liam Fleming 2018-09-18 16:05:01 +01:00
parent 9903fee60e
commit c33c1de079

View File

@ -65,51 +65,36 @@ function! s:update_wordcount(force_update)
endif endif
endfunction endfunction
if exists('##TextChanged')
let s:supported_autocmds = 'TextChanged,TextChangedI'
function! s:on_check()
call s:update_wordcount(0)
endfunction
else
let s:supported_autocmds = 'CursorMoved,CursorMovedI'
" without TextChanged, use "b:changedtick" to track changes
function! s:on_check()
if get(b:, 'airline_change_tick', -1) != b:changedtick
let b:airline_change_tick = b:changedtick
call s:update_wordcount(0)
endif
endfunction
endif
" public {{{1
let s:visual_active = 0 " Boolean: for when to get visual wordcount let s:visual_active = 0 " Boolean: for when to get visual wordcount
function airline#extensions#wordcount#get() function airline#extensions#wordcount#get()
return s:visual_active if s:visual_active
\ ? s:format_wordcount(s:get_wordcount(1)) return s:format_wordcount(s:get_wordcount(1))
\ : b:airline_wordcount else
if b:airline_changedtick != b:changedtick
call s:update_wordcount(0)
let b:airline_changedtick = b:changedtick
endif
return b:airline_wordcount
endif
endfunction endfunction
" autocmds & airline functions {{{1 " airline functions {{{1
" default filetypes: " default filetypes:
let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail'] let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail']
function! airline#extensions#wordcount#apply(...) function! airline#extensions#wordcount#apply(...)
let filetypes = get(g:, 'airline#extensions#wordcount#filetypes', s:filetypes) let filetypes = get(g:, 'airline#extensions#wordcount#filetypes', s:filetypes)
" check if autocmd updates are neccessary " Check if filetype needs testing
if did_filetype() || filetypes isnot s:filetypes if did_filetype() || filetypes isnot s:filetypes
let s:filetypes = filetypes let s:filetypes = filetypes
" Select test based on type of "filetypes": new=list, old=string " Select test based on type of "filetypes": new=list, old=string
if type(filetypes) == v:t_list if type(filetypes) == v:t_list
\ ? index(filetypes, &filetype) > -1 || index(filetypes, 'all') > -1 \ ? index(filetypes, &filetype) > -1 || index(filetypes, 'all') > -1
\ : match(&ft, filetypes) > -1 \ : match(&filetype, filetypes) > -1
execute 'autocmd! airline_wordcount BufEnter,'.s:supported_autocmds let b:airline_changedtick = -1
\ .' <buffer> nested call s:on_check()' call s:update_wordcount(1) " force update: ensures initial worcount exists
call s:update_wordcount(1) " force update ensures initial worcount exists elseif exists('b:airline_wordcount') " cleanup when filetype is removed
elseif exists('b:airline_wordcount') " cleanup
autocmd! airline_wordcount * <buffer>
unlet b:airline_wordcount unlet b:airline_wordcount
endif endif
endif endif