download

Sign in or create your account | Project List | Help

download Git Source Tree

Root/auto_wc.vim

1scriptencoding utf-8
2if &cp || exists("g:auto_wc_loaded")
3    finish
4endif
5let g:auto_wc_loaded = 1
6
7augroup WC
8    autocmd!
9    autocmd BufUnload,FileWritePre,BufWritePre * call <SID>WC()
10augroup END
11
12function! 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
23endfunction
24
25function! 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
35endfunction
36
37function! 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
45endfunction
46
47finish
48
49==============================================================================
50auto_wc.vim : ファイルの文字数を自動的に集計する
51------------------------------------------------------------------------------
52$VIMRUNTIMEPATH/plugin/auto_wc.vim
53==============================================================================
54author : 小見 拓
55url : http://nanasi.jp/
56email : mail@nanasi.jp
57version : 2009/12/19 16:00:00
58==============================================================================
59ファイルに
60WC:[1920/4200]:
61という行を書いておくと、左側ファイル文字数カウンタが
62ファイル保存時に自動的に更新される。
63右側数値は、入力可能な最大文字数を想定。
64
65==============================================================================
66" vim: set et ft=vim nowrap :
67

Archive Download this file

Branches:
master