2018-01-05 04:37:59 -05:00
|
|
|
" MIT License. Copyright (c) 2013-2018 Bailey Ling et al.
|
2018-06-30 13:34:12 -04:00
|
|
|
" vim: et ts=2 sts=2 sw=2 fdm=marker
|
2015-10-05 10:17:04 -04:00
|
|
|
|
2016-09-23 20:16:30 -04:00
|
|
|
scriptencoding utf-8
|
|
|
|
|
2018-06-30 13:34:12 -04:00
|
|
|
" 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
|
2018-09-17 21:04:35 -04:00
|
|
|
" 11 : words, 5 : visual words (in visual mode)
|
2018-06-30 13:34:12 -04:00
|
|
|
let idx = (a:type == 'words') ? 11 : 5
|
|
|
|
|
|
|
|
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 15:00:05 -05:00
|
|
|
let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default')
|
2015-10-05 10:17:04 -04:00
|
|
|
|
2018-06-30 13:34:12 -04:00
|
|
|
" wrapper function for compatibility; redefined below for old-style formatters
|
|
|
|
function! s:format_wordcount(type)
|
2018-09-17 21:04:35 -04:00
|
|
|
return airline#extensions#wordcount#formatters#{s:formatter}#to_string(
|
2018-06-30 13:34:12 -04:00
|
|
|
\ 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
|
2018-09-17 21:04:35 -04:00
|
|
|
if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#to_string')
|
2018-06-30 13:34:12 -04:00
|
|
|
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
|
2018-06-30 14:40:39 -04:00
|
|
|
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
|
2016-01-25 15:00:05 -05:00
|
|
|
let b:airline_change_tick = b:changedtick
|
2018-06-30 14:40:39 -04:00
|
|
|
let b:airline_winwidth = winwidth(0)
|
2016-01-15 06:54:46 -05:00
|
|
|
endif
|
2018-06-30 14:40:39 -04:00
|
|
|
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', '')
|
2015-10-05 10:17:04 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-06-30 13:34:12 -04:00
|
|
|
" autocmds & airline functions {{{1
|
2018-06-30 14:40:39 -04:00
|
|
|
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
|
|
|
|
|
2018-06-30 13:34:12 -04:00
|
|
|
" default filetypes
|
2018-06-30 14:40:39 -04:00
|
|
|
let s:filetypes = ['help', 'markdown', 'rst', 'org', 'text', 'asciidoc', 'tex', 'mail']
|
2018-06-30 13:34:12 -04:00
|
|
|
|
2015-10-05 10:17:04 -04:00
|
|
|
function! airline#extensions#wordcount#apply(...)
|
2018-06-30 14:40:39 -04:00
|
|
|
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()}')
|
2015-10-05 10:17:04 -04:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#wordcount#init(ext)
|
2018-06-30 14:40:39 -04:00
|
|
|
augroup airline_wordcount
|
|
|
|
autocmd! User AirlineModeChanged nested
|
|
|
|
\ let s:visual = (mode() ==? 'v' || mode() ==? 's')
|
|
|
|
augroup END
|
2015-10-05 10:17:04 -04:00
|
|
|
call a:ext.add_statusline_func('airline#extensions#wordcount#apply')
|
|
|
|
endfunction
|