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

View File

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

View File

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