From 264c1fefd2f102ee55df5104ddc6fb7cd5c001e7 Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Sat, 22 Sep 2018 03:34:27 +0100 Subject: [PATCH] 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 --- autoload/airline/extensions/wordcount.vim | 29 +++++++++---------- .../wordcount/formatters/default.vim | 13 +++------ doc/airline.txt | 7 ++--- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/autoload/airline/extensions/wordcount.vim b/autoload/airline/extensions/wordcount.vim index cce2e8e..364c425 100644 --- a/autoload/airline/extensions/wordcount.vim +++ b/autoload/airline/extensions/wordcount.vim @@ -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\" - let stat = v:statusmsg + if !a:visual_mode_active && col('.') == col('$') + let save_pos = getpos('.') + execute "silent normal! g\" + call setpos('.', save_pos) + else + execute "silent normal! 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 diff --git a/autoload/airline/extensions/wordcount/formatters/default.vim b/autoload/airline/extensions/wordcount/formatters/default.vim index 6f1bae7..5682921 100644 --- a/autoload/airline/extensions/wordcount/formatters/default.vim +++ b/autoload/airline/extensions/wordcount/formatters/default.vim @@ -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 - diff --git a/doc/airline.txt b/doc/airline.txt index 222cbd0..c94c52e 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -592,16 +592,15 @@ eclim 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