vim72-20100325-kaoriya-w64j.zip
[MacVim/KaoriYa.git] / runtime / plugin / verifyenc.vim
blobd6df10321facdb60cb436913adc4894a98155fdd
1 " vi:set ts=8 sts=2 sw=2 tw=0:
3 " verifyenc.vim
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')
15   finish
16 endif
17 let s:debug = 1
19 " Set default options
20 if !exists("verifyenc_enable")
21   let verifyenc_enable = 1
22 endif
23 if !exists("verifyenc_maxlines")
24   let verifyenc_maxlines = 100
25 endif
27 "-----------------------------------------------------------------------------
29 command! -nargs=? VerifyEnc call <SID>Status(<q-args>)
31 " Set autocmd for VerifyEncoding
32 if has('autocmd')
33   augroup VerifyEncoding
34   au!
35   autocmd BufReadPost * silent call <SID>VerifyEncoding()
36   augroup END
37 endif
39 function! s:Status(argv)
40   if a: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
48   endif
49   echo 'Verify encoding is *'.(g:verifyenc_enable ? 'ON' : 'OFF').'*'
50 endfunction
52 function! s:VerifyEncoding()
53   if !has('iconv') || &modifiable == 0 || g:verifyenc_enable == 0
54     return
55   endif
56   " Check if empty file.
57   if &fileencoding != '' && line2byte(1) < 0
58     edit! ++enc=
59     doautocmd BufReadPost
60     return
61   endif
62   " Check whether multibyte is exists or not.
63   if &fileencoding != '' && &fileencoding !~ '^ucs' && s:Has_multibyte_character()
64     if s:debug
65       let b:verifyenc = 'NO MULTIBYTE'
66     endif
67     return
68   endif
69   " Check to be force euc-jp
70   if &encoding =~# '^euc-\%(jp\|jisx0213\)$' && s:Verify_euc_jp()
71     if s:debug
72       let b:verifyenc = 'FORCE EUC-JP'
73     endif
74     return
75   endif
76   " Nop
77   let b:verifyenc = 'NONE'
78 endfunction
80 "-----------------------------------------------------------------------------
81 " multibyte character
83 function! s:Has_multibyte_character()
84   if &fileencoding == '' && &encoding == &fileencoding
85     return 0
86   endif
87   let lnum = line('.')
88   let cnum = col('.')
89   if search("[^\t -~]", 'w') > 0
90     call cursor(lnum, cnum)
91     return 0
92   else
93     " No multibyte characters, then set 'fileencoding' to NULL
94     let &fileencoding = ""
95     return 1
96   endif
97 endfunction
99 "-----------------------------------------------------------------------------
100 " euc-jp
102 let s:mx_euc_kana = '['.nr2char(0x8ea4).nr2char(0x8ea5).']'.'\%([^\t -~]\)'
104 if s:debug
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'))
108   endfunction
109 endif
111 function! s:Verify_euc_jp()
112   if &encoding =~# '^euc-\%(jp\|jisx0213\)$' && &fileencoding != '' && &encoding != &fileencoding
113     " Range to check
114     let rangelast = line('$')
115     if rangelast > g:verifyenc_maxlines
116       let rangelast = g:verifyenc_maxlines
117     endif
118     " Checking loop
119     let linenum = 1
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
125         edit! ++enc=
126         doautocmd BufReadPost
127         return 1
128       endif
129       let linenum = linenum + 1
130     endwhile
131   endif
132   return 0
133 endfunction