Commit | Line | Data |
---|---|---|
0168c3d8 KH |
1 | /* Header for multibyte character handler. |
2 | Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN. | |
8f924df7 | 3 | Licensed to the Free Software Foundation. |
3ec0b7a9 | 4 | Copyright (C) 2003, 2006 |
0168c3d8 KH |
5 | National Institute of Advanced Industrial Science and Technology (AIST) |
6 | Registration Number H13PRO009 | |
7 | ||
8 | This file is part of GNU Emacs. | |
9 | ||
10 | GNU Emacs is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2, or (at your option) | |
13 | any later version. | |
14 | ||
15 | GNU Emacs is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with GNU Emacs; see the file COPYING. If not, write to | |
22 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | Boston, MA 02111-1307, USA. */ | |
24 | ||
25 | #ifndef EMACS_CHARACTER_H | |
26 | #define EMACS_CHARACTER_H | |
27 | ||
885317d8 KH |
28 | /* character code 1st byte byte sequence |
29 | -------------- -------- ------------- | |
30 | 0-7F 00..7F 0xxxxxxx | |
31 | 80-7FF C2..DF 110xxxxx 10xxxxxx | |
32 | 800-FFFF E0..EF 1110xxxx 10xxxxxx 10xxxxxx | |
33 | 10000-1FFFFF F0..F7 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | |
34 | 200000-3FFF7F F8 11111000 1000xxxx 10xxxxxx 10xxxxxx 10xxxxxx | |
c43e85a9 KH |
35 | 3FFF80-3FFFFF C0..C1 1100000x 10xxxxxx (for eight-bit-char) |
36 | 400000-... invalid | |
0168c3d8 | 37 | |
c43e85a9 KH |
38 | invalid 1st byte 80..BF 10xxxxxx |
39 | F9..FF 11111xxx (xxx != 000) | |
0168c3d8 KH |
40 | */ |
41 | ||
885317d8 | 42 | /* Maximum character code ((1 << CHARACTERBITS) - 1). */ |
0168c3d8 KH |
43 | #define MAX_CHAR 0x3FFFFF |
44 | ||
885317d8 | 45 | /* Maximum Unicode character code. */ |
0168c3d8 KH |
46 | #define MAX_UNICODE_CHAR 0x10FFFF |
47 | ||
885317d8 | 48 | /* Maximum N-byte character codes. */ |
0168c3d8 KH |
49 | #define MAX_1_BYTE_CHAR 0x7F |
50 | #define MAX_2_BYTE_CHAR 0x7FF | |
51 | #define MAX_3_BYTE_CHAR 0xFFFF | |
52 | #define MAX_4_BYTE_CHAR 0x1FFFFF | |
53 | #define MAX_5_BYTE_CHAR 0x3FFF7F | |
54 | ||
8bc28f69 KH |
55 | /* Nonzero iff C is a character that corresponds to a raw 8-bit |
56 | byte. */ | |
57 | #define CHAR_BYTE8_P(c) ((c) > MAX_5_BYTE_CHAR) | |
58 | ||
885317d8 | 59 | /* Return the character code for raw 8-bit byte BYTE. */ |
0168c3d8 | 60 | #define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00) |
885317d8 KH |
61 | |
62 | /* Return the raw 8-bit byte for character C. */ | |
8bc28f69 KH |
63 | #define CHAR_TO_BYTE8(c) \ |
64 | (CHAR_BYTE8_P (c) \ | |
65 | ? (c) - 0x3FFF00 \ | |
66 | : multibyte_char_to_unibyte (c, Qnil)) | |
885317d8 KH |
67 | |
68 | /* Nonzero iff BYTE is the 1st byte of a multibyte form of a character | |
69 | that corresponds to a raw 8-bit byte. */ | |
0168c3d8 KH |
70 | #define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1) |
71 | ||
43c47483 KH |
72 | /* Mapping table from unibyte chars to multibyte chars. */ |
73 | extern int unibyte_to_multibyte_table[256]; | |
3e411074 | 74 | |
43c47483 KH |
75 | /* Convert the unibyte character C to the corresponding multibyte |
76 | character. If C can't be converted, return C. */ | |
77 | #define unibyte_char_to_multibyte(c) \ | |
78 | ((c) < 256 ? unibyte_to_multibyte_table[(c)] : (c)) | |
3e411074 | 79 | |
1acbd067 KH |
80 | /* Nth element is 1 iff unibyte char N can be mapped to a multibyte |
81 | char. */ | |
82 | extern char unibyte_has_multibyte_table[256]; | |
83 | ||
84 | #define UNIBYTE_CHAR_HAS_MULTIBYTE_P(c) (unibyte_has_multibyte_table[(c)]) | |
85 | ||
43c47483 KH |
86 | /* If C is not ASCII, make it unibyte. */ |
87 | #define MAKE_CHAR_UNIBYTE(c) \ | |
88 | do { \ | |
89 | if (! ASCII_CHAR_P (c)) \ | |
90 | c = CHAR_TO_BYTE8 (c); \ | |
91 | } while (0) | |
3e411074 | 92 | |
3e411074 | 93 | |
43c47483 KH |
94 | /* If C is not ASCII, make it multibyte. It assumes C < 256. */ |
95 | #define MAKE_CHAR_MULTIBYTE(c) ((c) = unibyte_to_multibyte_table[(c)]) | |
3e411074 | 96 | |
885317d8 | 97 | /* This is the maximum byte length of multibyte form. */ |
0168c3d8 KH |
98 | #define MAX_MULTIBYTE_LENGTH 5 |
99 | ||
b583cead KH |
100 | /* Return a Lisp character whose character code is C. It assumes C is |
101 | a valid character code. */ | |
0168c3d8 KH |
102 | #define make_char(c) make_number (c) |
103 | ||
104 | /* Nonzero iff C is an ASCII byte. */ | |
105 | #define ASCII_BYTE_P(c) ((unsigned) (c) < 0x80) | |
106 | ||
107 | /* Nonzero iff X is a character. */ | |
108 | #define CHARACTERP(x) (NATNUMP (x) && XFASTINT (x) <= MAX_CHAR) | |
109 | ||
f65c6d94 | 110 | /* Nonzero iff C is valid as a character code. GENERICP is not used |
885317d8 KH |
111 | now. */ |
112 | #define CHAR_VALID_P(c, genericp) ((unsigned) (c) <= MAX_CHAR) | |
0168c3d8 KH |
113 | |
114 | /* Check if Lisp object X is a character or not. */ | |
63db3c1b MB |
115 | #define CHECK_CHARACTER(x) \ |
116 | CHECK_TYPE (CHARACTERP (x), Qcharacterp, x) | |
0168c3d8 | 117 | |
8f924df7 KH |
118 | #define CHECK_CHARACTER_CAR(x) \ |
119 | do { \ | |
120 | Lisp_Object tmp = XCAR (x); \ | |
121 | CHECK_CHARACTER (tmp); \ | |
122 | XSETCAR ((x), tmp); \ | |
123 | } while (0) | |
124 | ||
125 | #define CHECK_CHARACTER_CDR(x) \ | |
126 | do { \ | |
127 | Lisp_Object tmp = XCDR (x); \ | |
128 | CHECK_CHARACTER (tmp); \ | |
129 | XSETCDR ((x), tmp); \ | |
130 | } while (0) | |
131 | ||
0168c3d8 KH |
132 | /* Nonzero iff C is an ASCII character. */ |
133 | #define ASCII_CHAR_P(c) ((unsigned) (c) < 0x80) | |
134 | ||
135 | /* Nonzero iff C is a character of code less than 0x100. */ | |
136 | #define SINGLE_BYTE_CHAR_P(c) ((unsigned) (c) < 0x100) | |
137 | ||
138 | /* Nonzero if character C has a printable glyph. */ | |
139 | #define CHAR_PRINTABLE_P(c) \ | |
140 | (((c) >= 32 && ((c) < 127) \ | |
141 | || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c))))) | |
142 | ||
885317d8 | 143 | /* Return byte length of multibyte form for character C. */ |
0168c3d8 KH |
144 | #define CHAR_BYTES(c) \ |
145 | ( (c) <= MAX_1_BYTE_CHAR ? 1 \ | |
146 | : (c) <= MAX_2_BYTE_CHAR ? 2 \ | |
147 | : (c) <= MAX_3_BYTE_CHAR ? 3 \ | |
148 | : (c) <= MAX_4_BYTE_CHAR ? 4 \ | |
149 | : (c) <= MAX_5_BYTE_CHAR ? 5 \ | |
150 | : 2) | |
151 | ||
43c47483 KH |
152 | |
153 | /* Return the leading code of multibyte form of C. */ | |
154 | #define CHAR_LEADING_CODE(c) \ | |
155 | ((c) <= MAX_1_BYTE_CHAR ? c \ | |
156 | : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \ | |
157 | : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \ | |
158 | : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \ | |
159 | : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \ | |
160 | : (0xC0 | (((c) >> 6) & 0x01))) | |
161 | ||
162 | ||
885317d8 KH |
163 | /* Store multibyte form of the character C in P. The caller should |
164 | allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance. | |
165 | Returns the length of the multibyte form. */ | |
0168c3d8 KH |
166 | |
167 | #define CHAR_STRING(c, p) \ | |
168 | ((unsigned) (c) <= MAX_1_BYTE_CHAR \ | |
169 | ? ((p)[0] = (c), \ | |
170 | 1) \ | |
171 | : (unsigned) (c) <= MAX_2_BYTE_CHAR \ | |
172 | ? ((p)[0] = (0xC0 | ((c) >> 6)), \ | |
173 | (p)[1] = (0x80 | ((c) & 0x3F)), \ | |
174 | 2) \ | |
175 | : (unsigned) (c) <= MAX_3_BYTE_CHAR \ | |
176 | ? ((p)[0] = (0xE0 | ((c) >> 12)), \ | |
177 | (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \ | |
178 | (p)[2] = (0x80 | ((c) & 0x3F)), \ | |
179 | 3) \ | |
eb41da4c | 180 | : char_string (c, p)) |
0168c3d8 | 181 | |
eb41da4c KH |
182 | /* Store multibyte form of byte B in P. The caller should allocate at |
183 | least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the | |
184 | length of the multibyte form. */ | |
1106ea2b KH |
185 | |
186 | #define BYTE8_STRING(b, p) \ | |
187 | ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \ | |
7f464917 | 188 | (p)[1] = (0x80 | ((b) & 0x3F)), \ |
1106ea2b KH |
189 | 2) |
190 | ||
0168c3d8 | 191 | |
885317d8 KH |
192 | /* Store multibyte form of the character C in P. The caller should |
193 | allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance. | |
194 | And, advance P to the end of the multibyte form. */ | |
0168c3d8 | 195 | |
eb41da4c KH |
196 | #define CHAR_STRING_ADVANCE(c, p) \ |
197 | do { \ | |
198 | if ((c) <= MAX_1_BYTE_CHAR) \ | |
199 | *(p)++ = (c); \ | |
200 | else if ((c) <= MAX_2_BYTE_CHAR) \ | |
201 | *(p)++ = (0xC0 | ((c) >> 6)), \ | |
202 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
203 | else if ((c) <= MAX_3_BYTE_CHAR) \ | |
204 | *(p)++ = (0xE0 | ((c) >> 12)), \ | |
205 | *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \ | |
206 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
207 | else \ | |
208 | (p) += char_string ((c), (p)); \ | |
885317d8 | 209 | } while (0) |
0168c3d8 | 210 | |
eb41da4c | 211 | |
0168c3d8 KH |
212 | /* Nonzero iff BYTE starts a non-ASCII character in a multibyte |
213 | form. */ | |
214 | #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0) | |
215 | ||
b5c7dbe6 KH |
216 | /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a |
217 | multibyte form. */ | |
218 | #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80) | |
219 | ||
885317d8 KH |
220 | /* Nonzero iff BYTE starts a character in a multibyte form. |
221 | This is equivalent to: | |
222 | (ASCII_BYTE_P (byte) || LEADING_CODE_P (byte)) */ | |
223 | #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80) | |
224 | ||
0168c3d8 KH |
225 | /* Just kept for backward compatibility. This macro will be removed |
226 | in the future. */ | |
227 | #define BASE_LEADING_CODE_P LEADING_CODE_P | |
228 | ||
229 | /* How many bytes a character that starts with BYTE occupies in a | |
230 | multibyte form. */ | |
231 | #define BYTES_BY_CHAR_HEAD(byte) \ | |
232 | (!((byte) & 0x80) ? 1 \ | |
233 | : !((byte) & 0x20) ? 2 \ | |
234 | : !((byte) & 0x10) ? 3 \ | |
235 | : !((byte) & 0x08) ? 4 \ | |
236 | : 5) | |
237 | ||
238 | ||
239 | /* Return the length of the multi-byte form at string STR of length | |
240 | LEN while assuming that STR points a valid multi-byte form. As | |
241 | this macro isn't necessary anymore, all callers will be changed to | |
242 | use BYTES_BY_CHAR_HEAD directly in the future. */ | |
243 | ||
244 | #define MULTIBYTE_FORM_LENGTH(str, len) \ | |
245 | BYTES_BY_CHAR_HEAD (*(str)) | |
246 | ||
247 | /* Parse multibyte string STR of length LENGTH and set BYTES to the | |
248 | byte length of a character at STR while assuming that STR points a | |
249 | valid multibyte form. As this macro isn't necessary anymore, all | |
250 | callers will be changed to use BYTES_BY_CHAR_HEAD directly in the | |
251 | future. */ | |
252 | ||
253 | #define PARSE_MULTIBYTE_SEQ(str, length, bytes) \ | |
254 | (bytes) = BYTES_BY_CHAR_HEAD (*(str)) | |
255 | ||
256 | /* The byte length of multibyte form at unibyte string P ending at | |
257 | PEND. If STR doesn't point a valid multibyte form, return 0. */ | |
258 | ||
259 | #define MULTIBYTE_LENGTH(p, pend) \ | |
260 | (p >= pend ? 0 \ | |
261 | : !((p)[0] & 0x80) ? 1 \ | |
262 | : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \ | |
263 | : ((p)[0] & 0xE0) == 0xC0 ? 2 \ | |
264 | : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \ | |
265 | : ((p)[0] & 0xF0) == 0xE0 ? 3 \ | |
266 | : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \ | |
267 | : ((p)[0] & 0xF8) == 0xF0 ? 4 \ | |
268 | : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \ | |
269 | : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \ | |
270 | : 0) | |
271 | ||
272 | ||
273 | /* Like MULTIBYTE_LENGTH but don't check the ending address. */ | |
274 | ||
275 | #define MULTIBYTE_LENGTH_NO_CHECK(p) \ | |
276 | (!((p)[0] & 0x80) ? 1 \ | |
277 | : ((p)[1] & 0xC0) != 0x80 ? 0 \ | |
278 | : ((p)[0] & 0xE0) == 0xC0 ? 2 \ | |
279 | : ((p)[2] & 0xC0) != 0x80 ? 0 \ | |
280 | : ((p)[0] & 0xF0) == 0xE0 ? 3 \ | |
281 | : ((p)[3] & 0xC0) != 0x80 ? 0 \ | |
282 | : ((p)[0] & 0xF8) == 0xF0 ? 4 \ | |
283 | : ((p)[4] & 0xC0) != 0x80 ? 0 \ | |
284 | : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \ | |
285 | : 0) | |
286 | ||
8f924df7 KH |
287 | /* If P is before LIMIT, advance P to the next character boundary. It |
288 | assumes that P is already at a character boundary of the sane | |
289 | mulitbyte form whose end address is LIMIT. */ | |
290 | ||
291 | #define NEXT_CHAR_BOUNDARY(p, limit) \ | |
292 | do { \ | |
293 | if ((p) < (limit)) \ | |
294 | (p) += BYTES_BY_CHAR_HEAD (*(p)); \ | |
295 | } while (0) | |
296 | ||
297 | ||
298 | /* If P is after LIMIT, advance P to the previous character boundary. | |
299 | It assumes that P is already at a character boundary of the sane | |
300 | mulitbyte form whose beginning address is LIMIT. */ | |
301 | ||
302 | #define PREV_CHAR_BOUNDARY(p, limit) \ | |
303 | do { \ | |
304 | if ((p) > (limit)) \ | |
305 | { \ | |
306 | const unsigned char *p0 = (p); \ | |
307 | do { \ | |
308 | p0--; \ | |
309 | } while (p0 >= limit && ! CHAR_HEAD_P (*p0)); \ | |
310 | (p) = (BYTES_BY_CHAR_HEAD (*p0) == (p) - p0) ? p0 : (p) - 1; \ | |
311 | } \ | |
312 | } while (0) | |
0168c3d8 KH |
313 | |
314 | /* Return the character code of character whose multibyte form is at | |
315 | P. The argument LEN is ignored. It will be removed in the | |
316 | future. */ | |
317 | ||
318 | #define STRING_CHAR(p, len) \ | |
319 | (!((p)[0] & 0x80) \ | |
320 | ? (p)[0] \ | |
321 | : ! ((p)[0] & 0x20) \ | |
322 | ? (((((p)[0] & 0x1F) << 6) \ | |
323 | | ((p)[1] & 0x3F)) \ | |
324 | + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \ | |
325 | : ! ((p)[0] & 0x10) \ | |
326 | ? ((((p)[0] & 0x0F) << 12) \ | |
327 | | (((p)[1] & 0x3F) << 6) \ | |
328 | | ((p)[2] & 0x3F)) \ | |
eb41da4c | 329 | : string_char ((p), NULL, NULL)) |
0168c3d8 KH |
330 | |
331 | ||
332 | /* Like STRING_CHAR but set ACTUAL_LEN to the length of multibyte | |
333 | form. The argument LEN is ignored. It will be removed in the | |
334 | future. */ | |
335 | ||
336 | #define STRING_CHAR_AND_LENGTH(p, len, actual_len) \ | |
337 | (!((p)[0] & 0x80) \ | |
338 | ? ((actual_len) = 1, (p)[0]) \ | |
339 | : ! ((p)[0] & 0x20) \ | |
340 | ? ((actual_len) = 2, \ | |
341 | (((((p)[0] & 0x1F) << 6) \ | |
342 | | ((p)[1] & 0x3F)) \ | |
343 | + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \ | |
344 | : ! ((p)[0] & 0x10) \ | |
345 | ? ((actual_len) = 3, \ | |
346 | ((((p)[0] & 0x0F) << 12) \ | |
347 | | (((p)[1] & 0x3F) << 6) \ | |
348 | | ((p)[2] & 0x3F))) \ | |
eb41da4c | 349 | : string_char ((p), NULL, &actual_len)) |
0168c3d8 KH |
350 | |
351 | ||
b583cead | 352 | /* Like STRING_CHAR but advance P to the end of multibyte form. */ |
0168c3d8 KH |
353 | |
354 | #define STRING_CHAR_ADVANCE(p) \ | |
355 | (!((p)[0] & 0x80) \ | |
356 | ? *(p)++ \ | |
357 | : ! ((p)[0] & 0x20) \ | |
358 | ? ((p) += 2, \ | |
359 | ((((p)[-2] & 0x1F) << 6) \ | |
360 | | ((p)[-1] & 0x3F) \ | |
8f924df7 | 361 | | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \ |
0168c3d8 KH |
362 | : ! ((p)[0] & 0x10) \ |
363 | ? ((p) += 3, \ | |
364 | ((((p)[-3] & 0x0F) << 12) \ | |
365 | | (((p)[-2] & 0x3F) << 6) \ | |
366 | | ((p)[-1] & 0x3F))) \ | |
eb41da4c | 367 | : string_char ((p), &(p), NULL)) |
0168c3d8 KH |
368 | |
369 | ||
370 | /* Fetch the "next" character from Lisp string STRING at byte position | |
371 | BYTEIDX, character position CHARIDX. Store it into OUTPUT. | |
372 | ||
373 | All the args must be side-effect-free. | |
374 | BYTEIDX and CHARIDX must be lvalues; | |
375 | we increment them past the character fetched. */ | |
376 | ||
377 | #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \ | |
378 | if (1) \ | |
379 | { \ | |
380 | CHARIDX++; \ | |
381 | if (STRING_MULTIBYTE (STRING)) \ | |
382 | { \ | |
383 | unsigned char *ptr = &XSTRING (STRING)->data[BYTEIDX]; \ | |
384 | int len; \ | |
385 | \ | |
386 | OUTPUT = STRING_CHAR_AND_LENGTH (ptr, 0, len); \ | |
387 | BYTEIDX += len; \ | |
388 | } \ | |
389 | else \ | |
390 | OUTPUT = XSTRING (STRING)->data[BYTEIDX++]; \ | |
391 | } \ | |
392 | else | |
393 | ||
b583cead KH |
394 | /* Like FETCH_STRING_CHAR_ADVANCE but return a multibyte character eve |
395 | if STRING is unibyte. */ | |
43c47483 KH |
396 | |
397 | #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \ | |
398 | if (1) \ | |
399 | { \ | |
400 | CHARIDX++; \ | |
401 | if (STRING_MULTIBYTE (STRING)) \ | |
402 | { \ | |
403 | unsigned char *ptr = &XSTRING (STRING)->data[BYTEIDX]; \ | |
404 | int len; \ | |
405 | \ | |
406 | OUTPUT = STRING_CHAR_AND_LENGTH (ptr, 0, len); \ | |
407 | BYTEIDX += len; \ | |
408 | } \ | |
409 | else \ | |
410 | { \ | |
411 | OUTPUT = XSTRING (STRING)->data[BYTEIDX++]; \ | |
412 | MAKE_CHAR_MULTIBYTE (OUTPUT); \ | |
413 | } \ | |
414 | } \ | |
415 | else | |
416 | ||
0168c3d8 KH |
417 | |
418 | /* Like FETCH_STRING_CHAR_ADVANCE but assumes STRING is multibyte. */ | |
419 | ||
420 | #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \ | |
421 | if (1) \ | |
422 | { \ | |
423 | unsigned char *ptr = &XSTRING (STRING)->data[BYTEIDX]; \ | |
424 | int len; \ | |
425 | \ | |
426 | OUTPUT = STRING_CHAR_AND_LENGTH (ptr, 0, len); \ | |
427 | BYTEIDX += len; \ | |
428 | CHARIDX++; \ | |
429 | } \ | |
430 | else | |
431 | ||
432 | ||
433 | /* Like FETCH_STRING_CHAR_ADVANCE but fetch character from the current | |
434 | buffer. */ | |
435 | ||
436 | #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \ | |
437 | if (1) \ | |
438 | { \ | |
439 | CHARIDX++; \ | |
440 | if (!NILP (current_buffer->enable_multibyte_characters)) \ | |
441 | { \ | |
442 | unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \ | |
443 | int len; \ | |
444 | \ | |
445 | OUTPUT= STRING_CHAR_AND_LENGTH (ptr, 0, len); \ | |
446 | BYTEIDX += len; \ | |
447 | } \ | |
448 | else \ | |
449 | { \ | |
450 | OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \ | |
451 | BYTEIDX++; \ | |
452 | } \ | |
453 | } \ | |
454 | else | |
455 | ||
456 | ||
b583cead | 457 | /* Like FETCH_CHAR_ADVANCE but assumes the current buffer is multibyte. */ |
0168c3d8 KH |
458 | |
459 | #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \ | |
460 | if (1) \ | |
461 | { \ | |
462 | unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \ | |
463 | int len; \ | |
464 | \ | |
465 | OUTPUT= STRING_CHAR_AND_LENGTH (ptr, 0, len); \ | |
466 | BYTEIDX += len; \ | |
467 | CHARIDX++; \ | |
468 | } \ | |
469 | else | |
470 | ||
471 | ||
472 | /* Increase the buffer byte position POS_BYTE of the current buffer to | |
473 | the next character boundary. No range checking of POS. */ | |
474 | ||
475 | #define INC_POS(pos_byte) \ | |
476 | do { \ | |
477 | unsigned char *p = BYTE_POS_ADDR (pos_byte); \ | |
478 | pos_byte += BYTES_BY_CHAR_HEAD (*p); \ | |
479 | } while (0) | |
480 | ||
481 | ||
482 | /* Decrease the buffer byte position POS_BYTE of the current buffer to | |
483 | the previous character boundary. No range checking of POS. */ | |
484 | ||
485 | #define DEC_POS(pos_byte) \ | |
486 | do { \ | |
487 | unsigned char *p; \ | |
488 | \ | |
489 | pos_byte--; \ | |
490 | if (pos_byte < GPT_BYTE) \ | |
491 | p = BEG_ADDR + pos_byte - 1; \ | |
492 | else \ | |
493 | p = BEG_ADDR + GAP_SIZE + pos_byte - 1; \ | |
494 | while (!CHAR_HEAD_P (*p)) \ | |
495 | { \ | |
496 | p--; \ | |
497 | pos_byte--; \ | |
498 | } \ | |
499 | } while (0) | |
500 | ||
501 | /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */ | |
502 | ||
503 | #define INC_BOTH(charpos, bytepos) \ | |
504 | do \ | |
505 | { \ | |
506 | (charpos)++; \ | |
507 | if (NILP (current_buffer->enable_multibyte_characters)) \ | |
508 | (bytepos)++; \ | |
509 | else \ | |
510 | INC_POS ((bytepos)); \ | |
511 | } \ | |
512 | while (0) | |
513 | ||
514 | ||
515 | /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */ | |
516 | ||
517 | #define DEC_BOTH(charpos, bytepos) \ | |
518 | do \ | |
519 | { \ | |
520 | (charpos)--; \ | |
521 | if (NILP (current_buffer->enable_multibyte_characters)) \ | |
522 | (bytepos)--; \ | |
523 | else \ | |
524 | DEC_POS ((bytepos)); \ | |
525 | } \ | |
526 | while (0) | |
527 | ||
528 | ||
529 | /* Increase the buffer byte position POS_BYTE of the current buffer to | |
530 | the next character boundary. This macro relies on the fact that | |
531 | *GPT_ADDR and *Z_ADDR are always accessible and the values are | |
532 | '\0'. No range checking of POS_BYTE. */ | |
533 | ||
534 | #define BUF_INC_POS(buf, pos_byte) \ | |
535 | do { \ | |
536 | unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \ | |
537 | pos_byte += BYTES_BY_CHAR_HEAD (*p); \ | |
538 | } while (0) | |
539 | ||
540 | ||
541 | /* Decrease the buffer byte position POS_BYTE of the current buffer to | |
542 | the previous character boundary. No range checking of POS_BYTE. */ | |
543 | ||
544 | #define BUF_DEC_POS(buf, pos_byte) \ | |
545 | do { \ | |
546 | unsigned char *p; \ | |
547 | pos_byte--; \ | |
548 | if (pos_byte < BUF_GPT_BYTE (buf)) \ | |
549 | p = BUF_BEG_ADDR (buf) + pos_byte - 1; \ | |
550 | else \ | |
551 | p = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - 1; \ | |
552 | while (!CHAR_HEAD_P (*p)) \ | |
553 | { \ | |
554 | p--; \ | |
555 | pos_byte--; \ | |
556 | } \ | |
557 | } while (0) | |
558 | ||
559 | ||
b583cead KH |
560 | /* If C is a character to be unified with a Unicode character, return |
561 | the unified Unicode character. */ | |
562 | ||
fc9d9d2a | 563 | #define MAYBE_UNIFY_CHAR(c) \ |
eb41da4c KH |
564 | if (c > MAX_UNICODE_CHAR \ |
565 | && CHAR_TABLE_P (Vchar_unify_table)) \ | |
fc9d9d2a KH |
566 | { \ |
567 | Lisp_Object val; \ | |
568 | int unified; \ | |
569 | \ | |
570 | val = CHAR_TABLE_REF (Vchar_unify_table, c); \ | |
571 | if (! NILP (val)) \ | |
572 | { \ | |
573 | if (SYMBOLP (val)) \ | |
574 | { \ | |
b5c7dbe6 | 575 | Funify_charset (val, Qnil, Qnil); \ |
fc9d9d2a KH |
576 | val = CHAR_TABLE_REF (Vchar_unify_table, c); \ |
577 | } \ | |
578 | if ((unified = XINT (val)) >= 0) \ | |
579 | c = unified; \ | |
580 | } \ | |
581 | } \ | |
0168c3d8 KH |
582 | else |
583 | ||
fc9d9d2a | 584 | |
0168c3d8 KH |
585 | /* Return the width of ASCII character C. The width is measured by |
586 | how many columns occupied on the screen when displayed in the | |
587 | current buffer. */ | |
588 | ||
589 | #define ASCII_CHAR_WIDTH(c) \ | |
590 | (c < 0x20 \ | |
591 | ? (c == '\t' \ | |
592 | ? XFASTINT (current_buffer->tab_width) \ | |
593 | : (c == '\n' ? 0 : (NILP (current_buffer->ctl_arrow) ? 4 : 2))) \ | |
594 | : (c < 0x7f \ | |
595 | ? 1 \ | |
596 | : ((NILP (current_buffer->ctl_arrow) ? 4 : 2)))) | |
597 | ||
598 | /* Return the width of character C. The width is measured by how many | |
599 | columns occupied on the screen when displayed in the current | |
600 | buffer. */ | |
601 | ||
602 | #define CHAR_WIDTH(c) \ | |
603 | (ASCII_CHAR_P (c) \ | |
604 | ? ASCII_CHAR_WIDTH (c) \ | |
605 | : XINT (CHAR_TABLE_REF (Vchar_width_table, c))) | |
606 | ||
eb41da4c KH |
607 | extern int char_resolve_modifier_mask P_ ((int)); |
608 | extern int char_string P_ ((int, unsigned char *)); | |
609 | extern int string_char P_ ((const unsigned char *, | |
610 | const unsigned char **, int *)); | |
0168c3d8 KH |
611 | |
612 | extern int translate_char P_ ((Lisp_Object, int c)); | |
613 | extern int char_printable_p P_ ((int c)); | |
8f924df7 KH |
614 | extern void parse_str_as_multibyte P_ ((const unsigned char *, int, int *, |
615 | int *)); | |
0168c3d8 KH |
616 | extern int parse_str_to_multibyte P_ ((unsigned char *, int)); |
617 | extern int str_as_multibyte P_ ((unsigned char *, int, int, int *)); | |
618 | extern int str_to_multibyte P_ ((unsigned char *, int, int)); | |
619 | extern int str_as_unibyte P_ ((unsigned char *, int)); | |
620 | extern int strwidth P_ ((unsigned char *, int)); | |
8f924df7 | 621 | extern int c_string_width P_ ((const unsigned char *, int, int, int *, int *)); |
0168c3d8 KH |
622 | extern int lisp_string_width P_ ((Lisp_Object, int, int *, int *)); |
623 | ||
624 | extern Lisp_Object Vprintable_chars; | |
625 | ||
626 | extern Lisp_Object Qcharacterp, Qauto_fill_chars; | |
627 | extern Lisp_Object Vtranslation_table_vector; | |
628 | extern Lisp_Object Vchar_width_table; | |
629 | extern Lisp_Object Vchar_direction_table; | |
630 | extern Lisp_Object Vchar_unify_table; | |
631 | ||
fac2bdc4 DL |
632 | extern Lisp_Object string_escape_byte8 P_ ((Lisp_Object)); |
633 | ||
0168c3d8 KH |
634 | /* Return a translation table of id number ID. */ |
635 | #define GET_TRANSLATION_TABLE(id) \ | |
636 | (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)])) | |
637 | ||
638 | /* A char-table for characters which may invoke auto-filling. */ | |
639 | extern Lisp_Object Vauto_fill_chars; | |
640 | ||
e18ef64a | 641 | extern Lisp_Object Vchar_script_table; |
e0d6e5a5 | 642 | extern Lisp_Object Vscript_representative_chars; |
b5c7dbe6 | 643 | |
0168c3d8 KH |
644 | /* Copy LEN bytes from FROM to TO. This macro should be used only |
645 | when a caller knows that LEN is short and the obvious copy loop is | |
646 | faster than calling bcopy which has some overhead. Copying a | |
647 | multibyte sequence of a character is the typical case. */ | |
648 | ||
649 | #define BCOPY_SHORT(from, to, len) \ | |
650 | do { \ | |
651 | int i = len; \ | |
652 | unsigned char *from_p = from, *to_p = to; \ | |
653 | while (i--) *to_p++ = *from_p++; \ | |
654 | } while (0) | |
655 | ||
656 | #define DEFSYM(sym, name) \ | |
657 | do { (sym) = intern ((name)); staticpro (&(sym)); } while (0) | |
658 | ||
659 | #endif /* EMACS_CHARACTER_H */ | |
fbaf0946 MB |
660 | |
661 | /* arch-tag: 4ef86004-2eff-4073-8cea-cfcbcf7188ac | |
662 | (do not change this comment) */ |