From 781c40bb3d95c4d273937f99db545e6824f05d4f Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Thu, 28 Jun 2018 18:44:26 +0100 Subject: [PATCH 1/7] Create AirlineModeChanged autocmd --- autoload/airline.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/autoload/airline.vim b/autoload/airline.vim index cafedbc..4a55b45 100644 --- a/autoload/airline.vim +++ b/autoload/airline.vim @@ -189,6 +189,7 @@ function! airline#check_mode(winnr) if get(w:, 'airline_lastmode', '') != mode_string call airline#highlighter#highlight_modified_inactive(context.bufnr) call airline#highlighter#highlight(l:mode, context.bufnr) + silent doautocmd User AirlineModeChanged let w:airline_lastmode = mode_string endif From 44da0a476169815f9536f43a05f859b6553dfbbe Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Sat, 30 Jun 2018 18:34:12 +0100 Subject: [PATCH 2/7] wordcount: Replace formatter interface Currently the formatter, and not the wordcount plugin, is responsible for providing the wordcount as well as formatting it. The default formatter allows visual mode word counting, although this is not documented. The new interface - a transform() function, allows the main wordcount plugin to internalise this logic. Providing the wordcount simplifies formatter implementations: - All formatters can display the visual wordcount. - Formatters do not have to worry about compatibility with different vim versions. The old format() function can now be deprecated, although the wordcount plugin retains compatibility with formatters using it. The default formatter will also be used as a fallback if no suitable function is found. The default formatter is rewritten to use the new interface. --- autoload/airline/extensions/wordcount.vim | 64 +++++++++++++++++-- .../wordcount/formatters/default.vim | 54 ++++------------ 2 files changed, 71 insertions(+), 47 deletions(-) diff --git a/autoload/airline/extensions/wordcount.vim b/autoload/airline/extensions/wordcount.vim index 0a8988c..e2eb305 100644 --- a/autoload/airline/extensions/wordcount.vim +++ b/autoload/airline/extensions/wordcount.vim @@ -1,12 +1,59 @@ " MIT License. Copyright (c) 2013-2018 Bailey Ling et al. -" vim: et ts=2 sts=2 sw=2 +" vim: et ts=2 sts=2 sw=2 fdm=marker scriptencoding utf-8 -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') +" get wordcount {{{1 +if exists('*wordcount') + function! s:get_wordcount(type) + return string(wordcount()[a:type]) + endfunction +else + function! s:get_wordcount(type) + " index to retrieve from whitespace-separated output of g_CTRL-G + " 11 - words, 5 - visual words (in visual mode) + let idx = (a:type == 'words') ? 11 : 5 + let save_status = v:statusmsg + execute "silent normal! g\" + let stat = v:statusmsg + let v:statusmsg = save_status + + let parts = split(stat) + if len(parts) > idx + return parts[idx] + endif + endfunction +endif + +" format {{{1 +let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default') + +" wrapper function for compatibility; redefined below for old-style formatters +function! s:format_wordcount(type) + return airline#extensions#wordcount#formatters#{s:formatter}#transform( + \ s:get_wordcount(a:type)) +endfunction + +" check user-defined formatter exists and has appropriate functions, otherwise +" fall back to default +if s:formatter !=# 'default' + execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter + if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#transform') + if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#format') + let s:formatter = 'default' + else + " redefine for backwords compatibility + function! s:format_wordcount(type) + if a:type !=# 'visual_words' + return airline#extensions#wordcount#formatters#{s:formatter}#format() + endif + endfunction + endif + endif +endif + +" update {{{1 function! s:wordcount_update() if empty(bufname('')) return @@ -14,7 +61,7 @@ function! s:wordcount_update() 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' - let b:airline_wordcount = airline#extensions#wordcount#formatters#{s:formatter}#format() + let b:airline_wordcount = s:format_wordcount('visual_words') let b:airline_change_tick = b:changedtick else if get(b:, 'airline_wordcount_cache', '') is# '' || @@ -22,7 +69,7 @@ function! s:wordcount_update() \ 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 = s:format_wordcount('words') let b:airline_wordcount_cache = b:airline_wordcount let b:airline_change_tick = b:changedtick let b:airline_winwidth = winwidth(0) @@ -31,6 +78,11 @@ function! s:wordcount_update() endif endfunction +" autocmds & airline functions {{{1 +" default filetypes +let g:airline#extensions#wordcount#filetypes = get(g:, 'airline#extensions#wordcount#filetypes', + \ '\vhelp|markdown|rst|org|text|asciidoc|tex|mail') + function! airline#extensions#wordcount#apply(...) if match(&ft, get(g:, 'airline#extensions#wordcount#filetypes')) > -1 call airline#extensions#prepend_to_section('z', '%{get(b:, "airline_wordcount", "")}') diff --git a/autoload/airline/extensions/wordcount/formatters/default.vim b/autoload/airline/extensions/wordcount/formatters/default.vim index d7b5fae..846b4e0 100644 --- a/autoload/airline/extensions/wordcount/formatters/default.vim +++ b/autoload/airline/extensions/wordcount/formatters/default.vim @@ -3,59 +3,30 @@ scriptencoding utf-8 -function! airline#extensions#wordcount#formatters#default#format() - let fmt = get(g:, 'airline#extensions#wordcount#formatter#default#fmt', '%s words') - let fmt_short = get(g:, 'airline#extensions#wordcount#formatter#default#fmt_short', fmt == '%s words' ? '%sW' : fmt) - let words = string(s:wordcount()) - if empty(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) + +function! airline#extensions#wordcount#formatters#default#transform(text) + if empty(a:text) return endif + 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) + if a:text > 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') + let text = substitute(a:text, '\d\@<=\(\(\d\{3\}\)\+\)$', separator.'&', 'g') + else + let text = a:text endif - let result = printf(fmt, words). result + let result = printf(s:fmt, a:text). result else - let result = printf(fmt_short, words). result + let result = printf(s:fmt_short, a:text). result endif return result endfunction -function! s:wordcount() - if exists("*wordcount") - let l:mode = mode() - if l:mode ==# 'v' || l:mode ==# 'V' || l:mode ==# 's' || l:mode ==# 'S' - let l:visual_words = wordcount()['visual_words'] - if l:visual_words != '' - return l:visual_words - else - return 0 - endif - else - return wordcount()['words'] - endif - elseif mode() =~? 's' - return - else - let old_status = v:statusmsg - let position = getpos(".") - exe "silent normal! g\" - let stat = v:statusmsg - call setpos('.', position) - let v:statusmsg = old_status - - let parts = split(stat) - if len(parts) > 11 - return str2nr(parts[11]) - else - return - endif - endif -endfunction - function! s:get_decimal_group() if match(get(v:, 'lang', ''), '\v\cC|en') > -1 return ',' @@ -64,3 +35,4 @@ function! s:get_decimal_group() endif return '' endfunction + From 8715d13cc5721c406c5a014ac4b9e9e82fd1944f Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Sat, 30 Jun 2018 19:40:39 +0100 Subject: [PATCH 3/7] wordcount: Refactor updating and autocmds Various improvements: - Seperate out visual mode detection. - Use TextChanged rather than CursorMoved where supported. - Let users specify the filetypes for which wordcounting is enabled with a list rather than a pattern. - Move the filetype check to when airline is refreshed, as opposed to on every update; autocommands are not created if wordcounting is disabled --- autoload/airline/extensions/wordcount.vim | 82 ++++++++++++++++------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/autoload/airline/extensions/wordcount.vim b/autoload/airline/extensions/wordcount.vim index e2eb305..4a937e8 100644 --- a/autoload/airline/extensions/wordcount.vim +++ b/autoload/airline/extensions/wordcount.vim @@ -54,42 +54,74 @@ if s:formatter !=# 'default' endif " update {{{1 -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' - let b:airline_wordcount = s:format_wordcount('visual_words') +if exists('##TextChanged') + let s:supported_autocmds = 'TextChanged,TextChangedI' + + function! s:update_wordcount() + let b:airline_wordcount = s:format_wordcount('words') + endfunction +else + let s:supported_autocmds = 'CursorMoved,CursorMovedI' + + " without TextChanged a check is performed on every cursor movement, so + " cache for performance + function! s:update_wordcount() + if get(b:, 'airline_wordcount_cache', '') is# '' || + \ get(b:, 'airline_change_tick', 0) != b:changedtick || + \ get(b:, 'airline_winwidth', 0) != winwidth(0) + let b:airline_wordcount = s:format_wordcount('words') + let b:airline_wordcount_cache = b:airline_wordcount let b:airline_change_tick = b:changedtick - 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_winwidth', 0) != winwidth(0) - " cache data - let b:airline_wordcount = s:format_wordcount('words') - let b:airline_wordcount_cache = b:airline_wordcount - let b:airline_change_tick = b:changedtick - let b:airline_winwidth = winwidth(0) - endif + let b:airline_winwidth = winwidth(0) endif - endif + endfunction +endif + +" public {{{1 +" s:visual tracks visual mode +function airline#extensions#wordcount#get() + return s:visual + \ ? s:format_wordcount('visual_words') + \ : get(b:, 'airline_wordcount', '') endfunction " autocmds & airline functions {{{1 +function s:modify_autocmds() + if !exists('#airline_wordcount#BufEnter#') + execute 'autocmd! airline_wordcount BufEnter,'.s:supported_autocmds + \ .' nested call s:update_wordcount()' + " ensure we have a starting wordcount + call s:update_wordcount() + else + execute 'autocmd! airline_wordcount BufEnter,'.'s:supported_autocmds' + endif +endfunction + " default filetypes -let g:airline#extensions#wordcount#filetypes = get(g:, 'airline#extensions#wordcount#filetypes', - \ '\vhelp|markdown|rst|org|text|asciidoc|tex|mail') +let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail'] function! airline#extensions#wordcount#apply(...) - if match(&ft, get(g:, 'airline#extensions#wordcount#filetypes')) > -1 - call airline#extensions#prepend_to_section('z', '%{get(b:, "airline_wordcount", "")}') + let filetypes = get(g:, 'airline#extensions#wordcount#filetypes', s:filetypes) + + " filetypes used to be a regex-matching string, so check both + if type(filetypes) == v:t_list + \ && (index(filetypes, &filetype) > -1 || empty(filetypes)) + \ || type(filetypes) == v:t_string && match(&ft, filetypes) > -1 + " redo autocommands if filetype has changed + if filetypes isnot s:filetypes || did_filetype() + call s:modify_autocmds() + let s:filetypes = filetypes + endif + + call airline#extensions#prepend_to_section( + \ 'z', '%{airline#extensions#wordcount#get()}') endif endfunction function! airline#extensions#wordcount#init(ext) + augroup airline_wordcount + autocmd! User AirlineModeChanged nested + \ let s:visual = (mode() ==? 'v' || mode() ==? 's') + augroup END call a:ext.add_statusline_func('airline#extensions#wordcount#apply') - autocmd BufReadPost,CursorMoved,CursorMovedI * call s:wordcount_update() endfunction From 487d26290162e417328bc39205c9ae508f779cfd Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Tue, 18 Sep 2018 02:04:35 +0100 Subject: [PATCH 4/7] wordcount: formatter: code quality improvements - Rename new formatting function: transform() -> to_string() - Optimise separator selection - Other quality improvements --- autoload/airline/extensions/wordcount.vim | 6 ++-- .../wordcount/formatters/default.vim | 35 +++++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/autoload/airline/extensions/wordcount.vim b/autoload/airline/extensions/wordcount.vim index 4a937e8..07426ef 100644 --- a/autoload/airline/extensions/wordcount.vim +++ b/autoload/airline/extensions/wordcount.vim @@ -11,7 +11,7 @@ if exists('*wordcount') else function! s:get_wordcount(type) " index to retrieve from whitespace-separated output of g_CTRL-G - " 11 - words, 5 - visual words (in visual mode) + " 11 : words, 5 : visual words (in visual mode) let idx = (a:type == 'words') ? 11 : 5 let save_status = v:statusmsg @@ -31,7 +31,7 @@ let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default') " wrapper function for compatibility; redefined below for old-style formatters function! s:format_wordcount(type) - return airline#extensions#wordcount#formatters#{s:formatter}#transform( + return airline#extensions#wordcount#formatters#{s:formatter}#to_string( \ s:get_wordcount(a:type)) endfunction @@ -39,7 +39,7 @@ endfunction " fall back to default if s:formatter !=# 'default' execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter - if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#transform') + if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#to_string') if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#format') let s:formatter = 'default' else diff --git a/autoload/airline/extensions/wordcount/formatters/default.vim b/autoload/airline/extensions/wordcount/formatters/default.vim index 846b4e0..813fe61 100644 --- a/autoload/airline/extensions/wordcount/formatters/default.vim +++ b/autoload/airline/extensions/wordcount/formatters/default.vim @@ -6,33 +6,30 @@ scriptencoding utf-8 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) -function! airline#extensions#wordcount#formatters#default#transform(text) - if empty(a:text) +if match(get(v:, 'lang', ''), '\v\cC|en') > -1 + let s:decimal_group = ',' +elseif match(get(v:, 'lang', ''), '\v\cde|dk|fr|pt') > -1 + let s:decimal_group = '.' +else + let s:decimal_group = '' +endif + +function! airline#extensions#wordcount#formatters#default#to_string(wordcount) + if empty(a:wordcount) return endif - 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 a:text > 999 && !empty(separator) + if a:wordcount > 999 " Format number according to locale, e.g. German: 1.245 or English: 1,245 - let text = substitute(a:text, '\d\@<=\(\(\d\{3\}\)\+\)$', separator.'&', 'g') + let wordcount = substitute(a:wordcount, '\d\@<=\(\(\d\{3\}\)\+\)$', s:decimal_group.'&', 'g') else - let text = a:text + let wordcount = a:wordcount endif - let result = printf(s:fmt, a:text). result + let str = printf(s:fmt, wordcount) else - let result = printf(s:fmt_short, a:text). result + let str = printf(s:fmt_short, a:wordcount) endif - return result -endfunction - -function! s:get_decimal_group() - if match(get(v:, 'lang', ''), '\v\cC|en') > -1 - return ',' - elseif match(get(v:, 'lang', ''), '\v\cde|dk|fr|pt') > -1 - return '.' - endif - return '' + return str . g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space endfunction From 9903fee60eb1db9e9ea9d7f548b03cfc322b2b73 Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Tue, 18 Sep 2018 14:41:04 +0100 Subject: [PATCH 5/7] wordcount: Optimise formatter calls, cleanup code - Formatter is now only called when the wordcount changes - ...#apply() now only compares against the filetype list when necessary - Old format() function is no longer called for (unsupported) counting of visual words - Misc code quality improvements --- autoload/airline/extensions/wordcount.vim | 98 ++++++++++++----------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/autoload/airline/extensions/wordcount.vim b/autoload/airline/extensions/wordcount.vim index 07426ef..1b4fea4 100644 --- a/autoload/airline/extensions/wordcount.vim +++ b/autoload/airline/extensions/wordcount.vim @@ -5,14 +5,15 @@ scriptencoding utf-8 " get wordcount {{{1 if exists('*wordcount') - function! s:get_wordcount(type) - return string(wordcount()[a:type]) + function! s:get_wordcount(visual_mode_active) + let query = a:visual_mode_active ? 'visual_words' : 'words' + return string(wordcount()[query]) endfunction else - function! s:get_wordcount(type) + 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:type == 'words') ? 11 : 5 + let idx = a:visual_mode_active ? 5 : 11 let save_status = v:statusmsg execute "silent normal! g\" @@ -30,12 +31,11 @@ endif let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default') " wrapper function for compatibility; redefined below for old-style formatters -function! s:format_wordcount(type) - return airline#extensions#wordcount#formatters#{s:formatter}#to_string( - \ s:get_wordcount(a:type)) +function! s:format_wordcount(wordcount) + return airline#extensions#wordcount#formatters#{s:formatter}#to_string(a:wordcount) endfunction -" check user-defined formatter exists and has appropriate functions, otherwise +" check user-defined formatter exists with appropriate functions, otherwise " fall back to default if s:formatter !=# 'default' execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter @@ -44,8 +44,10 @@ if s:formatter !=# 'default' let s:formatter = 'default' else " redefine for backwords compatibility - function! s:format_wordcount(type) - if a:type !=# 'visual_words' + function! s:format_wordcount(_) + if mode() ==? 'v' + return b:airline_wordcount + else return airline#extensions#wordcount#formatters#{s:formatter}#format() endif endfunction @@ -54,65 +56,65 @@ if s:formatter !=# 'default' endif " update {{{1 +let s:wordcount_cache = 0 " cache wordcount for performance when force_update=0 +function! s:update_wordcount(force_update) + let wordcount = s:get_wordcount(0) + if wordcount != s:wordcount_cache || a:force_update + let s:wordcount_cache = wordcount + let b:airline_wordcount = s:format_wordcount(wordcount) + endif +endfunction + if exists('##TextChanged') let s:supported_autocmds = 'TextChanged,TextChangedI' - function! s:update_wordcount() - let b:airline_wordcount = s:format_wordcount('words') + function! s:on_check() + call s:update_wordcount(0) endfunction else let s:supported_autocmds = 'CursorMoved,CursorMovedI' - " without TextChanged a check is performed on every cursor movement, so - " cache for performance - function! s:update_wordcount() - if get(b:, 'airline_wordcount_cache', '') is# '' || - \ get(b:, 'airline_change_tick', 0) != b:changedtick || - \ get(b:, 'airline_winwidth', 0) != winwidth(0) - let b:airline_wordcount = s:format_wordcount('words') - let b:airline_wordcount_cache = b:airline_wordcount + " without TextChanged, use "b:changedtick" to track changes + function! s:on_check() + if get(b:, 'airline_change_tick', -1) != b:changedtick let b:airline_change_tick = b:changedtick - let b:airline_winwidth = winwidth(0) + call s:update_wordcount(0) endif endfunction endif " public {{{1 -" s:visual tracks visual mode +let s:visual_active = 0 " Boolean: for when to get visual wordcount function airline#extensions#wordcount#get() - return s:visual - \ ? s:format_wordcount('visual_words') - \ : get(b:, 'airline_wordcount', '') + return s:visual_active + \ ? s:format_wordcount(s:get_wordcount(1)) + \ : b:airline_wordcount endfunction " autocmds & airline functions {{{1 -function s:modify_autocmds() - if !exists('#airline_wordcount#BufEnter#') - execute 'autocmd! airline_wordcount BufEnter,'.s:supported_autocmds - \ .' nested call s:update_wordcount()' - " ensure we have a starting wordcount - call s:update_wordcount() - else - execute 'autocmd! airline_wordcount BufEnter,'.'s:supported_autocmds' - endif -endfunction - -" default filetypes +" default filetypes: let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail'] - function! airline#extensions#wordcount#apply(...) let filetypes = get(g:, 'airline#extensions#wordcount#filetypes', s:filetypes) - " filetypes used to be a regex-matching string, so check both - if type(filetypes) == v:t_list - \ && (index(filetypes, &filetype) > -1 || empty(filetypes)) - \ || type(filetypes) == v:t_string && match(&ft, filetypes) > -1 - " redo autocommands if filetype has changed - if filetypes isnot s:filetypes || did_filetype() - call s:modify_autocmds() - let s:filetypes = filetypes - endif + " check if autocmd updates are neccessary + if did_filetype() || filetypes isnot s:filetypes + let s:filetypes = filetypes + " Select test based on type of "filetypes": new=list, old=string + if type(filetypes) == v:t_list + \ ? index(filetypes, &filetype) > -1 || index(filetypes, 'all') > -1 + \ : match(&ft, filetypes) > -1 + execute 'autocmd! airline_wordcount BufEnter,'.s:supported_autocmds + \ .' nested call s:on_check()' + call s:update_wordcount(1) " force update ensures initial worcount exists + elseif exists('b:airline_wordcount') " cleanup + autocmd! airline_wordcount * + unlet b:airline_wordcount + endif + endif + + if exists('b:airline_wordcount') call airline#extensions#prepend_to_section( \ 'z', '%{airline#extensions#wordcount#get()}') endif @@ -121,7 +123,7 @@ endfunction function! airline#extensions#wordcount#init(ext) augroup airline_wordcount autocmd! User AirlineModeChanged nested - \ let s:visual = (mode() ==? 'v' || mode() ==? 's') + \ let s:visual_active = (mode() ==? 'v' || mode() ==? 's') augroup END call a:ext.add_statusline_func('airline#extensions#wordcount#apply') endfunction From c33c1de079ca7ac8143fd4de00fd3dbfbdf87c5b Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Tue, 18 Sep 2018 16:05:01 +0100 Subject: [PATCH 6/7] wordcount: Simplify update strategy Checking for wordcount changes now uses a b:changedtick comparison in the statusline funcref. The autocommand strategy that used to do this is removed, simplifying the code. --- autoload/airline/extensions/wordcount.vim | 45 ++++++++--------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/autoload/airline/extensions/wordcount.vim b/autoload/airline/extensions/wordcount.vim index 1b4fea4..7486929 100644 --- a/autoload/airline/extensions/wordcount.vim +++ b/autoload/airline/extensions/wordcount.vim @@ -65,51 +65,36 @@ function! s:update_wordcount(force_update) endif endfunction -if exists('##TextChanged') - let s:supported_autocmds = 'TextChanged,TextChangedI' - - function! s:on_check() - call s:update_wordcount(0) - endfunction -else - let s:supported_autocmds = 'CursorMoved,CursorMovedI' - - " without TextChanged, use "b:changedtick" to track changes - function! s:on_check() - if get(b:, 'airline_change_tick', -1) != b:changedtick - let b:airline_change_tick = b:changedtick - call s:update_wordcount(0) - endif - endfunction -endif - -" public {{{1 let s:visual_active = 0 " Boolean: for when to get visual wordcount function airline#extensions#wordcount#get() - return s:visual_active - \ ? s:format_wordcount(s:get_wordcount(1)) - \ : b:airline_wordcount + if s:visual_active + return s:format_wordcount(s:get_wordcount(1)) + else + if b:airline_changedtick != b:changedtick + call s:update_wordcount(0) + let b:airline_changedtick = b:changedtick + endif + return b:airline_wordcount + endif endfunction -" autocmds & airline functions {{{1 +" airline functions {{{1 " default filetypes: let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail'] function! airline#extensions#wordcount#apply(...) let filetypes = get(g:, 'airline#extensions#wordcount#filetypes', s:filetypes) - " check if autocmd updates are neccessary + " Check if filetype needs testing if did_filetype() || filetypes isnot s:filetypes let s:filetypes = filetypes " Select test based on type of "filetypes": new=list, old=string if type(filetypes) == v:t_list \ ? index(filetypes, &filetype) > -1 || index(filetypes, 'all') > -1 - \ : match(&ft, filetypes) > -1 - execute 'autocmd! airline_wordcount BufEnter,'.s:supported_autocmds - \ .' nested call s:on_check()' - call s:update_wordcount(1) " force update ensures initial worcount exists - elseif exists('b:airline_wordcount') " cleanup - autocmd! airline_wordcount * + \ : match(&filetype, filetypes) > -1 + let b:airline_changedtick = -1 + call s:update_wordcount(1) " force update: ensures initial worcount exists + elseif exists('b:airline_wordcount') " cleanup when filetype is removed unlet b:airline_wordcount endif endif From 0756b9a4aa3c999a9fa36f0be8bc2bd9654554ee Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Tue, 18 Sep 2018 20:09:22 +0100 Subject: [PATCH 7/7] wordcount: Re-enable updating the format string --- .../extensions/wordcount/formatters/default.vim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/autoload/airline/extensions/wordcount/formatters/default.vim b/autoload/airline/extensions/wordcount/formatters/default.vim index 813fe61..a2b3935 100644 --- a/autoload/airline/extensions/wordcount/formatters/default.vim +++ b/autoload/airline/extensions/wordcount/formatters/default.vim @@ -3,8 +3,14 @@ scriptencoding utf-8 -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) +function s: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() +call airline#add_statusline_funcref(function('s:update_fmt')) if match(get(v:, 'lang', ''), '\v\cC|en') > -1 let s:decimal_group = ','