2021-01-01 20:57:00 +08:00
|
|
|
" MIT License. Copyright (c) 2013-2021 Bailey Ling et al.
|
2018-07-01 01:34:12 +08:00
|
|
|
" vim: et ts=2 sts=2 sw=2 fdm=marker
|
2015-10-05 22:17:04 +08:00
|
|
|
|
2016-09-24 08:16:30 +08:00
|
|
|
scriptencoding utf-8
|
|
|
|
|
2018-07-01 01:34:12 +08:00
|
|
|
" get wordcount {{{1
|
|
|
|
if exists('*wordcount')
|
2018-09-18 21:41:04 +08:00
|
|
|
function! s:get_wordcount(visual_mode_active)
|
2019-08-20 20:28:54 +08:00
|
|
|
if get(g:, 'actual_curbuf', '') != bufnr('')
|
|
|
|
return
|
|
|
|
endif
|
2021-11-12 05:47:54 +08:00
|
|
|
if &filetype ==# 'tex' && exists('b:vimtex') && get(g:, 'airline#extensions#vimtex#wordcount', 0)
|
|
|
|
" We're in a TeX file and vimtex is a plugin, so use vimtex's wordcount...
|
2021-11-03 05:27:37 +08:00
|
|
|
return vimtex#misc#wordcount()
|
|
|
|
else
|
|
|
|
let query = a:visual_mode_active ? 'visual_words' : 'words'
|
|
|
|
return get(wordcount(), query, 0)
|
|
|
|
endif
|
2018-07-01 01:34:12 +08:00
|
|
|
endfunction
|
2018-09-22 10:34:27 +08:00
|
|
|
else " Pull wordcount from the g_ctrl-g stats
|
2018-09-18 21:41:04 +08:00
|
|
|
function! s:get_wordcount(visual_mode_active)
|
2018-09-22 10:34:27 +08:00
|
|
|
let pattern = a:visual_mode_active
|
2018-09-24 02:07:09 +08:00
|
|
|
\ ? '^.\D*\d\+\D\+\d\+\D\+\zs\d\+'
|
|
|
|
\ : '^.\D*\%(\d\+\D\+\)\{5}\zs\d\+'
|
2018-07-01 01:34:12 +08:00
|
|
|
|
|
|
|
let save_status = v:statusmsg
|
2018-09-22 10:34:27 +08:00
|
|
|
if !a:visual_mode_active && col('.') == col('$')
|
|
|
|
let save_pos = getpos('.')
|
|
|
|
execute "silent normal! g\<c-g>"
|
|
|
|
call setpos('.', save_pos)
|
|
|
|
else
|
|
|
|
execute "silent normal! g\<c-g>"
|
|
|
|
endif
|
|
|
|
let stats = v:statusmsg
|
2018-07-01 01:34:12 +08:00
|
|
|
let v:statusmsg = save_status
|
|
|
|
|
2018-09-22 10:34:27 +08:00
|
|
|
return str2nr(matchstr(stats, pattern))
|
2018-07-01 01:34:12 +08:00
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
|
|
|
|
" format {{{1
|
2016-01-26 04:00:05 +08:00
|
|
|
let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default')
|
2015-10-05 22:17:04 +08:00
|
|
|
|
2018-07-01 01:34:12 +08:00
|
|
|
" wrapper function for compatibility; redefined below for old-style formatters
|
2018-09-18 21:41:04 +08:00
|
|
|
function! s:format_wordcount(wordcount)
|
|
|
|
return airline#extensions#wordcount#formatters#{s:formatter}#to_string(a:wordcount)
|
2018-07-01 01:34:12 +08:00
|
|
|
endfunction
|
|
|
|
|
2018-09-18 21:41:04 +08:00
|
|
|
" check user-defined formatter exists with appropriate functions, otherwise
|
2018-07-01 01:34:12 +08:00
|
|
|
" fall back to default
|
|
|
|
if s:formatter !=# 'default'
|
2019-04-10 02:48:01 +08:00
|
|
|
execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter.'.vim'
|
2018-09-18 09:04:35 +08:00
|
|
|
if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#to_string')
|
2018-07-01 01:34:12 +08:00
|
|
|
if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#format')
|
|
|
|
let s:formatter = 'default'
|
|
|
|
else
|
|
|
|
" redefine for backwords compatibility
|
2018-09-18 21:41:04 +08:00
|
|
|
function! s:format_wordcount(_)
|
|
|
|
if mode() ==? 'v'
|
|
|
|
return b:airline_wordcount
|
|
|
|
else
|
2018-07-01 01:34:12 +08:00
|
|
|
return airline#extensions#wordcount#formatters#{s:formatter}#format()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
" update {{{1
|
2018-09-18 21:41:04 +08: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-07-01 02:40:39 +08:00
|
|
|
function airline#extensions#wordcount#get()
|
2019-02-04 15:25:45 +08:00
|
|
|
if get(g:, 'airline#visual_active', 0)
|
2018-09-18 23:05:01 +08:00
|
|
|
return s:format_wordcount(s:get_wordcount(1))
|
|
|
|
else
|
2018-12-10 16:11:10 +08:00
|
|
|
if get(b:, 'airline_changedtick', 0) != b:changedtick
|
2018-09-18 23:05:01 +08:00
|
|
|
call s:update_wordcount(0)
|
|
|
|
let b:airline_changedtick = b:changedtick
|
|
|
|
endif
|
2018-12-10 16:11:10 +08:00
|
|
|
return get(b:, 'airline_wordcount', '')
|
2018-09-18 23:05:01 +08:00
|
|
|
endif
|
2015-10-05 22:17:04 +08:00
|
|
|
endfunction
|
|
|
|
|
2018-09-18 23:05:01 +08:00
|
|
|
" airline functions {{{1
|
2018-09-18 21:41:04 +08:00
|
|
|
" default filetypes:
|
2015-10-05 22:17:04 +08:00
|
|
|
function! airline#extensions#wordcount#apply(...)
|
2022-10-01 11:25:47 +08:00
|
|
|
let filetypes = get(g:, 'airline#extensions#wordcount#filetypes',
|
2022-10-01 11:14:43 +08:00
|
|
|
\ ['asciidoc', 'help', 'mail', 'markdown', 'rmd', 'nroff', 'org', 'rst', 'plaintex', 'tex', 'text'])
|
2019-03-19 15:28:51 +08:00
|
|
|
" export current filetypes settings to global namespace
|
|
|
|
let g:airline#extensions#wordcount#filetypes = filetypes
|
2018-07-01 02:40:39 +08:00
|
|
|
|
2018-09-18 23:05:01 +08:00
|
|
|
" Check if filetype needs testing
|
2019-03-19 15:28:51 +08:00
|
|
|
if did_filetype()
|
2020-02-21 02:20:00 +08:00
|
|
|
" correctly test for compound filetypes (e.g. markdown.pandoc)
|
|
|
|
let ft = substitute(&filetype, '\.', '\\|', 'g')
|
2018-09-18 21:41:04 +08:00
|
|
|
|
|
|
|
" Select test based on type of "filetypes": new=list, old=string
|
2018-09-20 17:44:17 +08:00
|
|
|
if type(filetypes) == get(v:, 't_list', type([]))
|
2020-04-15 01:53:18 +08:00
|
|
|
\ ? match(filetypes, '\<'. ft. '\>') > -1 || index(filetypes, 'all') > -1
|
2018-09-18 23:05:01 +08:00
|
|
|
\ : 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
|
2018-09-18 21:41:04 +08:00
|
|
|
unlet b:airline_wordcount
|
2018-07-01 02:40:39 +08:00
|
|
|
endif
|
2018-09-18 21:41:04 +08:00
|
|
|
endif
|
2018-07-01 02:40:39 +08:00
|
|
|
|
2018-09-18 21:41:04 +08:00
|
|
|
if exists('b:airline_wordcount')
|
2018-07-01 02:40:39 +08:00
|
|
|
call airline#extensions#prepend_to_section(
|
|
|
|
\ 'z', '%{airline#extensions#wordcount#get()}')
|
2015-10-05 22:17:04 +08:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#wordcount#init(ext)
|
|
|
|
call a:ext.add_statusline_func('airline#extensions#wordcount#apply')
|
|
|
|
endfunction
|