Improved VimtexWordCount (fixes #166)

This commit is contained in:
Karl Yngve Lervåg 2015-05-16 22:14:52 +02:00
parent 9b5b013953
commit b084da901b
2 changed files with 54 additions and 12 deletions

View File

@ -64,6 +64,51 @@ function! vimtex#info() " {{{1
endfor
endfunction
" }}}1
function! vimtex#wordcount(detailed) " {{{1
" Run texcount, save output to lines variable
let data = g:vimtex#data[b:vimtex.id]
let cmd = 'cd ' . vimtex#util#fnameescape(data.root)
let cmd .= '; texcount -nosub -sum '
let cmd .= a:detailed > 0 ? '-inc ' : '-merge '
let cmd .= vimtex#util#fnameescape(data.base)
let lines = split(system(cmd), '\n')
" Create wordcount window
if bufnr('TeXcount') >= 0
bwipeout TeXcount
endif
split TeXcount
" Add lines to buffer
for line in lines
call append('$', printf('%s', line))
endfor
0delete _
" Set mappings
nnoremap <buffer> <silent> q :bwipeout<cr>
" Set buffer options
setlocal bufhidden=wipe
setlocal buftype=nofile
setlocal cursorline
setlocal listchars=
setlocal nobuflisted
setlocal nolist
setlocal nospell
setlocal noswapfile
setlocal nowrap
setlocal tabstop=8
setlocal nomodifiable
" Set highlighting
syntax match TexcountText /^.*:.*/ contains=TexcountValue
syntax match TexcountValue /.*:\zs.*/
highlight link TexcountText VimtexMsg
highlight link TexcountValue Constant
endfunction
" }}}1
function! s:init_environment() " {{{1
@ -91,7 +136,6 @@ function! s:init_environment() " {{{1
function data.out() dict
return s:get_main_ext(self, 'pdf')
endfunction
let data.words = function('s:get_wordcount')
call add(g:vimtex#data, data)
let b:vimtex.id = len(g:vimtex#data) - 1
@ -99,8 +143,7 @@ function! s:init_environment() " {{{1
" Define commands
command! -buffer VimtexInfo call vimtex#info()
command! -buffer VimtexWordCount
\ echo g:vimtex#data[b:vimtex.id].words()
command! -buffer -bang VimtexWordCount call vimtex#wordcount(<q-bang> == "!")
" Define mappings
nnoremap <buffer> <plug>(vimtex-info) :call vimtex#info()<cr>
@ -295,14 +338,6 @@ function! s:get_main_ext(self, ext) " {{{1
return ''
endfunction
" }}}1
function! s:get_wordcount() dict " {{{1
let cmd = 'cd ' . vimtex#util#fnameescape(self.root)
let cmd .= '; texcount -sum -brief -merge '
\ . vimtex#util#fnameescape(self.base)
return str2nr(matchstr(system(cmd), '^\d\+'))
endfunction
" }}}1
function! s:print_dict(dict, ...) " {{{1

View File

@ -735,7 +735,14 @@ Commands~
The count is created with `texcount` through a call
on the main project file similar to: >
texcount -sum -brief -merge FILE
texcount -nosub -sub -merge FILE
<
*VimtexWordCount!*
:VimtexWordCount! Similar to |VimtexWordCount|, but show separate
reports for included files. I.e. presents the
result of >
texcount -nosub -sub -inc FILE
<
------------------------------------------------------------------------------
Map definitions~