2018-01-05 10:37:59 +01:00
|
|
|
" MIT License. Copyright (c) 2013-2018 Bailey Ling et al.
|
2018-06-30 18:34:12 +01:00
|
|
|
" vim: et ts=2 sts=2 sw=2 fdm=marker
|
2015-10-05 10:17:04 -04:00
|
|
|
|
2016-09-24 08:16:30 +08:00
|
|
|
scriptencoding utf-8
|
|
|
|
|
2018-06-30 18:34:12 +01:00
|
|
|
" get wordcount {{{1
|
|
|
|
if exists('*wordcount')
|
2018-09-18 14:41:04 +01:00
|
|
|
function! s:get_wordcount(visual_mode_active)
|
|
|
|
let query = a:visual_mode_active ? 'visual_words' : 'words'
|
|
|
|
return string(wordcount()[query])
|
2018-06-30 18:34:12 +01:00
|
|
|
endfunction
|
|
|
|
else
|
2018-09-18 14:41:04 +01:00
|
|
|
function! s:get_wordcount(visual_mode_active)
|
2018-06-30 18:34:12 +01:00
|
|
|
" index to retrieve from whitespace-separated output of g_CTRL-G
|
2018-09-18 02:04:35 +01:00
|
|
|
" 11 : words, 5 : visual words (in visual mode)
|
2018-09-18 14:41:04 +01:00
|
|
|
let idx = a:visual_mode_active ? 5 : 11
|
2018-06-30 18:34:12 +01:00
|
|
|
|
|
|
|
let save_status = v:statusmsg
|
|
|
|
execute "silent normal! g\<cn-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
|
2016-01-25 21:00:05 +01:00
|
|
|
let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default')
|
2015-10-05 10:17:04 -04:00
|
|
|
|
2018-06-30 18:34:12 +01:00
|
|
|
" wrapper function for compatibility; redefined below for old-style formatters
|
2018-09-18 14:41:04 +01:00
|
|
|
function! s:format_wordcount(wordcount)
|
|
|
|
return airline#extensions#wordcount#formatters#{s:formatter}#to_string(a:wordcount)
|
2018-06-30 18:34:12 +01:00
|
|
|
endfunction
|
|
|
|
|
2018-09-18 14:41:04 +01:00
|
|
|
" check user-defined formatter exists with appropriate functions, otherwise
|
2018-06-30 18:34:12 +01:00
|
|
|
" fall back to default
|
|
|
|
if s:formatter !=# 'default'
|
|
|
|
execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter
|
2018-09-18 02:04:35 +01:00
|
|
|
if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#to_string')
|
2018-06-30 18:34:12 +01:00
|
|
|
if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#format')
|
|
|
|
let s:formatter = 'default'
|
|
|
|
else
|
|
|
|
" redefine for backwords compatibility
|
2018-09-18 14:41:04 +01:00
|
|
|
function! s:format_wordcount(_)
|
|
|
|
if mode() ==? 'v'
|
|
|
|
return b:airline_wordcount
|
|
|
|
else
|
2018-06-30 18:34:12 +01:00
|
|
|
return airline#extensions#wordcount#formatters#{s:formatter}#format()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
" update {{{1
|
2018-09-18 14:41:04 +01:00
|
|
|
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
|
|
|
|
|
2018-06-30 19:40:39 +01:00
|
|
|
if exists('##TextChanged')
|
|
|
|
let s:supported_autocmds = 'TextChanged,TextChangedI'
|
|
|
|
|
2018-09-18 14:41:04 +01:00
|
|
|
function! s:on_check()
|
|
|
|
call s:update_wordcount(0)
|
2018-06-30 19:40:39 +01:00
|
|
|
endfunction
|
|
|
|
else
|
|
|
|
let s:supported_autocmds = 'CursorMoved,CursorMovedI'
|
|
|
|
|
2018-09-18 14:41:04 +01:00
|
|
|
" without TextChanged, use "b:changedtick" to track changes
|
|
|
|
function! s:on_check()
|
|
|
|
if get(b:, 'airline_change_tick', -1) != b:changedtick
|
2016-01-25 21:00:05 +01:00
|
|
|
let b:airline_change_tick = b:changedtick
|
2018-09-18 14:41:04 +01:00
|
|
|
call s:update_wordcount(0)
|
2016-01-15 12:54:46 +01:00
|
|
|
endif
|
2018-06-30 19:40:39 +01:00
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
|
|
|
" public {{{1
|
2018-09-18 14:41:04 +01:00
|
|
|
let s:visual_active = 0 " Boolean: for when to get visual wordcount
|
2018-06-30 19:40:39 +01:00
|
|
|
function airline#extensions#wordcount#get()
|
2018-09-18 14:41:04 +01:00
|
|
|
return s:visual_active
|
|
|
|
\ ? s:format_wordcount(s:get_wordcount(1))
|
|
|
|
\ : b:airline_wordcount
|
2015-10-05 10:17:04 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-06-30 18:34:12 +01:00
|
|
|
" autocmds & airline functions {{{1
|
2018-09-18 14:41:04 +01:00
|
|
|
" default filetypes:
|
2018-06-30 19:40:39 +01:00
|
|
|
let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail']
|
2015-10-05 10:17:04 -04:00
|
|
|
function! airline#extensions#wordcount#apply(...)
|
2018-06-30 19:40:39 +01:00
|
|
|
let filetypes = get(g:, 'airline#extensions#wordcount#filetypes', s:filetypes)
|
|
|
|
|
2018-09-18 14:41:04 +01:00
|
|
|
" 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
|
2018-06-30 19:40:39 +01:00
|
|
|
endif
|
2018-09-18 14:41:04 +01:00
|
|
|
endif
|
2018-06-30 19:40:39 +01:00
|
|
|
|
2018-09-18 14:41:04 +01:00
|
|
|
if exists('b:airline_wordcount')
|
2018-06-30 19:40:39 +01:00
|
|
|
call airline#extensions#prepend_to_section(
|
|
|
|
\ 'z', '%{airline#extensions#wordcount#get()}')
|
2015-10-05 10:17:04 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#wordcount#init(ext)
|
2018-06-30 19:40:39 +01:00
|
|
|
augroup airline_wordcount
|
|
|
|
autocmd! User AirlineModeChanged nested
|
2018-09-18 14:41:04 +01:00
|
|
|
\ let s:visual_active = (mode() ==? 'v' || mode() ==? 's')
|
2018-06-30 19:40:39 +01:00
|
|
|
augroup END
|
2015-10-05 10:17:04 -04:00
|
|
|
call a:ext.add_statusline_func('airline#extensions#wordcount#apply')
|
|
|
|
endfunction
|