Set `iso-8859-2' for `nonascii-translation'.
[bpt/emacs.git] / src / charset.h
CommitLineData
3263d5a2 1/* Header for charset handler.
4a2f9c6a 2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
75c8c592 3 Licensed to the Free Software Foundation.
e06aa1f9 4 Copyright (C) 2001 Free Software Foundation, Inc.
3263d5a2
KH
5 Copyright (C) 2001, 2002
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H13PRO009
4ed46869 8
369314dc
KH
9This file is part of GNU Emacs.
10
11GNU Emacs is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation; either version 2, or (at your option)
14any later version.
4ed46869 15
369314dc
KH
16GNU Emacs is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
4ed46869 20
369314dc
KH
21You should have received a copy of the GNU General Public License
22along with GNU Emacs; see the file COPYING. If not, write to
23the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24Boston, MA 02111-1307, USA. */
4ed46869 25
aa01a892
KH
26#ifndef EMACS_CHARSET_H
27#define EMACS_CHARSET_H
4ed46869 28
3263d5a2
KH
29/* Index to arguments of Fdefine_charset_internal. */
30
31enum define_charset_arg_index
32 {
33 charset_arg_name,
34 charset_arg_dimension,
35 charset_arg_code_space,
36 charset_arg_iso_final,
37 charset_arg_iso_revision,
38 charset_arg_emacs_mule_id,
39 charset_arg_ascii_compatible_p,
40 charset_arg_supplementary_p,
41 charset_arg_invalid_code,
42 charset_arg_code_offset,
43 charset_arg_map,
44 charset_arg_parents,
45 charset_arg_unify_map,
46 charset_arg_plist,
47 charset_arg_max
48 };
49
50
51/* Indices to charset attributes vector. */
52
53enum charset_attr_index
54 {
55 /* ID number of the charset. */
56 charset_id,
4ed46869 57
3263d5a2
KH
58 /* Name of the charset (symbol). */
59 charset_name,
60
61 /* Property list of the charset. */
62 charset_plist,
63
64 /* If the method of the charset is `MAP_DEFERRED', the value is a
65 mappint vector or a file name that contains mapping vector.
66 Otherwise, nil. */
67 charset_map,
68
69 /* If the method of the charset is `MAP', the value is a vector
70 that maps code points of the charset to characters. The vector
71 is indexed by a character index. A character index is
72 calculated from a code point and the code-space table of the
73 charset. */
74 charset_decoder,
75
76 /* If the method of the charset is `MAP', the value is a
77 char-table that maps characters of the charset to code
78 points. */
79 charset_encoder,
80
81 /* If the method of the charset is `INHERIT', the value is a list
82 of the form (PARENT-CHARSET-ID . CODE-OFFSET). */
83 charset_parents,
84
85 /* */
86 charset_unify_map,
87
88 /* */
89 charset_deunifier,
90
91 /* The length of charset attribute vector. */
92 charset_attr_max
93 };
94
95/* Methods for converting code points and characters of charsets. */
96
97enum charset_method
98 {
99 /* For a charset of this method, a character code is calculated
100 from a character index (which is calculated from a code point)
101 simply by adding an offset value. */
102 CHARSET_METHOD_OFFSET,
103
104 /* For a charset of this method, a decoder vector and an encoder
105 char-table is used for code point <-> character code
106 conversion. */
107 CHARSET_METHOD_MAP,
108
109 /* Same as above but decoder and encoder are loaded from a file on
110 demand. Once loaded, the method is changed to
111 CHARSET_METHOD_MAP. */
112 CHARSET_METHOD_MAP_DEFERRED,
113
114 /* A charset of this method inherits characters from the other
115 charsets. */
116 CHARSET_METHOD_INHERIT,
117 };
118
119struct charset
120{
121 int id;
122
123 int hash_index;
124
125 /* Dimension of the charset: 1, 2, 3, or 4. */
126 int dimension;
127
128 /* Minimum byte code in each dimension. */
129 int code_space[16];
130
131 /* 1 if there's no gap in code-points. */
132 int code_linear_p;
133
134 /* If the charset is treated as 94-chars in ISO-2022, the value is 0.
135 If the charset is treated as 96-chars in ISO-2022, the value is 1. */
136 int iso_chars_96;
137
138 /* ISO final character code for the charset: 48..127.
139 It may be 0 if the charset doesn't conform to ISO-2022. */
140 int iso_final;
141
142 int iso_revision;
143
144 /* If the charset is identical to what supported by Emacs 21 and the
145 priors, the identification number of the charset used in those
146 version. Otherwise, -1. */
147 int emacs_mule_id;
148
149 /* Nonzero iff the charset is compatible with ASCII. */
150 int ascii_compatible_p;
151
152 /* Nonzero iff the charset is supplementary. */
153 int supplementary_p;
154
155 /* Nonzero iff all the code points are representable by Lisp_Int. */
156 int compact_codes_p;
157
158 /* The method for encoding/decoding characters of the charset. */
159 enum charset_method method;
160
161 /* Mininum and Maximum code points of the charset. */
162 unsigned min_code, max_code;
163
164 /* Mininum and Maximum character codes of the charset. If the
165 charset is compatible with ASCII, min_char is a minimum non-ASCII
166 character of the charset. */
167 int min_char, max_char;
168
169 /* The code returned by ENCODE_CHAR if a character is not encodable
170 by the charset. */
171 unsigned invalid_code;
172
173 /* If the method of the charset is CHARSET_METHOD_MAP, this is a
174 table of bits used to quickly and roughly guess if a character
175 belongs to the charset.
176
177 The first 64 elements are 512 bits for characters less than
178 0x10000. Each bit corresponds to 128-character block. The last
179 126 elements are 1008 bits for the greater characters
180 (0x10000..0x3FFFFF). Each bit corresponds to 4096-character
181 block.
182
183 If a bit is 1, at least one character in the corresponds block is
184 in this charset. */
185 unsigned char fast_map[190];
186
187 /* Offset value to calculate a character code from code-point, and
188 visa versa. */
189 int code_offset;
190
191 int unified_p;
192};
193
194/* Hash table of charset symbols vs. the correponding attribute
195 vectors. */
196extern Lisp_Object Vcharset_hash_table;
197
198/* Table of struct charset. */
199extern struct charset *charset_table;
200extern int charset_table_used;
201
202#define CHARSET_FROM_ID(id) (charset_table + (id))
203
204extern Lisp_Object Vcharset_list;
205extern Lisp_Object Viso_2022_charset_list;
206extern Lisp_Object Vemacs_mule_charset_list;
207
208extern struct charset *emacs_mule_charset[256];
209
210
211/* Macros to access information about charset. */
212
213/* Return the attribute vector of charset whose symbol is SYMBOL. */
214#define CHARSET_SYMBOL_ATTRIBUTES(symbol) \
215 Fgethash ((symbol), Vcharset_hash_table, Qnil)
216
217#define CHARSET_ATTR_ID(attrs) AREF ((attrs), charset_id)
218#define CHARSET_ATTR_NAME(attrs) AREF ((attrs), charset_name)
219#define CHARSET_ATTR_PLIST(attrs) AREF ((attrs), charset_plist)
220#define CHARSET_ATTR_MAP(attrs) AREF ((attrs), charset_map)
221#define CHARSET_ATTR_DECODER(attrs) AREF ((attrs), charset_decoder)
222#define CHARSET_ATTR_ENCODER(attrs) AREF ((attrs), charset_encoder)
223#define CHARSET_ATTR_PARENTS(attrs) AREF ((attrs), charset_parents)
224#define CHARSET_ATTR_UNIFY_MAP(attrs) AREF ((attrs), charset_unify_map)
225#define CHARSET_ATTR_DEUNIFIER(attrs) AREF ((attrs), charset_deunifier)
226
227#define CHARSET_SYMBOL_ID(symbol) \
228 CHARSET_ATTR_ID (CHARSET_SYMBOL_ATTRIBUTES (symbol))
229
230/* Return an index to Vcharset_hash_table of the charset whose symbol
231 is SYMBOL. */
232#define CHARSET_SYMBOL_HASH_INDEX(symbol) \
233 hash_lookup (XHASH_TABLE (Vcharset_hash_table), symbol, NULL)
234
235/* Return the attribute vector of CHARSET. */
236#define CHARSET_ATTRIBUTES(charset) \
237 (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), (charset)->hash_index))
238
239#define CHARSET_ID(charset) ((charset)->id)
240#define CHARSET_HASH_INDEX(charset) ((charset)->hash_index)
241#define CHARSET_DIMENSION(charset) ((charset)->dimension)
242#define CHARSET_CODE_SPACE(charset) ((charset)->code_space)
243#define CHARSET_CODE_LINEAR_P(charset) ((charset)->code_linear_p)
244#define CHARSET_ISO_CHARS_96(charset) ((charset)->iso_chars_96)
245#define CHARSET_ISO_FINAL(charset) ((charset)->iso_final)
246#define CHARSET_ISO_PLANE(charset) ((charset)->iso_plane)
247#define CHARSET_ISO_REVISION(charset) ((charset)->iso_revision)
248#define CHARSET_EMACS_MULE_ID(charset) ((charset)->emacs_mule_id)
249#define CHARSET_ASCII_COMPATIBLE_P(charset) ((charset)->ascii_compatible_p)
250#define CHARSET_COMPACT_CODES_P(charset) ((charset)->compact_codes_p)
251#define CHARSET_METHOD(charset) ((charset)->method)
252#define CHARSET_MIN_CODE(charset) ((charset)->min_code)
253#define CHARSET_MAX_CODE(charset) ((charset)->max_code)
254#define CHARSET_INVALID_CODE(charset) ((charset)->invalid_code)
255#define CHARSET_MIN_CHAR(charset) ((charset)->min_char)
256#define CHARSET_MAX_CHAR(charset) ((charset)->max_char)
257#define CHARSET_CODE_OFFSET(charset) ((charset)->code_offset)
258#define CHARSET_UNIFIED_P(charset) ((charset)->unified_p)
259
260#define CHARSET_NAME(charset) \
261 (CHARSET_ATTR_NAME (CHARSET_ATTRIBUTES (charset)))
262#define CHARSET_MAP(charset) \
263 (CHARSET_ATTR_MAP (CHARSET_ATTRIBUTES (charset)))
264#define CHARSET_DECODER(charset) \
265 (CHARSET_ATTR_DECODER (CHARSET_ATTRIBUTES (charset)))
266#define CHARSET_ENCODER(charset) \
267 (CHARSET_ATTR_ENCODER (CHARSET_ATTRIBUTES (charset)))
268#define CHARSET_PARENTS(charset) \
269 (CHARSET_ATTR_PARENTS (CHARSET_ATTRIBUTES (charset)))
270#define CHARSET_UNIFY_MAP(charset) \
271 (CHARSET_ATTR_UNIFY_MAP (CHARSET_ATTRIBUTES (charset)))
272#define CHARSET_DEUNIFIER(charset) \
273 (CHARSET_ATTR_DEUNIFIER (CHARSET_ATTRIBUTES (charset)))
274
275
276/* Nonzero iff OBJ is a valid charset symbol. */
277#define CHARSETP(obj) (CHARSET_SYMBOL_HASH_INDEX (obj) >= 0)
278
279/* Check if X is a valid charset symbol. If not, signal an error. */
280#define CHECK_CHARSET(x) \
384107f2 281 do { \
3263d5a2
KH
282 if (! SYMBOLP (x) || CHARSET_SYMBOL_HASH_INDEX (x) < 0) \
283 x = wrong_type_argument (Qcharsetp, (x)); \
384107f2 284 } while (0)
54e15bb9 285
4ed46869 286
3263d5a2
KH
287/* Check if X is a valid charset symbol. If valid, set ID to the id
288 number of the charset. Otherwise, signal an error. */
289#define CHECK_CHARSET_GET_ID(x, id) \
290 do { \
291 int idx; \
292 \
293 if (! SYMBOLP (x) || (idx = CHARSET_SYMBOL_HASH_INDEX (x)) < 0) \
294 x = wrong_type_argument (Qcharsetp, (x)); \
295 id = AREF (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), idx), \
296 charset_id); \
4ed46869
KH
297 } while (0)
298
6e4dc3e1 299
3263d5a2
KH
300/* Check if X is a valid charset symbol. If valid, set ATTR to the
301 attr vector of the charset. Otherwise, signal an error. */
302#define CHECK_CHARSET_GET_ATTR(x, attr) \
303 do { \
304 if (!SYMBOLP (x) || NILP (attr = CHARSET_SYMBOL_ATTRIBUTES (x))) \
305 x = wrong_type_argument (Qcharsetp, (x)); \
6e4dc3e1
KH
306 } while (0)
307
6e4dc3e1 308
3263d5a2
KH
309#define CHECK_CHARSET_GET_CHARSET(x, charset) \
310 do { \
311 int id; \
312 CHECK_CHARSET_GET_ID (x, id); \
313 charset = CHARSET_FROM_ID (id); \
c399b461
RS
314 } while (0)
315
c399b461 316
3263d5a2
KH
317/* Lookup Vcharset_order_list and return the first charset that
318 contains the character C. */
319#define CHAR_CHARSET(c) \
320 char_charset ((c), Qnil, NULL)
321
322#if 0
323/* Char-table of charset-sets. Each element is a bool vector indexed
324 by a charset ID. */
325extern Lisp_Object Vchar_charset_set;
326
327/* Charset-bag of character C. */
328#define CHAR_CHARSET_SET(c) \
329 CHAR_TABLE_REF (Vchar_charset_set, c)
330
331/* Check if two characters C1 and C2 belong to the same charset. */
332#define SAME_CHARSET_P(c1, c2) \
333 intersection_p (CHAR_CHARSET_SET (c1), CHAR_CHARSET_SET (c2))
334
335#endif
336
337
338/* Return a character correponding to the code-point CODE of CHARSET.
339 Try some optimization before calling decode_char. */
340
341#define DECODE_CHAR(charset, code) \
342 ((ASCII_BYTE_P (code) && (charset)->ascii_compatible_p) \
343 ? (code) \
344 : ((code) < (charset)->min_code || (code) > (charset)->max_code) \
345 ? -1 \
346 : (charset)->unified_p \
347 ? decode_char ((charset), (code)) \
348 : (charset)->method == CHARSET_METHOD_OFFSET \
349 ? ((charset)->code_linear_p \
350 ? (code) - (charset)->min_code + (charset)->code_offset \
351 : decode_char ((charset), (code))) \
352 : (charset)->method == CHARSET_METHOD_MAP \
353 ? ((charset)->code_linear_p \
354 ? XINT (AREF (CHARSET_DECODER (charset), \
355 (code) - (charset)->min_code)) \
356 : decode_char ((charset), (code))) \
357 : decode_char ((charset), (code)))
358
359
360/* Return a code point of CHAR in CHARSET.
361 Try some optimization before calling encode_char. */
362
363#define ENCODE_CHAR(charset, c) \
364 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
365 ? (c) \
366 : (charset)->unified_p \
367 ? encode_char ((charset), (c)) \
368 : ((c) < (charset)->min_char || (c) > (charset)->max_char) \
369 ? (charset)->invalid_code \
370 : (charset)->method == CHARSET_METHOD_OFFSET \
371 ? ((charset)->code_linear_p \
372 ? (c) - (charset)->code_offset + (charset)->min_code \
373 : encode_char ((charset), (c))) \
374 : (charset)->method == CHARSET_METHOD_MAP \
375 ? ((charset)->compact_codes_p \
376 ? XFASTINT (CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c))) \
377 : encode_char ((charset), (c))) \
378 : encode_char ((charset), (c)))
379
380
381/* Set to 1 when a charset map is loaded to warn that a buffer text
382 and a string data may be relocated. */
383extern int charset_map_loaded;
384
385
386/* Set CHARSET to the charset highest priority of C, CODE to the
387 code-point of C in CHARSET. */
388#define SPLIT_CHAR(c, charset, code) \
389 ((charset) = char_charset ((c), Qnil, &(code)))
390
391
392#define ISO_MAX_DIMENSION 3
393#define ISO_MAX_CHARS 2
394#define ISO_MAX_FINAL 0x80 /* only 0x30..0xFF are used */
395
396/* Mapping table from ISO2022's charset (specified by DIMENSION,
397 CHARS, and FINAL_CHAR) to Emacs' charset ID. Should be accessed by
398 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
399extern int iso_charset_table[ISO_MAX_DIMENSION][ISO_MAX_CHARS][ISO_MAX_FINAL];
c399b461 400
3263d5a2
KH
401/* A charset of type iso2022 who has DIMENSION, CHARS, and FINAL
402 (final character). */
403#define ISO_CHARSET_TABLE(dimension, chars_96, final) \
404 iso_charset_table[(dimension) - 1][(chars_96)][(final)]
6e4dc3e1 405
3263d5a2
KH
406/* Nonzero iff the charset who has FAST_MAP may contain C. */
407#define CHARSET_FAST_MAP_REF(c, fast_map) \
408 ((c) < 0x10000 \
409 ? fast_map[(c) >> 10] & (1 << (((c) >> 7) & 7)) \
410 : fast_map[((c) >> 15) + 62] & (1 << (((c) >> 12) & 7)))
6e4dc3e1 411
3263d5a2 412#define CHARSET_FAST_MAP_SET(c, fast_map) \
384107f2 413 do { \
3263d5a2
KH
414 if ((c) < 0x10000) \
415 (fast_map)[(c) >> 10] |= 1 << (((c) >> 7) & 7); \
384107f2 416 else \
3263d5a2 417 (fast_map)[((c) >> 15) + 62] |= 1 << (((c) >> 12) & 7); \
384107f2
KH
418 } while (0)
419
6e4dc3e1 420
6e4dc3e1 421
3263d5a2
KH
422/* 1 iff CHARSET may contain the character C. */
423#define CHAR_CHARSET_P(c, charset) \
424 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
425 || (CHARSET_UNIFIED_P (charset) \
426 ? encode_char ((charset), (c)) != (charset)->invalid_code \
427 : (CHARSET_FAST_MAP_REF ((c), (charset)->fast_map) \
428 && ((charset)->method == CHARSET_METHOD_OFFSET \
429 ? (c) >= (charset)->min_char && (c) <= (charset)->max_char \
430 : ((charset)->method == CHARSET_METHOD_MAP \
431 && (charset)->compact_codes_p) \
432 ? (XFASTINT (CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c))) \
433 != (charset)->invalid_code) \
434 : encode_char ((charset), (c)) != (charset)->invalid_code))))
6e4dc3e1 435
4ed46869 436
3263d5a2
KH
437extern Lisp_Object Qcharsetp;
438
439extern Lisp_Object Qascii, Qunicode;
440extern int charset_ascii, charset_8_bit_control, charset_8_bit_graphic;
441extern int charset_iso_8859_1;
442extern int charset_primary;
443
444extern struct charset *char_charset P_ ((int, Lisp_Object, unsigned *));
445extern Lisp_Object charset_attributes P_ ((int));
446
447extern int decode_char P_ ((struct charset *, unsigned));
448extern unsigned encode_char P_ ((struct charset *, int));
449extern int string_xstring_p P_ ((Lisp_Object));
450
451EXFUN (Funify_charset, 2);
c1f6608b 452
aa01a892 453#endif /* EMACS_CHARSET_H */