| 1 | # $File: //member/autrijus/.vimrc $ $Author: autrijus $ | ||
| 2 | # $Revision: #1 $ $Change: 1649 $ $DateTime: 2002/10/24 15:21:23 $ | ||
| 3 | |||
| 4 | package Encode::compat::Alias; | ||
| 5 | our $VERSION = '0.05'; | ||
| 6 | |||
| 7 | 1; | ||
| 8 | |||
| 9 | package Encode::Alias; | ||
| 10 | use strict; | ||
| 11 | our $VERSION = '0.05'; | ||
| 12 | our $DEBUG = 0; | ||
| 13 | |||
| 14 | use base qw(Exporter); | ||
| 15 | |||
| 16 | # Public, encouraged API is exported by default | ||
| 17 | |||
| 18 | our @EXPORT = | ||
| 19 | qw ( | ||
| 20 | define_alias | ||
| 21 | find_alias | ||
| 22 | ); | ||
| 23 | |||
| 24 | our @Alias; # ordered matching list | ||
| 25 | our %Alias; # cached known aliases | ||
| 26 | |||
| 27 | sub find_alias | ||
| 28 | { | ||
| 29 | my $class = shift; | ||
| 30 | local $_ = shift; | ||
| 31 | unless (exists $Alias{$_}) | ||
| 32 | { | ||
| 33 | $Alias{$_} = undef; # Recursion guard | ||
| 34 | for (my $i=0; $i < @Alias; $i += 2) | ||
| 35 | { | ||
| 36 | my $alias = $Alias[$i]; | ||
| 37 | my $val = $Alias[$i+1]; | ||
| 38 | my $new; | ||
| 39 | if (ref($alias) eq 'Regexp' && $_ =~ $alias) | ||
| 40 | { | ||
| 41 | $DEBUG and warn "eval $val"; | ||
| 42 | $new = eval $val; | ||
| 43 | # $@ and warn "$val, $@"; | ||
| 44 | } | ||
| 45 | elsif (ref($alias) eq 'CODE') | ||
| 46 | { | ||
| 47 | $DEBUG and warn "$alias", "->", "($val)"; | ||
| 48 | $new = $alias->($val); | ||
| 49 | } | ||
| 50 | elsif (lc($_) eq lc($alias)) | ||
| 51 | { | ||
| 52 | $new = $val; | ||
| 53 | } | ||
| 54 | if (defined($new)) | ||
| 55 | { | ||
| 56 | next if $new eq $_; # avoid (direct) recursion on bugs | ||
| 57 | $DEBUG and warn "$alias, $new"; | ||
| 58 | my $enc = (ref($new)) ? $new : Encode::find_encoding($new); | ||
| 59 | if ($enc) | ||
| 60 | { | ||
| 61 | $Alias{$_} = $enc; | ||
| 62 | last; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | if ($DEBUG){ | ||
| 68 | my $name; | ||
| 69 | if (my $e = $Alias{$_}){ | ||
| 70 | $name = $e->name; | ||
| 71 | }else{ | ||
| 72 | $name = ""; | ||
| 73 | } | ||
| 74 | warn "find_alias($class, $_)->name = $name"; | ||
| 75 | } | ||
| 76 | return $Alias{$_}; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub define_alias | ||
| 80 | { | ||
| 81 | while (@_) | ||
| 82 | { | ||
| 83 | my ($alias,$name) = splice(@_,0,2); | ||
| 84 | unshift(@Alias, $alias => $name); # newer one has precedence | ||
| 85 | # clear %Alias cache to allow overrides | ||
| 86 | if (ref($alias)){ | ||
| 87 | my @a = keys %Alias; | ||
| 88 | for my $k (@a){ | ||
| 89 | if (ref($alias) eq 'Regexp' && $k =~ $alias) | ||
| 90 | { | ||
| 91 | $DEBUG and warn "delete \$Alias\{$k\}"; | ||
| 92 | delete $Alias{$k}; | ||
| 93 | } | ||
| 94 | elsif (ref($alias) eq 'CODE') | ||
| 95 | { | ||
| 96 | $DEBUG and warn "delete \$Alias\{$k\}"; | ||
| 97 | delete $Alias{$alias->($name)}; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | }else{ | ||
| 101 | $DEBUG and warn "delete \$Alias\{$alias\}"; | ||
| 102 | delete $Alias{$alias}; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | # Allow latin-1 style names as well | ||
| 108 | # 0 1 2 3 4 5 6 7 8 9 10 | ||
| 109 | our @Latin2iso = ( 0, 1, 2, 3, 4, 9, 10, 13, 14, 15, 16 ); | ||
| 110 | # Allow winlatin1 style names as well | ||
| 111 | our %Winlatin2cp = ( | ||
| 112 | 'latin1' => 1252, | ||
| 113 | 'latin2' => 1250, | ||
| 114 | 'cyrillic' => 1251, | ||
| 115 | 'greek' => 1253, | ||
| 116 | 'turkish' => 1254, | ||
| 117 | 'hebrew' => 1255, | ||
| 118 | 'arabic' => 1256, | ||
| 119 | 'baltic' => 1257, | ||
| 120 | 'vietnamese' => 1258, | ||
| 121 | ); | ||
| 122 | |||
| 123 | init_aliases(); | ||
| 124 | |||
| 125 | sub undef_aliases{ | ||
| 126 | @Alias = (); | ||
| 127 | %Alias = (); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub init_aliases | ||
| 131 | { | ||
| 132 | undef_aliases(); | ||
| 133 | |||
| 134 | # Try all-lower-case version should all else fails | ||
| 135 | define_alias( qr/^(.*)$/ => '"\L$1"' ); | ||
| 136 | |||
| 137 | # UTF/UCS stuff | ||
| 138 | define_alias( qr/^UCS-?2-?LE$/i => '"UCS-2LE"' ); | ||
| 139 | define_alias( qr/^UCS-?2-?(BE)?$/i => '"UCS-2BE"', | ||
| 140 | qr/^UCS-?4-?(BE|LE)?$/i => 'uc("UTF-32$1")', | ||
| 141 | qr/^iso-10646-1$/i => '"UCS-2BE"' ); | ||
| 142 | define_alias( qr/^UTF(16|32)-?BE$/i => '"UTF-$1BE"', | ||
| 143 | qr/^UTF(16|32)-?LE$/i => '"UTF-$1LE"', | ||
| 144 | qr/^UTF(16|32)$/i => '"UTF-$1"', | ||
| 145 | ); | ||
| 146 | # ASCII | ||
| 147 | define_alias(qr/^(?:US-?)ascii$/i => '"ascii"'); | ||
| 148 | define_alias('C' => 'ascii'); | ||
| 149 | define_alias(qr/\bISO[-_]?646[-_]?US$/i => '"ascii"'); | ||
| 150 | # Allow variants of iso-8859-1 etc. | ||
| 151 | define_alias( qr/\biso[-_]?(\d+)[-_](\d+)$/i => '"iso-$1-$2"' ); | ||
| 152 | |||
| 153 | # At least HP-UX has these. | ||
| 154 | define_alias( qr/\biso8859(\d+)$/i => '"iso-8859-$1"' ); | ||
| 155 | |||
| 156 | # More HP stuff. | ||
| 157 | define_alias( qr/\b(?:hp-)?(arabic|greek|hebrew|kana|roman|thai|turkish)8$/i => '"${1}8"' ); | ||
| 158 | |||
| 159 | # The Official name of ASCII. | ||
| 160 | define_alias( qr/\bANSI[-_]?X3\.4[-_]?1968$/i => '"ascii"' ); | ||
| 161 | |||
| 162 | # This is a font issue, not an encoding issue. | ||
| 163 | # (The currency symbol of the Latin 1 upper half | ||
| 164 | # has been redefined as the euro symbol.) | ||
| 165 | define_alias( qr/^(.+)\@euro$/i => '"$1"' ); | ||
| 166 | |||
| 167 | define_alias( qr/\b(?:iso[-_]?)?latin[-_]?(\d+)$/i | ||
| 168 | => 'defined $Encode::Alias::Latin2iso[$1] ? "iso-8859-$Encode::Alias::Latin2iso[$1]" : undef' ); | ||
| 169 | |||
| 170 | define_alias( qr/\bwin(latin[12]|cyrillic|baltic|greek|turkish| | ||
| 171 | hebrew|arabic|baltic|vietnamese)$/ix => | ||
| 172 | '"cp" . $Encode::Alias::Winlatin2cp{lc($1)}' ); | ||
| 173 | |||
| 174 | # Common names for non-latin prefered MIME names | ||
| 175 | define_alias( 'ascii' => 'US-ascii', | ||
| 176 | 'cyrillic' => 'iso-8859-5', | ||
| 177 | 'arabic' => 'iso-8859-6', | ||
| 178 | 'greek' => 'iso-8859-7', | ||
| 179 | 'hebrew' => 'iso-8859-8', | ||
| 180 | 'thai' => 'iso-8859-11', | ||
| 181 | 'tis620' => 'iso-8859-11', | ||
| 182 | ); | ||
| 183 | |||
| 184 | # At least AIX has IBM-NNN (surprisingly...) instead of cpNNN. | ||
| 185 | # And Microsoft has their own naming (again, surprisingly). | ||
| 186 | # And windows-* is registered in IANA! | ||
| 187 | define_alias( qr/\b(?:cp|ibm|ms|windows)[-_ ]?(\d{2,4})$/i => '"cp$1"'); | ||
| 188 | |||
| 189 | # Sometimes seen with a leading zero. | ||
| 190 | # define_alias( qr/\bcp037\b/i => '"cp37"'); | ||
| 191 | |||
| 192 | # Mac Mappings | ||
| 193 | # predefined in *.ucm; unneeded | ||
| 194 | # define_alias( qr/\bmacIcelandic$/i => '"macIceland"'); | ||
| 195 | define_alias( qr/^mac_(.*)$/i => '"mac$1"'); | ||
| 196 | # Ououououou. gone. They are differente! | ||
| 197 | # define_alias( qr/\bmacRomanian$/i => '"macRumanian"'); | ||
| 198 | |||
| 199 | # Standardize on the dashed versions. | ||
| 200 | # define_alias( qr/\butf8$/i => 'utf-8' ); | ||
| 201 | define_alias( qr/\bkoi8r$/i => 'koi8-r' ); | ||
| 202 | define_alias( qr/\bkoi8u$/i => 'koi8-u' ); | ||
| 203 | |||
| 204 | unless ($Encode::ON_EBCDIC){ | ||
| 205 | # for Encode::CN | ||
| 206 | define_alias( qr/\beuc.*cn$/i => '"euc-cn"' ); | ||
| 207 | define_alias( qr/\bcn.*euc$/i => '"euc-cn"' ); | ||
| 208 | # define_alias( qr/\bGB[- ]?(\d+)$/i => '"euc-cn"' ) | ||
| 209 | # CP936 doesn't have vendor-addon for GBK, so they're identical. | ||
| 210 | define_alias( qr/^gbk$/i => '"cp936"'); | ||
| 211 | # This fixes gb2312 vs. euc-cn confusion, practically | ||
| 212 | define_alias( qr/\bGB[-_ ]?2312(?:\D.*$|$)/i => '"euc-cn"' ); | ||
| 213 | # for Encode::JP | ||
| 214 | define_alias( qr/\bjis$/i => '"7bit-jis"' ); | ||
| 215 | define_alias( qr/\beuc.*jp$/i => '"euc-jp"' ); | ||
| 216 | define_alias( qr/\bjp.*euc$/i => '"euc-jp"' ); | ||
| 217 | define_alias( qr/\bujis$/i => '"euc-jp"' ); | ||
| 218 | define_alias( qr/\bshift.*jis$/i => '"shiftjis"' ); | ||
| 219 | define_alias( qr/\bsjis$/i => '"shiftjis"' ); | ||
| 220 | # for Encode::KR | ||
| 221 | define_alias( qr/\beuc.*kr$/i => '"euc-kr"' ); | ||
| 222 | define_alias( qr/\bkr.*euc$/i => '"euc-kr"' ); | ||
| 223 | # This fixes ksc5601 vs. euc-kr confusion, practically | ||
| 224 | define_alias( qr/(?:x-)?uhc$/i => '"cp949"' ); | ||
| 225 | define_alias( qr/(?:x-)?windows-949$/i => '"cp949"' ); | ||
| 226 | define_alias( qr/\bks_c_5601-1987$/i => '"cp949"' ); | ||
| 227 | # for Encode::TW | ||
| 228 | define_alias( qr/\bbig-?5$/i => '"big5-eten"' ); | ||
| 229 | define_alias( qr/\bbig5-?et(?:en)?$/i => '"big5-eten"' ); | ||
| 230 | define_alias( qr/\btca[-_]?big5$/i => '"big5-eten"' ); | ||
| 231 | define_alias( qr/\bbig5-?hk(?:scs)?$/i => '"big5-hkscs"' ); | ||
| 232 | define_alias( qr/\bhk(?:scs)?[-_]?big5$/i => '"big5-hkscs"' ); | ||
| 233 | } | ||
| 234 | # utf8 is blessed :) | ||
| 235 | define_alias( qr/^UTF-8$/i => '"utf8"',); | ||
| 236 | # At last, Map white space and _ to '-' | ||
| 237 | define_alias( qr/^(\S+)[\s_]+(.*)$/i => '"$1-$2"' ); | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 | __END__ |