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
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
26 /* character code 1st byte byte sequence
27 -------------- -------- -------------
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
33 3FFF80-3FFFFF C0..C1 1100000x 10xxxxxx (for eight-bit-char)
36 invalid 1st byte 80..BF 10xxxxxx
37 F9..FF 11111xxx (xxx != 000)
40 /* Maximum character code ((1 << CHARACTERBITS) - 1). */
41 #define MAX_CHAR 0x3FFFFF
43 /* Maximum Unicode character code. */
44 #define MAX_UNICODE_CHAR 0x10FFFF
46 /* Maximum N-byte character codes. */
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
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
58 /* Nonzero iff C is a character that corresponds to a raw 8-bit
60 #define CHAR_BYTE8_P(c) ((c) > MAX_5_BYTE_CHAR)
62 /* Return the character code for raw 8-bit byte BYTE. */
63 #define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00)
65 /* Return the raw 8-bit byte for character C. */
66 #define CHAR_TO_BYTE8(c) \
69 : multibyte_char_to_unibyte (c, Qnil))
71 /* Nonzero iff BYTE is the 1st byte of a multibyte form of a character
72 that corresponds to a raw 8-bit byte. */
73 #define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1)
75 /* Mapping table from unibyte chars to multibyte chars. */
76 extern int unibyte_to_multibyte_table
[256];
78 /* Convert the unibyte character C to the corresponding multibyte
79 character. If C can't be converted, return C. */
80 #define unibyte_char_to_multibyte(c) \
81 ((c) < 256 ? unibyte_to_multibyte_table[(c)] : (c))
83 /* Nth element is 1 iff unibyte char N can be mapped to a multibyte
85 extern char unibyte_has_multibyte_table
[256];
87 #define UNIBYTE_CHAR_HAS_MULTIBYTE_P(c) (unibyte_has_multibyte_table[(c)])
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. It assumes C < 256. */
98 #define MAKE_CHAR_MULTIBYTE(c) \
99 (eassert ((c) >= 0 && (c) < 256), (c) = unibyte_to_multibyte_table[(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. It 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) (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. GENERICP is not used
116 #define CHAR_VALID_P(c, genericp) ((unsigned) (c) <= MAX_CHAR)
118 /* Check if Lisp object X is a character or not. */
119 #define CHECK_CHARACTER(x) \
120 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
122 #define CHECK_CHARACTER_CAR(x) \
124 Lisp_Object tmp = XCAR (x); \
125 CHECK_CHARACTER (tmp); \
126 XSETCAR ((x), tmp); \
129 #define CHECK_CHARACTER_CDR(x) \
131 Lisp_Object tmp = XCDR (x); \
132 CHECK_CHARACTER (tmp); \
133 XSETCDR ((x), tmp); \
136 /* Nonzero iff C is an ASCII character. */
137 #define ASCII_CHAR_P(c) ((unsigned) (c) < 0x80)
139 /* Nonzero iff C is a character of code less than 0x100. */
140 #define SINGLE_BYTE_CHAR_P(c) ((unsigned) (c) < 0x100)
142 /* Nonzero if character C has a printable glyph. */
143 #define CHAR_PRINTABLE_P(c) \
144 (((c) >= 32 && ((c) < 127) \
145 || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c)))))
147 /* Return byte length of multibyte form for character C. */
148 #define CHAR_BYTES(c) \
149 ( (c) <= MAX_1_BYTE_CHAR ? 1 \
150 : (c) <= MAX_2_BYTE_CHAR ? 2 \
151 : (c) <= MAX_3_BYTE_CHAR ? 3 \
152 : (c) <= MAX_4_BYTE_CHAR ? 4 \
153 : (c) <= MAX_5_BYTE_CHAR ? 5 \
157 /* Return the leading code of multibyte form of C. */
158 #define CHAR_LEADING_CODE(c) \
159 ((c) <= MAX_1_BYTE_CHAR ? c \
160 : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \
161 : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \
162 : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \
163 : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \
164 : (0xC0 | (((c) >> 6) & 0x01)))
167 /* Store multibyte form of the character C in P. The caller should
168 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
169 Returns the length of the multibyte form. */
171 #define CHAR_STRING(c, p) \
172 ((unsigned) (c) <= MAX_1_BYTE_CHAR \
175 : (unsigned) (c) <= MAX_2_BYTE_CHAR \
176 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
177 (p)[1] = (0x80 | ((c) & 0x3F)), \
179 : (unsigned) (c) <= MAX_3_BYTE_CHAR \
180 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
181 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
182 (p)[2] = (0x80 | ((c) & 0x3F)), \
184 : char_string ((unsigned) c, p))
186 /* Store multibyte form of byte B in P. The caller should allocate at
187 least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the
188 length of the multibyte form. */
190 #define BYTE8_STRING(b, p) \
191 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
192 (p)[1] = (0x80 | ((b) & 0x3F)), \
196 /* Store multibyte form of the character C in P. The caller should
197 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
198 And, advance P to the end of the multibyte form. */
200 #define CHAR_STRING_ADVANCE(c, p) \
202 if ((c) <= MAX_1_BYTE_CHAR) \
204 else if ((c) <= MAX_2_BYTE_CHAR) \
205 *(p)++ = (0xC0 | ((c) >> 6)), \
206 *(p)++ = (0x80 | ((c) & 0x3F)); \
207 else if ((c) <= MAX_3_BYTE_CHAR) \
208 *(p)++ = (0xE0 | ((c) >> 12)), \
209 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
210 *(p)++ = (0x80 | ((c) & 0x3F)); \
212 (p) += char_string ((c), (p)); \
216 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
218 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
220 /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
222 #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
224 /* Nonzero iff BYTE starts a character in a multibyte form.
225 This is equivalent to:
226 (ASCII_BYTE_P (byte) || LEADING_CODE_P (byte)) */
227 #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
229 /* Just kept for backward compatibility. This macro will be removed
231 #define BASE_LEADING_CODE_P LEADING_CODE_P
233 /* How many bytes a character that starts with BYTE occupies in a
235 #define BYTES_BY_CHAR_HEAD(byte) \
236 (!((byte) & 0x80) ? 1 \
237 : !((byte) & 0x20) ? 2 \
238 : !((byte) & 0x10) ? 3 \
239 : !((byte) & 0x08) ? 4 \
243 /* Return the length of the multi-byte form at string STR of length
244 LEN while assuming that STR points a valid multi-byte form. As
245 this macro isn't necessary anymore, all callers will be changed to
246 use BYTES_BY_CHAR_HEAD directly in the future. */
248 #define MULTIBYTE_FORM_LENGTH(str, len) \
249 BYTES_BY_CHAR_HEAD (*(str))
251 /* Parse multibyte string STR of length LENGTH and set BYTES to the
252 byte length of a character at STR while assuming that STR points a
253 valid multibyte form. As this macro isn't necessary anymore, all
254 callers will be changed to use BYTES_BY_CHAR_HEAD directly in the
257 #define PARSE_MULTIBYTE_SEQ(str, length, bytes) \
258 (bytes) = BYTES_BY_CHAR_HEAD (*(str))
260 /* The byte length of multibyte form at unibyte string P ending at
261 PEND. If STR doesn't point a valid multibyte form, return 0. */
263 #define MULTIBYTE_LENGTH(p, pend) \
265 : !((p)[0] & 0x80) ? 1 \
266 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
267 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
268 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
269 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
270 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
271 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
272 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
273 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
277 /* Like MULTIBYTE_LENGTH but don't check the ending address. */
279 #define MULTIBYTE_LENGTH_NO_CHECK(p) \
280 (!((p)[0] & 0x80) ? 1 \
281 : ((p)[1] & 0xC0) != 0x80 ? 0 \
282 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
283 : ((p)[2] & 0xC0) != 0x80 ? 0 \
284 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
285 : ((p)[3] & 0xC0) != 0x80 ? 0 \
286 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
287 : ((p)[4] & 0xC0) != 0x80 ? 0 \
288 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
291 /* If P is before LIMIT, advance P to the next character boundary. It
292 assumes that P is already at a character boundary of the sane
293 mulitbyte form whose end address is LIMIT. */
295 #define NEXT_CHAR_BOUNDARY(p, limit) \
298 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
302 /* If P is after LIMIT, advance P to the previous character boundary.
303 It assumes that P is already at a character boundary of the sane
304 mulitbyte form whose beginning address is LIMIT. */
306 #define PREV_CHAR_BOUNDARY(p, limit) \
310 const unsigned char *p0 = (p); \
313 } while (p0 >= limit && ! CHAR_HEAD_P (*p0)); \
314 (p) = (BYTES_BY_CHAR_HEAD (*p0) == (p) - p0) ? p0 : (p) - 1; \
318 /* Return the character code of character whose multibyte form is at
319 P. The argument LEN is ignored. It will be removed in the
322 #define STRING_CHAR(p, len) \
325 : ! ((p)[0] & 0x20) \
326 ? (((((p)[0] & 0x1F) << 6) \
328 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \
329 : ! ((p)[0] & 0x10) \
330 ? ((((p)[0] & 0x0F) << 12) \
331 | (((p)[1] & 0x3F) << 6) \
333 : string_char ((p), NULL, NULL))
336 /* Like STRING_CHAR but set ACTUAL_LEN to the length of multibyte
337 form. The argument LEN is ignored. It will be removed in the
340 #define STRING_CHAR_AND_LENGTH(p, len, actual_len) \
342 ? ((actual_len) = 1, (p)[0]) \
343 : ! ((p)[0] & 0x20) \
344 ? ((actual_len) = 2, \
345 (((((p)[0] & 0x1F) << 6) \
347 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \
348 : ! ((p)[0] & 0x10) \
349 ? ((actual_len) = 3, \
350 ((((p)[0] & 0x0F) << 12) \
351 | (((p)[1] & 0x3F) << 6) \
352 | ((p)[2] & 0x3F))) \
353 : string_char ((p), NULL, &actual_len))
356 /* Like STRING_CHAR but advance P to the end of multibyte form. */
358 #define STRING_CHAR_ADVANCE(p) \
361 : ! ((p)[0] & 0x20) \
363 ((((p)[-2] & 0x1F) << 6) \
365 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
366 : ! ((p)[0] & 0x10) \
368 ((((p)[-3] & 0x0F) << 12) \
369 | (((p)[-2] & 0x3F) << 6) \
370 | ((p)[-1] & 0x3F))) \
371 : string_char ((p), &(p), NULL))
374 /* Fetch the "next" character from Lisp string STRING at byte position
375 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
377 All the args must be side-effect-free.
378 BYTEIDX and CHARIDX must be lvalues;
379 we increment them past the character fetched. */
381 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
385 if (STRING_MULTIBYTE (STRING)) \
387 unsigned char *ptr = &SDATA (STRING)[BYTEIDX]; \
390 OUTPUT = STRING_CHAR_AND_LENGTH (ptr, 0, len); \
395 OUTPUT = SREF (STRING, BYTEIDX); \
401 /* Like FETCH_STRING_CHAR_ADVANCE but return a multibyte character eve
402 if STRING is unibyte. */
404 #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
408 if (STRING_MULTIBYTE (STRING)) \
410 unsigned char *ptr = &SDATA (STRING)[BYTEIDX]; \
413 OUTPUT = STRING_CHAR_AND_LENGTH (ptr, 0, len); \
418 OUTPUT = SREF (STRING, BYTEIDX); \
420 MAKE_CHAR_MULTIBYTE (OUTPUT); \
426 /* Like FETCH_STRING_CHAR_ADVANCE but assumes STRING is multibyte. */
428 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
431 unsigned char *ptr = &SDATA (STRING)[BYTEIDX]; \
434 OUTPUT = STRING_CHAR_AND_LENGTH (ptr, 0, len); \
441 /* Like FETCH_STRING_CHAR_ADVANCE but fetch character from the current
444 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
448 if (!NILP (current_buffer->enable_multibyte_characters)) \
450 unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \
453 OUTPUT= STRING_CHAR_AND_LENGTH (ptr, 0, len); \
458 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
465 /* Like FETCH_CHAR_ADVANCE but assumes the current buffer is multibyte. */
467 #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
470 unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \
473 OUTPUT= STRING_CHAR_AND_LENGTH (ptr, 0, len); \
480 /* Increase the buffer byte position POS_BYTE of the current buffer to
481 the next character boundary. No range checking of POS. */
483 #define INC_POS(pos_byte) \
485 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
486 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
490 /* Decrease the buffer byte position POS_BYTE of the current buffer to
491 the previous character boundary. No range checking of POS. */
493 #define DEC_POS(pos_byte) \
498 if (pos_byte < GPT_BYTE) \
499 p = BEG_ADDR + pos_byte - BEG_BYTE; \
501 p = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE;\
502 while (!CHAR_HEAD_P (*p)) \
509 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
511 #define INC_BOTH(charpos, bytepos) \
515 if (NILP (current_buffer->enable_multibyte_characters)) \
518 INC_POS ((bytepos)); \
523 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
525 #define DEC_BOTH(charpos, bytepos) \
529 if (NILP (current_buffer->enable_multibyte_characters)) \
532 DEC_POS ((bytepos)); \
537 /* Increase the buffer byte position POS_BYTE of the current buffer to
538 the next character boundary. This macro relies on the fact that
539 *GPT_ADDR and *Z_ADDR are always accessible and the values are
540 '\0'. No range checking of POS_BYTE. */
542 #define BUF_INC_POS(buf, pos_byte) \
544 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \
545 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
549 /* Decrease the buffer byte position POS_BYTE of the current buffer to
550 the previous character boundary. No range checking of POS_BYTE. */
552 #define BUF_DEC_POS(buf, pos_byte) \
556 if (pos_byte < BUF_GPT_BYTE (buf)) \
557 p = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
559 p = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
560 while (!CHAR_HEAD_P (*p)) \
568 /* If C is a character to be unified with a Unicode character, return
569 the unified Unicode character. */
571 #define MAYBE_UNIFY_CHAR(c) \
572 if (c > MAX_UNICODE_CHAR \
573 && CHAR_TABLE_P (Vchar_unify_table)) \
578 val = CHAR_TABLE_REF (Vchar_unify_table, c); \
583 Funify_charset (val, Qnil, Qnil); \
584 val = CHAR_TABLE_REF (Vchar_unify_table, c); \
586 if ((unified = XINT (val)) >= 0) \
593 /* Return the width of ASCII character C. The width is measured by
594 how many columns occupied on the screen when displayed in the
597 #define ASCII_CHAR_WIDTH(c) \
600 ? XFASTINT (current_buffer->tab_width) \
601 : (c == '\n' ? 0 : (NILP (current_buffer->ctl_arrow) ? 4 : 2))) \
604 : ((NILP (current_buffer->ctl_arrow) ? 4 : 2))))
606 /* Return the width of character C. The width is measured by how many
607 columns occupied on the screen when displayed in the current
610 #define CHAR_WIDTH(c) \
612 ? ASCII_CHAR_WIDTH (c) \
613 : XINT (CHAR_TABLE_REF (Vchar_width_table, c)))
615 extern int char_resolve_modifier_mask
P_ ((int));
616 extern int char_string
P_ ((unsigned, unsigned char *));
617 extern int string_char
P_ ((const unsigned char *,
618 const unsigned char **, int *));
620 extern int translate_char
P_ ((Lisp_Object
, int c
));
621 extern int char_printable_p
P_ ((int c
));
622 extern void parse_str_as_multibyte
P_ ((const unsigned char *, int, int *,
624 extern int parse_str_to_multibyte
P_ ((unsigned char *, int));
625 extern int str_as_multibyte
P_ ((unsigned char *, int, int, int *));
626 extern int str_to_multibyte
P_ ((unsigned char *, int, int));
627 extern int str_as_unibyte
P_ ((unsigned char *, int));
628 extern int strwidth
P_ ((unsigned char *, int));
629 extern int c_string_width
P_ ((const unsigned char *, int, int, int *, int *));
630 extern int lisp_string_width
P_ ((Lisp_Object
, int, int *, int *));
632 extern Lisp_Object Vprintable_chars
;
634 extern Lisp_Object Qcharacterp
, Qauto_fill_chars
;
635 extern Lisp_Object Vtranslation_table_vector
;
636 extern Lisp_Object Vchar_width_table
;
637 extern Lisp_Object Vchar_direction_table
;
638 extern Lisp_Object Vchar_unify_table
;
640 extern Lisp_Object string_escape_byte8
P_ ((Lisp_Object
));
642 /* Return a translation table of id number ID. */
643 #define GET_TRANSLATION_TABLE(id) \
644 (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)]))
646 /* A char-table for characters which may invoke auto-filling. */
647 extern Lisp_Object Vauto_fill_chars
;
649 extern Lisp_Object Vchar_script_table
;
650 extern Lisp_Object Vscript_representative_chars
;
652 /* Copy LEN bytes from FROM to TO. This macro should be used only
653 when a caller knows that LEN is short and the obvious copy loop is
654 faster than calling bcopy which has some overhead. Copying a
655 multibyte sequence of a character is the typical case. */
657 #define BCOPY_SHORT(from, to, len) \
660 unsigned char *from_p = from, *to_p = to; \
661 while (i--) *to_p++ = *from_p++; \
664 #define DEFSYM(sym, name) \
665 do { (sym) = intern ((name)); staticpro (&(sym)); } while (0)
667 #endif /* EMACS_CHARACTER_H */
669 /* arch-tag: 4ef86004-2eff-4073-8cea-cfcbcf7188ac
670 (do not change this comment) */