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
This commit is contained in:
parent
487d262901
commit
9903fee60e
@ -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\<cn-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
|
||||
\ .' <buffer> 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()
|
||||
" 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
|
||||
\ .' <buffer> 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 * <buffer>
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user