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
8 This file is part of GNU Emacs.
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.
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.
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/>. */
23 #ifndef EMACS_CHARACTER_H
24 #define EMACS_CHARACTER_H
30 /* character code 1st byte byte sequence
31 -------------- -------- -------------
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)
40 invalid 1st byte 80..BF 10xxxxxx
41 F9..FF 11111xxx (xxx != 000)
44 /* Maximum character code ((1 << CHARACTERBITS) - 1). */
45 #define MAX_CHAR 0x3FFFFF
47 /* Maximum Unicode character code. */
48 #define MAX_UNICODE_CHAR 0x10FFFF
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
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
62 /* Nonzero iff C is a character that corresponds to a raw 8-bit
64 #define CHAR_BYTE8_P(c) ((c) > MAX_5_BYTE_CHAR)
66 /* Return the character code for raw 8-bit byte BYTE. */
67 #define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00)
69 #define UNIBYTE_TO_CHAR(byte) \
70 (ASCII_CHAR_P (byte) ? (byte) : BYTE8_TO_CHAR (byte))
72 /* Return the raw 8-bit byte for character C. */
73 #define CHAR_TO_BYTE8(c) (CHAR_BYTE8_P (c) ? (c) - 0x3FFF00 : (c & 0xFF))
75 /* Return the raw 8-bit byte for character C,
76 or -1 if C doesn't correspond to a byte. */
77 #define CHAR_TO_BYTE_SAFE(c) \
78 (ASCII_CHAR_P (c) ? c : (CHAR_BYTE8_P (c) ? (c) - 0x3FFF00 : -1))
80 /* Nonzero iff BYTE is the 1st byte of a multibyte form of a character
81 that corresponds to a raw 8-bit byte. */
82 #define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1)
84 /* If C is not ASCII, make it unibyte. */
85 #define MAKE_CHAR_UNIBYTE(c) \
87 if (! ASCII_CHAR_P (c)) \
88 c = CHAR_TO_BYTE8 (c); \
92 /* If C is not ASCII, make it multibyte. Assumes C < 256. */
93 #define MAKE_CHAR_MULTIBYTE(c) \
94 (eassert ((c) >= 0 && (c) < 256), (c) = UNIBYTE_TO_CHAR (c))
96 /* This is the maximum byte length of multibyte form. */
97 #define MAX_MULTIBYTE_LENGTH 5
99 /* Nonzero iff X is a character. */
100 #define CHARACTERP(x) (NATNUMP (x) && XFASTINT (x) <= MAX_CHAR)
102 /* Nonzero iff C is valid as a character code. */
103 #define CHAR_VALID_P(c) UNSIGNED_CMP (c, <=, MAX_CHAR)
105 /* Check if Lisp object X is a character or not. */
106 #define CHECK_CHARACTER(x) \
107 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
109 #define CHECK_CHARACTER_CAR(x) \
111 Lisp_Object tmp = XCAR (x); \
112 CHECK_CHARACTER (tmp); \
113 XSETCAR ((x), tmp); \
116 #define CHECK_CHARACTER_CDR(x) \
118 Lisp_Object tmp = XCDR (x); \
119 CHECK_CHARACTER (tmp); \
120 XSETCDR ((x), tmp); \
123 /* Nonzero iff C is a character of code less than 0x100. */
124 #define SINGLE_BYTE_CHAR_P(c) UNSIGNED_CMP (c, <, 0x100)
126 /* Nonzero if character C has a printable glyph. */
127 #define CHAR_PRINTABLE_P(c) \
128 (((c) >= 32 && (c) < 127) \
129 || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c))))
131 /* Return byte length of multibyte form for character C. */
132 #define CHAR_BYTES(c) \
133 ( (c) <= MAX_1_BYTE_CHAR ? 1 \
134 : (c) <= MAX_2_BYTE_CHAR ? 2 \
135 : (c) <= MAX_3_BYTE_CHAR ? 3 \
136 : (c) <= MAX_4_BYTE_CHAR ? 4 \
137 : (c) <= MAX_5_BYTE_CHAR ? 5 \
141 /* Return the leading code of multibyte form of C. */
142 #define CHAR_LEADING_CODE(c) \
143 ((c) <= MAX_1_BYTE_CHAR ? c \
144 : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \
145 : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \
146 : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \
147 : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \
148 : (0xC0 | (((c) >> 6) & 0x01)))
151 /* Store multibyte form of the character C in P. The caller should
152 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
153 Returns the length of the multibyte form. */
155 #define CHAR_STRING(c, p) \
156 (UNSIGNED_CMP (c, <=, MAX_1_BYTE_CHAR) \
159 : UNSIGNED_CMP (c, <=, MAX_2_BYTE_CHAR) \
160 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
161 (p)[1] = (0x80 | ((c) & 0x3F)), \
163 : UNSIGNED_CMP (c, <=, MAX_3_BYTE_CHAR) \
164 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
165 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
166 (p)[2] = (0x80 | ((c) & 0x3F)), \
168 : verify_expr (sizeof (c) <= sizeof (unsigned), char_string (c, p)))
170 /* Store multibyte form of byte B in P. The caller should allocate at
171 least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the
172 length of the multibyte form. */
174 #define BYTE8_STRING(b, p) \
175 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
176 (p)[1] = (0x80 | ((b) & 0x3F)), \
180 /* Store multibyte form of the character C in P and advance P to the
181 end of the multibyte form. The caller should allocate at least
182 MAX_MULTIBYTE_LENGTH bytes area at P in advance. */
184 #define CHAR_STRING_ADVANCE(c, p) \
186 if ((c) <= MAX_1_BYTE_CHAR) \
188 else if ((c) <= MAX_2_BYTE_CHAR) \
189 *(p)++ = (0xC0 | ((c) >> 6)), \
190 *(p)++ = (0x80 | ((c) & 0x3F)); \
191 else if ((c) <= MAX_3_BYTE_CHAR) \
192 *(p)++ = (0xE0 | ((c) >> 12)), \
193 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
194 *(p)++ = (0x80 | ((c) & 0x3F)); \
197 verify (sizeof (c) <= sizeof (unsigned)); \
198 (p) += char_string (c, p); \
203 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
205 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
207 /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
209 #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
211 /* Nonzero iff BYTE starts a character in a multibyte form.
212 This is equivalent to:
213 (ASCII_CHAR_P (byte) || LEADING_CODE_P (byte)) */
214 #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
216 /* How many bytes a character that starts with BYTE occupies in a
218 #define BYTES_BY_CHAR_HEAD(byte) \
219 (!((byte) & 0x80) ? 1 \
220 : !((byte) & 0x20) ? 2 \
221 : !((byte) & 0x10) ? 3 \
222 : !((byte) & 0x08) ? 4 \
226 /* The byte length of multibyte form at unibyte string P ending at
227 PEND. If STR doesn't point to a valid multibyte form, return 0. */
229 #define MULTIBYTE_LENGTH(p, pend) \
231 : !((p)[0] & 0x80) ? 1 \
232 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
233 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
234 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
235 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
236 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
237 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
238 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
239 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
243 /* Like MULTIBYTE_LENGTH, but don't check the ending address. */
245 #define MULTIBYTE_LENGTH_NO_CHECK(p) \
246 (!((p)[0] & 0x80) ? 1 \
247 : ((p)[1] & 0xC0) != 0x80 ? 0 \
248 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
249 : ((p)[2] & 0xC0) != 0x80 ? 0 \
250 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
251 : ((p)[3] & 0xC0) != 0x80 ? 0 \
252 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
253 : ((p)[4] & 0xC0) != 0x80 ? 0 \
254 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
257 /* If P is before LIMIT, advance P to the next character boundary.
258 Assumes that P is already at a character boundary of the same
259 multibyte form whose end address is LIMIT. */
261 #define NEXT_CHAR_BOUNDARY(p, limit) \
264 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
268 /* If P is after LIMIT, advance P to the previous character boundary.
269 Assumes that P is already at a character boundary of the same
270 multibyte form whose beginning address is LIMIT. */
272 #define PREV_CHAR_BOUNDARY(p, limit) \
276 const unsigned char *chp = (p); \
279 } while (chp >= limit && ! CHAR_HEAD_P (*chp)); \
280 (p) = (BYTES_BY_CHAR_HEAD (*chp) == (p) - chp) ? chp : (p) - 1; \
284 /* Return the character code of character whose multibyte form is at
285 P. Note that this macro unifies CJK characters whose codepoints
286 are in the Private Use Areas (PUAs), so it might return a different
287 codepoint from the one actually stored at P. */
289 #define STRING_CHAR(p) \
292 : ! ((p)[0] & 0x20) \
293 ? (((((p)[0] & 0x1F) << 6) \
295 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \
296 : ! ((p)[0] & 0x10) \
297 ? ((((p)[0] & 0x0F) << 12) \
298 | (((p)[1] & 0x3F) << 6) \
300 : string_char ((p), NULL, NULL))
303 /* Like STRING_CHAR, but set ACTUAL_LEN to the length of multibyte
306 Note: This macro returns the actual length of the character's
307 multibyte sequence as it is stored in a buffer or string. The
308 character it returns might have a different codepoint that has a
309 different multibyte sequence of a different length, due to possible
310 unification of CJK characters inside string_char. Therefore do NOT
311 assume that the length returned by this macro is identical to the
312 length of the multibyte sequence of the character it returns. */
314 #define STRING_CHAR_AND_LENGTH(p, actual_len) \
316 ? ((actual_len) = 1, (p)[0]) \
317 : ! ((p)[0] & 0x20) \
318 ? ((actual_len) = 2, \
319 (((((p)[0] & 0x1F) << 6) \
321 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \
322 : ! ((p)[0] & 0x10) \
323 ? ((actual_len) = 3, \
324 ((((p)[0] & 0x0F) << 12) \
325 | (((p)[1] & 0x3F) << 6) \
326 | ((p)[2] & 0x3F))) \
327 : string_char ((p), NULL, &actual_len))
330 /* Like STRING_CHAR, but advance P to the end of multibyte form. */
332 #define STRING_CHAR_ADVANCE(p) \
335 : ! ((p)[0] & 0x20) \
337 ((((p)[-2] & 0x1F) << 6) \
339 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
340 : ! ((p)[0] & 0x10) \
342 ((((p)[-3] & 0x0F) << 12) \
343 | (((p)[-2] & 0x3F) << 6) \
344 | ((p)[-1] & 0x3F))) \
345 : string_char ((p), &(p), NULL))
348 /* Fetch the "next" character from Lisp string STRING at byte position
349 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
351 All the args must be side-effect-free.
352 BYTEIDX and CHARIDX must be lvalues;
353 we increment them past the character fetched. */
355 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
359 if (STRING_MULTIBYTE (STRING)) \
361 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
364 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
369 OUTPUT = SREF (STRING, BYTEIDX); \
375 /* Like FETCH_STRING_CHAR_ADVANCE, but return a multibyte character
376 even if STRING is unibyte. */
378 #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
382 if (STRING_MULTIBYTE (STRING)) \
384 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
387 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
392 OUTPUT = SREF (STRING, BYTEIDX); \
394 MAKE_CHAR_MULTIBYTE (OUTPUT); \
400 /* Like FETCH_STRING_CHAR_ADVANCE, but assumes STRING is multibyte. */
402 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
405 unsigned char *fetch_ptr = &SDATA (STRING)[BYTEIDX]; \
408 OUTPUT = STRING_CHAR_AND_LENGTH (fetch_ptr, fetch_len); \
409 BYTEIDX += fetch_len; \
415 /* Like FETCH_STRING_CHAR_ADVANCE, but fetch character from the current
418 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
422 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) \
424 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
427 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
432 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
439 /* Like FETCH_CHAR_ADVANCE, but assumes the current buffer is multibyte. */
441 #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
444 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
447 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
454 /* Increment the buffer byte position POS_BYTE of the current buffer to
455 the next character boundary. No range checking of POS. */
457 #define INC_POS(pos_byte) \
459 unsigned char *chp = BYTE_POS_ADDR (pos_byte); \
460 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
464 /* Decrement the buffer byte position POS_BYTE of the current buffer to
465 the previous character boundary. No range checking of POS. */
467 #define DEC_POS(pos_byte) \
469 unsigned char *chp; \
472 if (pos_byte < GPT_BYTE) \
473 chp = BEG_ADDR + pos_byte - BEG_BYTE; \
475 chp = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE; \
476 while (!CHAR_HEAD_P (*chp)) \
483 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
485 #define INC_BOTH(charpos, bytepos) \
489 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
492 INC_POS ((bytepos)); \
497 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
499 #define DEC_BOTH(charpos, bytepos) \
503 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
506 DEC_POS ((bytepos)); \
511 /* Increment the buffer byte position POS_BYTE of the current buffer to
512 the next character boundary. This macro relies on the fact that
513 *GPT_ADDR and *Z_ADDR are always accessible and the values are
514 '\0'. No range checking of POS_BYTE. */
516 #define BUF_INC_POS(buf, pos_byte) \
518 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte); \
519 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
523 /* Decrement the buffer byte position POS_BYTE of the current buffer to
524 the previous character boundary. No range checking of POS_BYTE. */
526 #define BUF_DEC_POS(buf, pos_byte) \
528 unsigned char *chp; \
530 if (pos_byte < BUF_GPT_BYTE (buf)) \
531 chp = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
533 chp = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
534 while (!CHAR_HEAD_P (*chp)) \
542 /* Return a non-outlandish value for the tab width. */
544 #define SANE_TAB_WIDTH(buf) \
545 sanitize_tab_width (XFASTINT (BVAR (buf, tab_width)))
547 sanitize_tab_width (EMACS_INT width
)
549 return 0 < width
&& width
<= 1000 ? width
: 8;
552 /* Return the width of ASCII character C. The width is measured by
553 how many columns C will occupy on the screen when displayed in the
556 #define ASCII_CHAR_WIDTH(c) \
559 ? SANE_TAB_WIDTH (current_buffer) \
560 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
563 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
565 /* Return a non-outlandish value for a character width. */
568 sanitize_char_width (EMACS_INT width
)
570 return 0 <= width
&& width
<= 1000 ? width
: 1000;
573 /* Return the width of character C. The width is measured by how many
574 columns C will occupy on the screen when displayed in the current
577 #define CHAR_WIDTH(c) \
579 ? ASCII_CHAR_WIDTH (c) \
580 : sanitize_char_width (XINT (CHAR_TABLE_REF (Vchar_width_table, c))))
582 /* If C is a variation selector, return the index of the
583 variation selector (1..256). Otherwise, return 0. */
585 #define CHAR_VARIATION_SELECTOR_P(c) \
587 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
588 : (c) < 0xE0100 ? 0 \
589 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
592 /* If C is a high surrogate, return 1. If C is a low surrogate,
593 return 2. Otherwise, return 0. */
595 #define CHAR_SURROGATE_PAIR_P(c) \
597 : (c) <= 0xDBFF ? 1 \
598 : (c) <= 0xDFFF ? 2 \
601 /* Data type for Unicode general category.
603 The order of members must be in sync with the 8th element of the
604 member of unidata-prop-alist (in admin/unidata/unidata-gen.el) for
605 Unicode character property `general-category'. */
608 UNICODE_CATEGORY_UNKNOWN
= 0,
639 } unicode_category_t
;
641 extern EMACS_INT
char_resolve_modifier_mask (EMACS_INT
) ATTRIBUTE_CONST
;
642 extern int char_string (unsigned, unsigned char *);
643 extern int string_char (const unsigned char *,
644 const unsigned char **, int *);
646 extern int translate_char (Lisp_Object
, int c
);
647 extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t);
648 extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t,
650 extern ptrdiff_t str_to_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t);
651 extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t);
652 extern ptrdiff_t str_to_unibyte (const unsigned char *, unsigned char *,
654 extern ptrdiff_t strwidth (const char *, ptrdiff_t);
655 extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int,
656 ptrdiff_t *, ptrdiff_t *);
657 extern ptrdiff_t lisp_string_width (Lisp_Object
, ptrdiff_t,
658 ptrdiff_t *, ptrdiff_t *);
660 extern Lisp_Object Vchar_unify_table
;
661 extern Lisp_Object
string_escape_byte8 (Lisp_Object
);
663 extern bool alphabeticp (int);
664 extern bool decimalnump (int);
666 /* Return a translation table of id number ID. */
667 #define GET_TRANSLATION_TABLE(id) \
668 (XCDR (XVECTOR (Vtranslation_table_vector)->contents[(id)]))
670 /* Look up the element in char table OBJ at index CH, and return it as
671 an integer. If the element is not a character, return CH itself. */
674 char_table_translate (Lisp_Object obj
, int ch
)
676 /* This internal function is expected to be called with valid arguments,
677 so there is a eassert instead of CHECK_xxx for the sake of speed. */
678 eassert (CHAR_VALID_P (ch
));
679 eassert (CHAR_TABLE_P (obj
));
680 obj
= CHAR_TABLE_REF (obj
, ch
);
681 return CHARACTERP (obj
) ? XINT (obj
) : ch
;
686 #endif /* EMACS_CHARACTER_H */