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
28 /* character code 1st byte byte sequence
29 -------------- -------- -------------
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
35 3FFF80-3FFFFF C0..C1 1100000x 10xxxxxx (for eight-bit-char)
38 invalid 1st byte 80..BF 10xxxxxx
39 F9..FF 11111xxx (xxx != 000)
42 /* Maximum character code ((1 << CHARACTERBITS) - 1). */
43 #define MAX_CHAR 0x3FFFFF
45 /* Maximum Unicode character code. */
46 #define MAX_UNICODE_CHAR 0x10FFFF
48 /* Maximum N-byte character codes. */
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
55 /* Minimum leading code of multibyte characters. */
56 #define MIN_MULTIBYTE_LEADING_CODE 0xC0
57 /* Maximum leading code of multibyte characters. */
58 #define MAX_MULTIBYTE_LEADING_CODE 0xF8
60 /* Nonzero iff C is a character that corresponds to a raw 8-bit
62 #define CHAR_BYTE8_P(c) ((c) > MAX_5_BYTE_CHAR)
64 /* Return the character code for raw 8-bit byte BYTE. */
65 #define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00)
67 #define UNIBYTE_TO_CHAR(byte) \
68 (ASCII_BYTE_P (byte) ? (byte) : BYTE8_TO_CHAR (byte))
70 /* Return the raw 8-bit byte for character C. */
71 #define CHAR_TO_BYTE8(c) \
74 : multibyte_char_to_unibyte (c))
76 /* Return the raw 8-bit byte for character C,
77 or -1 if C doesn't correspond to a byte. */
78 #define CHAR_TO_BYTE_SAFE(c) \
81 : multibyte_char_to_unibyte_safe (c))
83 /* Nonzero iff BYTE is the 1st byte of a multibyte form of a character
84 that corresponds to a raw 8-bit byte. */
85 #define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1)
87 /* If C is not ASCII, make it unibyte. */
88 #define MAKE_CHAR_UNIBYTE(c) \
90 if (! ASCII_CHAR_P (c)) \
91 c = CHAR_TO_BYTE8 (c); \
95 /* If C is not ASCII, make it multibyte. Assumes C < 256. */
96 #define MAKE_CHAR_MULTIBYTE(c) \
97 (eassert ((c) >= 0 && (c) < 256), (c) = UNIBYTE_TO_CHAR (c))
99 /* This is the maximum byte length of multibyte form. */
100 #define MAX_MULTIBYTE_LENGTH 5
102 /* Return a Lisp character whose character code is C. Assumes C is
103 a valid character code. */
104 #define make_char(c) make_number (c)
106 /* Nonzero iff C is an ASCII byte. */
107 #define ASCII_BYTE_P(c) UNSIGNED_CMP (c, <, 0x80)
109 /* Nonzero iff X is a character. */
110 #define CHARACTERP(x) (NATNUMP (x) && XFASTINT (x) <= MAX_CHAR)
112 /* Nonzero iff C is valid as a character code. */
113 #define CHAR_VALID_P(c) UNSIGNED_CMP (c, <=, MAX_CHAR)
115 /* Check if Lisp object X is a character or not. */
116 #define CHECK_CHARACTER(x) \
117 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
119 #define CHECK_CHARACTER_CAR(x) \
121 Lisp_Object tmp = XCAR (x); \
122 CHECK_CHARACTER (tmp); \
123 XSETCAR ((x), tmp); \
126 #define CHECK_CHARACTER_CDR(x) \
128 Lisp_Object tmp = XCDR (x); \
129 CHECK_CHARACTER (tmp); \
130 XSETCDR ((x), tmp); \
133 /* Nonzero iff C is a character of code less than 0x100. */
134 #define SINGLE_BYTE_CHAR_P(c) UNSIGNED_CMP (c, <, 0x100)
136 /* Nonzero if character C has a printable glyph. */
137 #define CHAR_PRINTABLE_P(c) \
138 (((c) >= 32 && (c) < 127) \
139 || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c))))
141 /* Return byte length of multibyte form for character C. */
142 #define CHAR_BYTES(c) \
143 ( (c) <= MAX_1_BYTE_CHAR ? 1 \
144 : (c) <= MAX_2_BYTE_CHAR ? 2 \
145 : (c) <= MAX_3_BYTE_CHAR ? 3 \
146 : (c) <= MAX_4_BYTE_CHAR ? 4 \
147 : (c) <= MAX_5_BYTE_CHAR ? 5 \
151 /* Return the leading code of multibyte form of C. */
152 #define CHAR_LEADING_CODE(c) \
153 ((c) <= MAX_1_BYTE_CHAR ? c \
154 : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \
155 : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \
156 : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \
157 : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \
158 : (0xC0 | (((c) >> 6) & 0x01)))
161 /* Store multibyte form of the character C in P. The caller should
162 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
163 Returns the length of the multibyte form. */
165 #define CHAR_STRING(c, p) \
166 (UNSIGNED_CMP (c, <=, MAX_1_BYTE_CHAR) \
169 : UNSIGNED_CMP (c, <=, MAX_2_BYTE_CHAR) \
170 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
171 (p)[1] = (0x80 | ((c) & 0x3F)), \
173 : UNSIGNED_CMP (c, <=, MAX_3_BYTE_CHAR) \
174 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
175 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
176 (p)[2] = (0x80 | ((c) & 0x3F)), \
178 : verify_expr (sizeof (c) <= sizeof (unsigned), char_string (c, p)))
180 /* Store multibyte form of byte B in P. The caller should allocate at
181 least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the
182 length of the multibyte form. */
184 #define BYTE8_STRING(b, p) \
185 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
186 (p)[1] = (0x80 | ((b) & 0x3F)), \
190 /* Store multibyte form of the character C in P and advance P to the
191 end of the multibyte form. The caller should allocate at least
192 MAX_MULTIBYTE_LENGTH bytes area at P in advance. */
194 #define CHAR_STRING_ADVANCE(c, p) \
196 if ((c) <= MAX_1_BYTE_CHAR) \
198 else if ((c) <= MAX_2_BYTE_CHAR) \
199 *(p)++ = (0xC0 | ((c) >> 6)), \
200 *(p)++ = (0x80 | ((c) & 0x3F)); \
201 else if ((c) <= MAX_3_BYTE_CHAR) \
202 *(p)++ = (0xE0 | ((c) >> 12)), \
203 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
204 *(p)++ = (0x80 | ((c) & 0x3F)); \
207 verify (sizeof (c) <= sizeof (unsigned)); \
208 (p) += char_string (c, p); \
213 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
215 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
217 /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
219 #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
221 /* Nonzero iff BYTE starts a character in a multibyte form.
222 This is equivalent to:
223 (ASCII_BYTE_P (byte) || LEADING_CODE_P (byte)) */
224 #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
226 /* How many bytes a character that starts with BYTE occupies in a
228 #define BYTES_BY_CHAR_HEAD(byte) \
229 (!((byte) & 0x80) ? 1 \
230 : !((byte) & 0x20) ? 2 \
231 : !((byte) & 0x10) ? 3 \
232 : !((byte) & 0x08) ? 4 \
236 /* The byte length of multibyte form at unibyte string P ending at
237 PEND. If STR doesn't point to a valid multibyte form, return 0. */
239 #define MULTIBYTE_LENGTH(p, pend) \
241 : !((p)[0] & 0x80) ? 1 \
242 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
243 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
244 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
245 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
246 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
247 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
248 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
249 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
253 /* Like MULTIBYTE_LENGTH, but don't check the ending address. */
255 #define MULTIBYTE_LENGTH_NO_CHECK(p) \
256 (!((p)[0] & 0x80) ? 1 \
257 : ((p)[1] & 0xC0) != 0x80 ? 0 \
258 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
259 : ((p)[2] & 0xC0) != 0x80 ? 0 \
260 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
261 : ((p)[3] & 0xC0) != 0x80 ? 0 \
262 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
263 : ((p)[4] & 0xC0) != 0x80 ? 0 \
264 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
267 /* If P is before LIMIT, advance P to the next character boundary.
268 Assumes that P is already at a character boundary of the same
269 multibyte form whose end address is LIMIT. */
271 #define NEXT_CHAR_BOUNDARY(p, limit) \
274 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
278 /* If P is after LIMIT, advance P to the previous character boundary.
279 Assumes that P is already at a character boundary of the same
280 multibyte form whose beginning address is LIMIT. */
282 #define PREV_CHAR_BOUNDARY(p, limit) \
286 const unsigned char *chp = (p); \
289 } while (chp >= limit && ! CHAR_HEAD_P (*chp)); \
290 (p) = (BYTES_BY_CHAR_HEAD (*chp) == (p) - chp) ? chp : (p) - 1; \
294 /* Return the character code of character whose multibyte form is at
297 #define STRING_CHAR(p) \
300 : ! ((p)[0] & 0x20) \
301 ? (((((p)[0] & 0x1F) << 6) \
303 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \
304 : ! ((p)[0] & 0x10) \
305 ? ((((p)[0] & 0x0F) << 12) \
306 | (((p)[1] & 0x3F) << 6) \
308 : string_char ((p), NULL, NULL))
311 /* Like STRING_CHAR, but set ACTUAL_LEN to the length of multibyte
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 /* If C is a character to be unified with a Unicode character, return
543 the unified Unicode character. */
545 #define MAYBE_UNIFY_CHAR(c) \
547 if (c > MAX_UNICODE_CHAR && c <= MAX_5_BYTE_CHAR) \
550 val = CHAR_TABLE_REF (Vchar_unify_table, c); \
551 if (INTEGERP (val)) \
552 c = XFASTINT (val); \
553 else if (! NILP (val)) \
554 c = maybe_unify_char (c, val); \
559 /* Return a non-outlandish value for the tab width. */
561 #define SANE_TAB_WIDTH(buf) \
562 sanitize_tab_width (XFASTINT (BVAR (buf, tab_width)))
564 sanitize_tab_width (EMACS_INT width
)
566 return 0 < width
&& width
<= 1000 ? width
: 8;
569 /* Return the width of ASCII character C. The width is measured by
570 how many columns C will occupy on the screen when displayed in the
573 #define ASCII_CHAR_WIDTH(c) \
576 ? SANE_TAB_WIDTH (current_buffer) \
577 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
580 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
582 /* Return a non-outlandish value for a character width. */
585 sanitize_char_width (EMACS_INT width
)
587 return 0 <= width
&& width
<= 1000 ? width
: 1000;
590 /* Return the width of character C. The width is measured by how many
591 columns C will occupy on the screen when displayed in the current
594 #define CHAR_WIDTH(c) \
596 ? ASCII_CHAR_WIDTH (c) \
597 : sanitize_char_width (XINT (CHAR_TABLE_REF (Vchar_width_table, c))))
599 /* If C is a variation selector, return the index numnber of the
600 variation selector (1..256). Otherwise, return 0. */
602 #define CHAR_VARIATION_SELECTOR_P(c) \
604 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
605 : (c) < 0xE0100 ? 0 \
606 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
609 /* If C is a high surrogate, return 1. If C is a low surrogate,
610 return 0. Otherwise, return 0. */
612 #define CHAR_SURROGATE_PAIR_P(c) \
614 : (c) <= 0xDBFF ? 1 \
615 : (c) <= 0xDFFF ? 2 \
618 /* Data type for Unicode general category.
620 The order of members must be in sync with the 8th element of the
621 member of unidata-prop-alist (in admin/unidata/unidata-getn.el) for
622 Unicode character property `general-category'. */
625 UNICODE_CATEGORY_UNKNOWN
= 0,
656 } unicode_category_t
;
658 extern int char_resolve_modifier_mask (int);
659 extern int char_string (unsigned, unsigned char *);
660 extern int string_char (const unsigned char *,
661 const unsigned char **, int *);
663 extern int translate_char (Lisp_Object
, int c
);
664 extern int char_printable_p (int c
);
665 extern void parse_str_as_multibyte (const unsigned char *,
666 EMACS_INT
, EMACS_INT
*, EMACS_INT
*);
667 extern EMACS_INT
count_size_as_multibyte (const unsigned char *, EMACS_INT
);
668 extern EMACS_INT
str_as_multibyte (unsigned char *, EMACS_INT
, EMACS_INT
,
670 extern EMACS_INT
str_to_multibyte (unsigned char *, EMACS_INT
, EMACS_INT
);
671 extern EMACS_INT
str_as_unibyte (unsigned char *, EMACS_INT
);
672 extern EMACS_INT
str_to_unibyte (const unsigned char *, unsigned char *,
674 extern EMACS_INT
strwidth (const char *, EMACS_INT
);
675 extern EMACS_INT
c_string_width (const unsigned char *, EMACS_INT
, int,
676 EMACS_INT
*, EMACS_INT
*);
677 extern EMACS_INT
lisp_string_width (Lisp_Object
, EMACS_INT
,
678 EMACS_INT
*, EMACS_INT
*);
680 extern Lisp_Object Qcharacterp
;
681 extern Lisp_Object Vchar_unify_table
;
682 extern Lisp_Object
string_escape_byte8 (Lisp_Object
);
684 /* Return a translation table of id number ID. */
685 #define GET_TRANSLATION_TABLE(id) \
686 (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)]))
688 #endif /* EMACS_CHARACTER_H */