(kCGBitmapByteOrder32Host): New define for non-universal SDKs.
[emacs.git] / src / charset.h
blobde7a16a01c72d4f5332e59dbb896d178ba857e96
1 /* Header for multibyte character handler.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
9 This file is part of GNU Emacs.
11 GNU Emacs is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3, or (at your option)
14 any later version.
16 GNU Emacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU Emacs; see the file COPYING. If not, write to
23 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
26 #ifndef EMACS_CHARSET_H
27 #define EMACS_CHARSET_H
29 /* #define BYTE_COMBINING_DEBUG */
31 /*** GENERAL NOTE on CHARACTER SET (CHARSET) ***
33 A character set ("charset" hereafter) is a meaningful collection
34 (i.e. language, culture, functionality, etc) of characters. Emacs
35 handles multiple charsets at once. Each charset corresponds to one
36 of the ISO charsets. Emacs identifies a charset by a unique
37 identification number, whereas ISO identifies a charset by a triplet
38 of DIMENSION, CHARS and FINAL-CHAR. So, hereafter, just saying
39 "charset" means an identification number (integer value).
41 The value range of charsets is 0x00, 0x81..0xFE. There are four
42 kinds of charset depending on DIMENSION (1 or 2) and CHARS (94 or
43 96). For instance, a charset of DIMENSION2_CHARS94 contains 94x94
44 characters.
46 Within Emacs Lisp, a charset is treated as a symbol which has a
47 property `charset'. The property value is a vector containing
48 various information about the charset. For readability of C code,
49 we use the following convention for C variable names:
50 charset_symbol: Emacs Lisp symbol of a charset
51 charset_id: Emacs Lisp integer of an identification number of a charset
52 charset: C integer of an identification number of a charset
54 Each charset (except for ascii) is assigned a base leading-code
55 (range 0x80..0x9E). In addition, a charset of greater than 0xA0
56 (whose base leading-code is 0x9A..0x9D) is assigned an extended
57 leading-code (range 0xA0..0xFE). In this case, each base
58 leading-code specifies the allowable range of extended leading-code
59 as shown in the table below. A leading-code is used to represent a
60 character in Emacs' buffer and string.
62 We call a charset which has extended leading-code a "private
63 charset" because those are mainly for a charset which is not yet
64 registered by ISO. On the contrary, we call a charset which does
65 not have extended leading-code an "official charset".
67 ---------------------------------------------------------------------------
68 charset dimension base leading-code extended leading-code
69 ---------------------------------------------------------------------------
70 0x00 official dim1 -- none -- -- none --
71 (ASCII)
72 0x01..0x7F --never used--
73 0x80 official dim1 -- none -- -- none --
74 (eight-bit-graphic)
75 0x81..0x8F official dim1 same as charset -- none --
76 0x90..0x99 official dim2 same as charset -- none --
77 0x9A..0x9D --never used--
78 0x9E official dim1 same as charset -- none --
79 (eight-bit-control)
80 0x9F --never used--
81 0xA0..0xDF private dim1 0x9A same as charset
82 of 1-column width
83 0xE0..0xEF private dim1 0x9B same as charset
84 of 2-column width
85 0xF0..0xF4 private dim2 0x9C same as charset
86 of 1-column width
87 0xF5..0xFE private dim2 0x9D same as charset
88 of 2-column width
89 0xFF --never used--
90 ---------------------------------------------------------------------------
94 /* Definition of special leading-codes. */
95 /* Leading-code followed by extended leading-code. */
96 #define LEADING_CODE_PRIVATE_11 0x9A /* for private DIMENSION1 of 1-column */
97 #define LEADING_CODE_PRIVATE_12 0x9B /* for private DIMENSION1 of 2-column */
98 #define LEADING_CODE_PRIVATE_21 0x9C /* for private DIMENSION2 of 1-column */
99 #define LEADING_CODE_PRIVATE_22 0x9D /* for private DIMENSION2 of 2-column */
101 #define LEADING_CODE_8_BIT_CONTROL 0x9E /* for `eight-bit-control' */
103 /* Extended leading-code. */
104 /* Start of each extended leading-codes. */
105 #define LEADING_CODE_EXT_11 0xA0 /* follows LEADING_CODE_PRIVATE_11 */
106 #define LEADING_CODE_EXT_12 0xE0 /* follows LEADING_CODE_PRIVATE_12 */
107 #define LEADING_CODE_EXT_21 0xF0 /* follows LEADING_CODE_PRIVATE_21 */
108 #define LEADING_CODE_EXT_22 0xF5 /* follows LEADING_CODE_PRIVATE_22 */
109 /* Maximum value of extended leading-codes. */
110 #define LEADING_CODE_EXT_MAX 0xFE
112 /* Definition of minimum/maximum charset of each DIMENSION. */
113 #define MIN_CHARSET_OFFICIAL_DIMENSION1 0x80
114 #define MAX_CHARSET_OFFICIAL_DIMENSION1 0x8F
115 #define MIN_CHARSET_OFFICIAL_DIMENSION2 0x90
116 #define MAX_CHARSET_OFFICIAL_DIMENSION2 0x99
117 #define MIN_CHARSET_PRIVATE_DIMENSION1 LEADING_CODE_EXT_11
118 #define MIN_CHARSET_PRIVATE_DIMENSION2 LEADING_CODE_EXT_21
120 /* Maximum value of overall charset identification number. */
121 #define MAX_CHARSET 0xFE
123 /* Definition of special charsets. */
124 #define CHARSET_ASCII 0 /* 0x00..0x7F */
125 #define CHARSET_8_BIT_CONTROL 0x9E /* 0x80..0x9F */
126 #define CHARSET_8_BIT_GRAPHIC 0x80 /* 0xA0..0xFF */
128 extern int charset_latin_iso8859_1; /* ISO8859-1 (Latin-1) */
129 extern int charset_jisx0208_1978; /* JISX0208.1978 (Japanese Kanji old set) */
130 extern int charset_jisx0208; /* JISX0208.1983 (Japanese Kanji) */
131 extern int charset_katakana_jisx0201; /* JISX0201.Kana (Japanese Katakana) */
132 extern int charset_latin_jisx0201; /* JISX0201.Roman (Japanese Roman) */
133 extern int charset_big5_1; /* Big5 Level 1 (Chinese Traditional) */
134 extern int charset_big5_2; /* Big5 Level 2 (Chinese Traditional) */
135 extern int charset_mule_unicode_0100_24ff;
136 extern int charset_mule_unicode_2500_33ff;
137 extern int charset_mule_unicode_e000_ffff;
139 /* Check if CH is an ASCII character or a base leading-code.
140 Nowadays, any byte can be the first byte of a character in a
141 multibyte buffer/string. So this macro name is not appropriate. */
142 #define CHAR_HEAD_P(ch) ((unsigned char) (ch) < 0xA0)
144 /*** GENERAL NOTE on CHARACTER REPRESENTATION ***
146 Firstly, the term "character" or "char" is used for a multilingual
147 character (of course, including ASCII characters), not for a byte in
148 computer memory. We use the term "code" or "byte" for the latter
149 case.
151 A character is identified by charset and one or two POSITION-CODEs.
152 POSITION-CODE is the position of the character in the charset. A
153 character of DIMENSION1 charset has one POSITION-CODE: POSITION-CODE-1.
154 A character of DIMENSION2 charset has two POSITION-CODE:
155 POSITION-CODE-1 and POSITION-CODE-2. The code range of
156 POSITION-CODE is 0x20..0x7F.
158 Emacs has two kinds of representation of a character: multi-byte
159 form (for buffers and strings) and single-word form (for character
160 objects in Emacs Lisp). The latter is called "character code"
161 hereafter. Both representations encode the information of charset
162 and POSITION-CODE but in a different way (for instance, the MSB of
163 POSITION-CODE is set in multi-byte form).
165 For details of the multi-byte form, see the section "2. Emacs
166 internal format handlers" of `coding.c'.
168 Emacs uses 19 bits for a character code. The bits are divided into
169 3 fields: FIELD1(5bits):FIELD2(7bits):FIELD3(7bits).
171 A character code of DIMENSION1 character uses FIELD2 to hold charset
172 and FIELD3 to hold POSITION-CODE-1. A character code of DIMENSION2
173 character uses FIELD1 to hold charset, FIELD2 and FIELD3 to hold
174 POSITION-CODE-1 and POSITION-CODE-2 respectively.
176 More precisely...
178 FIELD2 of DIMENSION1 character (except for ascii, eight-bit-control,
179 and eight-bit-graphic) is "charset - 0x70". This is to make all
180 character codes except for ASCII and 8-bit codes greater than 256.
181 So, the range of FIELD2 of DIMENSION1 character is 0, 1, or
182 0x11..0x7F.
184 FIELD1 of DIMENSION2 character is "charset - 0x8F" for official
185 charset and "charset - 0xE0" for private charset. So, the range of
186 FIELD1 of DIMENSION2 character is 0x01..0x1E.
188 -----------------------------------------------------------------------------
189 charset FIELD1 (5-bit) FIELD2 (7-bit) FIELD3 (7-bit)
190 -----------------------------------------------------------------------------
191 ascii 0 0 0x00..0x7F
192 eight-bit-control 0 1 0x00..0x1F
193 eight-bit-graphic 0 1 0x20..0x7F
194 DIMENSION1 0 charset - 0x70 POSITION-CODE-1
195 DIMENSION2(o) charset - 0x8F POSITION-CODE-1 POSITION-CODE-2
196 DIMENSION2(p) charset - 0xE0 POSITION-CODE-1 POSITION-CODE-2
197 -----------------------------------------------------------------------------
198 "(o)": official, "(p)": private
199 -----------------------------------------------------------------------------
202 /* Masks of each field of character code. */
203 #define CHAR_FIELD1_MASK (0x1F << 14)
204 #define CHAR_FIELD2_MASK (0x7F << 7)
205 #define CHAR_FIELD3_MASK 0x7F
207 /* Macros to access each field of character C. */
208 #define CHAR_FIELD1(c) (((c) & CHAR_FIELD1_MASK) >> 14)
209 #define CHAR_FIELD2(c) (((c) & CHAR_FIELD2_MASK) >> 7)
210 #define CHAR_FIELD3(c) ((c) & CHAR_FIELD3_MASK)
212 /* Minimum character code of character of each DIMENSION. */
213 #define MIN_CHAR_OFFICIAL_DIMENSION1 \
214 ((0x81 - 0x70) << 7)
215 #define MIN_CHAR_PRIVATE_DIMENSION1 \
216 ((MIN_CHARSET_PRIVATE_DIMENSION1 - 0x70) << 7)
217 #define MIN_CHAR_OFFICIAL_DIMENSION2 \
218 ((MIN_CHARSET_OFFICIAL_DIMENSION2 - 0x8F) << 14)
219 #define MIN_CHAR_PRIVATE_DIMENSION2 \
220 ((MIN_CHARSET_PRIVATE_DIMENSION2 - 0xE0) << 14)
221 /* Maximum character code currently used plus 1. */
222 #define MAX_CHAR (0x1F << 14)
224 /* 1 if C is a single byte character, else 0. */
225 #define SINGLE_BYTE_CHAR_P(c) (((unsigned)(c) & 0xFF) == (c))
227 /* 1 if BYTE is an ASCII character in itself, in multibyte mode. */
228 #define ASCII_BYTE_P(byte) ((byte) < 0x80)
230 /* A char-table containing information on each character set.
232 Unlike ordinary char-tables, this doesn't contain any nested tables.
233 Only the top level elements are used. Each element is a vector of
234 the following information:
235 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
236 LEADING-CODE-BASE, LEADING-CODE-EXT,
237 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
238 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
239 PLIST.
241 CHARSET-ID (integer) is the identification number of the charset.
243 BYTES (integer) is the length of the multi-byte form of a character
244 in the charset: one of 1, 2, 3, and 4.
246 DIMENSION (integer) is the number of bytes to represent a character: 1 or 2.
248 CHARS (integer) is the number of characters in a dimension: 94 or 96.
250 WIDTH (integer) is the number of columns a character in the charset
251 occupies on the screen: one of 0, 1, and 2..
253 DIRECTION (integer) is the rendering direction of characters in the
254 charset when rendering. If 0, render from left to right, else
255 render from right to left.
257 LEADING-CODE-BASE (integer) is the base leading-code for the
258 charset.
260 LEADING-CODE-EXT (integer) is the extended leading-code for the
261 charset. All charsets of less than 0xA0 have the value 0.
263 ISO-FINAL-CHAR (character) is the final character of the
264 corresponding ISO 2022 charset. It is -1 for such a character
265 that is used only internally (e.g. `eight-bit-control').
267 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
268 while encoding to variants of ISO 2022 coding system, one of the
269 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR). It
270 is -1 for such a character that is used only internally
271 (e.g. `eight-bit-control').
273 REVERSE-CHARSET (integer) is the charset which differs only in
274 LEFT-TO-RIGHT value from the charset. If there's no such a
275 charset, the value is -1.
277 SHORT-NAME (string) is the short name to refer to the charset.
279 LONG-NAME (string) is the long name to refer to the charset.
281 DESCRIPTION (string) is the description string of the charset.
283 PLIST (property list) may contain any type of information a user
284 wants to put and get by functions `put-charset-property' and
285 `get-charset-property' respectively. */
286 extern Lisp_Object Vcharset_table;
288 /* Macros to access various information of CHARSET in Vcharset_table.
289 We provide these macros for efficiency. No range check of CHARSET. */
291 /* Return entry of CHARSET (C integer) in Vcharset_table. */
292 #define CHARSET_TABLE_ENTRY(charset) \
293 XCHAR_TABLE (Vcharset_table)->contents[((charset) == CHARSET_ASCII \
294 ? 0 : (charset) + 128)]
296 /* Return information INFO-IDX of CHARSET. */
297 #define CHARSET_TABLE_INFO(charset, info_idx) \
298 XVECTOR (CHARSET_TABLE_ENTRY (charset))->contents[info_idx]
300 #define CHARSET_ID_IDX (0)
301 #define CHARSET_BYTES_IDX (1)
302 #define CHARSET_DIMENSION_IDX (2)
303 #define CHARSET_CHARS_IDX (3)
304 #define CHARSET_WIDTH_IDX (4)
305 #define CHARSET_DIRECTION_IDX (5)
306 #define CHARSET_LEADING_CODE_BASE_IDX (6)
307 #define CHARSET_LEADING_CODE_EXT_IDX (7)
308 #define CHARSET_ISO_FINAL_CHAR_IDX (8)
309 #define CHARSET_ISO_GRAPHIC_PLANE_IDX (9)
310 #define CHARSET_REVERSE_CHARSET_IDX (10)
311 #define CHARSET_SHORT_NAME_IDX (11)
312 #define CHARSET_LONG_NAME_IDX (12)
313 #define CHARSET_DESCRIPTION_IDX (13)
314 #define CHARSET_PLIST_IDX (14)
315 /* Size of a vector of each entry of Vcharset_table. */
316 #define CHARSET_MAX_IDX (15)
318 /* And several more macros to be used frequently. */
319 #define CHARSET_BYTES(charset) \
320 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_BYTES_IDX))
321 #define CHARSET_DIMENSION(charset) \
322 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIMENSION_IDX))
323 #define CHARSET_CHARS(charset) \
324 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_CHARS_IDX))
325 #define CHARSET_WIDTH(charset) \
326 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_WIDTH_IDX))
327 #define CHARSET_DIRECTION(charset) \
328 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIRECTION_IDX))
329 #define CHARSET_LEADING_CODE_BASE(charset) \
330 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_BASE_IDX))
331 #define CHARSET_LEADING_CODE_EXT(charset) \
332 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_EXT_IDX))
333 #define CHARSET_ISO_FINAL_CHAR(charset) \
334 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_FINAL_CHAR_IDX))
335 #define CHARSET_ISO_GRAPHIC_PLANE(charset) \
336 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_GRAPHIC_PLANE_IDX))
337 #define CHARSET_REVERSE_CHARSET(charset) \
338 XINT (CHARSET_TABLE_INFO (charset, CHARSET_REVERSE_CHARSET_IDX))
340 /* Macros to specify direction of a charset. */
341 #define CHARSET_DIRECTION_LEFT_TO_RIGHT 0
342 #define CHARSET_DIRECTION_RIGHT_TO_LEFT 1
344 /* A vector of charset symbol indexed by charset-id. This is used
345 only for returning charset symbol from C functions. */
346 extern Lisp_Object Vcharset_symbol_table;
348 /* Return symbol of CHARSET. */
349 #define CHARSET_SYMBOL(charset) \
350 XVECTOR (Vcharset_symbol_table)->contents[charset]
352 /* 1 if CHARSET is in valid value range, else 0. */
353 #define CHARSET_VALID_P(charset) \
354 ((charset) == 0 \
355 || ((charset) > 0x80 && (charset) <= MAX_CHARSET_OFFICIAL_DIMENSION2) \
356 || ((charset) >= MIN_CHARSET_PRIVATE_DIMENSION1 \
357 && (charset) <= MAX_CHARSET) \
358 || ((charset) == CHARSET_8_BIT_CONTROL) \
359 || ((charset) == CHARSET_8_BIT_GRAPHIC))
361 /* 1 if CHARSET is already defined, else 0. */
362 #define CHARSET_DEFINED_P(charset) \
363 (((charset) >= 0) && ((charset) <= MAX_CHARSET) \
364 && !NILP (CHARSET_TABLE_ENTRY (charset)))
366 /* Since the information CHARSET-BYTES and CHARSET-WIDTH of
367 Vcharset_table can be retrieved only by the first byte of
368 multi-byte form (an ASCII code or a base leading-code), we provide
369 here tables to be used by macros BYTES_BY_CHAR_HEAD and
370 WIDTH_BY_CHAR_HEAD for faster information retrieval. */
371 extern int bytes_by_char_head[256];
372 extern int width_by_char_head[256];
374 #define BYTES_BY_CHAR_HEAD(char_head) \
375 (ASCII_BYTE_P (char_head) ? 1 : bytes_by_char_head[char_head])
376 #define WIDTH_BY_CHAR_HEAD(char_head) \
377 (ASCII_BYTE_P (char_head) ? 1 : width_by_char_head[char_head])
379 /* Charset of the character C. */
380 #define CHAR_CHARSET(c) \
381 (SINGLE_BYTE_CHAR_P (c) \
382 ? (ASCII_BYTE_P (c) \
383 ? CHARSET_ASCII \
384 : (c) < 0xA0 ? CHARSET_8_BIT_CONTROL : CHARSET_8_BIT_GRAPHIC) \
385 : ((c) < MIN_CHAR_OFFICIAL_DIMENSION2 \
386 ? CHAR_FIELD2 (c) + 0x70 \
387 : ((c) < MIN_CHAR_PRIVATE_DIMENSION2 \
388 ? CHAR_FIELD1 (c) + 0x8F \
389 : CHAR_FIELD1 (c) + 0xE0)))
391 /* Check if two characters C1 and C2 belong to the same charset. */
392 #define SAME_CHARSET_P(c1, c2) \
393 (c1 < MIN_CHAR_OFFICIAL_DIMENSION2 \
394 ? (c1 & CHAR_FIELD2_MASK) == (c2 & CHAR_FIELD2_MASK) \
395 : (c1 & CHAR_FIELD1_MASK) == (c2 & CHAR_FIELD1_MASK))
397 /* Return a character of which charset is CHARSET and position-codes
398 are C1 and C2. DIMENSION1 character ignores C2. */
399 #define MAKE_CHAR(charset, c1, c2) \
400 ((charset) == CHARSET_ASCII \
401 ? (c1) & 0x7F \
402 : (((charset) == CHARSET_8_BIT_CONTROL \
403 || (charset) == CHARSET_8_BIT_GRAPHIC) \
404 ? ((c1) & 0x7F) | 0x80 \
405 : ((CHARSET_DEFINED_P (charset) \
406 ? CHARSET_DIMENSION (charset) == 1 \
407 : (charset) < MIN_CHARSET_PRIVATE_DIMENSION2) \
408 ? (((charset) - 0x70) << 7) | ((c1) <= 0 ? 0 : ((c1) & 0x7F)) \
409 : ((((charset) \
410 - ((charset) < MIN_CHARSET_PRIVATE_DIMENSION2 ? 0x8F : 0xE0)) \
411 << 14) \
412 | ((c2) <= 0 ? 0 : ((c2) & 0x7F)) \
413 | ((c1) <= 0 ? 0 : (((c1) & 0x7F) << 7))))))
416 /* If GENERICP is nonzero, return nonzero if C is a valid normal or
417 generic character. If GENERICP is zero, return nonzero if C is a
418 valid normal character. */
419 #define CHAR_VALID_P(c, genericp) \
420 ((c) >= 0 \
421 && (SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, genericp)))
423 /* This default value is used when nonascii-translation-table or
424 nonascii-insert-offset fail to convert unibyte character to a valid
425 multibyte character. This makes a Latin-1 character. */
427 #define DEFAULT_NONASCII_INSERT_OFFSET 0x800
429 /* Parse multibyte string STR of length LENGTH and set BYTES to the
430 byte length of a character at STR. */
432 #ifdef BYTE_COMBINING_DEBUG
434 #define PARSE_MULTIBYTE_SEQ(str, length, bytes) \
435 do { \
436 int i = 1; \
437 while (i < (length) && ! CHAR_HEAD_P ((str)[i])) i++; \
438 (bytes) = BYTES_BY_CHAR_HEAD ((str)[0]); \
439 if ((bytes) > i) \
440 abort (); \
441 } while (0)
443 #else /* not BYTE_COMBINING_DEBUG */
445 #define PARSE_MULTIBYTE_SEQ(str, length, bytes) \
446 ((void)(length), (bytes) = BYTES_BY_CHAR_HEAD ((str)[0]))
448 #endif /* not BYTE_COMBINING_DEBUG */
450 #define VALID_LEADING_CODE_P(code) \
451 (! NILP (CHARSET_TABLE_ENTRY (code)))
453 /* Return 1 if the byte sequence at unibyte string STR (LENGTH bytes)
454 is valid as a multibyte form. If valid, by a side effect, BYTES is
455 set to the byte length of the multibyte form. */
457 #define UNIBYTE_STR_AS_MULTIBYTE_P(str, length, bytes) \
458 (((str)[0] < 0x80 || (str)[0] >= 0xA0) \
459 ? ((bytes) = 1) \
460 : (((bytes) = BYTES_BY_CHAR_HEAD ((str)[0])), \
461 ((bytes) <= (length) \
462 && !CHAR_HEAD_P ((str)[1]) \
463 && ((bytes) == 2 \
464 ? (str)[0] != LEADING_CODE_8_BIT_CONTROL \
465 : (!CHAR_HEAD_P ((str)[2]) \
466 && ((bytes) == 3 \
467 ? (((str)[0] != LEADING_CODE_PRIVATE_11 \
468 && (str)[0] != LEADING_CODE_PRIVATE_12) \
469 || VALID_LEADING_CODE_P (str[1])) \
470 : (!CHAR_HEAD_P ((str)[3]) \
471 && VALID_LEADING_CODE_P (str[1]))))))))
474 /* Return 1 if the byte sequence at multibyte string STR is valid as
475 a unibyte form. By a side effect, BYTES is set to the byte length
476 of one character at STR. */
478 #define MULTIBYTE_STR_AS_UNIBYTE_P(str, bytes) \
479 ((bytes) = BYTES_BY_CHAR_HEAD ((str)[0]), \
480 (str)[0] != LEADING_CODE_8_BIT_CONTROL)
482 /* The charset of character C is stored in CHARSET, and the
483 position-codes of C are stored in C1 and C2.
484 We store -1 in C2 if the dimension of the charset is 1. */
486 #define SPLIT_CHAR(c, charset, c1, c2) \
487 (SINGLE_BYTE_CHAR_P (c) \
488 ? ((charset \
489 = (ASCII_BYTE_P (c) \
490 ? CHARSET_ASCII \
491 : ((c) < 0xA0 ? CHARSET_8_BIT_CONTROL : CHARSET_8_BIT_GRAPHIC))), \
492 c1 = (c), c2 = -1) \
493 : ((c) & CHAR_FIELD1_MASK \
494 ? (charset = (CHAR_FIELD1 (c) \
495 + ((c) < MIN_CHAR_PRIVATE_DIMENSION2 ? 0x8F : 0xE0)), \
496 c1 = CHAR_FIELD2 (c), \
497 c2 = CHAR_FIELD3 (c)) \
498 : (charset = CHAR_FIELD2 (c) + 0x70, \
499 c1 = CHAR_FIELD3 (c), \
500 c2 = -1)))
502 /* Return 1 if character C has valid printable glyph. */
503 #define CHAR_PRINTABLE_P(c) (ASCII_BYTE_P (c) || char_printable_p (c))
505 /* The charset of the character at STR is stored in CHARSET, and the
506 position-codes are stored in C1 and C2.
507 We store -1 in C2 if the character is just 2 bytes. */
509 #define SPLIT_STRING(str, len, charset, c1, c2) \
510 ((BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) < 2 \
511 || BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) > len \
512 || split_string (str, len, &charset, &c1, &c2) < 0) \
513 ? c1 = *(str), charset = CHARSET_ASCII \
514 : charset)
516 /* Mapping table from ISO2022's charset (specified by DIMENSION,
517 CHARS, and FINAL_CHAR) to Emacs' charset. Should be accessed by
518 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
519 extern int iso_charset_table[2][2][128];
521 #define ISO_CHARSET_TABLE(dimension, chars, final_char) \
522 iso_charset_table[XINT (dimension) - 1][XINT (chars) > 94][XINT (final_char)]
524 #define BASE_LEADING_CODE_P(c) (BYTES_BY_CHAR_HEAD ((unsigned char) (c)) > 1)
526 /* Return how many bytes C will occupy in a multibyte buffer. */
527 #define CHAR_BYTES(c) \
528 (SINGLE_BYTE_CHAR_P (c) \
529 ? ((ASCII_BYTE_P (c) || (c) >= 0xA0) ? 1 : 2) \
530 : char_bytes (c))
532 /* The following two macros CHAR_STRING and STRING_CHAR are the main
533 entry points to convert between Emacs's two types of character
534 representations: multi-byte form and single-word form (character
535 code). */
537 /* Store multi-byte form of the character C in STR. The caller should
538 allocate at least MAX_MULTIBYTE_LENGTH bytes area at STR in
539 advance. Returns the length of the multi-byte form. If C is an
540 invalid character code, signal an error. */
542 #define CHAR_STRING(c, str) \
543 (SINGLE_BYTE_CHAR_P (c) \
544 ? ((ASCII_BYTE_P (c) || c >= 0xA0) \
545 ? (*(str) = (unsigned char)(c), 1) \
546 : (*(str) = LEADING_CODE_8_BIT_CONTROL, *((str)+ 1) = c + 0x20, 2)) \
547 : char_to_string (c, (unsigned char *) str))
549 /* Like CHAR_STRING but don't signal an error if C is invalid.
550 Value is -1 in this case. */
552 #define CHAR_STRING_NO_SIGNAL(c, str) \
553 (SINGLE_BYTE_CHAR_P (c) \
554 ? ((ASCII_BYTE_P (c) || c >= 0xA0) \
555 ? (*(str) = (unsigned char)(c), 1) \
556 : (*(str) = LEADING_CODE_8_BIT_CONTROL, *((str)+ 1) = c + 0x20, 2)) \
557 : char_to_string_1 (c, (unsigned char *) str))
559 /* Return a character code of the character of which multi-byte form
560 is at STR and the length is LEN. If STR doesn't contain valid
561 multi-byte form, only the first byte in STR is returned. */
563 #define STRING_CHAR(str, len) \
564 (BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
565 ? (unsigned char) *(str) \
566 : string_to_char (str, len, 0))
568 /* This is like STRING_CHAR but the third arg ACTUAL_LEN is set to the
569 length of the multi-byte form. Just to know the length, use
570 MULTIBYTE_FORM_LENGTH. */
572 #define STRING_CHAR_AND_LENGTH(str, len, actual_len) \
573 (BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
574 ? ((actual_len) = 1), (unsigned char) *(str) \
575 : string_to_char (str, len, &(actual_len)))
577 /* Fetch the "next" character from Lisp string STRING at byte position
578 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
580 All the args must be side-effect-free.
581 BYTEIDX and CHARIDX must be lvalues;
582 we increment them past the character fetched. */
584 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
585 if (1) \
587 CHARIDX++; \
588 if (STRING_MULTIBYTE (STRING)) \
590 const unsigned char *ptr = SDATA (STRING) + BYTEIDX; \
591 int space_left = SBYTES (STRING) - BYTEIDX; \
592 int actual_len; \
594 OUTPUT = STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
595 BYTEIDX += actual_len; \
597 else \
598 OUTPUT = SREF (STRING, BYTEIDX++); \
600 else
602 /* Like FETCH_STRING_CHAR_ADVANCE but assume STRING is multibyte. */
604 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
605 if (1) \
607 const unsigned char *fetch_string_char_ptr = SDATA (STRING) + BYTEIDX; \
608 int fetch_string_char_space_left = SBYTES (STRING) - BYTEIDX; \
609 int actual_len; \
611 OUTPUT \
612 = STRING_CHAR_AND_LENGTH (fetch_string_char_ptr, \
613 fetch_string_char_space_left, actual_len); \
615 BYTEIDX += actual_len; \
616 CHARIDX++; \
618 else
620 /* Like FETCH_STRING_CHAR_ADVANCE but fetch character from the current
621 buffer. */
623 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
624 if (1) \
626 CHARIDX++; \
627 if (!NILP (current_buffer->enable_multibyte_characters)) \
629 unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \
630 int space_left = ((CHARIDX < GPT ? GPT_BYTE : Z_BYTE) - BYTEIDX); \
631 int actual_len; \
633 OUTPUT= STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
634 BYTEIDX += actual_len; \
636 else \
638 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
639 BYTEIDX++; \
642 else
644 /* Return the length of the multi-byte form at string STR of length LEN. */
646 #define MULTIBYTE_FORM_LENGTH(str, len) \
647 (BYTES_BY_CHAR_HEAD (*(unsigned char *)(str)) == 1 \
648 ? 1 \
649 : multibyte_form_length (str, len))
651 /* If P is before LIMIT, advance P to the next character boundary. It
652 assumes that P is already at a character boundary of the sane
653 mulitbyte form whose end address is LIMIT. */
655 #define NEXT_CHAR_BOUNDARY(p, limit) \
656 do { \
657 if ((p) < (limit)) \
658 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
659 } while (0)
662 /* If P is after LIMIT, advance P to the previous character boundary. */
664 #define PREV_CHAR_BOUNDARY(p, limit) \
665 do { \
666 if ((p) > (limit)) \
668 const unsigned char *p0 = (p); \
669 const unsigned char *p_limit = max (limit, p0 - MAX_MULTIBYTE_LENGTH);\
670 do { \
671 p0--; \
672 } while (p0 >= p_limit && ! CHAR_HEAD_P (*p0)); \
673 /* If BBCH(*p0) > p-p0, it means we were not on a boundary. */ \
674 (p) = (BYTES_BY_CHAR_HEAD (*p0) >= (p) - p0) ? p0 : (p) - 1; \
676 } while (0)
678 #define AT_CHAR_BOUNDARY_P(result, p, limit) \
679 do { \
680 if (CHAR_HEAD_P (*(p)) || (p) <= limit) \
681 /* Optimization for the common case. */ \
682 (result) = 1; \
683 else \
685 const unsigned char *p_aux = (p)+1; \
686 PREV_CHAR_BOUNDARY (p_aux, limit); \
687 (result) = (p_aux == (p)); \
689 } while (0)
691 #ifdef emacs
693 /* Increase the buffer byte position POS_BYTE of the current buffer to
694 the next character boundary. This macro relies on the fact that
695 *GPT_ADDR and *Z_ADDR are always accessible and the values are
696 '\0'. No range checking of POS. */
698 #ifdef BYTE_COMBINING_DEBUG
700 #define INC_POS(pos_byte) \
701 do { \
702 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
703 if (BASE_LEADING_CODE_P (*p)) \
705 int len, bytes; \
706 len = Z_BYTE - pos_byte; \
707 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
708 pos_byte += bytes; \
710 else \
711 pos_byte++; \
712 } while (0)
714 #else /* not BYTE_COMBINING_DEBUG */
716 #define INC_POS(pos_byte) \
717 do { \
718 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
719 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
720 } while (0)
722 #endif /* not BYTE_COMBINING_DEBUG */
724 /* Decrease the buffer byte position POS_BYTE of the current buffer to
725 the previous character boundary. No range checking of POS. */
726 #define DEC_POS(pos_byte) \
727 do { \
728 unsigned char *p, *p_min; \
730 pos_byte--; \
731 if (pos_byte < GPT_BYTE) \
732 p = BEG_ADDR + pos_byte - BEG_BYTE, p_min = BEG_ADDR; \
733 else \
734 p = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE, p_min = GAP_END_ADDR;\
735 if (p > p_min && !CHAR_HEAD_P (*p)) \
737 unsigned char *pend = p--; \
738 int len, bytes; \
739 if (p_min < p - MAX_MULTIBYTE_LENGTH) \
740 p_min = p - MAX_MULTIBYTE_LENGTH; \
741 while (p > p_min && !CHAR_HEAD_P (*p)) p--; \
742 len = pend + 1 - p; \
743 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
744 if (bytes == len) \
745 pos_byte -= len - 1; \
747 } while (0)
749 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
751 #define INC_BOTH(charpos, bytepos) \
752 do \
754 (charpos)++; \
755 if (NILP (current_buffer->enable_multibyte_characters)) \
756 (bytepos)++; \
757 else \
758 INC_POS ((bytepos)); \
760 while (0)
762 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
764 #define DEC_BOTH(charpos, bytepos) \
765 do \
767 (charpos)--; \
768 if (NILP (current_buffer->enable_multibyte_characters)) \
769 (bytepos)--; \
770 else \
771 DEC_POS ((bytepos)); \
773 while (0)
775 /* Increase the buffer byte position POS_BYTE of the current buffer to
776 the next character boundary. This macro relies on the fact that
777 *GPT_ADDR and *Z_ADDR are always accessible and the values are
778 '\0'. No range checking of POS_BYTE. */
780 #ifdef BYTE_COMBINING_DEBUG
782 #define BUF_INC_POS(buf, pos_byte) \
783 do { \
784 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \
785 if (BASE_LEADING_CODE_P (*p)) \
787 int len, bytes; \
788 len = BUF_Z_BYTE (buf) - pos_byte; \
789 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
790 pos_byte += bytes; \
792 else \
793 pos_byte++; \
794 } while (0)
796 #else /* not BYTE_COMBINING_DEBUG */
798 #define BUF_INC_POS(buf, pos_byte) \
799 do { \
800 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \
801 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
802 } while (0)
804 #endif /* not BYTE_COMBINING_DEBUG */
806 /* Decrease the buffer byte position POS_BYTE of the current buffer to
807 the previous character boundary. No range checking of POS_BYTE. */
808 #define BUF_DEC_POS(buf, pos_byte) \
809 do { \
810 unsigned char *p, *p_min; \
811 pos_byte--; \
812 if (pos_byte < BUF_GPT_BYTE (buf)) \
814 p = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
815 p_min = BUF_BEG_ADDR (buf); \
817 else \
819 p = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
820 p_min = BUF_GAP_END_ADDR (buf); \
822 if (p > p_min && !CHAR_HEAD_P (*p)) \
824 unsigned char *pend = p--; \
825 int len, bytes; \
826 if (p_min < p - MAX_MULTIBYTE_LENGTH) \
827 p_min = p - MAX_MULTIBYTE_LENGTH; \
828 while (p > p_min && !CHAR_HEAD_P (*p)) p--; \
829 len = pend + 1 - p; \
830 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
831 if (bytes == len) \
832 pos_byte -= len - 1; \
834 } while (0)
836 #endif /* emacs */
838 /* This is the maximum byte length of multi-byte sequence. */
839 #define MAX_MULTIBYTE_LENGTH 4
841 extern void invalid_character P_ ((int)) NO_RETURN;
843 extern int translate_char P_ ((Lisp_Object, int, int, int, int));
844 extern int split_string P_ ((const unsigned char *, int, int *,
845 unsigned char *, unsigned char *));
846 extern int char_to_string P_ ((int, unsigned char *));
847 extern int char_to_string_1 P_ ((int, unsigned char *));
848 extern int string_to_char P_ ((const unsigned char *, int, int *));
849 extern int char_printable_p P_ ((int c));
850 extern int multibyte_form_length P_ ((const unsigned char *, int));
851 extern void parse_str_as_multibyte P_ ((const unsigned char *, int, int *,
852 int *));
853 extern int str_as_multibyte P_ ((unsigned char *, int, int, int *));
854 extern int parse_str_to_multibyte P_ ((unsigned char *, int));
855 extern int str_to_multibyte P_ ((unsigned char *, int, int));
856 extern int str_as_unibyte P_ ((unsigned char *, int));
857 extern int get_charset_id P_ ((Lisp_Object));
858 extern int find_charset_in_text P_ ((const unsigned char *, int, int, int *,
859 Lisp_Object));
860 extern int strwidth P_ ((unsigned char *, int));
861 extern int c_string_width P_ ((const unsigned char *, int, int, int *, int *));
862 extern int lisp_string_width P_ ((Lisp_Object, int, int *, int *));
863 extern int char_bytes P_ ((int));
864 extern int char_valid_p P_ ((int, int));
866 EXFUN (Funibyte_char_to_multibyte, 1);
868 extern Lisp_Object Vtranslation_table_vector;
870 /* Return a translation table of id number ID. */
871 #define GET_TRANSLATION_TABLE(id) \
872 (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)]))
874 /* A char-table for characters which may invoke auto-filling. */
875 extern Lisp_Object Vauto_fill_chars;
877 /* Copy LEN bytes from FROM to TO. This macro should be used only
878 when a caller knows that LEN is short and the obvious copy loop is
879 faster than calling bcopy which has some overhead. Copying a
880 multibyte sequence of a multibyte character is the typical case. */
882 #define BCOPY_SHORT(from, to, len) \
883 do { \
884 int i = len; \
885 const unsigned char *from_p = from; \
886 unsigned char *to_p = to; \
887 while (i--) *to_p++ = *from_p++; \
888 } while (0)
890 #endif /* EMACS_CHARSET_H */
892 /* arch-tag: 3b96db55-4961-481d-ac3e-219f46a2b3aa
893 (do not change this comment) */