wordcount: Refactor updating and autocmds

Various improvements:
 - Seperate out visual mode detection.
 - Use TextChanged rather than CursorMoved where supported.
 - Let users specify the filetypes for which wordcounting is enabled
   with a list rather than a pattern.
 - Move the filetype check to when airline is refreshed, as opposed to
   on every update; autocommands are not created if wordcounting is
   disabled
This commit is contained in:
Liam Fleming 2018-06-30 19:40:39 +01:00
parent 44da0a4761
commit 8715d13cc5

View File

@ -54,42 +54,74 @@ if s:formatter !=# 'default'
endif endif
" update {{{1 " update {{{1
function! s:wordcount_update() if exists('##TextChanged')
if empty(bufname('')) let s:supported_autocmds = 'TextChanged,TextChangedI'
return
endif function! s:update_wordcount()
if match(&ft, get(g:, 'airline#extensions#wordcount#filetypes')) > -1 let b:airline_wordcount = s:format_wordcount('words')
let l:mode = mode() endfunction
if l:mode ==# 'v' || l:mode ==# 'V' || l:mode ==# 's' || l:mode ==# 'S' else
let b:airline_wordcount = s:format_wordcount('visual_words') let s:supported_autocmds = 'CursorMoved,CursorMovedI'
" without TextChanged a check is performed on every cursor movement, so
" cache for performance
function! s:update_wordcount()
if get(b:, 'airline_wordcount_cache', '') is# '' ||
\ get(b:, 'airline_change_tick', 0) != b:changedtick ||
\ get(b:, 'airline_winwidth', 0) != winwidth(0)
let b:airline_wordcount = s:format_wordcount('words')
let b:airline_wordcount_cache = b:airline_wordcount
let b:airline_change_tick = b:changedtick let b:airline_change_tick = b:changedtick
else let b:airline_winwidth = winwidth(0)
if get(b:, 'airline_wordcount_cache', '') is# '' ||
\ b:airline_wordcount_cache isnot# get(b:, 'airline_wordcount', '') ||
\ get(b:, 'airline_change_tick', 0) != b:changedtick ||
\ get(b:, 'airline_winwidth', 0) != winwidth(0)
" cache data
let b:airline_wordcount = s:format_wordcount('words')
let b:airline_wordcount_cache = b:airline_wordcount
let b:airline_change_tick = b:changedtick
let b:airline_winwidth = winwidth(0)
endif
endif endif
endif endfunction
endif
" public {{{1
" s:visual tracks visual mode
function airline#extensions#wordcount#get()
return s:visual
\ ? s:format_wordcount('visual_words')
\ : get(b:, 'airline_wordcount', '')
endfunction endfunction
" autocmds & airline functions {{{1 " autocmds & airline functions {{{1
function s:modify_autocmds()
if !exists('#airline_wordcount#BufEnter#')
execute 'autocmd! airline_wordcount BufEnter,'.s:supported_autocmds
\ .' <buffer> nested call s:update_wordcount()'
" ensure we have a starting wordcount
call s:update_wordcount()
else
execute 'autocmd! airline_wordcount BufEnter,'.'s:supported_autocmds'
endif
endfunction
" default filetypes " default filetypes
let g:airline#extensions#wordcount#filetypes = get(g:, 'airline#extensions#wordcount#filetypes', let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail']
\ '\vhelp|markdown|rst|org|text|asciidoc|tex|mail')
function! airline#extensions#wordcount#apply(...) function! airline#extensions#wordcount#apply(...)
if match(&ft, get(g:, 'airline#extensions#wordcount#filetypes')) > -1 let filetypes = get(g:, 'airline#extensions#wordcount#filetypes', s:filetypes)
call airline#extensions#prepend_to_section('z', '%{get(b:, "airline_wordcount", "")}')
" filetypes used to be a regex-matching string, so check both
if type(filetypes) == v:t_list
\ && (index(filetypes, &filetype) > -1 || empty(filetypes))
\ || type(filetypes) == v:t_string && match(&ft, filetypes) > -1
" redo autocommands if filetype has changed
if filetypes isnot s:filetypes || did_filetype()
call s:modify_autocmds()
let s:filetypes = filetypes
endif
call airline#extensions#prepend_to_section(
\ 'z', '%{airline#extensions#wordcount#get()}')
endif endif
endfunction endfunction
function! airline#extensions#wordcount#init(ext) function! airline#extensions#wordcount#init(ext)
augroup airline_wordcount
autocmd! User AirlineModeChanged nested
\ let s:visual = (mode() ==? 'v' || mode() ==? 's')
augroup END
call a:ext.add_statusline_func('airline#extensions#wordcount#apply') call a:ext.add_statusline_func('airline#extensions#wordcount#apply')
autocmd BufReadPost,CursorMoved,CursorMovedI * call s:wordcount_update()
endfunction endfunction