mirror of
https://github.com/vim-airline/vim-airline.git
synced 2025-12-22 20:01:26 +08:00
Some improvements to the wordcount plugin
1) allow for custom formatting of the output of the wordcount formatter This allows for formatting numbers correctly e.g. 1,042 in English locale and 1.042 in German locale. 2) cache values, so that no on every cursor move the wordcount needs to be recalculated.
This commit is contained in:
@@ -3,33 +3,17 @@
|
||||
|
||||
let s:filetypes = get(g:, 'airline#extensions#wordcount#filetypes', '\vhelp|markdown|rst|org|text')
|
||||
let s:format = get(g:, 'airline#extensions#wordcount#format', '%d words')
|
||||
let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default')
|
||||
|
||||
" adapted from http://stackoverflow.com/questions/114431/fast-word-count-function-in-vim
|
||||
function! s:update()
|
||||
if &ft !~ s:filetypes
|
||||
unlet! b:airline_wordcount
|
||||
return
|
||||
elseif exists("*wordcount")
|
||||
let b:airline_wordcount = printf(s:format, wordcount()['words']).
|
||||
\ g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space
|
||||
elseif mode() =~? 's'
|
||||
" Bail on select mode
|
||||
return
|
||||
else
|
||||
let old_status = v:statusmsg
|
||||
let position = getpos(".")
|
||||
exe "silent normal! g\<c-g>"
|
||||
let stat = v:statusmsg
|
||||
call setpos('.', position)
|
||||
let v:statusmsg = old_status
|
||||
|
||||
let parts = split(stat)
|
||||
if len(parts) > 11
|
||||
let cnt = str2nr(split(stat)[11])
|
||||
let spc = g:airline_symbols.space
|
||||
let b:airline_wordcount = printf(s:format, cnt) . spc . g:airline_right_alt_sep . spc
|
||||
else
|
||||
unlet! b:airline_wordcount
|
||||
if match(&ft, s:filetypes) > -1
|
||||
if get(b:, 'airline_wordcount_cache', '') is# '' ||
|
||||
\ b:airline_wordcount_cache isnot# get(b:, 'airline_wordcount', '') ||
|
||||
\ get(b:, 'airline_change_tick', 0) != b:changedtick
|
||||
" cache data
|
||||
let b:airline_wordcount = airline#extensions#wordcount#formatters#{s:formatter}#format()
|
||||
let b:airline_wordcount_cache = b:airline_wordcount
|
||||
let b:airline_change_tick = b:changedtick
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
@@ -44,4 +28,3 @@ function! airline#extensions#wordcount#init(ext)
|
||||
call a:ext.add_statusline_func('airline#extensions#wordcount#apply')
|
||||
autocmd BufReadPost,CursorMoved,CursorMovedI * call s:update()
|
||||
endfunction
|
||||
|
||||
|
||||
Reference in New Issue
Block a user