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_BYTE_P (byte) ? (byte) : BYTE8_TO_CHAR (byte))
72 /* Return the raw 8-bit byte for character C. */
73 #define CHAR_TO_BYTE8(c) \
76 : multibyte_char_to_unibyte (c))
78 /* Return the raw 8-bit byte for character C,
79 or -1 if C doesn't correspond to a byte. */
80 #define CHAR_TO_BYTE_SAFE(c) \
83 : multibyte_char_to_unibyte_safe (c))
85 /* Nonzero iff BYTE is the 1st byte of a multibyte form of a character
86 that corresponds to a raw 8-bit byte. */
87 #define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1)
89 /* If C is not ASCII, make it unibyte. */
90 #define MAKE_CHAR_UNIBYTE(c) \
92 if (! ASCII_CHAR_P (c)) \
93 c = CHAR_TO_BYTE8 (c); \
97 /* If C is not ASCII, make it multibyte. Assumes C < 256. */
98 #define MAKE_CHAR_MULTIBYTE(c) \
99 (eassert ((c) >= 0 && (c) < 256), (c) = UNIBYTE_TO_CHAR (c))
101 /* This is the maximum byte length of multibyte form. */
102 #define MAX_MULTIBYTE_LENGTH 5
104 /* Return a Lisp character whose character code is C. Assumes C is
105 a valid character code. */
106 #define make_char(c) make_number (c)
108 /* Nonzero iff C is an ASCII byte. */
109 #define ASCII_BYTE_P(c) UNSIGNED_CMP (c, <, 0x80)
111 /* Nonzero iff X is a character. */
112 #define CHARACTERP(x) (NATNUMP (x) && XFASTINT (x) <= MAX_CHAR)
114 /* Nonzero iff C is valid as a character code. */
115 #define CHAR_VALID_P(c) UNSIGNED_CMP (c, <=, MAX_CHAR)
117 /* Check if Lisp object X is a character or not. */
118 #define CHECK_CHARACTER(x) \
119 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
121 #define CHECK_CHARACTER_CAR(x) \
123 Lisp_Object tmp = XCAR (x); \
124 CHECK_CHARACTER (tmp); \
125 XSETCAR ((x), tmp); \
128 #define CHECK_CHARACTER_CDR(x) \
130 Lisp_Object tmp = XCDR (x); \
131 CHECK_CHARACTER (tmp); \
132 XSETCDR ((x), tmp); \
135 /* Nonzero iff C is a character of code less than 0x100. */
136 #define SINGLE_BYTE_CHAR_P(c) UNSIGNED_CMP (c, <, 0x100)
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))))
143 /* Return byte length of multibyte form for character C. */
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 \
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)))
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. */
167 #define CHAR_STRING(c, p) \
168 (UNSIGNED_CMP (c, <=, MAX_1_BYTE_CHAR) \
171 : UNSIGNED_CMP (c, <=, MAX_2_BYTE_CHAR) \
172 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
173 (p)[1] = (0x80 | ((c) & 0x3F)), \
175 : UNSIGNED_CMP (c, <=, MAX_3_BYTE_CHAR) \
176 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
177 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
178 (p)[2] = (0x80 | ((c) & 0x3F)), \
180 : verify_expr (sizeof (c) <= sizeof (unsigned), char_string (c, p)))
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. */
186 #define BYTE8_STRING(b, p) \
187 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
188 (p)[1] = (0x80 | ((b) & 0x3F)), \
192 /* Store multibyte form of the character C in P and advance P to the
193 end of the multibyte form. The caller should allocate at least
194 MAX_MULTIBYTE_LENGTH bytes area at P in advance. */
196 #define CHAR_STRING_ADVANCE(c, p) \
198 if ((c) <= MAX_1_BYTE_CHAR) \
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)); \
209 verify (sizeof (c) <= sizeof (unsigned)); \
210 (p) += char_string (c, p); \
215 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
217 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
219 /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
221 #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
223 /* Nonzero iff BYTE starts a character in a multibyte form.
224 This is equivalent to:
225 (ASCII_BYTE_P (byte) || LEADING_CODE_P (byte)) */
226 #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
228 /* How many bytes a character that starts with BYTE occupies in a
230 #define BYTES_BY_CHAR_HEAD(byte) \
231 (!((byte) & 0x80) ? 1 \
232 : !((byte) & 0x20) ? 2 \
233 : !((byte) & 0x10) ? 3 \
234 : !((byte) & 0x08) ? 4 \
238 /* The byte length of multibyte form at unibyte string P ending at
239 PEND. If STR doesn't point to a valid multibyte form, return 0. */
241 #define MULTIBYTE_LENGTH(p, pend) \
243 : !((p)[0] & 0x80) ? 1 \
244 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
245 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
246 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
247 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
248 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
249 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
250 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
251 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
255 /* Like MULTIBYTE_LENGTH, but don't check the ending address. */
257 #define MULTIBYTE_LENGTH_NO_CHECK(p) \
258 (!((p)[0] & 0x80) ? 1 \
259 : ((p)[1] & 0xC0) != 0x80 ? 0 \
260 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
261 : ((p)[2] & 0xC0) != 0x80 ? 0 \
262 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
263 : ((p)[3] & 0xC0) != 0x80 ? 0 \
264 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
265 : ((p)[4] & 0xC0) != 0x80 ? 0 \
266 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
269 /* If P is before LIMIT, advance P to the next character boundary.
270 Assumes that P is already at a character boundary of the same
271 multibyte form whose end address is LIMIT. */
273 #define NEXT_CHAR_BOUNDARY(p, limit) \
276 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
280 /* If P is after LIMIT, advance P to the previous character boundary.
281 Assumes that P is already at a character boundary of the same
282 multibyte form whose beginning address is LIMIT. */
284 #define PREV_CHAR_BOUNDARY(p, limit) \
288 const unsigned char *chp = (p); \
291 } while (chp >= limit && ! CHAR_HEAD_P (*chp)); \
292 (p) = (BYTES_BY_CHAR_HEAD (*chp) == (p) - chp) ? chp : (p) - 1; \
296 /* Return the character code of character whose multibyte form is at
297 P. Note that this macro unifies CJK characters whose codepoints
298 are in the Private Use Areas (PUAs), so it might return a different
299 codepoint from the one actually stored at P. */
301 #define STRING_CHAR(p) \
304 : ! ((p)[0] & 0x20) \
305 ? (((((p)[0] & 0x1F) << 6) \
307 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \
308 : ! ((p)[0] & 0x10) \
309 ? ((((p)[0] & 0x0F) << 12) \
310 | (((p)[1] & 0x3F) << 6) \
312 : string_char ((p), NULL, NULL))
315 /* Like STRING_CHAR, but set ACTUAL_LEN to the length of multibyte
318 Note: This macro returns the actual length of the character's
319 multibyte sequence as it is stored in a buffer or string. The
320 character it returns might have a different codepoint that has a
321 different multibyte sequence of a different length, due to possible
322 unification of CJK characters inside string_char. Therefore do NOT
323 assume that the length returned by this macro is identical to the
324 length of the multibyte sequence of the character it returns. */
326 #define STRING_CHAR_AND_LENGTH(p, actual_len) \
328 ? ((actual_len) = 1, (p)[0]) \
329 : ! ((p)[0] & 0x20) \
330 ? ((actual_len) = 2, \
331 (((((p)[0] & 0x1F) << 6) \
333 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \
334 : ! ((p)[0] & 0x10) \
335 ? ((actual_len) = 3, \
336 ((((p)[0] & 0x0F) << 12) \
337 | (((p)[1] & 0x3F) << 6) \
338 | ((p)[2] & 0x3F))) \
339 : string_char ((p), NULL, &actual_len))
342 /* Like STRING_CHAR, but advance P to the end of multibyte form. */
344 #define STRING_CHAR_ADVANCE(p) \
347 : ! ((p)[0] & 0x20) \
349 ((((p)[-2] & 0x1F) << 6) \
351 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
352 : ! ((p)[0] & 0x10) \
354 ((((p)[-3] & 0x0F) << 12) \
355 | (((p)[-2] & 0x3F) << 6) \
356 | ((p)[-1] & 0x3F))) \
357 : string_char ((p), &(p), NULL))
360 /* Fetch the "next" character from Lisp string STRING at byte position
361 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
363 All the args must be side-effect-free.
364 BYTEIDX and CHARIDX must be lvalues;
365 we increment them past the character fetched. */
367 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
371 if (STRING_MULTIBYTE (STRING)) \
373 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
376 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
381 OUTPUT = SREF (STRING, BYTEIDX); \
387 /* Like FETCH_STRING_CHAR_ADVANCE, but return a multibyte character
388 even if STRING is unibyte. */
390 #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
394 if (STRING_MULTIBYTE (STRING)) \
396 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
399 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
404 OUTPUT = SREF (STRING, BYTEIDX); \
406 MAKE_CHAR_MULTIBYTE (OUTPUT); \
412 /* Like FETCH_STRING_CHAR_ADVANCE, but assumes STRING is multibyte. */
414 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
417 unsigned char *fetch_ptr = &SDATA (STRING)[BYTEIDX]; \
420 OUTPUT = STRING_CHAR_AND_LENGTH (fetch_ptr, fetch_len); \
421 BYTEIDX += fetch_len; \
427 /* Like FETCH_STRING_CHAR_ADVANCE, but fetch character from the current
430 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
434 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) \
436 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
439 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
444 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
451 /* Like FETCH_CHAR_ADVANCE, but assumes the current buffer is multibyte. */
453 #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
456 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
459 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
466 /* Increment the buffer byte position POS_BYTE of the current buffer to
467 the next character boundary. No range checking of POS. */
469 #define INC_POS(pos_byte) \
471 unsigned char *chp = BYTE_POS_ADDR (pos_byte); \
472 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
476 /* Decrement the buffer byte position POS_BYTE of the current buffer to
477 the previous character boundary. No range checking of POS. */
479 #define DEC_POS(pos_byte) \
481 unsigned char *chp; \
484 if (pos_byte < GPT_BYTE) \
485 chp = BEG_ADDR + pos_byte - BEG_BYTE; \
487 chp = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE; \
488 while (!CHAR_HEAD_P (*chp)) \
495 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
497 #define INC_BOTH(charpos, bytepos) \
501 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
504 INC_POS ((bytepos)); \
509 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
511 #define DEC_BOTH(charpos, bytepos) \
515 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
518 DEC_POS ((bytepos)); \
523 /* Increment the buffer byte position POS_BYTE of the current buffer to
524 the next character boundary. This macro relies on the fact that
525 *GPT_ADDR and *Z_ADDR are always accessible and the values are
526 '\0'. No range checking of POS_BYTE. */
528 #define BUF_INC_POS(buf, pos_byte) \
530 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte); \
531 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
535 /* Decrement the buffer byte position POS_BYTE of the current buffer to
536 the previous character boundary. No range checking of POS_BYTE. */
538 #define BUF_DEC_POS(buf, pos_byte) \
540 unsigned char *chp; \
542 if (pos_byte < BUF_GPT_BYTE (buf)) \
543 chp = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
545 chp = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
546 while (!CHAR_HEAD_P (*chp)) \
554 /* Return a non-outlandish value for the tab width. */
556 #define SANE_TAB_WIDTH(buf) \
557 sanitize_tab_width (XFASTINT (BVAR (buf, tab_width)))
559 sanitize_tab_width (EMACS_INT width
)
561 return 0 < width
&& width
<= 1000 ? width
: 8;
564 /* Return the width of ASCII character C. The width is measured by
565 how many columns C will occupy on the screen when displayed in the
568 #define ASCII_CHAR_WIDTH(c) \
571 ? SANE_TAB_WIDTH (current_buffer) \
572 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
575 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
577 /* Return a non-outlandish value for a character width. */
580 sanitize_char_width (EMACS_INT width
)
582 return 0 <= width
&& width
<= 1000 ? width
: 1000;
585 /* Return the width of character C. The width is measured by how many
586 columns C will occupy on the screen when displayed in the current
589 #define CHAR_WIDTH(c) \
591 ? ASCII_CHAR_WIDTH (c) \
592 : sanitize_char_width (XINT (CHAR_TABLE_REF (Vchar_width_table, c))))
594 /* If C is a variation selector, return the index of the
595 variation selector (1..256). Otherwise, return 0. */
597 #define CHAR_VARIATION_SELECTOR_P(c) \
599 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
600 : (c) < 0xE0100 ? 0 \
601 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
604 /* If C is a high surrogate, return 1. If C is a low surrogate,
605 return 2. Otherwise, return 0. */
607 #define CHAR_SURROGATE_PAIR_P(c) \
609 : (c) <= 0xDBFF ? 1 \
610 : (c) <= 0xDFFF ? 2 \
613 /* Data type for Unicode general category.
615 The order of members must be in sync with the 8th element of the
616 member of unidata-prop-alist (in admin/unidata/unidata-gen.el) for
617 Unicode character property `general-category'. */
620 UNICODE_CATEGORY_UNKNOWN
= 0,
651 } unicode_category_t
;
653 extern EMACS_INT
char_resolve_modifier_mask (EMACS_INT
) ATTRIBUTE_CONST
;
654 extern int char_string (unsigned, unsigned char *);
655 extern int string_char (const unsigned char *,
656 const unsigned char **, int *);
658 extern int translate_char (Lisp_Object
, int c
);
659 extern void parse_str_as_multibyte (const unsigned char *,
660 ptrdiff_t, ptrdiff_t *, ptrdiff_t *);
661 extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t);
662 extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t,
664 extern ptrdiff_t str_to_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t);
665 extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t);
666 extern ptrdiff_t str_to_unibyte (const unsigned char *, unsigned char *,
668 extern ptrdiff_t strwidth (const char *, ptrdiff_t);
669 extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int,
670 ptrdiff_t *, ptrdiff_t *);
671 extern ptrdiff_t lisp_string_width (Lisp_Object
, ptrdiff_t,
672 ptrdiff_t *, ptrdiff_t *);
674 extern Lisp_Object Qcharacterp
;
675 extern Lisp_Object Vchar_unify_table
;
676 extern Lisp_Object
string_escape_byte8 (Lisp_Object
);
678 /* Return a translation table of id number ID. */
679 #define GET_TRANSLATION_TABLE(id) \
680 (XCDR (XVECTOR (Vtranslation_table_vector)->contents[(id)]))
684 #endif /* EMACS_CHARACTER_H */