wordcount: Compatibility fixes and General upkeep

Compatibility:
- Don't use a script-local function to update the format strings
- Protect against `:normal!' moving the cursor on on the wordcount check

Bugfix:
- Let to_string() try to return something for all values
  - Now returns correctly when passed both 0 and '0'

Upkeep:
- Simplify check again no valid key from winwidth()
- Old wordcount check: use matchstr() - more expressive and fewer steps
- Improve documentation style/clarity/detail
This commit is contained in:
Liam Fleming 2018-09-22 03:34:27 +01:00
parent ae024293e4
commit 264c1fefd2
3 changed files with 21 additions and 28 deletions

View File

@ -7,27 +7,26 @@ scriptencoding utf-8
if exists('*wordcount')
function! s:get_wordcount(visual_mode_active)
let query = a:visual_mode_active ? 'visual_words' : 'words'
let result = wordcount()
if has_key(result, query)
return string(result[query])
endif
return ''
return get(wordcount(), query, 0)
endfunction
else
else " Pull wordcount from the g_ctrl-g stats
function! s:get_wordcount(visual_mode_active)
" index to retrieve from whitespace-separated output of g_CTRL-G
" 11 : words, 5 : visual words (in visual mode)
let idx = a:visual_mode_active ? 5 : 11
let pattern = a:visual_mode_active
\ ? '\d\+\ze Words;'
\ : 'Word \d\+ of \zs\d\+'
let save_status = v:statusmsg
execute "silent normal! g\<c-g>"
let stat = v:statusmsg
if !a:visual_mode_active && col('.') == col('$')
let save_pos = getpos('.')
execute "silent normal! g\<c-g>"
call setpos('.', save_pos)
else
execute "silent normal! g\<c-g>"
endif
let stats = v:statusmsg
let v:statusmsg = save_status
let parts = split(substitute(stat, ';', '', 'g'))
if len(parts) > idx
return parts[idx]
endif
return str2nr(matchstr(stats, pattern))
endfunction
endif

View File

@ -3,16 +3,16 @@
scriptencoding utf-8
function! s:update_fmt(...)
function! airline#extensions#wordcount#formatters#default#update_fmt(...)
let s:fmt = get(g:, 'airline#extensions#wordcount#formatter#default#fmt', '%s words')
let s:fmt_short = get(g:, 'airline#extensions#wordcount#formatter#default#fmt_short', s:fmt == '%s words' ? '%sW' : s:fmt)
endfunction
" Reload format when statusline is rebuilt
call s:update_fmt()
if index(g:airline_statusline_funcrefs, function('s:update_fmt')) == -1
call airline#extensions#wordcount#formatters#default#update_fmt()
if index(g:airline_statusline_funcrefs, function('airline#extensions#wordcount#formatters#default#update_fmt')) == -1
" only add it, if not already done
call airline#add_statusline_funcref(function('s:update_fmt'))
call airline#add_statusline_funcref(function('airline#extensions#wordcount#formatters#default#update_fmt'))
endif
if match(get(v:, 'lang', ''), '\v\cC|en') > -1
@ -24,10 +24,6 @@ else
endif
function! airline#extensions#wordcount#formatters#default#to_string(wordcount)
if empty(a:wordcount)
return
endif
if winwidth(0) >= 80
if a:wordcount > 999
" Format number according to locale, e.g. German: 1.245 or English: 1,245
@ -41,4 +37,3 @@ function! airline#extensions#wordcount#formatters#default#to_string(wordcount)
endif
return str . g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space
endfunction

View File

@ -592,16 +592,15 @@ eclim <https://eclim.org>
let g:airline#extensions#eclim#enabled = 1
------------------------------------- *airline-wordcount*
display the word count of the document or visual selection
* enable/disable word counting. >
* enable/disable word counting of the document/visual selection >
let g:airline#extensions#wordcount#enabled = 1
<
* set list of filetypes for which word counting is enabled: >
" the default value matches filetypes typically used for documentation
" The default value matches filetypes typically used for documentation
" such as markdown and help files.
let g:airline#extensions#wordcount#filetypes =
\ ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail']
" Use ['all'] to enable for all filetypes.
* defines the name of a formatter for word count will be displayed: >
" The default will try to guess LC_NUMERIC and format number accordingly