]> code.delx.au - gnu-emacs/blob - src/character.h
Give names to Unicode code points in C code
[gnu-emacs] / src / character.h
1 /* Header for multibyte character handler.
2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 3 of the License, or
13 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
22
23 #ifndef EMACS_CHARACTER_H
24 #define EMACS_CHARACTER_H
25
26 #include <verify.h>
27
28 INLINE_HEADER_BEGIN
29
30 /* character code 1st byte byte sequence
31 -------------- -------- -------------
32 0-7F 00..7F 0xxxxxxx
33 80-7FF C2..DF 110xxxxx 10xxxxxx
34 800-FFFF E0..EF 1110xxxx 10xxxxxx 10xxxxxx
35 10000-1FFFFF F0..F7 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
36 200000-3FFF7F F8 11111000 1000xxxx 10xxxxxx 10xxxxxx 10xxxxxx
37 3FFF80-3FFFFF C0..C1 1100000x 10xxxxxx (for eight-bit-char)
38 400000-... invalid
39
40 invalid 1st byte 80..BF 10xxxxxx
41 F9..FF 11111xxx (xxx != 000)
42 */
43
44 /* Maximum character code ((1 << CHARACTERBITS) - 1). */
45 #define MAX_CHAR 0x3FFFFF
46
47 /* Maximum Unicode character code. */
48 #define MAX_UNICODE_CHAR 0x10FFFF
49
50 /* Maximum N-byte character codes. */
51 #define MAX_1_BYTE_CHAR 0x7F
52 #define MAX_2_BYTE_CHAR 0x7FF
53 #define MAX_3_BYTE_CHAR 0xFFFF
54 #define MAX_4_BYTE_CHAR 0x1FFFFF
55 #define MAX_5_BYTE_CHAR 0x3FFF7F
56
57 /* Minimum leading code of multibyte characters. */
58 #define MIN_MULTIBYTE_LEADING_CODE 0xC0
59 /* Maximum leading code of multibyte characters. */
60 #define MAX_MULTIBYTE_LEADING_CODE 0xF8
61
62 /* Unicode character values. */
63 enum
64 {
65 NO_BREAK_SPACE = 0x00A0,
66 SOFT_HYPHEN = 0x00AD,
67 ZERO_WIDTH_NON_JOINER = 0x200C,
68 ZERO_WIDTH_JOINER = 0x200D,
69 HYPHEN = 0x2010,
70 NON_BREAKING_HYPHEN = 0x2011,
71 LEFT_SINGLE_QUOTATION_MARK = 0x2018,
72 RIGHT_SINGLE_QUOTATION_MARK = 0x2019,
73 PARAGRAPH_SEPARATOR = 0x2029,
74 LEFT_POINTING_ANGLE_BRACKET = 0x2329,
75 RIGHT_POINTING_ANGLE_BRACKET = 0x232A,
76 LEFT_ANGLE_BRACKET = 0x3008,
77 RIGHT_ANGLE_BRACKET = 0x3009,
78 OBJECT_REPLACEMENT_CHARACTER = 0xFFFC,
79 };
80
81 /* Nonzero iff C is a character that corresponds to a raw 8-bit
82 byte. */
83 #define CHAR_BYTE8_P(c) ((c) > MAX_5_BYTE_CHAR)
84
85 /* Return the character code for raw 8-bit byte BYTE. */
86 #define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00)
87
88 #define UNIBYTE_TO_CHAR(byte) \
89 (ASCII_CHAR_P (byte) ? (byte) : BYTE8_TO_CHAR (byte))
90
91 /* Return the raw 8-bit byte for character C. */
92 #define CHAR_TO_BYTE8(c) (CHAR_BYTE8_P (c) ? (c) - 0x3FFF00 : (c & 0xFF))
93
94 /* Return the raw 8-bit byte for character C,
95 or -1 if C doesn't correspond to a byte. */
96 #define CHAR_TO_BYTE_SAFE(c) \
97 (ASCII_CHAR_P (c) ? c : (CHAR_BYTE8_P (c) ? (c) - 0x3FFF00 : -1))
98
99 /* Nonzero iff BYTE is the 1st byte of a multibyte form of a character
100 that corresponds to a raw 8-bit byte. */
101 #define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1)
102
103 /* If C is not ASCII, make it unibyte. */
104 #define MAKE_CHAR_UNIBYTE(c) \
105 do { \
106 if (! ASCII_CHAR_P (c)) \
107 c = CHAR_TO_BYTE8 (c); \
108 } while (false)
109
110
111 /* If C is not ASCII, make it multibyte. Assumes C < 256. */
112 #define MAKE_CHAR_MULTIBYTE(c) \
113 (eassert ((c) >= 0 && (c) < 256), (c) = UNIBYTE_TO_CHAR (c))
114
115 /* This is the maximum byte length of multibyte form. */
116 #define MAX_MULTIBYTE_LENGTH 5
117
118 /* Nonzero iff X is a character. */
119 #define CHARACTERP(x) (NATNUMP (x) && XFASTINT (x) <= MAX_CHAR)
120
121 /* Nonzero iff C is valid as a character code. */
122 #define CHAR_VALID_P(c) UNSIGNED_CMP (c, <=, MAX_CHAR)
123
124 /* Check if Lisp object X is a character or not. */
125 #define CHECK_CHARACTER(x) \
126 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
127
128 #define CHECK_CHARACTER_CAR(x) \
129 do { \
130 Lisp_Object tmp = XCAR (x); \
131 CHECK_CHARACTER (tmp); \
132 XSETCAR ((x), tmp); \
133 } while (false)
134
135 #define CHECK_CHARACTER_CDR(x) \
136 do { \
137 Lisp_Object tmp = XCDR (x); \
138 CHECK_CHARACTER (tmp); \
139 XSETCDR ((x), tmp); \
140 } while (false)
141
142 /* Nonzero iff C is a character of code less than 0x100. */
143 #define SINGLE_BYTE_CHAR_P(c) UNSIGNED_CMP (c, <, 0x100)
144
145 /* Nonzero if character C has a printable glyph. */
146 #define CHAR_PRINTABLE_P(c) \
147 (((c) >= 32 && (c) < 127) \
148 || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c))))
149
150 /* Return byte length of multibyte form for character C. */
151 #define CHAR_BYTES(c) \
152 ( (c) <= MAX_1_BYTE_CHAR ? 1 \
153 : (c) <= MAX_2_BYTE_CHAR ? 2 \
154 : (c) <= MAX_3_BYTE_CHAR ? 3 \
155 : (c) <= MAX_4_BYTE_CHAR ? 4 \
156 : (c) <= MAX_5_BYTE_CHAR ? 5 \
157 : 2)
158
159
160 /* Return the leading code of multibyte form of C. */
161 #define CHAR_LEADING_CODE(c) \
162 ((c) <= MAX_1_BYTE_CHAR ? c \
163 : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \
164 : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \
165 : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \
166 : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \
167 : (0xC0 | (((c) >> 6) & 0x01)))
168
169
170 /* Store multibyte form of the character C in P. The caller should
171 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
172 Returns the length of the multibyte form. */
173
174 #define CHAR_STRING(c, p) \
175 (UNSIGNED_CMP (c, <=, MAX_1_BYTE_CHAR) \
176 ? ((p)[0] = (c), \
177 1) \
178 : UNSIGNED_CMP (c, <=, MAX_2_BYTE_CHAR) \
179 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
180 (p)[1] = (0x80 | ((c) & 0x3F)), \
181 2) \
182 : UNSIGNED_CMP (c, <=, MAX_3_BYTE_CHAR) \
183 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
184 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
185 (p)[2] = (0x80 | ((c) & 0x3F)), \
186 3) \
187 : verify_expr (sizeof (c) <= sizeof (unsigned), char_string (c, p)))
188
189 /* Store multibyte form of byte B in P. The caller should allocate at
190 least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the
191 length of the multibyte form. */
192
193 #define BYTE8_STRING(b, p) \
194 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
195 (p)[1] = (0x80 | ((b) & 0x3F)), \
196 2)
197
198
199 /* Store multibyte form of the character C in P and advance P to the
200 end of the multibyte form. The caller should allocate at least
201 MAX_MULTIBYTE_LENGTH bytes area at P in advance. */
202
203 #define CHAR_STRING_ADVANCE(c, p) \
204 do { \
205 if ((c) <= MAX_1_BYTE_CHAR) \
206 *(p)++ = (c); \
207 else if ((c) <= MAX_2_BYTE_CHAR) \
208 *(p)++ = (0xC0 | ((c) >> 6)), \
209 *(p)++ = (0x80 | ((c) & 0x3F)); \
210 else if ((c) <= MAX_3_BYTE_CHAR) \
211 *(p)++ = (0xE0 | ((c) >> 12)), \
212 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
213 *(p)++ = (0x80 | ((c) & 0x3F)); \
214 else \
215 { \
216 verify (sizeof (c) <= sizeof (unsigned)); \
217 (p) += char_string (c, p); \
218 } \
219 } while (false)
220
221
222 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
223 form. */
224 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
225
226 /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
227 multibyte form. */
228 #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
229
230 /* Nonzero iff BYTE starts a character in a multibyte form.
231 This is equivalent to:
232 (ASCII_CHAR_P (byte) || LEADING_CODE_P (byte)) */
233 #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
234
235 /* How many bytes a character that starts with BYTE occupies in a
236 multibyte form. */
237 #define BYTES_BY_CHAR_HEAD(byte) \
238 (!((byte) & 0x80) ? 1 \
239 : !((byte) & 0x20) ? 2 \
240 : !((byte) & 0x10) ? 3 \
241 : !((byte) & 0x08) ? 4 \
242 : 5)
243
244
245 /* The byte length of multibyte form at unibyte string P ending at
246 PEND. If STR doesn't point to a valid multibyte form, return 0. */
247
248 #define MULTIBYTE_LENGTH(p, pend) \
249 (p >= pend ? 0 \
250 : !((p)[0] & 0x80) ? 1 \
251 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
252 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
253 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
254 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
255 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
256 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
257 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
258 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
259 : 0)
260
261
262 /* Like MULTIBYTE_LENGTH, but don't check the ending address. */
263
264 #define MULTIBYTE_LENGTH_NO_CHECK(p) \
265 (!((p)[0] & 0x80) ? 1 \
266 : ((p)[1] & 0xC0) != 0x80 ? 0 \
267 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
268 : ((p)[2] & 0xC0) != 0x80 ? 0 \
269 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
270 : ((p)[3] & 0xC0) != 0x80 ? 0 \
271 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
272 : ((p)[4] & 0xC0) != 0x80 ? 0 \
273 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
274 : 0)
275
276 /* If P is before LIMIT, advance P to the next character boundary.
277 Assumes that P is already at a character boundary of the same
278 multibyte form whose end address is LIMIT. */
279
280 #define NEXT_CHAR_BOUNDARY(p, limit) \
281 do { \
282 if ((p) < (limit)) \
283 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
284 } while (false)
285
286
287 /* If P is after LIMIT, advance P to the previous character boundary.
288 Assumes that P is already at a character boundary of the same
289 multibyte form whose beginning address is LIMIT. */
290
291 #define PREV_CHAR_BOUNDARY(p, limit) \
292 do { \
293 if ((p) > (limit)) \
294 { \
295 const unsigned char *chp = (p); \
296 do { \
297 chp--; \
298 } while (chp >= limit && ! CHAR_HEAD_P (*chp)); \
299 (p) = (BYTES_BY_CHAR_HEAD (*chp) == (p) - chp) ? chp : (p) - 1; \
300 } \
301 } while (false)
302
303 /* Return the character code of character whose multibyte form is at
304 P. Note that this macro unifies CJK characters whose codepoints
305 are in the Private Use Areas (PUAs), so it might return a different
306 codepoint from the one actually stored at P. */
307
308 #define STRING_CHAR(p) \
309 (!((p)[0] & 0x80) \
310 ? (p)[0] \
311 : ! ((p)[0] & 0x20) \
312 ? (((((p)[0] & 0x1F) << 6) \
313 | ((p)[1] & 0x3F)) \
314 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \
315 : ! ((p)[0] & 0x10) \
316 ? ((((p)[0] & 0x0F) << 12) \
317 | (((p)[1] & 0x3F) << 6) \
318 | ((p)[2] & 0x3F)) \
319 : string_char ((p), NULL, NULL))
320
321
322 /* Like STRING_CHAR, but set ACTUAL_LEN to the length of multibyte
323 form.
324
325 Note: This macro returns the actual length of the character's
326 multibyte sequence as it is stored in a buffer or string. The
327 character it returns might have a different codepoint that has a
328 different multibyte sequence of a different length, due to possible
329 unification of CJK characters inside string_char. Therefore do NOT
330 assume that the length returned by this macro is identical to the
331 length of the multibyte sequence of the character it returns. */
332
333 #define STRING_CHAR_AND_LENGTH(p, actual_len) \
334 (!((p)[0] & 0x80) \
335 ? ((actual_len) = 1, (p)[0]) \
336 : ! ((p)[0] & 0x20) \
337 ? ((actual_len) = 2, \
338 (((((p)[0] & 0x1F) << 6) \
339 | ((p)[1] & 0x3F)) \
340 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \
341 : ! ((p)[0] & 0x10) \
342 ? ((actual_len) = 3, \
343 ((((p)[0] & 0x0F) << 12) \
344 | (((p)[1] & 0x3F) << 6) \
345 | ((p)[2] & 0x3F))) \
346 : string_char ((p), NULL, &actual_len))
347
348
349 /* Like STRING_CHAR, but advance P to the end of multibyte form. */
350
351 #define STRING_CHAR_ADVANCE(p) \
352 (!((p)[0] & 0x80) \
353 ? *(p)++ \
354 : ! ((p)[0] & 0x20) \
355 ? ((p) += 2, \
356 ((((p)[-2] & 0x1F) << 6) \
357 | ((p)[-1] & 0x3F) \
358 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
359 : ! ((p)[0] & 0x10) \
360 ? ((p) += 3, \
361 ((((p)[-3] & 0x0F) << 12) \
362 | (((p)[-2] & 0x3F) << 6) \
363 | ((p)[-1] & 0x3F))) \
364 : string_char ((p), &(p), NULL))
365
366
367 /* Fetch the "next" character from Lisp string STRING at byte position
368 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
369
370 All the args must be side-effect-free.
371 BYTEIDX and CHARIDX must be lvalues;
372 we increment them past the character fetched. */
373
374 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
375 do \
376 { \
377 CHARIDX++; \
378 if (STRING_MULTIBYTE (STRING)) \
379 { \
380 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
381 int chlen; \
382 \
383 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
384 BYTEIDX += chlen; \
385 } \
386 else \
387 { \
388 OUTPUT = SREF (STRING, BYTEIDX); \
389 BYTEIDX++; \
390 } \
391 } \
392 while (false)
393
394 /* Like FETCH_STRING_CHAR_ADVANCE, but return a multibyte character
395 even if STRING is unibyte. */
396
397 #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
398 do \
399 { \
400 CHARIDX++; \
401 if (STRING_MULTIBYTE (STRING)) \
402 { \
403 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
404 int chlen; \
405 \
406 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
407 BYTEIDX += chlen; \
408 } \
409 else \
410 { \
411 OUTPUT = SREF (STRING, BYTEIDX); \
412 BYTEIDX++; \
413 MAKE_CHAR_MULTIBYTE (OUTPUT); \
414 } \
415 } \
416 while (false)
417
418
419 /* Like FETCH_STRING_CHAR_ADVANCE, but assumes STRING is multibyte. */
420
421 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
422 do \
423 { \
424 unsigned char *fetch_ptr = &SDATA (STRING)[BYTEIDX]; \
425 int fetch_len; \
426 \
427 OUTPUT = STRING_CHAR_AND_LENGTH (fetch_ptr, fetch_len); \
428 BYTEIDX += fetch_len; \
429 CHARIDX++; \
430 } \
431 while (false)
432
433
434 /* Like FETCH_STRING_CHAR_ADVANCE, but fetch character from the current
435 buffer. */
436
437 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
438 do \
439 { \
440 CHARIDX++; \
441 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) \
442 { \
443 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
444 int chlen; \
445 \
446 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
447 BYTEIDX += chlen; \
448 } \
449 else \
450 { \
451 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
452 BYTEIDX++; \
453 } \
454 } \
455 while (false)
456
457
458 /* Like FETCH_CHAR_ADVANCE, but assumes the current buffer is multibyte. */
459
460 #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
461 do \
462 { \
463 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
464 int chlen; \
465 \
466 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
467 BYTEIDX += chlen; \
468 CHARIDX++; \
469 } \
470 while (false)
471
472
473 /* Increment the buffer byte position POS_BYTE of the current buffer to
474 the next character boundary. No range checking of POS. */
475
476 #define INC_POS(pos_byte) \
477 do { \
478 unsigned char *chp = BYTE_POS_ADDR (pos_byte); \
479 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
480 } while (false)
481
482
483 /* Decrement the buffer byte position POS_BYTE of the current buffer to
484 the previous character boundary. No range checking of POS. */
485
486 #define DEC_POS(pos_byte) \
487 do { \
488 unsigned char *chp; \
489 \
490 pos_byte--; \
491 if (pos_byte < GPT_BYTE) \
492 chp = BEG_ADDR + pos_byte - BEG_BYTE; \
493 else \
494 chp = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE; \
495 while (!CHAR_HEAD_P (*chp)) \
496 { \
497 chp--; \
498 pos_byte--; \
499 } \
500 } while (false)
501
502 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
503
504 #define INC_BOTH(charpos, bytepos) \
505 do \
506 { \
507 (charpos)++; \
508 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
509 (bytepos)++; \
510 else \
511 INC_POS ((bytepos)); \
512 } \
513 while (false)
514
515
516 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
517
518 #define DEC_BOTH(charpos, bytepos) \
519 do \
520 { \
521 (charpos)--; \
522 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
523 (bytepos)--; \
524 else \
525 DEC_POS ((bytepos)); \
526 } \
527 while (false)
528
529
530 /* Increment the buffer byte position POS_BYTE of the current buffer to
531 the next character boundary. This macro relies on the fact that
532 *GPT_ADDR and *Z_ADDR are always accessible and the values are
533 '\0'. No range checking of POS_BYTE. */
534
535 #define BUF_INC_POS(buf, pos_byte) \
536 do { \
537 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte); \
538 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
539 } while (false)
540
541
542 /* Decrement the buffer byte position POS_BYTE of the current buffer to
543 the previous character boundary. No range checking of POS_BYTE. */
544
545 #define BUF_DEC_POS(buf, pos_byte) \
546 do { \
547 unsigned char *chp; \
548 pos_byte--; \
549 if (pos_byte < BUF_GPT_BYTE (buf)) \
550 chp = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
551 else \
552 chp = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
553 while (!CHAR_HEAD_P (*chp)) \
554 { \
555 chp--; \
556 pos_byte--; \
557 } \
558 } while (false)
559
560
561 /* Return a non-outlandish value for the tab width. */
562
563 #define SANE_TAB_WIDTH(buf) \
564 sanitize_tab_width (XFASTINT (BVAR (buf, tab_width)))
565 INLINE int
566 sanitize_tab_width (EMACS_INT width)
567 {
568 return 0 < width && width <= 1000 ? width : 8;
569 }
570
571 /* Return the width of ASCII character C. The width is measured by
572 how many columns C will occupy on the screen when displayed in the
573 current buffer. */
574
575 #define ASCII_CHAR_WIDTH(c) \
576 (c < 0x20 \
577 ? (c == '\t' \
578 ? SANE_TAB_WIDTH (current_buffer) \
579 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
580 : (c < 0x7f \
581 ? 1 \
582 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
583
584 /* Return a non-outlandish value for a character width. */
585
586 INLINE int
587 sanitize_char_width (EMACS_INT width)
588 {
589 return 0 <= width && width <= 1000 ? width : 1000;
590 }
591
592 /* Return the width of character C. The width is measured by how many
593 columns C will occupy on the screen when displayed in the current
594 buffer. */
595
596 #define CHAR_WIDTH(c) \
597 (ASCII_CHAR_P (c) \
598 ? ASCII_CHAR_WIDTH (c) \
599 : sanitize_char_width (XINT (CHAR_TABLE_REF (Vchar_width_table, c))))
600
601 /* If C is a variation selector, return the index of the
602 variation selector (1..256). Otherwise, return 0. */
603
604 #define CHAR_VARIATION_SELECTOR_P(c) \
605 ((c) < 0xFE00 ? 0 \
606 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
607 : (c) < 0xE0100 ? 0 \
608 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
609 : 0)
610
611 /* If C is a high surrogate, return 1. If C is a low surrogate,
612 return 2. Otherwise, return 0. */
613
614 #define CHAR_SURROGATE_PAIR_P(c) \
615 ((c) < 0xD800 ? 0 \
616 : (c) <= 0xDBFF ? 1 \
617 : (c) <= 0xDFFF ? 2 \
618 : 0)
619
620 /* Data type for Unicode general category.
621
622 The order of members must be in sync with the 8th element of the
623 member of unidata-prop-alist (in admin/unidata/unidata-gen.el) for
624 Unicode character property `general-category'. */
625
626 typedef enum {
627 UNICODE_CATEGORY_UNKNOWN = 0,
628 UNICODE_CATEGORY_Lu,
629 UNICODE_CATEGORY_Ll,
630 UNICODE_CATEGORY_Lt,
631 UNICODE_CATEGORY_Lm,
632 UNICODE_CATEGORY_Lo,
633 UNICODE_CATEGORY_Mn,
634 UNICODE_CATEGORY_Mc,
635 UNICODE_CATEGORY_Me,
636 UNICODE_CATEGORY_Nd,
637 UNICODE_CATEGORY_Nl,
638 UNICODE_CATEGORY_No,
639 UNICODE_CATEGORY_Pc,
640 UNICODE_CATEGORY_Pd,
641 UNICODE_CATEGORY_Ps,
642 UNICODE_CATEGORY_Pe,
643 UNICODE_CATEGORY_Pi,
644 UNICODE_CATEGORY_Pf,
645 UNICODE_CATEGORY_Po,
646 UNICODE_CATEGORY_Sm,
647 UNICODE_CATEGORY_Sc,
648 UNICODE_CATEGORY_Sk,
649 UNICODE_CATEGORY_So,
650 UNICODE_CATEGORY_Zs,
651 UNICODE_CATEGORY_Zl,
652 UNICODE_CATEGORY_Zp,
653 UNICODE_CATEGORY_Cc,
654 UNICODE_CATEGORY_Cf,
655 UNICODE_CATEGORY_Cs,
656 UNICODE_CATEGORY_Co,
657 UNICODE_CATEGORY_Cn
658 } unicode_category_t;
659
660 extern EMACS_INT char_resolve_modifier_mask (EMACS_INT) ATTRIBUTE_CONST;
661 extern int char_string (unsigned, unsigned char *);
662 extern int string_char (const unsigned char *,
663 const unsigned char **, int *);
664
665 extern int translate_char (Lisp_Object, int c);
666 extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t);
667 extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t,
668 ptrdiff_t *);
669 extern ptrdiff_t str_to_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t);
670 extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t);
671 extern ptrdiff_t str_to_unibyte (const unsigned char *, unsigned char *,
672 ptrdiff_t);
673 extern ptrdiff_t strwidth (const char *, ptrdiff_t);
674 extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int,
675 ptrdiff_t *, ptrdiff_t *);
676 extern ptrdiff_t lisp_string_width (Lisp_Object, ptrdiff_t,
677 ptrdiff_t *, ptrdiff_t *);
678
679 extern Lisp_Object Vchar_unify_table;
680 extern Lisp_Object string_escape_byte8 (Lisp_Object);
681
682 extern bool alphabeticp (int);
683 extern bool decimalnump (int);
684 extern bool graphicp (int);
685 extern bool printablep (int);
686
687 /* Return a translation table of id number ID. */
688 #define GET_TRANSLATION_TABLE(id) \
689 (XCDR (XVECTOR (Vtranslation_table_vector)->contents[(id)]))
690
691 /* Look up the element in char table OBJ at index CH, and return it as
692 an integer. If the element is not a character, return CH itself. */
693
694 INLINE int
695 char_table_translate (Lisp_Object obj, int ch)
696 {
697 /* This internal function is expected to be called with valid arguments,
698 so there is a eassert instead of CHECK_xxx for the sake of speed. */
699 eassert (CHAR_VALID_P (ch));
700 eassert (CHAR_TABLE_P (obj));
701 obj = CHAR_TABLE_REF (obj, ch);
702 return CHARACTERP (obj) ? XINT (obj) : ch;
703 }
704
705 INLINE_HEADER_END
706
707 #endif /* EMACS_CHARACTER_H */