comparison src/charset.h @ 89483:2f877ed80fa6

*** empty log message ***
author Kenichi Handa <handa@m17n.org>
date Mon, 08 Sep 2003 12:53:41 +0000
parents 375f2633d815 0c9b64b6b6af
children 111c03888806
comparison
equal deleted inserted replaced
88123:375f2633d815 89483:2f877ed80fa6
1 /* Header for multibyte character handler. 1 /* Header for charset handler.
2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN. 2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation. 3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001 Free Software Foundation, Inc. 4 Copyright (C) 2001 Free Software Foundation, Inc.
5 Copyright (C) 2003
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H13PRO009
5 8
6 This file is part of GNU Emacs. 9 This file is part of GNU Emacs.
7 10
8 GNU Emacs is free software; you can redistribute it and/or modify 11 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by 12 it under the terms of the GNU General Public License as published by
21 Boston, MA 02111-1307, USA. */ 24 Boston, MA 02111-1307, USA. */
22 25
23 #ifndef EMACS_CHARSET_H 26 #ifndef EMACS_CHARSET_H
24 #define EMACS_CHARSET_H 27 #define EMACS_CHARSET_H
25 28
26 /* #define BYTE_COMBINING_DEBUG */ 29 /* Index to arguments of Fdefine_charset_internal. */
27 30
28 /*** GENERAL NOTE on CHARACTER SET (CHARSET) *** 31 enum define_charset_arg_index
29 32 {
30 A character set ("charset" hereafter) is a meaningful collection 33 charset_arg_name,
31 (i.e. language, culture, functionality, etc) of characters. Emacs 34 charset_arg_dimension,
32 handles multiple charsets at once. Each charset corresponds to one 35 charset_arg_code_space,
33 of the ISO charsets. Emacs identifies a charset by a unique 36 charset_arg_min_code,
34 identification number, whereas ISO identifies a charset by a triplet 37 charset_arg_max_code,
35 of DIMENSION, CHARS and FINAL-CHAR. So, hereafter, just saying 38 charset_arg_iso_final,
36 "charset" means an identification number (integer value). 39 charset_arg_iso_revision,
37 40 charset_arg_emacs_mule_id,
38 The value range of charsets is 0x00, 0x81..0xFE. There are four 41 charset_arg_ascii_compatible_p,
39 kinds of charset depending on DIMENSION (1 or 2) and CHARS (94 or 42 charset_arg_supplementary_p,
40 96). For instance, a charset of DIMENSION2_CHARS94 contains 94x94 43 charset_arg_invalid_code,
41 characters. 44 charset_arg_code_offset,
42 45 charset_arg_map,
43 Within Emacs Lisp, a charset is treated as a symbol which has a 46 charset_arg_subset,
44 property `charset'. The property value is a vector containing 47 charset_arg_superset,
45 various information about the charset. For readability of C code, 48 charset_arg_unify_map,
46 we use the following convention for C variable names: 49 charset_arg_plist,
47 charset_symbol: Emacs Lisp symbol of a charset 50 charset_arg_max
48 charset_id: Emacs Lisp integer of an identification number of a charset 51 };
49 charset: C integer of an identification number of a charset 52
50 53
51 Each charset (except for ascii) is assigned a base leading-code 54 /* Indices to charset attributes vector. */
52 (range 0x80..0x9E). In addition, a charset of greater than 0xA0 55
53 (whose base leading-code is 0x9A..0x9D) is assigned an extended 56 enum charset_attr_index
54 leading-code (range 0xA0..0xFE). In this case, each base 57 {
55 leading-code specifies the allowable range of extended leading-code 58 /* ID number of the charset. */
56 as shown in the table below. A leading-code is used to represent a 59 charset_id,
57 character in Emacs' buffer and string. 60
58 61 /* Name of the charset (symbol). */
59 We call a charset which has extended leading-code a "private 62 charset_name,
60 charset" because those are mainly for a charset which is not yet 63
61 registered by ISO. On the contrary, we call a charset which does 64 /* Property list of the charset. */
62 not have extended leading-code an "official charset". 65 charset_plist,
63 66
64 --------------------------------------------------------------------------- 67 /* If the method of the charset is `MAP_DEFERRED', the value is a
65 charset dimension base leading-code extended leading-code 68 mapping vector or a file name that contains mapping vector.
66 --------------------------------------------------------------------------- 69 Otherwise, nil. */
67 0x00 official dim1 -- none -- -- none -- 70 charset_map,
68 (ASCII) 71
69 0x01..0x7F --never used-- 72 /* If the method of the charset is `MAP', the value is a vector
70 0x80 official dim1 -- none -- -- none -- 73 that maps code points of the charset to characters. The vector
71 (eight-bit-graphic) 74 is indexed by a character index. A character index is
72 0x81..0x8F official dim1 same as charset -- none -- 75 calculated from a code point and the code-space table of the
73 0x90..0x99 official dim2 same as charset -- none -- 76 charset. */
74 0x9A..0x9D --never used-- 77 charset_decoder,
75 0x9E official dim1 same as charset -- none -- 78
76 (eight-bit-control) 79 /* If the method of the charset is `MAP', the value is a
77 0x9F --never used-- 80 char-table that maps characters of the charset to code
78 0xA0..0xDF private dim1 0x9A same as charset 81 points. */
79 of 1-column width 82 charset_encoder,
80 0xE0..0xEF private dim1 0x9B same as charset 83
81 of 2-column width 84 /* If the method of the charset is `SUBSET', the value is a vector
82 0xF0..0xF4 private dim2 0x9C same as charset 85 that has this form:
83 of 1-column width 86
84 0xF5..0xFE private dim2 0x9D same as charset 87 [ CHARSET-ID MIN-CODE MAX-CODE OFFSET ]
85 of 2-column width 88
86 0xFF --never used-- 89 CHARSET-ID is an ID number of a parent charset. MIN-CODE and
87 --------------------------------------------------------------------------- 90 MAX-CODE specify the range of characters inherited from the
88 91 parent. OFFSET is an integer value to add to a code point of
89 */ 92 the parent charset to get the corresponding code point of this
90 93 charset. */
91 /* Definition of special leading-codes. */ 94 charset_subset,
92 /* Leading-code followed by extended leading-code. */ 95
93 #define LEADING_CODE_PRIVATE_11 0x9A /* for private DIMENSION1 of 1-column */ 96 /* If the method of the charset is `SUPERSET', the value is a list
94 #define LEADING_CODE_PRIVATE_12 0x9B /* for private DIMENSION1 of 2-column */ 97 whose elements have this form:
95 #define LEADING_CODE_PRIVATE_21 0x9C /* for private DIMENSION2 of 1-column */ 98
96 #define LEADING_CODE_PRIVATE_22 0x9D /* for private DIMENSION2 of 2-column */ 99 (CHARSET-ID . OFFSET)
97 100
98 #define LEADING_CODE_8_BIT_CONTROL 0x9E /* for `eight-bit-control' */ 101 CHARSET-IDs are ID numbers of parent charsets. OFFSET is an
99 102 integer value to add to a code point of the parent charset to
100 /* Extended leading-code. */ 103 get the corresponding code point of this charset. */
101 /* Start of each extended leading-codes. */ 104 charset_superset,
102 #define LEADING_CODE_EXT_11 0xA0 /* follows LEADING_CODE_PRIVATE_11 */ 105
103 #define LEADING_CODE_EXT_12 0xE0 /* follows LEADING_CODE_PRIVATE_12 */ 106 /* The value is a mapping vector or a file name that contains the
104 #define LEADING_CODE_EXT_21 0xF0 /* follows LEADING_CODE_PRIVATE_21 */ 107 mapping. This defines how characters in the charset should be
105 #define LEADING_CODE_EXT_22 0xF5 /* follows LEADING_CODE_PRIVATE_22 */ 108 unified with Unicode. The value of the member
106 /* Maximum value of extended leading-codes. */ 109 `charset_deunifier' is created from this information. */
107 #define LEADING_CODE_EXT_MAX 0xFE 110 charset_unify_map,
108 111
109 /* Definition of minimum/maximum charset of each DIMENSION. */ 112 /* If characters in the charset must be unified Unicode, the value
110 #define MIN_CHARSET_OFFICIAL_DIMENSION1 0x80 113 is a char table that maps a character code in the charset to
111 #define MAX_CHARSET_OFFICIAL_DIMENSION1 0x8F 114 the corresponding Unicode character. */
112 #define MIN_CHARSET_OFFICIAL_DIMENSION2 0x90 115 charset_deunifier,
113 #define MAX_CHARSET_OFFICIAL_DIMENSION2 0x99 116
114 #define MIN_CHARSET_PRIVATE_DIMENSION1 LEADING_CODE_EXT_11 117 /* The length of the charset attribute vector. */
115 #define MIN_CHARSET_PRIVATE_DIMENSION2 LEADING_CODE_EXT_21 118 charset_attr_max
116 119 };
117 /* Maximum value of overall charset identification number. */ 120
118 #define MAX_CHARSET 0xFE 121 /* Methods for converting code points and characters of charsets. */
119 122
120 /* Definition of special charsets. */ 123 enum charset_method
121 #define CHARSET_ASCII 0 /* 0x00..0x7F */ 124 {
122 #define CHARSET_8_BIT_CONTROL 0x9E /* 0x80..0x9F */ 125 /* For a charset of this method, a character code is calculated
123 #define CHARSET_8_BIT_GRAPHIC 0x80 /* 0xA0..0xFF */ 126 from a character index (which is calculated from a code point)
124 127 simply by adding an offset value. */
125 extern int charset_latin_iso8859_1; /* ISO8859-1 (Latin-1) */ 128 CHARSET_METHOD_OFFSET,
126 extern int charset_jisx0208_1978; /* JISX0208.1978 (Japanese Kanji old set) */ 129
127 extern int charset_jisx0208; /* JISX0208.1983 (Japanese Kanji) */ 130 /* For a charset of this method, a decoder vector and an encoder
128 extern int charset_katakana_jisx0201; /* JISX0201.Kana (Japanese Katakana) */ 131 char-table is used for code point <-> character code
129 extern int charset_latin_jisx0201; /* JISX0201.Roman (Japanese Roman) */ 132 conversion. */
130 extern int charset_big5_1; /* Big5 Level 1 (Chinese Traditional) */ 133 CHARSET_METHOD_MAP,
131 extern int charset_big5_2; /* Big5 Level 2 (Chinese Traditional) */ 134
132 135 /* Same as above but decoder and encoder are loaded from a file on
133 /* Check if CH is an ASCII character or a base leading-code. 136 demand. Once loaded, the method is changed to
134 Nowadays, any byte can be the first byte of a character in a 137 CHARSET_METHOD_MAP. */
135 multibyte buffer/string. So this macro name is not appropriate. */ 138 CHARSET_METHOD_MAP_DEFERRED,
136 #define CHAR_HEAD_P(ch) ((unsigned char) (ch) < 0xA0) 139
137 140 /* A charset of this method is a subset of another charset. */
138 /*** GENERAL NOTE on CHARACTER REPRESENTATION *** 141 CHARSET_METHOD_SUBSET,
139 142
140 Firstly, the term "character" or "char" is used for a multilingual 143 /* A charset of this method is a superset of other charsets. */
141 character (of course, including ASCII characters), not for a byte in 144 CHARSET_METHOD_SUPERSET
142 computer memory. We use the term "code" or "byte" for the latter 145 };
143 case. 146
144 147 struct charset
145 A character is identified by charset and one or two POSITION-CODEs. 148 {
146 POSITION-CODE is the position of the character in the charset. A 149 /* Index to charset_table. */
147 character of DIMENSION1 charset has one POSITION-CODE: POSITION-CODE-1. 150 int id;
148 A character of DIMENSION2 charset has two POSITION-CODE: 151
149 POSITION-CODE-1 and POSITION-CODE-2. The code range of 152 /* Index to Vcharset_hash_table. */
150 POSITION-CODE is 0x20..0x7F. 153 int hash_index;
151 154
152 Emacs has two kinds of representation of a character: multi-byte 155 /* Dimension of the charset: 1, 2, 3, or 4. */
153 form (for buffers and strings) and single-word form (for character 156 int dimension;
154 objects in Emacs Lisp). The latter is called "character code" 157
155 hereafter. Both representations encode the information of charset 158 /* Byte code range of each dimension. <code_space>[4N] is a mininum
156 and POSITION-CODE but in a different way (for instance, the MSB of 159 byte code of the (N+1)th dimension, <code_space>[4N+1] is a
157 POSITION-CODE is set in multi-byte form). 160 maximum byte code of the (N+1)th dimension, <code_space>[4N+2] is
158 161 (<code_space>[4N+1] - <code_space>[4N] + 1), <code_space>[4N+3]
159 For details of the multi-byte form, see the section "2. Emacs 162 is a number of characters containd in the first to (N+1)th
160 internal format handlers" of `coding.c'. 163 dismesions. We get `char-index' of a `code-point' from this
161 164 information. */
162 Emacs uses 19 bits for a character code. The bits are divided into 165 int code_space[16];
163 3 fields: FIELD1(5bits):FIELD2(7bits):FIELD3(7bits). 166
164 167 /* If B is a byte of Nth dimension of a code-point, the (N-1)th bit
165 A character code of DIMENSION1 character uses FIELD2 to hold charset 168 of code_space_mask[B] is set. This array is used to quickly
166 and FIELD3 to hold POSITION-CODE-1. A character code of DIMENSION2 169 check if a code-point is in a valid range. */
167 character uses FIELD1 to hold charset, FIELD2 and FIELD3 to hold 170 unsigned char *code_space_mask;
168 POSITION-CODE-1 and POSITION-CODE-2 respectively. 171
169 172 /* 1 if there's no gap in code-points. */
170 More precisely... 173 int code_linear_p;
171 174
172 FIELD2 of DIMENSION1 character (except for ascii, eight-bit-control, 175 /* If the charset is treated as 94-chars in ISO-2022, the value is 0.
173 and eight-bit-graphic) is "charset - 0x70". This is to make all 176 If the charset is treated as 96-chars in ISO-2022, the value is 1. */
174 character codes except for ASCII and 8-bit codes greater than 256. 177 int iso_chars_96;
175 So, the range of FIELD2 of DIMENSION1 character is 0, 1, or 178
176 0x11..0x7F. 179 /* ISO final byte of the charset: 48..127. It may be -1 if the
177 180 charset doesn't conform to ISO-2022. */
178 FIELD1 of DIMENSION2 character is "charset - 0x8F" for official 181 int iso_final;
179 charset and "charset - 0xE0" for private charset. So, the range of 182
180 FIELD1 of DIMENSION2 character is 0x01..0x1E. 183 /* ISO revision number of the charset. */
181 184 int iso_revision;
182 ----------------------------------------------------------------------------- 185
183 charset FIELD1 (5-bit) FIELD2 (7-bit) FIELD3 (7-bit) 186 /* If the charset is identical to what supported by Emacs 21 and the
184 ----------------------------------------------------------------------------- 187 priors, the identification number of the charset used in those
185 ascii 0 0 0x00..0x7F 188 version. Otherwise, -1. */
186 eight-bit-control 0 1 0x00..0x1F 189 int emacs_mule_id;
187 eight-bit-graphic 0 1 0x20..0x7F 190
188 DIMENSION1 0 charset - 0x70 POSITION-CODE-1 191 /* Nonzero iff the charset is compatible with ASCII. */
189 DIMENSION2(o) charset - 0x8F POSITION-CODE-1 POSITION-CODE-2 192 int ascii_compatible_p;
190 DIMENSION2(p) charset - 0xE0 POSITION-CODE-1 POSITION-CODE-2 193
191 ----------------------------------------------------------------------------- 194 /* Nonzero iff the charset is supplementary. */
192 "(o)": official, "(p)": private 195 int supplementary_p;
193 ----------------------------------------------------------------------------- 196
194 */ 197 /* Nonzero iff all the code points are representable by Lisp_Int. */
195 198 int compact_codes_p;
196 /* Masks of each field of character code. */ 199
197 #define CHAR_FIELD1_MASK (0x1F << 14) 200 /* The method for encoding/decoding characters of the charset. */
198 #define CHAR_FIELD2_MASK (0x7F << 7) 201 enum charset_method method;
199 #define CHAR_FIELD3_MASK 0x7F 202
200 203 /* Mininum and Maximum code points of the charset. */
201 /* Macros to access each field of character C. */ 204 unsigned min_code, max_code;
202 #define CHAR_FIELD1(c) (((c) & CHAR_FIELD1_MASK) >> 14) 205
203 #define CHAR_FIELD2(c) (((c) & CHAR_FIELD2_MASK) >> 7) 206 /* Offset value used by macros CODE_POINT_TO_INDEX and
204 #define CHAR_FIELD3(c) ((c) & CHAR_FIELD3_MASK) 207 INDEX_TO_CODE_POINT. . */
205 208 unsigned char_index_offset;
206 /* Minimum character code of character of each DIMENSION. */ 209
207 #define MIN_CHAR_OFFICIAL_DIMENSION1 \ 210 /* Mininum and Maximum character codes of the charset. If the
208 ((0x81 - 0x70) << 7) 211 charset is compatible with ASCII, min_char is a minimum non-ASCII
209 #define MIN_CHAR_PRIVATE_DIMENSION1 \ 212 character of the charset. If the method of charset is
210 ((MIN_CHARSET_PRIVATE_DIMENSION1 - 0x70) << 7) 213 CHARSET_METHOD_OFFSET, even if the charset is unified, min_char
211 #define MIN_CHAR_OFFICIAL_DIMENSION2 \ 214 and max_char doesn't change. */
212 ((MIN_CHARSET_OFFICIAL_DIMENSION2 - 0x8F) << 14) 215 int min_char, max_char;
213 #define MIN_CHAR_PRIVATE_DIMENSION2 \ 216
214 ((MIN_CHARSET_PRIVATE_DIMENSION2 - 0xE0) << 14) 217 /* The code returned by ENCODE_CHAR if a character is not encodable
215 /* Maximum character code currently used plus 1. */ 218 by the charset. */
216 #define MAX_CHAR (0x1F << 14) 219 unsigned invalid_code;
217 220
218 /* 1 if C is a single byte character, else 0. */ 221 /* If the method of the charset is CHARSET_METHOD_MAP, this is a
219 #define SINGLE_BYTE_CHAR_P(c) ((unsigned) (c) < 0x100) 222 table of bits used to quickly and roughly guess if a character
220 223 belongs to the charset.
221 /* 1 if BYTE is an ASCII character in itself, in multibyte mode. */ 224
222 #define ASCII_BYTE_P(byte) ((byte) < 0x80) 225 The first 64 elements are 512 bits for characters less than
223 226 0x10000. Each bit corresponds to 128-character block. The last
224 /* A char-table containing information on each character set. 227 126 elements are 1008 bits for the greater characters
225 228 (0x10000..0x3FFFFF). Each bit corresponds to 4096-character
226 Unlike ordinary char-tables, this doesn't contain any nested tables. 229 block.
227 Only the top level elements are used. Each element is a vector of 230
228 the following information: 231 If a bit is 1, at least one character in the corresponding block is
229 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION, 232 in this charset. */
230 LEADING-CODE-BASE, LEADING-CODE-EXT, 233 unsigned char fast_map[190];
231 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE, 234
232 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION, 235 /* Offset value to calculate a character code from code-point, and
233 PLIST. 236 visa versa. */
234 237 int code_offset;
235 CHARSET-ID (integer) is the identification number of the charset. 238
236 239 int unified_p;
237 BYTES (integer) is the length of the multi-byte form of a character 240 };
238 in the charset: one of 1, 2, 3, and 4. 241
239 242 /* Hash table of charset symbols vs. the correponding attribute
240 DIMENSION (integer) is the number of bytes to represent a character: 1 or 2. 243 vectors. */
241 244 extern Lisp_Object Vcharset_hash_table;
242 CHARS (integer) is the number of characters in a dimension: 94 or 96. 245
243 246 /* Table of struct charset. */
244 WIDTH (integer) is the number of columns a character in the charset 247 extern struct charset *charset_table;
245 occupies on the screen: one of 0, 1, and 2.. 248 extern int charset_table_used;
246 249
247 DIRECTION (integer) is the rendering direction of characters in the 250 #define CHARSET_FROM_ID(id) (charset_table + (id))
248 charset when rendering. If 0, render from left to right, else 251
249 render from right to left. 252 extern Lisp_Object Vcharset_ordered_list;
250 253
251 LEADING-CODE-BASE (integer) is the base leading-code for the 254 /* Incremented everytime we change the priority of charsets. */
252 charset. 255 extern unsigned short charset_ordered_list_tick;
253 256
254 LEADING-CODE-EXT (integer) is the extended leading-code for the 257 extern Lisp_Object Vcharset_list;
255 charset. All charsets of less than 0xA0 have the value 0. 258 extern Lisp_Object Viso_2022_charset_list;
256 259 extern Lisp_Object Vemacs_mule_charset_list;
257 ISO-FINAL-CHAR (character) is the final character of the 260
258 corresponding ISO 2022 charset. It is -1 for such a character 261 extern struct charset *emacs_mule_charset[256];
259 that is used only internally (e.g. `eight-bit-control'). 262
260 263
261 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked 264 /* Macros to access information about charset. */
262 while encoding to variants of ISO 2022 coding system, one of the 265
263 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR). It 266 /* Return the attribute vector of charset whose symbol is SYMBOL. */
264 is -1 for such a character that is used only internally 267 #define CHARSET_SYMBOL_ATTRIBUTES(symbol) \
265 (e.g. `eight-bit-control'). 268 Fgethash ((symbol), Vcharset_hash_table, Qnil)
266 269
267 REVERSE-CHARSET (integer) is the charset which differs only in 270 #define CHARSET_ATTR_ID(attrs) AREF ((attrs), charset_id)
268 LEFT-TO-RIGHT value from the charset. If there's no such a 271 #define CHARSET_ATTR_NAME(attrs) AREF ((attrs), charset_name)
269 charset, the value is -1. 272 #define CHARSET_ATTR_PLIST(attrs) AREF ((attrs), charset_plist)
270 273 #define CHARSET_ATTR_MAP(attrs) AREF ((attrs), charset_map)
271 SHORT-NAME (string) is the short name to refer to the charset. 274 #define CHARSET_ATTR_DECODER(attrs) AREF ((attrs), charset_decoder)
272 275 #define CHARSET_ATTR_ENCODER(attrs) AREF ((attrs), charset_encoder)
273 LONG-NAME (string) is the long name to refer to the charset. 276 #define CHARSET_ATTR_SUBSET(attrs) AREF ((attrs), charset_subset)
274 277 #define CHARSET_ATTR_SUPERSET(attrs) AREF ((attrs), charset_superset)
275 DESCRIPTION (string) is the description string of the charset. 278 #define CHARSET_ATTR_UNIFY_MAP(attrs) AREF ((attrs), charset_unify_map)
276 279 #define CHARSET_ATTR_DEUNIFIER(attrs) AREF ((attrs), charset_deunifier)
277 PLIST (property list) may contain any type of information a user 280
278 wants to put and get by functions `put-charset-property' and 281 #define CHARSET_SYMBOL_ID(symbol) \
279 `get-charset-property' respectively. */ 282 CHARSET_ATTR_ID (CHARSET_SYMBOL_ATTRIBUTES (symbol))
280 extern Lisp_Object Vcharset_table; 283
281 284 /* Return an index to Vcharset_hash_table of the charset whose symbol
282 /* Macros to access various information of CHARSET in Vcharset_table. 285 is SYMBOL. */
283 We provide these macros for efficiency. No range check of CHARSET. */ 286 #define CHARSET_SYMBOL_HASH_INDEX(symbol) \
284 287 hash_lookup (XHASH_TABLE (Vcharset_hash_table), symbol, NULL)
285 /* Return entry of CHARSET (C integer) in Vcharset_table. */ 288
286 #define CHARSET_TABLE_ENTRY(charset) \ 289 /* Return the attribute vector of CHARSET. */
287 XCHAR_TABLE (Vcharset_table)->contents[((charset) == CHARSET_ASCII \ 290 #define CHARSET_ATTRIBUTES(charset) \
288 ? 0 : (charset) + 128)] 291 (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), (charset)->hash_index))
289 292
290 /* Return information INFO-IDX of CHARSET. */ 293 #define CHARSET_ID(charset) ((charset)->id)
291 #define CHARSET_TABLE_INFO(charset, info_idx) \ 294 #define CHARSET_HASH_INDEX(charset) ((charset)->hash_index)
292 XVECTOR (CHARSET_TABLE_ENTRY (charset))->contents[info_idx] 295 #define CHARSET_DIMENSION(charset) ((charset)->dimension)
293 296 #define CHARSET_CODE_SPACE(charset) ((charset)->code_space)
294 #define CHARSET_ID_IDX (0) 297 #define CHARSET_CODE_LINEAR_P(charset) ((charset)->code_linear_p)
295 #define CHARSET_BYTES_IDX (1) 298 #define CHARSET_ISO_CHARS_96(charset) ((charset)->iso_chars_96)
296 #define CHARSET_DIMENSION_IDX (2) 299 #define CHARSET_ISO_FINAL(charset) ((charset)->iso_final)
297 #define CHARSET_CHARS_IDX (3) 300 #define CHARSET_ISO_PLANE(charset) ((charset)->iso_plane)
298 #define CHARSET_WIDTH_IDX (4) 301 #define CHARSET_ISO_REVISION(charset) ((charset)->iso_revision)
299 #define CHARSET_DIRECTION_IDX (5) 302 #define CHARSET_EMACS_MULE_ID(charset) ((charset)->emacs_mule_id)
300 #define CHARSET_LEADING_CODE_BASE_IDX (6) 303 #define CHARSET_ASCII_COMPATIBLE_P(charset) ((charset)->ascii_compatible_p)
301 #define CHARSET_LEADING_CODE_EXT_IDX (7) 304 #define CHARSET_COMPACT_CODES_P(charset) ((charset)->compact_codes_p)
302 #define CHARSET_ISO_FINAL_CHAR_IDX (8) 305 #define CHARSET_METHOD(charset) ((charset)->method)
303 #define CHARSET_ISO_GRAPHIC_PLANE_IDX (9) 306 #define CHARSET_MIN_CODE(charset) ((charset)->min_code)
304 #define CHARSET_REVERSE_CHARSET_IDX (10) 307 #define CHARSET_MAX_CODE(charset) ((charset)->max_code)
305 #define CHARSET_SHORT_NAME_IDX (11) 308 #define CHARSET_INVALID_CODE(charset) ((charset)->invalid_code)
306 #define CHARSET_LONG_NAME_IDX (12) 309 #define CHARSET_MIN_CHAR(charset) ((charset)->min_char)
307 #define CHARSET_DESCRIPTION_IDX (13) 310 #define CHARSET_MAX_CHAR(charset) ((charset)->max_char)
308 #define CHARSET_PLIST_IDX (14) 311 #define CHARSET_CODE_OFFSET(charset) ((charset)->code_offset)
309 /* Size of a vector of each entry of Vcharset_table. */ 312 #define CHARSET_UNIFIED_P(charset) ((charset)->unified_p)
310 #define CHARSET_MAX_IDX (15) 313
311 314 #define CHARSET_NAME(charset) \
312 /* And several more macros to be used frequently. */ 315 (CHARSET_ATTR_NAME (CHARSET_ATTRIBUTES (charset)))
313 #define CHARSET_BYTES(charset) \ 316 #define CHARSET_MAP(charset) \
314 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_BYTES_IDX)) 317 (CHARSET_ATTR_MAP (CHARSET_ATTRIBUTES (charset)))
315 #define CHARSET_DIMENSION(charset) \ 318 #define CHARSET_DECODER(charset) \
316 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIMENSION_IDX)) 319 (CHARSET_ATTR_DECODER (CHARSET_ATTRIBUTES (charset)))
317 #define CHARSET_CHARS(charset) \ 320 #define CHARSET_ENCODER(charset) \
318 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_CHARS_IDX)) 321 (CHARSET_ATTR_ENCODER (CHARSET_ATTRIBUTES (charset)))
319 #define CHARSET_WIDTH(charset) \ 322 #define CHARSET_SUBSET(charset) \
320 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_WIDTH_IDX)) 323 (CHARSET_ATTR_SUBSET (CHARSET_ATTRIBUTES (charset)))
321 #define CHARSET_DIRECTION(charset) \ 324 #define CHARSET_SUPERSET(charset) \
322 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIRECTION_IDX)) 325 (CHARSET_ATTR_SUPERSET (CHARSET_ATTRIBUTES (charset)))
323 #define CHARSET_LEADING_CODE_BASE(charset) \ 326 #define CHARSET_UNIFY_MAP(charset) \
324 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_BASE_IDX)) 327 (CHARSET_ATTR_UNIFY_MAP (CHARSET_ATTRIBUTES (charset)))
325 #define CHARSET_LEADING_CODE_EXT(charset) \ 328 #define CHARSET_DEUNIFIER(charset) \
326 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_EXT_IDX)) 329 (CHARSET_ATTR_DEUNIFIER (CHARSET_ATTRIBUTES (charset)))
327 #define CHARSET_ISO_FINAL_CHAR(charset) \ 330
328 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_FINAL_CHAR_IDX)) 331
329 #define CHARSET_ISO_GRAPHIC_PLANE(charset) \ 332 /* Nonzero iff OBJ is a valid charset symbol. */
330 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_GRAPHIC_PLANE_IDX)) 333 #define CHARSETP(obj) (CHARSET_SYMBOL_HASH_INDEX (obj) >= 0)
331 #define CHARSET_REVERSE_CHARSET(charset) \ 334
332 XINT (CHARSET_TABLE_INFO (charset, CHARSET_REVERSE_CHARSET_IDX)) 335 /* Check if X is a valid charset symbol. If not, signal an error. */
333 336 #define CHECK_CHARSET(x) \
334 /* Macros to specify direction of a charset. */ 337 do { \
335 #define CHARSET_DIRECTION_LEFT_TO_RIGHT 0 338 if (! SYMBOLP (x) || CHARSET_SYMBOL_HASH_INDEX (x) < 0) \
336 #define CHARSET_DIRECTION_RIGHT_TO_LEFT 1 339 x = wrong_type_argument (Qcharsetp, (x)); \
337 340 } while (0)
338 /* A vector of charset symbol indexed by charset-id. This is used 341
339 only for returning charset symbol from C functions. */ 342
340 extern Lisp_Object Vcharset_symbol_table; 343 /* Check if X is a valid charset symbol. If valid, set ID to the id
341 344 number of the charset. Otherwise, signal an error. */
342 /* Return symbol of CHARSET. */ 345 #define CHECK_CHARSET_GET_ID(x, id) \
343 #define CHARSET_SYMBOL(charset) \ 346 do { \
344 XVECTOR (Vcharset_symbol_table)->contents[charset] 347 int idx; \
345 348 \
346 /* 1 if CHARSET is in valid value range, else 0. */ 349 if (! SYMBOLP (x) || (idx = CHARSET_SYMBOL_HASH_INDEX (x)) < 0) \
347 #define CHARSET_VALID_P(charset) \ 350 x = wrong_type_argument (Qcharsetp, (x)); \
348 ((charset) == 0 \ 351 id = XINT (AREF (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), idx), \
349 || ((charset) > 0x80 && (charset) <= MAX_CHARSET_OFFICIAL_DIMENSION2) \ 352 charset_id)); \
350 || ((charset) >= MIN_CHARSET_PRIVATE_DIMENSION1 \ 353 } while (0)
351 && (charset) <= MAX_CHARSET) \ 354
352 || ((charset) == CHARSET_8_BIT_CONTROL) \ 355
353 || ((charset) == CHARSET_8_BIT_GRAPHIC)) 356 /* Check if X is a valid charset symbol. If valid, set ATTR to the
354 357 attr vector of the charset. Otherwise, signal an error. */
355 /* 1 if CHARSET is already defined, else 0. */ 358 #define CHECK_CHARSET_GET_ATTR(x, attr) \
356 #define CHARSET_DEFINED_P(charset) \ 359 do { \
357 (((charset) >= 0) && ((charset) <= MAX_CHARSET) \ 360 if (!SYMBOLP (x) || NILP (attr = CHARSET_SYMBOL_ATTRIBUTES (x))) \
358 && !NILP (CHARSET_TABLE_ENTRY (charset))) 361 x = wrong_type_argument (Qcharsetp, (x)); \
359 362 } while (0)
360 /* Since the information CHARSET-BYTES and CHARSET-WIDTH of 363
361 Vcharset_table can be retrieved only by the first byte of 364
362 multi-byte form (an ASCII code or a base leading-code), we provide 365 #define CHECK_CHARSET_GET_CHARSET(x, charset) \
363 here tables to be used by macros BYTES_BY_CHAR_HEAD and 366 do { \
364 WIDTH_BY_CHAR_HEAD for faster information retrieval. */ 367 int id; \
365 extern int bytes_by_char_head[256]; 368 CHECK_CHARSET_GET_ID (x, id); \
366 extern int width_by_char_head[256]; 369 charset = CHARSET_FROM_ID (id); \
367 370 } while (0)
368 #define BYTES_BY_CHAR_HEAD(char_head) \ 371
369 (ASCII_BYTE_P (char_head) ? 1 : bytes_by_char_head[char_head]) 372
370 #define WIDTH_BY_CHAR_HEAD(char_head) \ 373 /* Lookup Vcharset_order_list and return the first charset that
371 (ASCII_BYTE_P (char_head) ? 1 : width_by_char_head[char_head]) 374 contains the character C. */
372 375 #define CHAR_CHARSET(c) \
373 /* Charset of the character C. */ 376 char_charset ((c), Qnil, NULL)
374 #define CHAR_CHARSET(c) \ 377
375 (SINGLE_BYTE_CHAR_P (c) \ 378 #if 0
376 ? (ASCII_BYTE_P (c) \ 379 /* Char-table of charset-sets. Each element is a bool vector indexed
377 ? CHARSET_ASCII \ 380 by a charset ID. */
378 : (c) < 0xA0 ? CHARSET_8_BIT_CONTROL : CHARSET_8_BIT_GRAPHIC) \ 381 extern Lisp_Object Vchar_charset_set;
379 : ((c) < MIN_CHAR_OFFICIAL_DIMENSION2 \ 382
380 ? CHAR_FIELD2 (c) + 0x70 \ 383 /* Charset-bag of character C. */
381 : ((c) < MIN_CHAR_PRIVATE_DIMENSION2 \ 384 #define CHAR_CHARSET_SET(c) \
382 ? CHAR_FIELD1 (c) + 0x8F \ 385 CHAR_TABLE_REF (Vchar_charset_set, c)
383 : CHAR_FIELD1 (c) + 0xE0)))
384 386
385 /* Check if two characters C1 and C2 belong to the same charset. */ 387 /* Check if two characters C1 and C2 belong to the same charset. */
386 #define SAME_CHARSET_P(c1, c2) \ 388 #define SAME_CHARSET_P(c1, c2) \
387 (c1 < MIN_CHAR_OFFICIAL_DIMENSION2 \ 389 intersection_p (CHAR_CHARSET_SET (c1), CHAR_CHARSET_SET (c2))
388 ? (c1 & CHAR_FIELD2_MASK) == (c2 & CHAR_FIELD2_MASK) \ 390
389 : (c1 & CHAR_FIELD1_MASK) == (c2 & CHAR_FIELD1_MASK)) 391 #endif
390 392
391 /* Return a character of which charset is CHARSET and position-codes 393
392 are C1 and C2. DIMENSION1 character ignores C2. */ 394 /* Return a character correponding to the code-point CODE of CHARSET.
393 #define MAKE_CHAR(charset, c1, c2) \ 395 Try some optimization before calling decode_char. */
394 ((charset) == CHARSET_ASCII \ 396
395 ? (c1) & 0x7F \ 397 #define DECODE_CHAR(charset, code) \
396 : (((charset) == CHARSET_8_BIT_CONTROL \ 398 ((ASCII_BYTE_P (code) && (charset)->ascii_compatible_p) \
397 || (charset) == CHARSET_8_BIT_GRAPHIC) \ 399 ? (code) \
398 ? ((c1) & 0x7F) | 0x80 \ 400 : ((code) < (charset)->min_code || (code) > (charset)->max_code) \
399 : ((CHARSET_DEFINED_P (charset) \ 401 ? -1 \
400 ? CHARSET_DIMENSION (charset) == 1 \ 402 : (charset)->unified_p \
401 : (charset) < MIN_CHARSET_PRIVATE_DIMENSION2) \ 403 ? decode_char ((charset), (code)) \
402 ? (((charset) - 0x70) << 7) | ((c1) <= 0 ? 0 : ((c1) & 0x7F)) \ 404 : (charset)->method == CHARSET_METHOD_OFFSET \
403 : ((((charset) \ 405 ? ((charset)->code_linear_p \
404 - ((charset) < MIN_CHARSET_PRIVATE_DIMENSION2 ? 0x8F : 0xE0)) \ 406 ? (code) - (charset)->min_code + (charset)->code_offset \
405 << 14) \ 407 : decode_char ((charset), (code))) \
406 | ((c2) <= 0 ? 0 : ((c2) & 0x7F)) \ 408 : (charset)->method == CHARSET_METHOD_MAP \
407 | ((c1) <= 0 ? 0 : (((c1) & 0x7F) << 7)))))) 409 ? ((charset)->code_linear_p \
408 410 ? XINT (AREF (CHARSET_DECODER (charset), \
409 411 (code) - (charset)->min_code)) \
410 /* If GENERICP is nonzero, return nonzero iff C is a valid normal or 412 : decode_char ((charset), (code))) \
411 generic character. If GENERICP is zero, return nonzero iff C is a 413 : decode_char ((charset), (code)))
412 valid normal character. */ 414
413 #define CHAR_VALID_P(c, genericp) \ 415
414 ((c) >= 0 \ 416 extern Lisp_Object charset_work;
415 && (SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, genericp))) 417
416 418 /* Return a code point of CHAR in CHARSET.
417 /* This default value is used when nonascii-translation-table or 419 Try some optimization before calling encode_char. */
418 nonascii-insert-offset fail to convert unibyte character to a valid 420
419 multibyte character. This makes a Latin-1 character. */ 421 #define ENCODE_CHAR(charset, c) \
420 422 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
421 #define DEFAULT_NONASCII_INSERT_OFFSET 0x800 423 ? (c) \
422 424 : ((charset)->unified_p \
423 /* Parse multibyte string STR of length LENGTH and set BYTES to the 425 || (charset)->method == CHARSET_METHOD_SUBSET \
424 byte length of a character at STR. */ 426 || (charset)->method == CHARSET_METHOD_SUPERSET) \
425 427 ? encode_char ((charset), (c)) \
426 #ifdef BYTE_COMBINING_DEBUG 428 : ((c) < (charset)->min_char || (c) > (charset)->max_char) \
427 429 ? (charset)->invalid_code \
428 #define PARSE_MULTIBYTE_SEQ(str, length, bytes) \ 430 : (charset)->method == CHARSET_METHOD_OFFSET \
431 ? ((charset)->code_linear_p \
432 ? (c) - (charset)->code_offset + (charset)->min_code \
433 : encode_char ((charset), (c))) \
434 : (charset)->method == CHARSET_METHOD_MAP \
435 ? ((charset)->compact_codes_p \
436 ? (charset_work = CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c)), \
437 (NILP (charset_work) \
438 ? (charset)->invalid_code \
439 : XFASTINT (charset_work))) \
440 : encode_char ((charset), (c))) \
441 : encode_char ((charset), (c)))
442
443
444 /* Set to 1 when a charset map is loaded to warn that a buffer text
445 and a string data may be relocated. */
446 extern int charset_map_loaded;
447
448
449 /* Set CHARSET to the charset highest priority of C, CODE to the
450 code-point of C in CHARSET. */
451 #define SPLIT_CHAR(c, charset, code) \
452 ((charset) = char_charset ((c), Qnil, &(code)))
453
454
455 #define ISO_MAX_DIMENSION 3
456 #define ISO_MAX_CHARS 2
457 #define ISO_MAX_FINAL 0x80 /* only 0x30..0xFF are used */
458
459 /* Mapping table from ISO2022's charset (specified by DIMENSION,
460 CHARS, and FINAL_CHAR) to Emacs' charset ID. Should be accessed by
461 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
462 extern int iso_charset_table[ISO_MAX_DIMENSION][ISO_MAX_CHARS][ISO_MAX_FINAL];
463
464 /* A charset of type iso2022 who has DIMENSION, CHARS, and FINAL
465 (final character). */
466 #define ISO_CHARSET_TABLE(dimension, chars_96, final) \
467 iso_charset_table[(dimension) - 1][(chars_96)][(final)]
468
469 /* Nonzero iff the charset who has FAST_MAP may contain C. */
470 #define CHARSET_FAST_MAP_REF(c, fast_map) \
471 ((c) < 0x10000 \
472 ? fast_map[(c) >> 10] & (1 << (((c) >> 7) & 7)) \
473 : fast_map[((c) >> 15) + 62] & (1 << (((c) >> 12) & 7)))
474
475 #define CHARSET_FAST_MAP_SET(c, fast_map) \
429 do { \ 476 do { \
430 int i = 1; \ 477 if ((c) < 0x10000) \
431 while (i < (length) && ! CHAR_HEAD_P ((str)[i])) i++; \ 478 (fast_map)[(c) >> 10] |= 1 << (((c) >> 7) & 7); \
432 (bytes) = BYTES_BY_CHAR_HEAD ((str)[0]); \ 479 else \
433 if ((bytes) > i) \ 480 (fast_map)[((c) >> 15) + 62] |= 1 << (((c) >> 12) & 7); \
434 abort (); \
435 } while (0) 481 } while (0)
436 482
437 #else /* not BYTE_COMBINING_DEBUG */ 483
438 484
439 #define PARSE_MULTIBYTE_SEQ(str, length, bytes) \ 485 /* 1 iff CHARSET may contain the character C. */
440 ((void)(length), (bytes) = BYTES_BY_CHAR_HEAD ((str)[0])) 486 #define CHAR_CHARSET_P(c, charset) \
441 487 ((ASCII_CHAR_P (c) && (charset)->ascii_compatible_p) \
442 #endif /* not BYTE_COMBINING_DEBUG */ 488 || (CHARSET_UNIFIED_P (charset) \
443 489 ? encode_char ((charset), (c)) != (charset)->invalid_code \
444 #define VALID_LEADING_CODE_P(code) \ 490 : (CHARSET_FAST_MAP_REF ((c), (charset)->fast_map) \
445 (! NILP (CHARSET_TABLE_ENTRY (code))) 491 && ((charset)->method == CHARSET_METHOD_OFFSET \
446 492 ? (c) >= (charset)->min_char && (c) <= (charset)->max_char \
447 /* Return 1 iff the byte sequence at unibyte string STR (LENGTH bytes) 493 : ((charset)->method == CHARSET_METHOD_MAP \
448 is valid as a multibyte form. If valid, by a side effect, BYTES is 494 && (charset)->compact_codes_p) \
449 set to the byte length of the multibyte form. */ 495 ? ! NILP (CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c))) \
450 496 : encode_char ((charset), (c)) != (charset)->invalid_code))))
451 #define UNIBYTE_STR_AS_MULTIBYTE_P(str, length, bytes) \ 497
452 (((str)[0] < 0x80 || (str)[0] >= 0xA0) \ 498
453 ? ((bytes) = 1) \ 499 /* Special macros for emacs-mule encoding. */
454 : (((bytes) = BYTES_BY_CHAR_HEAD ((str)[0])), \ 500
455 ((bytes) <= (length) \ 501 /* Leading-code followed by extended leading-code. DIMENSION/COLUMN */
456 && !CHAR_HEAD_P ((str)[1]) \ 502 #define EMACS_MULE_LEADING_CODE_PRIVATE_11 0x9A /* 1/1 */
457 && ((bytes) == 2 \ 503 #define EMACS_MULE_LEADING_CODE_PRIVATE_12 0x9B /* 1/2 */
458 ? (str)[0] != LEADING_CODE_8_BIT_CONTROL \ 504 #define EMACS_MULE_LEADING_CODE_PRIVATE_21 0x9C /* 2/2 */
459 : (!CHAR_HEAD_P ((str)[2]) \ 505 #define EMACS_MULE_LEADING_CODE_PRIVATE_22 0x9D /* 2/2 */
460 && ((bytes) == 3 \ 506
461 ? (((str)[0] != LEADING_CODE_PRIVATE_11 \ 507 extern struct charset *emacs_mule_charset[256];
462 && (str)[0] != LEADING_CODE_PRIVATE_12) \ 508
463 || VALID_LEADING_CODE_P (str[1])) \ 509
464 : (!CHAR_HEAD_P ((str)[3]) \ 510
465 && VALID_LEADING_CODE_P (str[1])))))))) 511 extern Lisp_Object Qcharsetp;
466 512
467 513 extern Lisp_Object Qascii, Qunicode;
468 /* Return 1 iff the byte sequence at multibyte string STR is valid as 514 extern int charset_ascii, charset_eight_bit;
469 a unibyte form. By a side effect, BYTES is set to the byte length 515 extern int charset_iso_8859_1;
470 of one character at STR. */ 516 extern int charset_jisx0201_roman;
471 517 extern int charset_jisx0208_1978;
472 #define MULTIBYTE_STR_AS_UNIBYTE_P(str, bytes) \ 518 extern int charset_jisx0208;
473 ((bytes) = BYTES_BY_CHAR_HEAD ((str)[0]), \ 519
474 (str)[0] != LEADING_CODE_8_BIT_CONTROL) 520 extern int charset_unibyte;
475 521
476 /* The charset of character C is stored in CHARSET, and the 522 extern struct charset *char_charset P_ ((int, Lisp_Object, unsigned *));
477 position-codes of C are stored in C1 and C2. 523 extern Lisp_Object charset_attributes P_ ((int));
478 We store -1 in C2 if the dimension of the charset is 1. */ 524
479 525 extern int decode_char P_ ((struct charset *, unsigned));
480 #define SPLIT_CHAR(c, charset, c1, c2) \ 526 extern unsigned encode_char P_ ((struct charset *, int));
481 (SINGLE_BYTE_CHAR_P (c) \ 527 extern int string_xstring_p P_ ((Lisp_Object));
482 ? ((charset \ 528
483 = (ASCII_BYTE_P (c) \ 529 extern void map_charset_chars P_ ((void (*) (Lisp_Object, Lisp_Object),
484 ? CHARSET_ASCII \ 530 Lisp_Object, Lisp_Object,
485 : ((c) < 0xA0 ? CHARSET_8_BIT_CONTROL : CHARSET_8_BIT_GRAPHIC))), \ 531 struct charset *, unsigned, unsigned));
486 c1 = (c), c2 = -1) \ 532
487 : ((c) & CHAR_FIELD1_MASK \ 533 EXFUN (Funify_charset, 3);
488 ? (charset = (CHAR_FIELD1 (c) \
489 + ((c) < MIN_CHAR_PRIVATE_DIMENSION2 ? 0x8F : 0xE0)), \
490 c1 = CHAR_FIELD2 (c), \
491 c2 = CHAR_FIELD3 (c)) \
492 : (charset = CHAR_FIELD2 (c) + 0x70, \
493 c1 = CHAR_FIELD3 (c), \
494 c2 = -1)))
495
496 /* Return 1 iff character C has valid printable glyph. */
497 #define CHAR_PRINTABLE_P(c) (ASCII_BYTE_P (c) || char_printable_p (c))
498
499 /* The charset of the character at STR is stored in CHARSET, and the
500 position-codes are stored in C1 and C2.
501 We store -1 in C2 if the character is just 2 bytes. */
502
503 #define SPLIT_STRING(str, len, charset, c1, c2) \
504 ((BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) < 2 \
505 || BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) > len \
506 || split_string (str, len, &charset, &c1, &c2) < 0) \
507 ? c1 = *(str), charset = CHARSET_ASCII \
508 : charset)
509
510 /* Mapping table from ISO2022's charset (specified by DIMENSION,
511 CHARS, and FINAL_CHAR) to Emacs' charset. Should be accessed by
512 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
513 extern int iso_charset_table[2][2][128];
514
515 #define ISO_CHARSET_TABLE(dimension, chars, final_char) \
516 iso_charset_table[XINT (dimension) - 1][XINT (chars) > 94][XINT (final_char)]
517
518 #define BASE_LEADING_CODE_P(c) (BYTES_BY_CHAR_HEAD ((unsigned char) (c)) > 1)
519
520 /* Return how many bytes C will occupy in a multibyte buffer. */
521 #define CHAR_BYTES(c) \
522 (SINGLE_BYTE_CHAR_P (c) \
523 ? ((ASCII_BYTE_P (c) || (c) >= 0xA0) ? 1 : 2) \
524 : char_bytes (c))
525
526 /* The following two macros CHAR_STRING and STRING_CHAR are the main
527 entry points to convert between Emacs's two types of character
528 representations: multi-byte form and single-word form (character
529 code). */
530
531 /* Store multi-byte form of the character C in STR. The caller should
532 allocate at least MAX_MULTIBYTE_LENGTH bytes area at STR in
533 advance. Returns the length of the multi-byte form. If C is an
534 invalid character code, signal an error. */
535
536 #define CHAR_STRING(c, str) \
537 (SINGLE_BYTE_CHAR_P (c) \
538 ? ((ASCII_BYTE_P (c) || c >= 0xA0) \
539 ? (*(str) = (unsigned char)(c), 1) \
540 : (*(str) = LEADING_CODE_8_BIT_CONTROL, *((str)+ 1) = c + 0x20, 2)) \
541 : char_to_string (c, (unsigned char *) str))
542
543 /* Like CHAR_STRING but don't signal an error if C is invalid.
544 Value is -1 in this case. */
545
546 #define CHAR_STRING_NO_SIGNAL(c, str) \
547 (SINGLE_BYTE_CHAR_P (c) \
548 ? ((ASCII_BYTE_P (c) || c >= 0xA0) \
549 ? (*(str) = (unsigned char)(c), 1) \
550 : (*(str) = LEADING_CODE_8_BIT_CONTROL, *((str)+ 1) = c + 0x20, 2)) \
551 : char_to_string_1 (c, (unsigned char *) str))
552
553 /* Return a character code of the character of which multi-byte form
554 is at STR and the length is LEN. If STR doesn't contain valid
555 multi-byte form, only the first byte in STR is returned. */
556
557 #define STRING_CHAR(str, len) \
558 (BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
559 ? (unsigned char) *(str) \
560 : string_to_char (str, len, 0))
561
562 /* This is like STRING_CHAR but the third arg ACTUAL_LEN is set to the
563 length of the multi-byte form. Just to know the length, use
564 MULTIBYTE_FORM_LENGTH. */
565
566 #define STRING_CHAR_AND_LENGTH(str, len, actual_len) \
567 (BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
568 ? ((actual_len) = 1), (unsigned char) *(str) \
569 : string_to_char (str, len, &(actual_len)))
570
571 /* Fetch the "next" character from Lisp string STRING at byte position
572 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
573
574 All the args must be side-effect-free.
575 BYTEIDX and CHARIDX must be lvalues;
576 we increment them past the character fetched. */
577
578 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
579 if (1) \
580 { \
581 CHARIDX++; \
582 if (STRING_MULTIBYTE (STRING)) \
583 { \
584 const unsigned char *ptr = SDATA (STRING) + BYTEIDX; \
585 int space_left = SBYTES (STRING) - BYTEIDX; \
586 int actual_len; \
587 \
588 OUTPUT = STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
589 BYTEIDX += actual_len; \
590 } \
591 else \
592 OUTPUT = SREF (STRING, BYTEIDX++); \
593 } \
594 else
595
596 /* Like FETCH_STRING_CHAR_ADVANCE but assume STRING is multibyte. */
597
598 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
599 if (1) \
600 { \
601 const unsigned char *fetch_string_char_ptr = SDATA (STRING) + BYTEIDX; \
602 int fetch_string_char_space_left = SBYTES (STRING) - BYTEIDX; \
603 int actual_len; \
604 \
605 OUTPUT \
606 = STRING_CHAR_AND_LENGTH (fetch_string_char_ptr, \
607 fetch_string_char_space_left, actual_len); \
608 \
609 BYTEIDX += actual_len; \
610 CHARIDX++; \
611 } \
612 else
613
614 /* Like FETCH_STRING_CHAR_ADVANCE but fetch character from the current
615 buffer. */
616
617 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
618 if (1) \
619 { \
620 CHARIDX++; \
621 if (!NILP (current_buffer->enable_multibyte_characters)) \
622 { \
623 unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \
624 int space_left = ((CHARIDX < GPT ? GPT_BYTE : Z_BYTE) - BYTEIDX); \
625 int actual_len; \
626 \
627 OUTPUT= STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
628 BYTEIDX += actual_len; \
629 } \
630 else \
631 { \
632 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
633 BYTEIDX++; \
634 } \
635 } \
636 else
637
638 /* Return the length of the multi-byte form at string STR of length LEN. */
639
640 #define MULTIBYTE_FORM_LENGTH(str, len) \
641 (BYTES_BY_CHAR_HEAD (*(unsigned char *)(str)) == 1 \
642 ? 1 \
643 : multibyte_form_length (str, len))
644
645 /* If P is before LIMIT, advance P to the next character boundary. It
646 assumes that P is already at a character boundary of the sane
647 mulitbyte form whose end address is LIMIT. */
648
649 #define NEXT_CHAR_BOUNDARY(p, limit) \
650 do { \
651 if ((p) < (limit)) \
652 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
653 } while (0)
654
655
656 /* If P is after LIMIT, advance P to the previous character boundary.
657 It assumes that P is already at a character boundary of the sane
658 mulitbyte form whose beginning address is LIMIT. */
659
660 #define PREV_CHAR_BOUNDARY(p, limit) \
661 do { \
662 if ((p) > (limit)) \
663 { \
664 const unsigned char *p0 = (p); \
665 do { \
666 p0--; \
667 } while (p0 >= limit && ! CHAR_HEAD_P (*p0)); \
668 (p) = (BYTES_BY_CHAR_HEAD (*p0) == (p) - p0) ? p0 : (p) - 1; \
669 } \
670 } while (0)
671
672
673 #ifdef emacs
674
675 /* Increase the buffer byte position POS_BYTE of the current buffer to
676 the next character boundary. This macro relies on the fact that
677 *GPT_ADDR and *Z_ADDR are always accessible and the values are
678 '\0'. No range checking of POS. */
679
680 #ifdef BYTE_COMBINING_DEBUG
681
682 #define INC_POS(pos_byte) \
683 do { \
684 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
685 if (BASE_LEADING_CODE_P (*p)) \
686 { \
687 int len, bytes; \
688 len = Z_BYTE - pos_byte; \
689 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
690 pos_byte += bytes; \
691 } \
692 else \
693 pos_byte++; \
694 } while (0)
695
696 #else /* not BYTE_COMBINING_DEBUG */
697
698 #define INC_POS(pos_byte) \
699 do { \
700 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
701 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
702 } while (0)
703
704 #endif /* not BYTE_COMBINING_DEBUG */
705
706 /* Decrease the buffer byte position POS_BYTE of the current buffer to
707 the previous character boundary. No range checking of POS. */
708 #define DEC_POS(pos_byte) \
709 do { \
710 unsigned char *p, *p_min; \
711 \
712 pos_byte--; \
713 if (pos_byte < GPT_BYTE) \
714 p = BEG_ADDR + pos_byte - BEG_BYTE, p_min = BEG_ADDR; \
715 else \
716 p = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE, p_min = GAP_END_ADDR;\
717 if (p > p_min && !CHAR_HEAD_P (*p)) \
718 { \
719 unsigned char *pend = p--; \
720 int len, bytes; \
721 if (p_min < p - MAX_MULTIBYTE_LENGTH) \
722 p_min = p - MAX_MULTIBYTE_LENGTH; \
723 while (p > p_min && !CHAR_HEAD_P (*p)) p--; \
724 len = pend + 1 - p; \
725 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
726 if (bytes == len) \
727 pos_byte -= len - 1; \
728 } \
729 } while (0)
730
731 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
732
733 #define INC_BOTH(charpos, bytepos) \
734 do \
735 { \
736 (charpos)++; \
737 if (NILP (current_buffer->enable_multibyte_characters)) \
738 (bytepos)++; \
739 else \
740 INC_POS ((bytepos)); \
741 } \
742 while (0)
743
744 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
745
746 #define DEC_BOTH(charpos, bytepos) \
747 do \
748 { \
749 (charpos)--; \
750 if (NILP (current_buffer->enable_multibyte_characters)) \
751 (bytepos)--; \
752 else \
753 DEC_POS ((bytepos)); \
754 } \
755 while (0)
756
757 /* Increase the buffer byte position POS_BYTE of the current buffer to
758 the next character boundary. This macro relies on the fact that
759 *GPT_ADDR and *Z_ADDR are always accessible and the values are
760 '\0'. No range checking of POS_BYTE. */
761
762 #ifdef BYTE_COMBINING_DEBUG
763
764 #define BUF_INC_POS(buf, pos_byte) \
765 do { \
766 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \
767 if (BASE_LEADING_CODE_P (*p)) \
768 { \
769 int len, bytes; \
770 len = BUF_Z_BYTE (buf) - pos_byte; \
771 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
772 pos_byte += bytes; \
773 } \
774 else \
775 pos_byte++; \
776 } while (0)
777
778 #else /* not BYTE_COMBINING_DEBUG */
779
780 #define BUF_INC_POS(buf, pos_byte) \
781 do { \
782 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \
783 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
784 } while (0)
785
786 #endif /* not BYTE_COMBINING_DEBUG */
787
788 /* Decrease the buffer byte position POS_BYTE of the current buffer to
789 the previous character boundary. No range checking of POS_BYTE. */
790 #define BUF_DEC_POS(buf, pos_byte) \
791 do { \
792 unsigned char *p, *p_min; \
793 pos_byte--; \
794 if (pos_byte < BUF_GPT_BYTE (buf)) \
795 { \
796 p = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
797 p_min = BUF_BEG_ADDR (buf); \
798 } \
799 else \
800 { \
801 p = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
802 p_min = BUF_GAP_END_ADDR (buf); \
803 } \
804 if (p > p_min && !CHAR_HEAD_P (*p)) \
805 { \
806 unsigned char *pend = p--; \
807 int len, bytes; \
808 if (p_min < p - MAX_MULTIBYTE_LENGTH) \
809 p_min = p - MAX_MULTIBYTE_LENGTH; \
810 while (p > p_min && !CHAR_HEAD_P (*p)) p--; \
811 len = pend + 1 - p; \
812 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
813 if (bytes == len) \
814 pos_byte -= len - 1; \
815 } \
816 } while (0)
817
818 #endif /* emacs */
819
820 /* This is the maximum byte length of multi-byte sequence. */
821 #define MAX_MULTIBYTE_LENGTH 4
822
823 extern void invalid_character P_ ((int));
824
825 extern int translate_char P_ ((Lisp_Object, int, int, int, int));
826 extern int split_string P_ ((const unsigned char *, int, int *,
827 unsigned char *, unsigned char *));
828 extern int char_to_string P_ ((int, unsigned char *));
829 extern int char_to_string_1 P_ ((int, unsigned char *));
830 extern int string_to_char P_ ((const unsigned char *, int, int *));
831 extern int char_printable_p P_ ((int c));
832 extern int multibyte_form_length P_ ((const unsigned char *, int));
833 extern void parse_str_as_multibyte P_ ((const unsigned char *, int, int *,
834 int *));
835 extern int str_as_multibyte P_ ((unsigned char *, int, int, int *));
836 extern int parse_str_to_multibyte P_ ((unsigned char *, int));
837 extern int str_to_multibyte P_ ((unsigned char *, int, int));
838 extern int str_as_unibyte P_ ((unsigned char *, int));
839 extern int get_charset_id P_ ((Lisp_Object));
840 extern int find_charset_in_text P_ ((const unsigned char *, int, int, int *,
841 Lisp_Object));
842 extern int strwidth P_ ((unsigned char *, int));
843 extern int c_string_width P_ ((const unsigned char *, int, int, int *, int *));
844 extern int lisp_string_width P_ ((Lisp_Object, int, int *, int *));
845 extern int char_bytes P_ ((int));
846 extern int char_valid_p P_ ((int, int));
847
848 EXFUN (Funibyte_char_to_multibyte, 1);
849
850 extern Lisp_Object Vtranslation_table_vector;
851
852 /* Return a translation table of id number ID. */
853 #define GET_TRANSLATION_TABLE(id) \
854 (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)]))
855
856 /* A char-table for characters which may invoke auto-filling. */
857 extern Lisp_Object Vauto_fill_chars;
858
859 /* Copy LEN bytes from FROM to TO. This macro should be used only
860 when a caller knows that LEN is short and the obvious copy loop is
861 faster than calling bcopy which has some overhead. Copying a
862 multibyte sequence of a multibyte character is the typical case. */
863
864 #define BCOPY_SHORT(from, to, len) \
865 do { \
866 int i = len; \
867 const unsigned char *from_p = from; \
868 unsigned char *to_p = to; \
869 while (i--) *to_p++ = *from_p++; \
870 } while (0)
871 534
872 #endif /* EMACS_CHARSET_H */ 535 #endif /* EMACS_CHARSET_H */