Merge pull request #1413 from chrisbra/wordformat

Improvements to wordcount formatting
This commit is contained in:
Christian Brabandt 2017-06-20 23:31:15 +02:00 committed by GitHub
commit 52c0afadb9
2 changed files with 19 additions and 11 deletions

View File

@ -3,12 +3,14 @@
scriptencoding utf-8
let s:format = get(g:, 'airline#extensions#wordcount#format', '%d words')
let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default')
let g:airline#extensions#wordcount#filetypes = get(g:, 'airline#extensions#wordcount#filetypes',
\ '\vhelp|markdown|rst|org|text|asciidoc|tex|mail')
function! s:update()
function! s:wordcount_update()
if empty(bufname(''))
return
endif
if match(&ft, get(g:, 'airline#extensions#wordcount#filetypes')) > -1
let l:mode = mode()
if l:mode ==# 'v' || l:mode ==# 'V' || l:mode ==# 's' || l:mode ==# 'S'
@ -17,11 +19,13 @@ function! s:update()
else
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_change_tick', 0) != b:changedtick ||
\ get(b:, 'airline_winwidth', 0) != winwidth(0)
" 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
let b:airline_winwidth = winwidth(0)
endif
endif
endif
@ -35,5 +39,5 @@ endfunction
function! airline#extensions#wordcount#init(ext)
call a:ext.add_statusline_func('airline#extensions#wordcount#apply')
autocmd BufReadPost,CursorMoved,CursorMovedI * call s:update()
autocmd BufReadPost,CursorMoved,CursorMovedI * call s:wordcount_update()
endfunction

View File

@ -8,14 +8,18 @@ function! airline#extensions#wordcount#formatters#default#format()
if empty(words)
return
endif
let separator = s:get_decimal_group()
if words > 999 && !empty(separator)
" Format number according to locale, e.g. German: 1.245 or English: 1,245
let a = join(reverse(split(words, '.\zs')),'')
let a = substitute(a, '...', '&'.separator, 'g')
let words = join(reverse(split(a, '.\zs')),'')
let result = g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space
if winwidth(0) >= 80
let separator = s:get_decimal_group()
if words > 999 && !empty(separator)
" Format number according to locale, e.g. German: 1.245 or English: 1,245
let words = substitute(words, '\d\@<=\(\(\d\{3\}\)\+\)$', separator.'&', 'g')
endif
let result = printf("%s%s", words, " words"). result
else
let result = printf("%s%s", words, "W"). result
endif
return words . " words" . g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space
return result
endfunction
function! s:wordcount()