1 " vi:set ts=8 sts=2 sw=2 tw=0:
4 " Verify the file is truly in 'fileencoding' encoding.
6 " Maintainer: MURAOKA Taro <koron@tka.att.ne.jp>
7 " Last Change: 27-Jul-2003.
8 " Options: 'verifyenc_enable' When 0, checking become disable.
9 " 'verifyenc_maxlines' Maximum range to check (for speed).
11 " To make vim NOT TO LOAD this plugin, write next line in your .vimrc:
12 " :let plugin_verifyenc_disable = 1
14 if exists('plugin_verifyenc_disable')
20 if !exists("verifyenc_enable")
21 let verifyenc_enable = 1
23 if !exists("verifyenc_maxlines")
24 let verifyenc_maxlines = 100
27 "-----------------------------------------------------------------------------
29 command! -nargs=? VerifyEnc call <SID>Status(<q-args>)
31 " Set autocmd for VerifyEncoding
33 augroup VerifyEncoding
35 autocmd BufReadPost * silent call <SID>VerifyEncoding()
39 function! s:Status(argv)
41 let g:verifyenc_enable = g:verifyenc_enable
42 elseif a:argv ==? 'on'
43 let g:verifyenc_enable = 1
44 elseif a:argv ==? 'off'
45 let g:verifyenc_enable = 0
46 elseif a:argv =~? '^t\%[oggle]$'
47 let g:verifyenc_enable = g:verifyenc_enable ? 0 : 1
49 echo 'Verify encoding is *'.(g:verifyenc_enable ? 'ON' : 'OFF').'*'
52 function! s:VerifyEncoding()
53 if !has('iconv') || &modifiable == 0 || g:verifyenc_enable == 0
56 " Check if empty file.
57 if &fileencoding != '' && line2byte(1) < 0
62 " Check whether multibyte is exists or not.
63 if &fileencoding != '' && &fileencoding !~ '^ucs' && s:Has_multibyte_character()
65 let b:verifyenc = 'NO MULTIBYTE'
69 " Check to be force euc-jp
70 if &encoding =~# '^euc-\%(jp\|jisx0213\)$' && s:Verify_euc_jp()
72 let b:verifyenc = 'FORCE EUC-JP'
77 let b:verifyenc = 'NONE'
80 "-----------------------------------------------------------------------------
83 function! s:Has_multibyte_character()
84 if &fileencoding == '' && &encoding == &fileencoding
89 if search("[^\t -~]", 'w') > 0
90 call cursor(lnum, cnum)
93 " No multibyte characters, then set 'fileencoding' to NULL
94 let &fileencoding = ""
99 "-----------------------------------------------------------------------------
102 let s:mx_euc_kana = '['.nr2char(0x8ea4).nr2char(0x8ea5).']'.'\%([^\t -~]\)'
105 function! CheckEucEUC()
106 echo "charlen=".strlen(substitute(substitute(getline('.'),'[\t -~]', '', 'g'), '.', "\1", 'g'))
107 echo "kanalen=".strlen(substitute(substitute(getline('.'), s:mx_euc_kana, "\1", 'g'), "[^\1]", '', 'g'))
111 function! s:Verify_euc_jp()
112 if &encoding =~# '^euc-\%(jp\|jisx0213\)$' && &fileencoding != '' && &encoding != &fileencoding
114 let rangelast = line('$')
115 if rangelast > g:verifyenc_maxlines
116 let rangelast = g:verifyenc_maxlines
120 while linenum <= rangelast
121 let curline = getline(linenum)
122 let charlen = strlen(substitute(substitute(curline,'[\t -~]', '', 'g'), '.', "\1", 'g'))
123 let kanalen = strlen(substitute(substitute(curline, s:mx_euc_kana, "\1", 'g'), "[^\1]", '', 'g'))
124 if charlen / 2 < kanalen * 3
126 doautocmd BufReadPost
129 let linenum = linenum + 1