* ccl.c (CCL_CODE_RANGE): Allow negative numbers. (Bug#8751)
[bpt/emacs.git] / src / character.h
CommitLineData
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.
5df4f04c 4 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0168c3d8
KH
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
7
8This file is part of GNU Emacs.
9
b9b1cc14 10GNU Emacs is free software: you can redistribute it and/or modify
0168c3d8 11it under the terms of the GNU General Public License as published by
b9b1cc14
GM
12the Free Software Foundation, either version 3 of the License, or
13(at your option) any later version.
0168c3d8
KH
14
15GNU Emacs is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
b9b1cc14 21along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
0168c3d8
KH
22
23#ifndef EMACS_CHARACTER_H
24#define EMACS_CHARACTER_H
25
885317d8
KH
26/* character code 1st byte byte sequence
27 -------------- -------- -------------
28 0-7F 00..7F 0xxxxxxx
29 80-7FF C2..DF 110xxxxx 10xxxxxx
30 800-FFFF E0..EF 1110xxxx 10xxxxxx 10xxxxxx
31 10000-1FFFFF F0..F7 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
32 200000-3FFF7F F8 11111000 1000xxxx 10xxxxxx 10xxxxxx 10xxxxxx
c43e85a9
KH
33 3FFF80-3FFFFF C0..C1 1100000x 10xxxxxx (for eight-bit-char)
34 400000-... invalid
0168c3d8 35
c43e85a9
KH
36 invalid 1st byte 80..BF 10xxxxxx
37 F9..FF 11111xxx (xxx != 000)
0168c3d8
KH
38*/
39
885317d8 40/* Maximum character code ((1 << CHARACTERBITS) - 1). */
0168c3d8
KH
41#define MAX_CHAR 0x3FFFFF
42
885317d8 43/* Maximum Unicode character code. */
0168c3d8
KH
44#define MAX_UNICODE_CHAR 0x10FFFF
45
885317d8 46/* Maximum N-byte character codes. */
0168c3d8
KH
47#define MAX_1_BYTE_CHAR 0x7F
48#define MAX_2_BYTE_CHAR 0x7FF
49#define MAX_3_BYTE_CHAR 0xFFFF
50#define MAX_4_BYTE_CHAR 0x1FFFFF
51#define MAX_5_BYTE_CHAR 0x3FFF7F
52
3a0a38de
KH
53/* Minimum leading code of multibyte characters. */
54#define MIN_MULTIBYTE_LEADING_CODE 0xC0
55/* Maximum leading code of multibyte characters. */
56#define MAX_MULTIBYTE_LEADING_CODE 0xF8
57
8bc28f69
KH
58/* Nonzero iff C is a character that corresponds to a raw 8-bit
59 byte. */
60#define CHAR_BYTE8_P(c) ((c) > MAX_5_BYTE_CHAR)
61
885317d8 62/* Return the character code for raw 8-bit byte BYTE. */
0168c3d8 63#define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00)
885317d8 64
4c0354d7
SM
65#define UNIBYTE_TO_CHAR(byte) \
66 (ASCII_BYTE_P (byte) ? (byte) : BYTE8_TO_CHAR (byte))
67
885317d8 68/* Return the raw 8-bit byte for character C. */
8bc28f69
KH
69#define CHAR_TO_BYTE8(c) \
70 (CHAR_BYTE8_P (c) \
71 ? (c) - 0x3FFF00 \
461c2ab9 72 : multibyte_char_to_unibyte (c))
885317d8 73
2afc21f5
SM
74/* Return the raw 8-bit byte for character C,
75 or -1 if C doesn't correspond to a byte. */
76#define CHAR_TO_BYTE_SAFE(c) \
77 (CHAR_BYTE8_P (c) \
78 ? (c) - 0x3FFF00 \
455af463 79 : multibyte_char_to_unibyte_safe (c))
2afc21f5 80
885317d8
KH
81/* Nonzero iff BYTE is the 1st byte of a multibyte form of a character
82 that corresponds to a raw 8-bit byte. */
0168c3d8
KH
83#define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1)
84
43c47483
KH
85/* If C is not ASCII, make it unibyte. */
86#define MAKE_CHAR_UNIBYTE(c) \
87 do { \
88 if (! ASCII_CHAR_P (c)) \
89 c = CHAR_TO_BYTE8 (c); \
90 } while (0)
3e411074 91
3e411074 92
18a2979d 93/* If C is not ASCII, make it multibyte. Assumes C < 256. */
c0dc8f64 94#define MAKE_CHAR_MULTIBYTE(c) \
4c0354d7 95 (eassert ((c) >= 0 && (c) < 256), (c) = UNIBYTE_TO_CHAR (c))
3e411074 96
885317d8 97/* This is the maximum byte length of multibyte form. */
0168c3d8
KH
98#define MAX_MULTIBYTE_LENGTH 5
99
18a2979d 100/* Return a Lisp character whose character code is C. Assumes C is
b583cead 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
18a2979d 110/* Nonzero iff C is valid as a character code. GENERICP is not used. */
885317d8 111#define CHAR_VALID_P(c, genericp) ((unsigned) (c) <= MAX_CHAR)
0168c3d8
KH
112
113/* Check if Lisp object X is a character or not. */
63db3c1b
MB
114#define CHECK_CHARACTER(x) \
115 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
0168c3d8 116
8f924df7
KH
117#define CHECK_CHARACTER_CAR(x) \
118 do { \
119 Lisp_Object tmp = XCAR (x); \
120 CHECK_CHARACTER (tmp); \
121 XSETCAR ((x), tmp); \
122 } while (0)
123
124#define CHECK_CHARACTER_CDR(x) \
125 do { \
126 Lisp_Object tmp = XCDR (x); \
127 CHECK_CHARACTER (tmp); \
128 XSETCDR ((x), tmp); \
129 } while (0)
130
0168c3d8
KH
131/* Nonzero iff C is a character of code less than 0x100. */
132#define SINGLE_BYTE_CHAR_P(c) ((unsigned) (c) < 0x100)
133
134/* Nonzero if character C has a printable glyph. */
135#define CHAR_PRINTABLE_P(c) \
944c7a26
AS
136 (((c) >= 32 && (c) < 127) \
137 || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c))))
0168c3d8 138
885317d8 139/* Return byte length of multibyte form for character C. */
0168c3d8
KH
140#define CHAR_BYTES(c) \
141 ( (c) <= MAX_1_BYTE_CHAR ? 1 \
142 : (c) <= MAX_2_BYTE_CHAR ? 2 \
143 : (c) <= MAX_3_BYTE_CHAR ? 3 \
144 : (c) <= MAX_4_BYTE_CHAR ? 4 \
145 : (c) <= MAX_5_BYTE_CHAR ? 5 \
146 : 2)
147
43c47483
KH
148
149/* Return the leading code of multibyte form of C. */
150#define CHAR_LEADING_CODE(c) \
151 ((c) <= MAX_1_BYTE_CHAR ? c \
152 : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \
153 : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \
154 : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \
155 : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \
156 : (0xC0 | (((c) >> 6) & 0x01)))
157
158
885317d8
KH
159/* Store multibyte form of the character C in P. The caller should
160 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
161 Returns the length of the multibyte form. */
0168c3d8
KH
162
163#define CHAR_STRING(c, p) \
164 ((unsigned) (c) <= MAX_1_BYTE_CHAR \
165 ? ((p)[0] = (c), \
166 1) \
167 : (unsigned) (c) <= MAX_2_BYTE_CHAR \
168 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
169 (p)[1] = (0x80 | ((c) & 0x3F)), \
170 2) \
171 : (unsigned) (c) <= MAX_3_BYTE_CHAR \
172 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
173 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
174 (p)[2] = (0x80 | ((c) & 0x3F)), \
175 3) \
f958a2fa 176 : char_string ((unsigned) c, p))
0168c3d8 177
eb41da4c
KH
178/* Store multibyte form of byte B in P. The caller should allocate at
179 least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the
180 length of the multibyte form. */
1106ea2b
KH
181
182#define BYTE8_STRING(b, p) \
183 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
7f464917 184 (p)[1] = (0x80 | ((b) & 0x3F)), \
1106ea2b
KH
185 2)
186
0168c3d8 187
18a2979d
EZ
188/* Store multibyte form of the character C in P and advance P to the
189 end of the multibyte form. The caller should allocate at least
190 MAX_MULTIBYTE_LENGTH bytes area at P in advance. */
0168c3d8 191
eb41da4c
KH
192#define CHAR_STRING_ADVANCE(c, p) \
193 do { \
194 if ((c) <= MAX_1_BYTE_CHAR) \
195 *(p)++ = (c); \
196 else if ((c) <= MAX_2_BYTE_CHAR) \
197 *(p)++ = (0xC0 | ((c) >> 6)), \
198 *(p)++ = (0x80 | ((c) & 0x3F)); \
199 else if ((c) <= MAX_3_BYTE_CHAR) \
200 *(p)++ = (0xE0 | ((c) >> 12)), \
201 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
202 *(p)++ = (0x80 | ((c) & 0x3F)); \
203 else \
204 (p) += char_string ((c), (p)); \
885317d8 205 } while (0)
0168c3d8 206
eb41da4c 207
0168c3d8
KH
208/* Nonzero iff BYTE starts a non-ASCII character in a multibyte
209 form. */
210#define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
211
b5c7dbe6
KH
212/* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
213 multibyte form. */
214#define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
215
885317d8
KH
216/* Nonzero iff BYTE starts a character in a multibyte form.
217 This is equivalent to:
218 (ASCII_BYTE_P (byte) || LEADING_CODE_P (byte)) */
219#define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
220
0168c3d8
KH
221/* How many bytes a character that starts with BYTE occupies in a
222 multibyte form. */
223#define BYTES_BY_CHAR_HEAD(byte) \
224 (!((byte) & 0x80) ? 1 \
225 : !((byte) & 0x20) ? 2 \
226 : !((byte) & 0x10) ? 3 \
227 : !((byte) & 0x08) ? 4 \
228 : 5)
229
230
0168c3d8 231/* The byte length of multibyte form at unibyte string P ending at
18a2979d 232 PEND. If STR doesn't point to a valid multibyte form, return 0. */
0168c3d8
KH
233
234#define MULTIBYTE_LENGTH(p, pend) \
235 (p >= pend ? 0 \
236 : !((p)[0] & 0x80) ? 1 \
237 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
238 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
239 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
240 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
241 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
242 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
243 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
244 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
245 : 0)
246
247
18a2979d 248/* Like MULTIBYTE_LENGTH, but don't check the ending address. */
0168c3d8
KH
249
250#define MULTIBYTE_LENGTH_NO_CHECK(p) \
251 (!((p)[0] & 0x80) ? 1 \
252 : ((p)[1] & 0xC0) != 0x80 ? 0 \
253 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
254 : ((p)[2] & 0xC0) != 0x80 ? 0 \
255 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
256 : ((p)[3] & 0xC0) != 0x80 ? 0 \
257 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
258 : ((p)[4] & 0xC0) != 0x80 ? 0 \
259 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
260 : 0)
261
18a2979d
EZ
262/* If P is before LIMIT, advance P to the next character boundary.
263 Assumes that P is already at a character boundary of the same
78edd3b7 264 multibyte form whose end address is LIMIT. */
8f924df7
KH
265
266#define NEXT_CHAR_BOUNDARY(p, limit) \
267 do { \
268 if ((p) < (limit)) \
269 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
270 } while (0)
271
272
273/* If P is after LIMIT, advance P to the previous character boundary.
18a2979d 274 Assumes that P is already at a character boundary of the same
ce5b453a 275 multibyte form whose beginning address is LIMIT. */
8f924df7
KH
276
277#define PREV_CHAR_BOUNDARY(p, limit) \
278 do { \
279 if ((p) > (limit)) \
280 { \
760fbc2c 281 const unsigned char *chp = (p); \
8f924df7 282 do { \
760fbc2c
PE
283 chp--; \
284 } while (chp >= limit && ! CHAR_HEAD_P (*chp)); \
285 (p) = (BYTES_BY_CHAR_HEAD (*chp) == (p) - chp) ? chp : (p) - 1; \
8f924df7
KH
286 } \
287 } while (0)
0168c3d8
KH
288
289/* Return the character code of character whose multibyte form is at
62a6e103 290 P. */
0168c3d8 291
62a6e103 292#define STRING_CHAR(p) \
0168c3d8
KH
293 (!((p)[0] & 0x80) \
294 ? (p)[0] \
295 : ! ((p)[0] & 0x20) \
296 ? (((((p)[0] & 0x1F) << 6) \
297 | ((p)[1] & 0x3F)) \
298 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \
299 : ! ((p)[0] & 0x10) \
300 ? ((((p)[0] & 0x0F) << 12) \
301 | (((p)[1] & 0x3F) << 6) \
302 | ((p)[2] & 0x3F)) \
eb41da4c 303 : string_char ((p), NULL, NULL))
0168c3d8
KH
304
305
18a2979d 306/* Like STRING_CHAR, but set ACTUAL_LEN to the length of multibyte
62a6e103 307 form. */
0168c3d8 308
62a6e103 309#define STRING_CHAR_AND_LENGTH(p, actual_len) \
0168c3d8
KH
310 (!((p)[0] & 0x80) \
311 ? ((actual_len) = 1, (p)[0]) \
312 : ! ((p)[0] & 0x20) \
313 ? ((actual_len) = 2, \
314 (((((p)[0] & 0x1F) << 6) \
315 | ((p)[1] & 0x3F)) \
316 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \
317 : ! ((p)[0] & 0x10) \
318 ? ((actual_len) = 3, \
319 ((((p)[0] & 0x0F) << 12) \
320 | (((p)[1] & 0x3F) << 6) \
321 | ((p)[2] & 0x3F))) \
eb41da4c 322 : string_char ((p), NULL, &actual_len))
0168c3d8
KH
323
324
18a2979d 325/* Like STRING_CHAR, but advance P to the end of multibyte form. */
0168c3d8
KH
326
327#define STRING_CHAR_ADVANCE(p) \
328 (!((p)[0] & 0x80) \
329 ? *(p)++ \
330 : ! ((p)[0] & 0x20) \
331 ? ((p) += 2, \
332 ((((p)[-2] & 0x1F) << 6) \
333 | ((p)[-1] & 0x3F) \
8f924df7 334 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
0168c3d8
KH
335 : ! ((p)[0] & 0x10) \
336 ? ((p) += 3, \
337 ((((p)[-3] & 0x0F) << 12) \
338 | (((p)[-2] & 0x3F) << 6) \
339 | ((p)[-1] & 0x3F))) \
eb41da4c 340 : string_char ((p), &(p), NULL))
0168c3d8
KH
341
342
343/* Fetch the "next" character from Lisp string STRING at byte position
344 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
345
346 All the args must be side-effect-free.
347 BYTEIDX and CHARIDX must be lvalues;
348 we increment them past the character fetched. */
349
350#define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
a1c2ac9a 351 do \
0168c3d8
KH
352 { \
353 CHARIDX++; \
354 if (STRING_MULTIBYTE (STRING)) \
355 { \
760fbc2c
PE
356 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
357 int chlen; \
0168c3d8 358 \
760fbc2c
PE
359 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
360 BYTEIDX += chlen; \
0168c3d8
KH
361 } \
362 else \
f1c99628
SM
363 { \
364 OUTPUT = SREF (STRING, BYTEIDX); \
365 BYTEIDX++; \
366 } \
0168c3d8 367 } \
a1c2ac9a 368 while (0)
0168c3d8 369
18a2979d
EZ
370/* Like FETCH_STRING_CHAR_ADVANCE, but return a multibyte character
371 even if STRING is unibyte. */
43c47483
KH
372
373#define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
a1c2ac9a 374 do \
43c47483
KH
375 { \
376 CHARIDX++; \
377 if (STRING_MULTIBYTE (STRING)) \
378 { \
760fbc2c
PE
379 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
380 int chlen; \
43c47483 381 \
760fbc2c
PE
382 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
383 BYTEIDX += chlen; \
43c47483
KH
384 } \
385 else \
386 { \
f1c99628
SM
387 OUTPUT = SREF (STRING, BYTEIDX); \
388 BYTEIDX++; \
43c47483
KH
389 MAKE_CHAR_MULTIBYTE (OUTPUT); \
390 } \
391 } \
a1c2ac9a 392 while (0)
43c47483 393
0168c3d8 394
18a2979d 395/* Like FETCH_STRING_CHAR_ADVANCE, but assumes STRING is multibyte. */
0168c3d8
KH
396
397#define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
a1c2ac9a 398 do \
0168c3d8 399 { \
a6670b0b
PE
400 unsigned char *fetch_ptr = &SDATA (STRING)[BYTEIDX]; \
401 int fetch_len; \
0168c3d8 402 \
a6670b0b
PE
403 OUTPUT = STRING_CHAR_AND_LENGTH (fetch_ptr, fetch_len); \
404 BYTEIDX += fetch_len; \
0168c3d8
KH
405 CHARIDX++; \
406 } \
a1c2ac9a 407 while (0)
0168c3d8
KH
408
409
18a2979d 410/* Like FETCH_STRING_CHAR_ADVANCE, but fetch character from the current
0168c3d8
KH
411 buffer. */
412
413#define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
a1c2ac9a 414 do \
0168c3d8
KH
415 { \
416 CHARIDX++; \
4b4deea2 417 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) \
0168c3d8 418 { \
760fbc2c
PE
419 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
420 int chlen; \
0168c3d8 421 \
760fbc2c
PE
422 OUTPUT= STRING_CHAR_AND_LENGTH (chp, chlen); \
423 BYTEIDX += chlen; \
0168c3d8
KH
424 } \
425 else \
426 { \
427 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
428 BYTEIDX++; \
429 } \
430 } \
a1c2ac9a 431 while (0)
0168c3d8
KH
432
433
18a2979d 434/* Like FETCH_CHAR_ADVANCE, but assumes the current buffer is multibyte. */
0168c3d8
KH
435
436#define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
a1c2ac9a 437 do \
0168c3d8 438 { \
760fbc2c
PE
439 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
440 int chlen; \
0168c3d8 441 \
760fbc2c
PE
442 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
443 BYTEIDX += chlen; \
0168c3d8
KH
444 CHARIDX++; \
445 } \
a1c2ac9a 446 while (0)
0168c3d8
KH
447
448
18a2979d 449/* Increment the buffer byte position POS_BYTE of the current buffer to
0168c3d8
KH
450 the next character boundary. No range checking of POS. */
451
452#define INC_POS(pos_byte) \
453 do { \
760fbc2c
PE
454 unsigned char *chp = BYTE_POS_ADDR (pos_byte); \
455 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
0168c3d8
KH
456 } while (0)
457
458
18a2979d 459/* Decrement the buffer byte position POS_BYTE of the current buffer to
0168c3d8
KH
460 the previous character boundary. No range checking of POS. */
461
462#define DEC_POS(pos_byte) \
463 do { \
760fbc2c 464 unsigned char *chp; \
0168c3d8
KH
465 \
466 pos_byte--; \
467 if (pos_byte < GPT_BYTE) \
760fbc2c 468 chp = BEG_ADDR + pos_byte - BEG_BYTE; \
0168c3d8 469 else \
760fbc2c
PE
470 chp = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE; \
471 while (!CHAR_HEAD_P (*chp)) \
0168c3d8 472 { \
760fbc2c 473 chp--; \
0168c3d8
KH
474 pos_byte--; \
475 } \
476 } while (0)
477
478/* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
479
480#define INC_BOTH(charpos, bytepos) \
481 do \
482 { \
483 (charpos)++; \
4b4deea2 484 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
0168c3d8
KH
485 (bytepos)++; \
486 else \
487 INC_POS ((bytepos)); \
488 } \
489 while (0)
490
491
492/* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
493
494#define DEC_BOTH(charpos, bytepos) \
495 do \
496 { \
497 (charpos)--; \
4b4deea2 498 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
0168c3d8
KH
499 (bytepos)--; \
500 else \
501 DEC_POS ((bytepos)); \
502 } \
503 while (0)
504
505
18a2979d 506/* Increment the buffer byte position POS_BYTE of the current buffer to
0168c3d8
KH
507 the next character boundary. This macro relies on the fact that
508 *GPT_ADDR and *Z_ADDR are always accessible and the values are
509 '\0'. No range checking of POS_BYTE. */
510
511#define BUF_INC_POS(buf, pos_byte) \
512 do { \
760fbc2c
PE
513 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte); \
514 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
0168c3d8
KH
515 } while (0)
516
517
18a2979d 518/* Decrement the buffer byte position POS_BYTE of the current buffer to
0168c3d8
KH
519 the previous character boundary. No range checking of POS_BYTE. */
520
521#define BUF_DEC_POS(buf, pos_byte) \
522 do { \
760fbc2c 523 unsigned char *chp; \
0168c3d8
KH
524 pos_byte--; \
525 if (pos_byte < BUF_GPT_BYTE (buf)) \
760fbc2c 526 chp = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
0168c3d8 527 else \
760fbc2c
PE
528 chp = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
529 while (!CHAR_HEAD_P (*chp)) \
0168c3d8 530 { \
760fbc2c 531 chp--; \
0168c3d8
KH
532 pos_byte--; \
533 } \
534 } while (0)
535
536
b583cead
KH
537/* If C is a character to be unified with a Unicode character, return
538 the unified Unicode character. */
539
55870ffc
KH
540#define MAYBE_UNIFY_CHAR(c) \
541 do { \
542 if (c > MAX_UNICODE_CHAR && c <= MAX_5_BYTE_CHAR) \
543 { \
544 Lisp_Object val; \
545 val = CHAR_TABLE_REF (Vchar_unify_table, c); \
546 if (INTEGERP (val)) \
547 c = XINT (val); \
548 else if (! NILP (val)) \
549 c = maybe_unify_char (c, val); \
550 } \
551 } while (0)
0168c3d8 552
fc9d9d2a 553
0168c3d8 554/* Return the width of ASCII character C. The width is measured by
18a2979d 555 how many columns C will occupy on the screen when displayed in the
0168c3d8
KH
556 current buffer. */
557
558#define ASCII_CHAR_WIDTH(c) \
559 (c < 0x20 \
560 ? (c == '\t' \
4b4deea2
TT
561 ? XFASTINT (BVAR (current_buffer, tab_width)) \
562 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
0168c3d8
KH
563 : (c < 0x7f \
564 ? 1 \
4b4deea2 565 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
0168c3d8
KH
566
567/* Return the width of character C. The width is measured by how many
18a2979d 568 columns C will occupy on the screen when displayed in the current
0168c3d8
KH
569 buffer. */
570
571#define CHAR_WIDTH(c) \
572 (ASCII_CHAR_P (c) \
573 ? ASCII_CHAR_WIDTH (c) \
574 : XINT (CHAR_TABLE_REF (Vchar_width_table, c)))
575
c19cab20
KH
576/* If C is a variation selector, return the index numnber of the
577 variation selector (1..256). Otherwise, return 0. */
578
579#define CHAR_VARIATION_SELECTOR_P(c) \
580 ((c) < 0xFE00 ? 0 \
581 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
582 : (c) < 0xE0100 ? 0 \
583 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
584 : 0)
585
586/* If C is a high surrogate, return 1. If C is a low surrogate,
409f2919 587 return 0. Otherwise, return 0. */
c19cab20
KH
588
589#define CHAR_SURROGATE_PAIR_P(c) \
590 ((c) < 0xD800 ? 0 \
591 : (c) <= 0xDBFF ? 1 \
592 : (c) <= 0xDFFF ? 2 \
593 : 0)
594
595
383e0970
J
596extern int char_resolve_modifier_mask (int);
597extern int char_string (unsigned, unsigned char *);
598extern int string_char (const unsigned char *,
599 const unsigned char **, int *);
600
601extern int translate_char (Lisp_Object, int c);
602extern int char_printable_p (int c);
14162469
EZ
603extern void parse_str_as_multibyte (const unsigned char *,
604 EMACS_INT, EMACS_INT *, EMACS_INT *);
de883a70 605extern EMACS_INT count_size_as_multibyte (const unsigned char *, EMACS_INT);
14162469
EZ
606extern EMACS_INT str_as_multibyte (unsigned char *, EMACS_INT, EMACS_INT,
607 EMACS_INT *);
608extern EMACS_INT str_to_multibyte (unsigned char *, EMACS_INT, EMACS_INT);
609extern EMACS_INT str_as_unibyte (unsigned char *, EMACS_INT);
383e0970
J
610extern EMACS_INT str_to_unibyte (const unsigned char *, unsigned char *,
611 EMACS_INT, int);
7469ef5d 612extern EMACS_INT strwidth (const char *, EMACS_INT);
579c18d0
EZ
613extern EMACS_INT c_string_width (const unsigned char *, EMACS_INT, int,
614 EMACS_INT *, EMACS_INT *);
2b4560a8 615extern EMACS_INT lisp_string_width (Lisp_Object, EMACS_INT,
579c18d0 616 EMACS_INT *, EMACS_INT *);
0168c3d8 617
955cbe7b 618extern Lisp_Object Qcharacterp;
0168c3d8 619extern Lisp_Object Vchar_unify_table;
383e0970 620extern Lisp_Object string_escape_byte8 (Lisp_Object);
fac2bdc4 621
0168c3d8
KH
622/* Return a translation table of id number ID. */
623#define GET_TRANSLATION_TABLE(id) \
624 (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)]))
625
0168c3d8 626#define DEFSYM(sym, name) \
5e2327cf 627 do { (sym) = intern_c_string ((name)); staticpro (&(sym)); } while (0)
0168c3d8
KH
628
629#endif /* EMACS_CHARACTER_H */