]> code.delx.au - gnu-emacs/blob - lisp/international/codepage.el
(cp1250-decode-table, cp1251-decode-table, cp1253-decode-table,
[gnu-emacs] / lisp / international / codepage.el
1 ;;; codepage.el --- MS-DOS specific coding systems.
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: Eli Zaretskii
6 ;; Maintainer: FSF
7 ;; Keywords: i18n ms-dos codepage
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Special coding systems for DOS codepage support.
29 ;;
30 ;; These coding systems perform conversion from the DOS codepage encoding
31 ;; to one of the ISO-8859 character sets. Each codepage has its corresponding
32 ;; ISO-8859 charset, chosen so as to be able to convert all (or most) of the
33 ;; characters. The idea is that Emacs internally works with the usual MULE
34 ;; charsets, and the conversion to and from the DOS codepage is performed
35 ;; on I/O only.
36 ;; See term/internal.el for the complementary setup of the DOS terminal
37 ;; display and input methods.
38 ;;
39 ;; Thanks to Ken'ichi Handa <handa@etl.go.jp> for writing the CCL
40 ;; encoders/decoders, and for help in debugging this code.
41
42 ;;; Code:
43
44 (defun cp-coding-system-for-codepage-1 (coding mnemonic iso-name
45 decoder encoder)
46 "Make coding system CODING for a DOS codepage using translation tables.
47 MNEMONIC is a character to be displayed on mode line for the coding system.
48 ISO-NAME is the name of the ISO-8859 charset which corresponds to this
49 codepage.
50 DECODER is a translation table for converting characters in the DOS codepage
51 encoding to Emacs multibyte characters.
52 ENCODER is a translation table for encoding Emacs multibyte characters into
53 external DOS codepage codes.
54
55 Note that the coding systems created by this function support automatic
56 detection of the EOL format. However, the decoders and encoders created
57 for these coding systems only support DOS and Unix style EOLs (the -mac
58 variety is actually just an alias for the -unix variety)."
59 (save-match-data
60 (let* ((coding-name (symbol-name coding))
61 (undef (if (eq system-type 'ms-dos)
62 (if dos-unsupported-char-glyph
63 (logand dos-unsupported-char-glyph 255)
64 127)
65 ??))
66 (ccl-decoder-dos
67 (ccl-compile
68 `(4 (loop (read r1)
69 (if (r1 != ?\r)
70 (if (r1 >= 128)
71 ((r0 = ,(charset-id 'ascii))
72 (translate-character ,decoder r0 r1)
73 (if (r0 == ,(charset-id 'ascii))
74 (write r1)
75 (write-multibyte-character r0 r1)))
76 (write r1)))
77 (repeat)))))
78 (ccl-decoder-unix
79 (ccl-compile
80 `(4 (loop (read r1)
81 (if (r1 >= 128)
82 ((r0 = ,(charset-id 'ascii))
83 (translate-character ,decoder r0 r1)
84 (if (r0 == ,(charset-id 'ascii))
85 (write r1)
86 (write-multibyte-character r0 r1)))
87 (write r1))
88 (repeat)))))
89 (ccl-encoder-dos
90 (ccl-compile
91 `(1 (loop (read-multibyte-character r0 r1)
92 (if (r1 == ?\n)
93 (write ?\r)
94 (if (r0 != ,(charset-id 'ascii))
95 ((translate-character ,encoder r0 r1)
96 (if (r0 == ,(charset-id 'japanese-jisx0208))
97 ((r1 = ,undef)
98 (write r1))))))
99 (write-repeat r1)))))
100 (ccl-encoder-unix
101 (ccl-compile
102 `(1 (loop (read-multibyte-character r0 r1)
103 (if (r0 != ,(charset-id 'ascii))
104 ((translate-character ,encoder r0 r1)
105 (if (r0 == ,(charset-id 'japanese-jisx0208))
106 ((r1 = ,undef)
107 (write r1)))))
108 (write-repeat r1))))))
109 (if (memq coding coding-system-list)
110 (setq coding-system-list (delq coding coding-system-list)))
111
112 ;; Make coding system CODING.
113 (make-coding-system
114 coding 4 mnemonic
115 (concat "8-bit encoding of " (symbol-name iso-name)
116 " characters using IBM codepage " coding-name)
117 (cons ccl-decoder-unix ccl-encoder-unix)
118 `((safe-charsets ascii ,iso-name)
119 (valid-codes (0 . 255))
120 (charset-origin-alist ,(list iso-name (symbol-name coding) encoder))))
121 ;;; Make coding systems CODING-unix, CODING-dos, CODING-mac.
122 (make-subsidiary-coding-system coding)
123 (put coding 'eol-type (vector (intern (format "%s-unix" coding))
124 (intern (format "%s-dos" coding))
125 (intern (format "%s-mac" coding))))
126 ;; Change CCL code for CODING-dos.
127 (let ((coding-spec (copy-sequence (get coding 'coding-system))))
128 (aset coding-spec 4
129 (cons (check-ccl-program
130 ccl-decoder-dos
131 (intern (format "%s-dos-decoder" coding)))
132 (check-ccl-program
133 ccl-encoder-dos
134 (intern (format "%s-dos-encoder" coding)))))
135 (put (intern (concat coding-name "-dos")) 'coding-system
136 coding-spec)))))
137
138 (defun cp-decoding-vector-for-codepage (table charset offset)
139 "Create a vector for decoding IBM PC characters using conversion table
140 TABLE into an ISO-8859 character set CHARSET whose first non-ASCII
141 character is generated by (make-char CHARSET OFFSET)."
142 (let* ((len (length table))
143 (undefined-char
144 (if (eq system-type 'ms-dos)
145 (if dos-unsupported-char-glyph
146 (logand dos-unsupported-char-glyph 255)
147 127)
148 32))
149 (vec1 (make-vector 256 undefined-char))
150 (i 0))
151 (while (< i offset)
152 (aset vec1 i i)
153 (setq i (1+ i)))
154 (setq i 0)
155 (while (< i len)
156 (if (aref table i)
157 (aset vec1 (aref table i) (make-char charset (+ i offset))))
158 (setq i (1+ i)))
159 vec1))
160
161 ;;; You don't think I created all these tables below by hand, do you?
162 ;;; The following Awk script will create the table for cp850-to-Latin-1
163 ;;; conversion from the RFC 1345 file (the other tables are left as an
164 ;;; excercise):
165 ;;; BEGIN { n_pages = 11;
166 ;;; pn["IBM437"] = 0; pn["IBM850"] = 1; pn["IBM851"] = 2;
167 ;;; pn["IBM852"] = 3; pn["IBM855"] = 4; pn["IBM860"] = 5;
168 ;;; pn["IBM861"] = 6; pn["IBM862"] = 7; pn["IBM863"] = 8;
169 ;;; pn["IBM864"] = 9; pn["IBM865"] = 10;
170 ;;; }
171 ;;; $1 == "&charset" { charset = $2; }
172 ;;; $1 == "&code" { code = $2; }
173 ;;; /^ [^&]/ {
174 ;;; if ((charset ~ /^IBM(437|8(5[0125]|6[0-5]))$/) || (charset ~ /^ISO_8859-1/))
175 ;;; {
176 ;;; for (i = 1; i <= NF; i++)
177 ;;; chars[charset,code++] = $i;
178 ;;; }
179 ;;; }
180 ;;;
181 ;;; END {
182 ;;; for (i = 160; i < 256; i++)
183 ;;; {
184 ;;; c = chars["ISO_8859-1:1987",i];
185 ;;; if (c == "??") # skip unused positions
186 ;;; {
187 ;;; printf " nil";
188 ;;; if ((i - 159)%16 == 0)
189 ;;; printf "\n";
190 ;;; continue;
191 ;;; }
192 ;;; found = 0;
193 ;;; for (j in pn)
194 ;;; map[j] = "nil";
195 ;;; for (combined in chars)
196 ;;; {
197 ;;; candidate = chars[combined];
198 ;;; split (combined, separate, SUBSEP);
199 ;;; if (separate[1] == "IBM850" && candidate == c)
200 ;;; {
201 ;;; found = 1;
202 ;;; map[separate[1]] = separate[2];
203 ;;; }
204 ;;; }
205 ;;; printf " %s", map["IBM850"];
206 ;;; if ((i - 159)%16 == 0)
207 ;;; printf "\n";
208 ;;; }
209 ;;; }
210
211 ;;; WARNING WARNING WARNING!!!
212 ;;;
213 ;;; If you want to get fancy with these tables, remember that the inverse
214 ;;; tables, created by `cp-decoding-vector-for-codepage' above, are installed
215 ;;; on MS-DOS as nonascii-translation-table (see `dos-codepage-setup' on
216 ;;; internal.el). Therefore, you should NOT put any codes below 128 in
217 ;;; these tables! Otherwise, various Emacs commands and functions will
218 ;;; mysteriously fail! For example, a typical screwup is to map the Latin-N
219 ;;; acute accent character to the apostrophe, and have all regexps which
220 ;;; end with "\\'" begin to fail (e.g., the automatic setting of the major
221 ;;; mode by file name extension will stop working).
222 ;;;
223 ;;; You HAVE BEEN warned!
224
225 ;; US/English/PC-8/IBM-2. This doesn't support Latin-1 characters very
226 ;; well, but why not use what we can salvage?
227 (defvar cp437-decode-table
228 ;; Nth element is the code of a cp437 glyph for the multibyte
229 ;; character created by (make-char 'latin-iso8859-1 (+ N 160)).
230 ;; The element nil means there's no corresponding cp437 glyph.
231 [
232 255 173 155 156 nil 157 179 nil nil nil 166 174 170 196 nil nil
233 248 241 253 nil nil nil nil 249 nil nil 167 175 172 171 nil 168
234 nil nil nil nil 142 143 146 128 nil 144 nil nil nil nil nil nil
235 nil 165 nil nil nil nil 153 nil nil nil nil nil 154 nil nil 225
236 133 160 131 nil 132 134 145 135 138 130 136 137 141 161 140 139
237 nil 164 149 162 147 nil 148 246 nil 151 163 150 129 nil nil 152]
238 "Table for converting ISO-8859-1 characters into codepage 437 glyphs.")
239 (setplist 'cp437-decode-table
240 '(charset latin-iso8859-1 language "Latin-1" offset 160))
241
242 ;; Multilingual (Latin-1)
243 (defvar cp850-decode-table
244 ;; Nth element is the code of a cp850 glyph for the multibyte
245 ;; character created by (make-char 'latin-iso8859-1 (+ N 160)).
246 ;; The element nil means there's no corresponding cp850 glyph.
247 [
248 255 173 189 156 207 190 221 245 249 184 166 174 170 240 169 nil
249 248 241 253 252 239 230 244 250 247 251 167 175 172 171 243 168
250 183 181 182 199 142 143 146 128 212 144 210 211 222 214 215 216
251 209 165 227 224 226 229 153 158 157 235 233 234 154 237 231 225
252 133 160 131 198 132 134 145 135 138 130 136 137 141 161 140 139
253 208 164 149 162 147 228 148 246 155 151 163 150 129 236 232 152]
254 "Table for converting ISO-8859-1 characters into codepage 850 glyphs.")
255 (setplist 'cp850-decode-table
256 '(charset latin-iso8859-1 language "Latin-1" offset 160))
257
258 ;; Greek
259 (defvar cp851-decode-table
260 [
261 255 nil nil 156 nil nil nil 245 249 nil nil 174 nil 240 nil nil
262 248 241 nil nil 239 nil 134 nil 141 143 144 175 146 171 149 152
263 161 164 165 166 167 168 169 170 172 173 181 182 184 183 189 190
264 198 199 nil 207 208 209 210 211 212 213 nil nil 155 157 158 159
265 252 214 215 216 221 222 224 225 226 227 228 229 230 231 232 233
266 234 235 237 236 238 242 243 244 246 250 160 251 162 163 253 nil]
267 "Table for converting ISO-8859-7 characters into codepage 851 glyphs.")
268 (setplist 'cp851-decode-table
269 '(charset greek-iso8859-7 language "Greek" offset 160))
270
271 ;; Slavic/Eastern Europe (Latin-2)
272 (defvar cp852-decode-table
273 [
274 255 164 244 157 207 149 151 245 249 230 184 155 141 240 166 189
275 248 165 247 136 239 150 152 243 242 231 173 156 171 241 167 190
276 232 181 182 198 142 145 143 128 172 144 168 211 183 214 215 210
277 209 227 213 224 226 138 153 158 252 222 233 235 154 237 221 225
278 234 160 131 199 132 146 134 135 159 130 169 137 216 161 140 212
279 208 228 229 162 147 139 148 246 253 133 163 251 129 236 238 250]
280 "Table for converting ISO-8859-2 characters into codepage 852 glyphs.")
281 (setplist 'cp852-decode-table
282 '(charset latin-iso8859-2 language "Latin-2" offset 160))
283
284 ;; Russian
285 (defvar cp855-decode-table
286 [
287 255 133 129 131 135 137 139 141 143 145 147 149 151 240 153 155
288 161 163 236 173 167 169 234 244 184 190 199 209 211 213 215 221
289 226 228 230 232 171 182 165 252 246 250 159 242 238 248 157 224
290 160 162 235 172 166 168 233 243 183 189 198 208 210 212 214 216
291 225 227 229 231 170 181 164 251 245 249 158 241 237 247 156 222
292 239 132 128 130 134 136 138 140 142 144 146 148 150 253 152 154]
293 "Table for converting ISO-8859-5 characters into codepage 855 glyphs.")
294 (setplist 'cp855-decode-table
295 '(charset cyrillic-iso8859-5 language "Cyrillic-ISO" offset 160))
296
297 ;; Turkish
298 (defvar cp857-decode-table
299 [
300 255 nil nil 156 207 nil 245 249 152 158 166 nil 240 nil
301 248 nil 253 252 239 nil nil nil nil 141 159 167 nil 171 nil
302 183 181 182 142 nil nil 128 212 144 210 211 222 214 215 216
303 165 227 224 226 nil 153 232 nil 235 233 234 154 nil nil 225
304 133 160 131 132 nil nil 135 138 130 136 137 236 161 140 139
305 164 149 162 147 nil 148 246 nil 151 163 150 129 nil nil 250]
306 "Table for converting ISO-8859-3 characters into codepage 857 glyphs.")
307 (setplist 'cp857-decode-table
308 '(charset latin-iso8859-3 language "Latin-3" offset 160))
309
310 ;; Portuguese
311 (defvar cp860-decode-table
312 [
313 255 173 155 156 nil nil 179 nil nil nil 166 174 170 nil nil nil
314 nil 241 253 nil nil nil nil 249 nil nil 167 175 172 171 nil 168
315 145 134 143 142 nil nil nil 128 146 144 137 nil 152 nil 139 nil
316 nil 165 159 169 140 153 nil nil nil 157 150 nil 154 nil nil nil
317 133 160 131 132 nil nil nil 135 138 130 136 nil 141 161 nil nil
318 nil 164 149 162 147 148 nil 246 nil 151 163 nil 129 nil nil nil]
319 "Table for converting ISO-8859-1 characters into codepage 860 glyphs.")
320 (setplist 'cp860-decode-table
321 '(charset latin-iso8859-1 language "Latin-1" offset 160))
322
323 ;; Icelandic
324 (defvar cp861-decode-table
325 [
326 255 173 nil 156 nil nil nil nil nil nil nil 174 170 nil nil nil
327 nil 241 253 nil nil nil nil 249 nil nil nil 175 172 171 nil 168
328 nil 164 nil nil 142 143 146 128 nil 144 nil nil nil 165 nil nil
329 139 nil 159 166 nil nil 153 nil 157 nil 167 nil 154 151 141 nil
330 133 160 131 nil 132 134 145 135 138 130 136 137 nil 161 nil nil
331 140 nil nil 162 147 nil 148 246 155 nil 163 150 129 152 149 nil]
332 "Table for converting ISO-8859-1 characters into codepage 861 glyphs.")
333 (setplist 'cp861-decode-table
334 '(charset latin-iso8859-1 language "Latin-1" offset 160))
335
336 ;; Hebrew
337 (defvar cp862-decode-table
338 ;; Nth element is the code of a cp862 glyph for the multibyte
339 ;; character created by (make-char 'hebrew-iso8859-8 (+ N 160)).
340 ;; The element nil means there's no corresponding cp850 glyph.
341 [
342 255 173 155 156 nil 157 179 nil nil nil nil 174 170 196 nil nil
343 248 241 253 nil nil 230 nil 249 nil nil 246 175 172 171 nil nil
344 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil
345 nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil 205
346 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
347 144 145 146 147 148 149 150 151 152 153 154 nil nil nil nil nil]
348 "Table for converting ISO-8859-8 characters into codepage 862 glyphs.")
349 (setplist 'cp862-decode-table
350 '(charset hebrew-iso8859-8 language "Hebrew" offset 160))
351
352 ;; French Canadian
353 (defvar cp863-decode-table
354 [
355 255 nil 155 156 152 nil 160 143 164 nil nil 174 170 nil nil 167
356 nil 241 253 166 161 nil 134 249 165 nil nil 175 172 171 173 nil
357 142 nil 132 nil nil nil nil 128 145 144 146 148 nil nil 168 149
358 nil nil nil nil 153 nil nil nil nil 157 nil 158 154 nil nil nil
359 133 nil 131 nil nil nil nil 135 138 130 136 137 141 nil 140 139
360 nil nil nil 162 147 nil nil 246 nil 151 163 150 129 nil nil nil]
361 "Table for converting ISO-8859-1 characters into codepage 863 glyphs.")
362 (setplist 'cp863-decode-table
363 '(charset latin-iso8859-1 language "Latin-1" offset 160))
364
365 ;; Arabic
366 ;; FIXME: Emacs doesn't seem to support the "Arabic" language
367 ;; environment yet. So this is only partially usable, for now
368 (defvar cp864-decode-table
369 [
370 255 nil nil nil 164 nil nil nil nil nil nil nil 172 161 nil nil
371 nil nil nil nil nil nil nil nil nil nil nil 187 nil nil nil 191
372 nil 193 194 195 196 nil 198 199 169 201 170 171 173 174 175 207
373 208 209 210 188 189 190 235 215 216 223 238 nil nil nil nil nil
374 224 247 248 252 251 239 242 243 232 233 253 nil nil nil nil nil
375 nil 241 nil nil nil nil nil nil nil nil nil nil nil nil nil nil]
376 "Table for converting ISO-8859-1 characters into codepage 863 glyphs.")
377 (setplist 'cp864-decode-table
378 '(charset arabic-iso8859-6 language nil offset 160))
379
380 ;; Nordic (Norwegian/Danish)
381 (defvar cp865-decode-table
382 [
383 255 173 nil 156 nil nil nil nil nil nil 166 174 170 nil nil nil
384 nil 241 253 nil nil nil nil 249 nil nil 167 175 172 171 nil 168
385 nil nil nil nil 142 143 146 128 nil 144 nil nil nil nil nil nil
386 nil 165 nil nil nil nil 153 nil 157 nil nil nil 154 nil nil nil
387 133 160 131 nil 132 134 145 135 138 130 136 137 141 161 140 139
388 nil 164 149 162 147 nil 148 246 155 151 163 150 129 nil nil 152]
389 "Table for converting ISO-8859-1 characters into codepage 865 glyphs.")
390 (setplist 'cp865-decode-table
391 '(charset latin-iso8859-1 language "Latin-1" offset 160))
392
393 ;; Greek (yes, another one!)
394 (defvar cp869-decode-table
395 [
396 255 139 140 156 nil nil 138 245 249 151 nil 174 137 240 nil 142
397 248 241 153 154 239 247 134 136 141 143 144 175 146 171 149 152
398 161 164 165 166 167 168 169 170 172 173 181 182 183 184 189 190
399 198 199 nil 207 208 209 210 211 212 213 145 150 155 157 158 159
400 252 214 215 216 221 222 224 225 226 227 228 229 230 231 232 233
401 234 235 237 236 238 242 243 244 246 250 160 251 162 163 253 nil]
402 "Table for converting ISO-8859-7 characters into codepage 869 glyphs.")
403 (setplist 'cp869-decode-table
404 '(charset greek-iso8859-7 language "Greek" offset 160))
405
406 ;; Conversion from codepage 775 to Latin-4 for Baltic countries.
407 (defvar cp775-decode-table
408 [
409 255 181 nil 138 150 nil 234 245 166 190 237 149 173 240 207 nil
410 248 208 nil 139 239 nil 235 nil nil 213 137 133 nil nil 216 nil
411 160 nil nil nil 142 143 146 189 182 144 183 nil 184 nil nil 161
412 nil 238 226 232 nil 229 153 158 157 198 nil nil 154 nil 199 225
413 131 nil nil nil 132 134 145 212 209 130 210 nil 211 nil nil 140
414 nil 236 147 233 nil 228 148 nil 155 214 nil nil 129 nil 215 nil]
415 "Table for converting ISO-8859-4 characters into codepage 775 glyphs.")
416 (setplist 'cp775-decode-table
417 '(charset latin-iso8859-4 language "Latin-4" offset 160))
418
419 ;; Support for the Windows 12xx series of codepages that MS has
420 ;; butchered from the ISO-8859 specs. This does not add support for
421 ;; the extended characters that MS has added in the 128 - 159 coding
422 ;; range, only translates those characters that can be expressed in
423 ;; the corresponding iso-8859 codepage.
424
425 ;; Codepage Mapping:
426 ;;
427 ;; Windows-1250: ISO-8859-2 (Central Europe) - differs in some positions
428 ;; Windows-1251: ISO-8859-5 (Cyrillic) - differs wildly
429 ;; Windows-1252: ISO-8859-1 (West Europe) - exact match
430 ;; Windows-1253: ISO-8859-7 (Greek) - differs in some positions
431 ;; Windows-1254: ISO-8859-9 (Turkish) - exact match
432 ;; Windows-1255: ISO-8859-8 (Hebrew) - exact match
433 ;; Windows-1256: ISO-8859-6 (Arabic) - half match
434 ;; Windows-1257: ISO-8859-4 (Baltic) - differs, future Latin-7
435 ;; Windows-1258: VISCII (Vietnamese) - Completely different
436
437 (defvar cp1250-decode-table
438 [
439 160 165 162 163 164 188 140 167 168 138 170 141 143 173 142 175
440 176 185 178 179 180 190 156 161 184 154 186 157 159 189 158 191
441 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
442 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
443 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
444 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 ]
445 "ISO-8859-2 to Windows-1250 (Central Europe) codepage decoding table")
446 (setplist 'cp1250-decode-table
447 '(charset latin-iso8859-2 language "Latin-2" offset 160))
448
449 (defvar cp1251-decode-table
450 [
451 160 168 128 129 170 189 178 175 163 138 140 142 141 173 161 143
452 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
453 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
454 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
455 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
456 185 184 144 131 186 190 179 191 188 154 156 158 157 167 162 159 ]
457 "ISO-8859-5 to Windows-1251 (Cyrillic) codepage decoding table")
458 (setplist 'cp1251-decode-table
459 '(charset cyrillic-iso8859-5 language "Cyrillic-ISO" offset 160))
460
461 ;; cp1253 is missing nbsp so we cannot quite translate perfectly. It
462 ;; also has two micro/mu characters which would require more complex
463 ;; processing to accomodate.
464 (defvar cp1253-decode-table
465 [
466 nil 145 146 163 nil nil 166 167 168 169 nil 171 172 173 nil 151
467 176 177 178 179 180 161 162 183 184 185 186 187 188 189 190 191
468 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
469 208 209 nil 211 212 213 214 215 216 217 218 219 220 221 222 223
470 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
471 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 nil ]
472 "ISO-8859-7 to Windows-1253 (Greek) codepage decoding table")
473 (setplist 'cp1253-decode-table
474 '(charset greek-iso8859-7 language "Greek" offset 160))
475
476 ;; Since Latin-7 is not yet official, and Emacs does not support it,
477 ;; provide translation between Windows-1257 and Latin-4 the best we
478 ;; can.
479 (defvar cp1257-decode-table
480 [
481 160 192 nil 170 164 nil 207 167 nil 208 199 204 nil 173 222 nil
482 176 224 nil 186 nil nil 239 nil nil 240 231 236 nil nil 254 nil
483 194 nil nil nil 196 197 175 193 200 201 198 nil 203 nil nil 206
484 nil 210 212 205 nil 213 214 215 168 216 nil nil 220 nil 219 223
485 226 nil nil nil 228 229 191 225 232 233 230 nil 235 nil nil 238
486 nil 242 244 237 nil 245 246 247 184 248 nil nil 252 nil 251 nil ]
487 "ISO-8859-4 to Windows-1257 (Baltic) codepage decoding table")
488 (setplist 'cp1257-decode-table
489 '(charset latin-iso8859-4 language "Latin-4" offset 160))
490
491 ;;;###autoload
492 (defun cp-make-coding-systems-for-codepage (codepage iso-name offset)
493 "Create a coding system to convert IBM CODEPAGE into charset ISO-NAME
494 whose first character is at offset OFFSET from the beginning of 8-bit
495 ASCII table.
496
497 The created coding system has the usual 3 subsidiary systems: for Unix-,
498 DOS- and Mac-style EOL conversion. However, unlike built-in coding
499 systems, the Mac-style EOL conversion is currently not supported by the
500 decoder and encoder created by this function."
501 (let* ((decode-table (intern (format "%s-decode-table" codepage)))
502 (nonascii-table
503 (intern (format "%s-nonascii-translation-table" codepage)))
504 (decode-translation
505 (intern (format "%s-decode-translation-table" codepage)))
506 (encode-translation
507 (intern (format "%s-encode-translation-table" codepage))))
508 (set nonascii-table
509 (make-translation-table-from-vector
510 (cp-decoding-vector-for-codepage
511 (symbol-value decode-table) iso-name offset)))
512 (define-translation-table encode-translation
513 (char-table-extra-slot (symbol-value nonascii-table) 0))
514 ;; For charsets other than ascii and ISO-NAME, set `?' for
515 ;; one-column charsets, and some Japanese character for
516 ;; wide-column charsets. CCL encoder convert that Japanese
517 ;; character to either dos-unsupported-char-glyph or "??".
518 (let ((tbl (char-table-extra-slot (symbol-value nonascii-table) 0))
519 (undef (if (eq system-type 'ms-dos)
520 (if dos-unsupported-char-glyph
521 (logand dos-unsupported-char-glyph 255)
522 127)
523 ??))
524 (charsets (delq 'ascii (delq iso-name
525 (copy-sequence charset-list))))
526 (wide-column-char (make-char 'japanese-jisx0208 32 32)))
527 (while charsets
528 (aset tbl (make-char (car charsets))
529 (if (= (charset-width (car charsets)) 1) undef wide-column-char))
530 (setq charsets (cdr charsets))))
531 (define-translation-table decode-translation
532 (symbol-value nonascii-table))
533 (cp-coding-system-for-codepage-1
534 (intern codepage) ?D iso-name decode-translation encode-translation)
535 ))
536
537 (defun cp-codepage-decoder (codepage)
538 "If CODEPAGE is the name of a supported codepage, return its decode table;
539 otherwise return nil."
540 (let ((cp (if (symbolp codepage) (symbol-name codepage) codepage)))
541 (cond
542 ((stringp cp)
543 (intern-soft (format "%s-decode-table" cp)))
544 (t nil))))
545
546 ;;;###autoload
547 (defun cp-charset-for-codepage (codepage)
548 "Return the charset for which there is a translation table to DOS CODEPAGE.
549 CODEPAGE must be the name of a DOS codepage, a string."
550 (let ((cp-decoder (cp-codepage-decoder codepage)))
551 (if (null cp-decoder)
552 (error "Unsupported codepage %s" codepage)
553 (get cp-decoder 'charset))))
554
555 ;;;###autoload
556 (defun cp-language-for-codepage (codepage)
557 "Return the name of the MULE language environment for CODEPAGE.
558 CODEPAGE must be the name of a DOS codepage, a string."
559 (let ((cp-decoder (cp-codepage-decoder codepage)))
560 (if (null cp-decoder)
561 (error "Unsupported codepage %s" codepage)
562 (get cp-decoder 'language))))
563
564 ;;;###autoload
565 (defun cp-offset-for-codepage (codepage)
566 "Return the offset to be used in setting up coding systems for CODEPAGE.
567 CODEPAGE must be the name of a DOS codepage, a string."
568 (let ((cp-decoder (cp-codepage-decoder codepage)))
569 (if (null cp-decoder)
570 (error "Unsupported codepage %s" codepage)
571 (get cp-decoder 'offset))))
572
573 ;;;###autoload
574 (defun cp-supported-codepages ()
575 "Return an alist of supported codepages.
576
577 Each association in the alist has the form (NNN . CHARSET), where NNN is the
578 codepage number, and CHARSET is the MULE charset which is the closest match
579 for the character set supported by that codepage.
580
581 A codepage NNN is supported if a variable called `cpNNN-decode-table' exists,
582 is a vector, and has a charset property."
583 (save-match-data
584 (let (alist chset sname)
585 (mapatoms
586 (function
587 (lambda (sym)
588 (if (and (boundp sym)
589 (string-match "\\`cp\\([1-9][0-9][0-9][0-9]?\\)-decode-table\\'"
590 (setq sname (symbol-name sym)))
591 (vectorp (symbol-value sym))
592 (setq chset (get sym 'charset)))
593 (setq alist
594 (cons (cons (match-string 1 sname) chset) alist))))))
595 alist)))
596
597 ;;;###autoload
598 (defun codepage-setup (codepage)
599 "Create a coding system cpCODEPAGE to support the IBM codepage CODEPAGE.
600
601 These coding systems are meant for encoding and decoding 8-bit non-ASCII
602 characters used by the IBM codepages, typically in conjunction with files
603 read/written by MS-DOS software, or for display on the MS-DOS terminal."
604 (interactive
605 (let ((completion-ignore-case t)
606 (candidates (cp-supported-codepages)))
607 (list (completing-read "Setup DOS Codepage: (default 437) " candidates
608 nil t nil nil "437"))))
609 (let ((cp (format "cp%s" codepage)))
610 (cp-make-coding-systems-for-codepage
611 cp (cp-charset-for-codepage cp) (cp-offset-for-codepage cp))))
612
613 (provide 'codepage)
614
615 ;; codepage.el ends here