From b084da901baa92feb9bf724eaa6044f3369d9102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 16 May 2015 22:14:52 +0200 Subject: [PATCH] Improved VimtexWordCount (fixes #166) --- autoload/vimtex.vim | 57 ++++++++++++++++++++++++++++++++++++--------- doc/vimtex.txt | 9 ++++++- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/autoload/vimtex.vim b/autoload/vimtex.vim index 61adb0c..c9f32f7 100644 --- a/autoload/vimtex.vim +++ b/autoload/vimtex.vim @@ -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 q :bwipeout + + " 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( == "!") " Define mappings nnoremap (vimtex-info) :call vimtex#info() @@ -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 diff --git a/doc/vimtex.txt b/doc/vimtex.txt index 0203e22..554e220 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -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~