| 1 | scriptencoding utf-8 |
| 2 | if &cp || exists("g:auto_wc_loaded") |
| 3 | finish |
| 4 | endif |
| 5 | let g:auto_wc_loaded = 1 |
| 6 | |
| 7 | augroup WC |
| 8 | autocmd! |
| 9 | autocmd BufUnload,FileWritePre,BufWritePre * call <SID>WC() |
| 10 | augroup END |
| 11 | |
| 12 | function! s:WC() |
| 13 | if &modifiable |
| 14 | let l:current = 0 |
| 15 | let l:last = line('$') |
| 16 | let l:charcount = s:CharCount() |
| 17 | while l:current <= l:last |
| 18 | let l:line = getline(l:current) |
| 19 | call s:SearchAndReplace(l:line, l:current, l:charcount) |
| 20 | let l:current += 1 |
| 21 | endwhile |
| 22 | endif |
| 23 | endfunction |
| 24 | |
| 25 | function! s:CharCount() |
| 26 | let l:count = 0 |
| 27 | let l:current = 0 |
| 28 | let l:last = line('$') |
| 29 | while l:current <= l:last |
| 30 | let l:line = getline(l:current) |
| 31 | let l:count += strlen(substitute(l:line, ".", "x", "g")) |
| 32 | let l:current += 1 |
| 33 | endwhile |
| 34 | return l:count |
| 35 | endfunction |
| 36 | |
| 37 | function! s:SearchAndReplace(linetext, lineno, charcount) |
| 38 | let l:found = match(a:linetext, 'WC:\[\d\{1,}/\d\{1,}]:') |
| 39 | if l:found >= 0 |
| 40 | let l:pre = substitute(a:linetext, '\(^.*WC:\[\)\d\{1,}\(/\d\{1,}]:.*$\)', '\1', '') |
| 41 | let l:post = substitute(a:linetext, '\(^.*WC:\[\)\d\{1,}\(/\d\{1,}]:.*$\)', '\2', '') |
| 42 | let l:newline = l:pre . a:charcount . l:post |
| 43 | call setline(a:lineno, l:newline) |
| 44 | endif |
| 45 | endfunction |
| 46 | |
| 47 | finish |
| 48 | |
| 49 | ============================================================================== |
| 50 | auto_wc.vim : ファイルの文字数を自動的に集計する |
| 51 | ------------------------------------------------------------------------------ |
| 52 | $VIMRUNTIMEPATH/plugin/auto_wc.vim |
| 53 | ============================================================================== |
| 54 | author : 小見 拓 |
| 55 | url : http://nanasi.jp/ |
| 56 | email : mail@nanasi.jp |
| 57 | version : 2009/12/19 16:00:00 |
| 58 | ============================================================================== |
| 59 | ファイルに |
| 60 | WC:[1920/4200]: |
| 61 | という行を書いておくと、左側ファイル文字数カウンタが |
| 62 | ファイル保存時に自動的に更新される。 |
| 63 | 右側数値は、入力可能な最大文字数を想定。 |
| 64 | |
| 65 | ============================================================================== |
| 66 | " vim: set et ft=vim nowrap : |
| 67 | |