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