wordcount.vim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. " MIT License. Copyright (c) 2013-2021 Bailey Ling et al.
  2. " vim: et ts=2 sts=2 sw=2 fdm=marker
  3. scriptencoding utf-8
  4. " get wordcount {{{1
  5. if exists('*wordcount')
  6. function! s:get_wordcount(visual_mode_active)
  7. if get(g:, 'actual_curbuf', '') != bufnr('')
  8. return
  9. endif
  10. if &filetype ==# 'tex' && exists('b:vimtex') && get(g:, 'airline#extensions#vimtex#wordcount', 0)
  11. " We're in a TeX file and vimtex is a plugin, so use vimtex's wordcount...
  12. if a:visual_mode_active
  13. " not useful?
  14. return
  15. else
  16. return vimtex#misc#wordcount()
  17. endif
  18. else
  19. let query = a:visual_mode_active ? 'visual_words' : 'words'
  20. return get(wordcount(), query, 0)
  21. endif
  22. endfunction
  23. else " Pull wordcount from the g_ctrl-g stats
  24. function! s:get_wordcount(visual_mode_active)
  25. let pattern = a:visual_mode_active
  26. \ ? '^.\D*\d\+\D\+\d\+\D\+\zs\d\+'
  27. \ : '^.\D*\%(\d\+\D\+\)\{5}\zs\d\+'
  28. let save_status = v:statusmsg
  29. if !a:visual_mode_active && col('.') == col('$')
  30. let save_pos = getpos('.')
  31. execute "silent normal! g\<c-g>"
  32. call setpos('.', save_pos)
  33. else
  34. execute "silent normal! g\<c-g>"
  35. endif
  36. let stats = v:statusmsg
  37. let v:statusmsg = save_status
  38. return str2nr(matchstr(stats, pattern))
  39. endfunction
  40. endif
  41. " format {{{1
  42. let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default')
  43. " wrapper function for compatibility; redefined below for old-style formatters
  44. function! s:format_wordcount(wordcount)
  45. return airline#extensions#wordcount#formatters#{s:formatter}#to_string(a:wordcount)
  46. endfunction
  47. " check user-defined formatter exists with appropriate functions, otherwise
  48. " fall back to default
  49. if s:formatter !=# 'default'
  50. execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter.'.vim'
  51. if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#to_string')
  52. if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#format')
  53. let s:formatter = 'default'
  54. else
  55. " redefine for backwords compatibility
  56. function! s:format_wordcount(_)
  57. if mode() ==? 'v'
  58. return b:airline_wordcount
  59. else
  60. return airline#extensions#wordcount#formatters#{s:formatter}#format()
  61. endif
  62. endfunction
  63. endif
  64. endif
  65. endif
  66. " update {{{1
  67. let s:wordcount_cache = 0 " cache wordcount for performance when force_update=0
  68. function! s:update_wordcount(force_update)
  69. let wordcount = s:get_wordcount(0)
  70. if wordcount != s:wordcount_cache || a:force_update
  71. let s:wordcount_cache = wordcount
  72. let b:airline_wordcount = s:format_wordcount(wordcount)
  73. endif
  74. endfunction
  75. function airline#extensions#wordcount#get()
  76. if get(g:, 'airline#visual_active', 0)
  77. return s:format_wordcount(s:get_wordcount(1))
  78. else
  79. if get(b:, 'airline_changedtick', 0) != b:changedtick
  80. call s:update_wordcount(0)
  81. let b:airline_changedtick = b:changedtick
  82. endif
  83. return get(b:, 'airline_wordcount', '')
  84. endif
  85. endfunction
  86. " airline functions {{{1
  87. " default filetypes:
  88. function! airline#extensions#wordcount#apply(...)
  89. let filetypes = get(g:, 'airline#extensions#wordcount#filetypes',
  90. \ ['asciidoc', 'help', 'mail', 'markdown', 'rmd', 'nroff', 'org', 'rst', 'plaintex', 'tex', 'text'])
  91. " export current filetypes settings to global namespace
  92. let g:airline#extensions#wordcount#filetypes = filetypes
  93. " Check if filetype needs testing
  94. if did_filetype()
  95. " correctly test for compound filetypes (e.g. markdown.pandoc)
  96. let ft = substitute(&filetype, '\.', '\\|', 'g')
  97. " Select test based on type of "filetypes": new=list, old=string
  98. if type(filetypes) == get(v:, 't_list', type([]))
  99. \ ? match(filetypes, '\<'. ft. '\>') > -1 || index(filetypes, 'all') > -1
  100. \ : match(&filetype, filetypes) > -1
  101. let b:airline_changedtick = -1
  102. call s:update_wordcount(1) " force update: ensures initial worcount exists
  103. elseif exists('b:airline_wordcount') " cleanup when filetype is removed
  104. unlet b:airline_wordcount
  105. endif
  106. endif
  107. if exists('b:airline_wordcount')
  108. call airline#extensions#prepend_to_section(
  109. \ 'z', '%{airline#extensions#wordcount#get()}')
  110. endif
  111. endfunction
  112. function! airline#extensions#wordcount#init(ext)
  113. call a:ext.add_statusline_func('airline#extensions#wordcount#apply')
  114. endfunction