(USE_XIM): New define.
[bpt/emacs.git] / src / charset.h
CommitLineData
f49b37c9 1/* Header for multibyte character handler.
4a2f9c6a 2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
75c8c592 3 Licensed to the Free Software Foundation.
4ed46869 4
369314dc
KH
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
4ed46869 11
369314dc
KH
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
4ed46869 16
369314dc
KH
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
4ed46869 21
aa01a892
KH
22#ifndef EMACS_CHARSET_H
23#define EMACS_CHARSET_H
4ed46869 24
6e4dc3e1
KH
25/* #define BYTE_COMBINING_DEBUG */
26
4ed46869
KH
27/*** GENERAL NOTE on CHARACTER SET (CHARSET) ***
28
29 A character set ("charset" hereafter) is a meaningful collection
30 (i.e. language, culture, functionality, etc) of characters. Emacs
31 handles multiple charsets at once. Each charset corresponds to one
f49b37c9
KH
32 of ISO charsets. Emacs identifies a charset by a unique
33 identification number, whereas ISO identifies a charset by a triplet
34 of DIMENSION, CHARS and FINAL-CHAR. So, hereafter, just saying
35 "charset" means an identification number (integer value).
4ed46869 36
f49b37c9 37 The value range of charset is 0x00, 0x81..0xFE. There are four
4ed46869
KH
38 kinds of charset depending on DIMENSION (1 or 2) and CHARS (94 or
39 96). For instance, a charset of DIMENSION2_CHARS94 contains 94x94
f49b37c9 40 characters.
4ed46869
KH
41
42 Within Emacs Lisp, a charset is treated as a symbol which has a
43 property `charset'. The property value is a vector containing
44 various information about the charset. For readability of C codes,
f49b37c9 45 we use the following convention for C variable names:
4ed46869
KH
46 charset_symbol: Emacs Lisp symbol of a charset
47 charset_id: Emacs Lisp integer of an identification number of a charset
48 charset: C integer of an identification number of a charset
49
6e4dc3e1
KH
50 Each charset (except for ascii) is assigned a base leading-code
51 (range 0x80..0x9E). In addition, a charset of greater than 0xA0
4ed46869
KH
52 (whose base leading-code is 0x9A..0x9D) is assigned an extended
53 leading-code (range 0xA0..0xFE). In this case, each base
54 leading-code specify the allowable range of extended leading-code as
55 shown in the table below. A leading-code is used to represent a
56 character in Emacs' buffer and string.
57
58 We call a charset which has extended leading-code as "private
f49b37c9 59 charset" because those are mainly for a charset which is not yet
4ed46869
KH
60 registered by ISO. On the contrary, we call a charset which does
61 not have extended leading-code as "official charset".
62
63 ---------------------------------------------------------------------------
64 charset dimension base leading-code extended leading-code
65 ---------------------------------------------------------------------------
66 0x00 official dim1 -- none -- -- none --
67 (ASCII)
68 0x01..0x7F --never used--
88b837a2
KH
69 0x80 official dim1 -- none -- -- none --
70 (eight-bit-graphic)
4ed46869
KH
71 0x81..0x8F official dim1 same as charset -- none --
72 0x90..0x99 official dim2 same as charset -- none --
6e4dc3e1
KH
73 0x9A..0x9D --never used--
74 0x9E official dim1 same as charset -- none --
75 (eight-bit-control)
88b837a2 76 0x9F --never used--
4ed46869
KH
77 0xA0..0xDF private dim1 0x9A same as charset
78 of 1-column width
79 0xE0..0xEF private dim1 0x9B same as charset
80 of 2-column width
81 0xF0..0xF4 private dim2 0x9C same as charset
82 of 1-column width
83 0xF5..0xFE private dim2 0x9D same as charset
84 of 2-column width
85 0xFF --never used--
86 ---------------------------------------------------------------------------
87
4ed46869
KH
88*/
89
90/* Definition of special leading-codes. */
4ed46869
KH
91/* Leading-code followed by extended leading-code. */
92#define LEADING_CODE_PRIVATE_11 0x9A /* for private DIMENSION1 of 1-column */
93#define LEADING_CODE_PRIVATE_12 0x9B /* for private DIMENSION1 of 2-column */
94#define LEADING_CODE_PRIVATE_21 0x9C /* for private DIMENSION2 of 1-column */
5c6257e5 95#define LEADING_CODE_PRIVATE_22 0x9D /* for private DIMENSION2 of 2-column */
4ed46869 96
6e4dc3e1
KH
97#define LEADING_CODE_8_BIT_CONTROL 0x9E /* for `eight-bit-control' */
98
4ed46869
KH
99/* Extended leading-code. */
100/* Start of each extended leading-codes. */
101#define LEADING_CODE_EXT_11 0xA0 /* follows LEADING_CODE_PRIVATE_11 */
102#define LEADING_CODE_EXT_12 0xE0 /* follows LEADING_CODE_PRIVATE_12 */
103#define LEADING_CODE_EXT_21 0xF0 /* follows LEADING_CODE_PRIVATE_21 */
104#define LEADING_CODE_EXT_22 0xF5 /* follows LEADING_CODE_PRIVATE_22 */
105/* Maximum value of extended leading-codes. */
106#define LEADING_CODE_EXT_MAX 0xFE
107
108/* Definition of minimum/maximum charset of each DIMENSION. */
109#define MIN_CHARSET_OFFICIAL_DIMENSION1 0x81
110#define MAX_CHARSET_OFFICIAL_DIMENSION1 0x8F
111#define MIN_CHARSET_OFFICIAL_DIMENSION2 0x90
112#define MAX_CHARSET_OFFICIAL_DIMENSION2 0x99
113#define MIN_CHARSET_PRIVATE_DIMENSION1 LEADING_CODE_EXT_11
114#define MIN_CHARSET_PRIVATE_DIMENSION2 LEADING_CODE_EXT_21
115
03e66c53
KH
116/* Maximum value of overall charset identification number. */
117#define MAX_CHARSET 0xFE
118
4ed46869 119/* Definition of special charsets. */
6e4dc3e1
KH
120#define CHARSET_ASCII 0 /* 0x00..0x7F */
121#define CHARSET_8_BIT_CONTROL 0x9E /* 0x80..0x9F */
88b837a2 122#define CHARSET_8_BIT_GRAPHIC 0x80 /* 0xA0..0xFF */
4ed46869 123
4ed46869
KH
124extern int charset_latin_iso8859_1; /* ISO8859-1 (Latin-1) */
125extern int charset_jisx0208_1978; /* JISX0208.1978 (Japanese Kanji old set) */
126extern int charset_jisx0208; /* JISX0208.1983 (Japanese Kanji) */
127extern int charset_katakana_jisx0201; /* JISX0201.Kana (Japanese Katakana) */
128extern int charset_latin_jisx0201; /* JISX0201.Roman (Japanese Roman) */
129extern int charset_big5_1; /* Big5 Level 1 (Chinese Traditional) */
130extern int charset_big5_2; /* Big5 Level 2 (Chinese Traditional) */
131
6e4dc3e1
KH
132/* Check if CH is an ASCII character or a base leading-code.
133 Nowadays, any byte can be the first byte of a character in a
134 multibyte buffer/string. So this macro name is not appropriate. */
c399b461 135#define CHAR_HEAD_P(ch) ((unsigned char) (ch) < 0xA0)
4ed46869
KH
136
137/*** GENERAL NOTE on CHARACTER REPRESENTATION ***
138
139 At first, the term "character" or "char" is used for a multilingual
140 character (of course, including ASCII character), not for a byte in
141 computer memory. We use the term "code" or "byte" for the latter
142 case.
143
144 A character is identified by charset and one or two POSITION-CODEs.
145 POSITION-CODE is the position of the character in the charset. A
146 character of DIMENSION1 charset has one POSITION-CODE: POSITION-CODE-1.
147 A character of DIMENSION2 charset has two POSITION-CODE:
148 POSITION-CODE-1 and POSITION-CODE-2. The code range of
149 POSITION-CODE is 0x20..0x7F.
150
151 Emacs has two kinds of representation of a character: multi-byte
152 form (for buffer and string) and single-word form (for character
153 object in Emacs Lisp). The latter is called "character code" here
94487c4e 154 after. Both representations encode the information of charset and
4ed46869
KH
155 POSITION-CODE but in a different way (for instance, MSB of
156 POSITION-CODE is set in multi-byte form).
157
158 For details of multi-byte form, see the section "2. Emacs internal
159 format handlers" of `coding.c'.
160
161 Emacs uses 19 bits for a character code. The bits are divided into
162 3 fields: FIELD1(5bits):FIELD2(7bits):FIELD3(7bits).
163
164 A character code of DIMENSION1 character uses FIELD2 to hold charset
165 and FIELD3 to hold POSITION-CODE-1. A character code of DIMENSION2
166 character uses FIELD1 to hold charset, FIELD2 and FIELD3 to hold
167 POSITION-CODE-1 and POSITION-CODE-2 respectively.
168
169 More precisely...
170
6e4dc3e1
KH
171 FIELD2 of DIMENSION1 character (except for ascii, eight-bit-control,
172 and eight-bit-graphic) is "charset - 0x70". This is to make all
173 character codes except for ASCII and 8-bit codes greater than 256.
174 So, the range of FIELD2 of DIMENSION1 character is 0, 1, or
175 0x11..0x7F.
4ed46869
KH
176
177 FIELD1 of DIMENSION2 character is "charset - 0x8F" for official
178 charset and "charset - 0xE0" for private charset. So, the range of
179 FIELD1 of DIMENSION2 character is 0x01..0x1E.
180
6e4dc3e1
KH
181 -----------------------------------------------------------------------------
182 charset FIELD1 (5-bit) FIELD2 (7-bit) FIELD3 (7-bit)
183 -----------------------------------------------------------------------------
184 ascii 0 0 0x00..0x7F
185 eight-bit-control 0 1 0x00..0x1F
186 eight-bit-graphic 0 1 0x20..0x7F
187 DIMENSION1 0 charset - 0x70 POSITION-CODE-1
188 DIMENSION2(o) charset - 0x8F POSITION-CODE-1 POSITION-CODE-2
189 DIMENSION2(p) charset - 0xE0 POSITION-CODE-1 POSITION-CODE-2
190 -----------------------------------------------------------------------------
4ed46869 191 "(o)": official, "(p)": private
6e4dc3e1 192 -----------------------------------------------------------------------------
4ed46869
KH
193*/
194
4ed46869
KH
195/* Masks of each field of character code. */
196#define CHAR_FIELD1_MASK (0x1F << 14)
197#define CHAR_FIELD2_MASK (0x7F << 7)
198#define CHAR_FIELD3_MASK 0x7F
199
200/* Macros to access each field of character C. */
201#define CHAR_FIELD1(c) (((c) & CHAR_FIELD1_MASK) >> 14)
202#define CHAR_FIELD2(c) (((c) & CHAR_FIELD2_MASK) >> 7)
203#define CHAR_FIELD3(c) ((c) & CHAR_FIELD3_MASK)
204
205/* Minimum character code of character of each DIMENSION. */
206#define MIN_CHAR_OFFICIAL_DIMENSION1 \
207 ((MIN_CHARSET_OFFICIAL_DIMENSION1 - 0x70) << 7)
208#define MIN_CHAR_PRIVATE_DIMENSION1 \
209 ((MIN_CHARSET_PRIVATE_DIMENSION1 - 0x70) << 7)
210#define MIN_CHAR_OFFICIAL_DIMENSION2 \
211 ((MIN_CHARSET_OFFICIAL_DIMENSION2 - 0x8F) << 14)
212#define MIN_CHAR_PRIVATE_DIMENSION2 \
213 ((MIN_CHARSET_PRIVATE_DIMENSION2 - 0xE0) << 14)
f49b37c9
KH
214/* Maximum character code currently used plus 1. */
215#define MAX_CHAR (0x1F << 14)
4ed46869 216
6e4dc3e1
KH
217/* 1 if C is a single byte character, else 0. */
218#define SINGLE_BYTE_CHAR_P(c) ((unsigned) (c) < 0x100)
4ed46869 219
6e4dc3e1 220/* 1 if BYTE is an ASCII character in itself, in multibyte mode. */
7614f779
KH
221#define ASCII_BYTE_P(byte) ((byte) < 0x80)
222
4ed46869
KH
223/* A char-table containing information of each character set.
224
225 Unlike ordinary char-tables, this doesn't contain any nested table.
226 Only the top level elements are used. Each element is a vector of
227 the following information:
228 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
229 LEADING-CODE-BASE, LEADING-CODE-EXT,
230 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
231 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
232 PLIST.
233
234 CHARSET-ID (integer) is the identification number of the charset.
235
0c01c27e 236 BYTES (integer) is the length of multi-byte form of a character in
4ed46869
KH
237 the charset: one of 1, 2, 3, and 4.
238
239 DIMENSION (integer) is the number of bytes to represent a character: 1 or 2.
240
241 CHARS (integer) is the number of characters in a dimension: 94 or 96.
242
243 WIDTH (integer) is the number of columns a character in the charset
6e4dc3e1 244 occupies on the screen: one of 0, 1, and 2..
4ed46869
KH
245
246 DIRECTION (integer) is the rendering direction of characters in the
28d0c844
KH
247 charset when rendering. If 0, render from left to right, else
248 render from right to left.
4ed46869
KH
249
250 LEADING-CODE-BASE (integer) is the base leading-code for the
251 charset.
252
253 LEADING-CODE-EXT (integer) is the extended leading-code for the
254 charset. All charsets of less than 0xA0 has the value 0.
255
256 ISO-FINAL-CHAR (character) is the final character of the
6e4dc3e1
KH
257 corresponding ISO 2022 charset. It is -1 for such a character
258 that is used only internally (e.g. `eight-bit-control').
4ed46869
KH
259
260 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
261 while encoding to variants of ISO 2022 coding system, one of the
6e4dc3e1
KH
262 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR). It
263 is -1 for such a character that is used only internally
264 (e.g. `eight-bit-control').
4ed46869
KH
265
266 REVERSE-CHARSET (integer) is the charset which differs only in
267 LEFT-TO-RIGHT value from the charset. If there's no such a
268 charset, the value is -1.
269
270 SHORT-NAME (string) is the short name to refer to the charset.
271
272 LONG-NAME (string) is the long name to refer to the charset.
273
274 DESCRIPTION (string) is the description string of the charset.
275
276 PLIST (property list) may contain any type of information a user
277 want to put and get by functions `put-charset-property' and
278 `get-charset-property' respectively. */
279extern Lisp_Object Vcharset_table;
280
281/* Macros to access various information of CHARSET in Vcharset_table.
282 We provide these macros for efficiency. No range check of CHARSET. */
283
a3f4e3d9 284/* Return entry of CHARSET (C integer) in Vcharset_table. */
d6f92c04
KH
285#define CHARSET_TABLE_ENTRY(charset) \
286 XCHAR_TABLE (Vcharset_table)->contents[((charset) == CHARSET_ASCII \
287 ? 0 : (charset) + 128)]
4ed46869
KH
288
289/* Return information INFO-IDX of CHARSET. */
290#define CHARSET_TABLE_INFO(charset, info_idx) \
291 XVECTOR (CHARSET_TABLE_ENTRY (charset))->contents[info_idx]
292
293#define CHARSET_ID_IDX (0)
294#define CHARSET_BYTES_IDX (1)
295#define CHARSET_DIMENSION_IDX (2)
296#define CHARSET_CHARS_IDX (3)
297#define CHARSET_WIDTH_IDX (4)
298#define CHARSET_DIRECTION_IDX (5)
299#define CHARSET_LEADING_CODE_BASE_IDX (6)
300#define CHARSET_LEADING_CODE_EXT_IDX (7)
301#define CHARSET_ISO_FINAL_CHAR_IDX (8)
302#define CHARSET_ISO_GRAPHIC_PLANE_IDX (9)
303#define CHARSET_REVERSE_CHARSET_IDX (10)
304#define CHARSET_SHORT_NAME_IDX (11)
305#define CHARSET_LONG_NAME_IDX (12)
306#define CHARSET_DESCRIPTION_IDX (13)
307#define CHARSET_PLIST_IDX (14)
308/* Size of a vector of each entry of Vcharset_table. */
309#define CHARSET_MAX_IDX (15)
310
311/* And several more macros to be used frequently. */
312#define CHARSET_BYTES(charset) \
313 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_BYTES_IDX))
314#define CHARSET_DIMENSION(charset) \
315 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIMENSION_IDX))
316#define CHARSET_CHARS(charset) \
317 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_CHARS_IDX))
318#define CHARSET_WIDTH(charset) \
319 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_WIDTH_IDX))
320#define CHARSET_DIRECTION(charset) \
321 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIRECTION_IDX))
322#define CHARSET_LEADING_CODE_BASE(charset) \
323 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_BASE_IDX))
324#define CHARSET_LEADING_CODE_EXT(charset) \
325 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_EXT_IDX))
326#define CHARSET_ISO_FINAL_CHAR(charset) \
6e4dc3e1 327 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_FINAL_CHAR_IDX))
4ed46869 328#define CHARSET_ISO_GRAPHIC_PLANE(charset) \
6e4dc3e1 329 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_GRAPHIC_PLANE_IDX))
4ed46869
KH
330#define CHARSET_REVERSE_CHARSET(charset) \
331 XINT (CHARSET_TABLE_INFO (charset, CHARSET_REVERSE_CHARSET_IDX))
332
333/* Macros to specify direction of a charset. */
334#define CHARSET_DIRECTION_LEFT_TO_RIGHT 0
335#define CHARSET_DIRECTION_RIGHT_TO_LEFT 1
336
337/* A vector of charset symbol indexed by charset-id. This is used
338 only for returning charset symbol from C functions. */
339extern Lisp_Object Vcharset_symbol_table;
340
341/* Return symbol of CHARSET. */
342#define CHARSET_SYMBOL(charset) \
343 XVECTOR (Vcharset_symbol_table)->contents[charset]
344
384107f2 345/* 1 if CHARSET is in valid value range, else 0. */
4ed46869
KH
346#define CHARSET_VALID_P(charset) \
347 ((charset) == 0 \
f49b37c9 348 || ((charset) > 0x80 && (charset) <= MAX_CHARSET_OFFICIAL_DIMENSION2) \
6e4dc3e1
KH
349 || ((charset) >= MIN_CHARSET_PRIVATE_DIMENSION1 \
350 && (charset) <= MAX_CHARSET) \
351 || ((charset) == CHARSET_8_BIT_CONTROL) \
352 || ((charset) == CHARSET_8_BIT_GRAPHIC))
4ed46869 353
f49b37c9 354/* 1 if CHARSET is already defined, else 0. */
4ed46869 355#define CHARSET_DEFINED_P(charset) \
03e66c53 356 (((charset) >= 0) && ((charset) <= MAX_CHARSET) \
4ed46869
KH
357 && !NILP (CHARSET_TABLE_ENTRY (charset)))
358
359/* Since the information CHARSET-BYTES and CHARSET-WIDTH of
6e4dc3e1 360 Vcharset_table can be retrieved only by the first byte of
4ed46869
KH
361 multi-byte form (an ASCII code or a base leading-code), we provide
362 here tables to be used by macros BYTES_BY_CHAR_HEAD and
363 WIDTH_BY_CHAR_HEAD for faster information retrieval. */
364extern int bytes_by_char_head[256];
365extern int width_by_char_head[256];
366
6e4dc3e1
KH
367#define BYTES_BY_CHAR_HEAD(char_head) \
368 (ASCII_BYTE_P (char_head) ? 1 : bytes_by_char_head[char_head])
369#define WIDTH_BY_CHAR_HEAD(char_head) \
370 (ASCII_BYTE_P (char_head) ? 1 : width_by_char_head[char_head])
4ed46869
KH
371
372/* Charset of the character C. */
6e4dc3e1
KH
373#define CHAR_CHARSET(c) \
374 (SINGLE_BYTE_CHAR_P (c) \
375 ? (ASCII_BYTE_P (c) \
376 ? CHARSET_ASCII \
377 : (c) < 0xA0 ? CHARSET_8_BIT_CONTROL : CHARSET_8_BIT_GRAPHIC) \
378 : ((c) < MIN_CHAR_OFFICIAL_DIMENSION2 \
379 ? CHAR_FIELD2 (c) + 0x70 \
380 : ((c) < MIN_CHAR_PRIVATE_DIMENSION2 \
381 ? CHAR_FIELD1 (c) + 0x8F \
f49b37c9 382 : CHAR_FIELD1 (c) + 0xE0)))
4ed46869 383
f49b37c9 384/* Check if two characters C1 and C2 belong to the same charset. */
6e4dc3e1
KH
385#define SAME_CHARSET_P(c1, c2) \
386 (c1 < MIN_CHAR_OFFICIAL_DIMENSION2 \
387 ? (c1 & CHAR_FIELD2_MASK) == (c2 & CHAR_FIELD2_MASK) \
388 : (c1 & CHAR_FIELD1_MASK) == (c2 & CHAR_FIELD1_MASK))
4ed46869
KH
389
390/* Return a character of which charset is CHARSET and position-codes
391 are C1 and C2. DIMENSION1 character ignores C2. */
6e4dc3e1
KH
392#define MAKE_CHAR(charset, c1, c2) \
393 ((charset) == CHARSET_ASCII \
394 ? (c1) & 0x7F \
395 : (((charset) == CHARSET_8_BIT_CONTROL \
396 || (charset) == CHARSET_8_BIT_GRAPHIC) \
397 ? ((c1) & 0x7F) | 0x80 \
398 : (! CHARSET_DEFINED_P (charset) || CHARSET_DIMENSION (charset) == 1 \
399 ? (((charset) - 0x70) << 7) | ((c1) <= 0 ? 0 : (c1)) \
400 : ((((charset) \
401 - ((charset) < MIN_CHARSET_PRIVATE_DIMENSION2 ? 0x8F : 0xE0)) \
402 << 14) \
403 | ((c2) <= 0 ? 0 : ((c2) & 0x7F)) \
404 | ((c1) <= 0 ? 0 : (((c1) & 0x7F) << 7))))))
405
4ed46869 406
15979e9e
KH
407/* If GENERICP is nonzero, return nonzero iff C is a valid normal or
408 generic character. If GENERICP is zero, return nonzero iff C is a
409 valid normal character. */
410#define CHAR_VALID_P(c, genericp) \
411 ((c) >= 0 \
412 && (SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, genericp)))
c6112b99 413
d74d0ca5 414/* This default value is used when nonascii-translation-table or
54e15bb9
KH
415 nonascii-insert-offset fail to convert unibyte character to a valid
416 multibyte character. This makes a Latin-1 character. */
417
418#define DEFAULT_NONASCII_INSERT_OFFSET 0x800
419
6e4dc3e1
KH
420/* Parse multibyte string STR of length LENGTH and set BYTES to the
421 byte length of a character at STR. */
422
423#ifdef BYTE_COMBINING_DEBUG
384107f2
KH
424
425#define PARSE_MULTIBYTE_SEQ(str, length, bytes) \
426 do { \
427 int i = 1; \
f49b37c9 428 while (i < (length) && ! CHAR_HEAD_P ((str)[i])) i++; \
6e4dc3e1
KH
429 (bytes) = BYTES_BY_CHAR_HEAD ((str)[0]); \
430 if ((bytes) > i) \
431 abort (); \
384107f2 432 } while (0)
54e15bb9 433
6e4dc3e1 434#else /* not BYTE_COMBINING_DEBUG */
feb453fa 435
6e4dc3e1
KH
436#define PARSE_MULTIBYTE_SEQ(str, length, bytes) \
437 (bytes) = BYTES_BY_CHAR_HEAD ((str)[0])
feb453fa 438
6e4dc3e1
KH
439#endif /* not BYTE_COMBINING_DEBUG */
440
441/* Return 1 iff the byte sequence at unibyte string STR (LENGTH bytes)
442 is valid as a multibyte form. If valid, by a side effect, BYTES is
443 set to the byte length of the multibyte form. */
444
445#define UNIBYTE_STR_AS_MULTIBYTE_P(str, length, bytes) \
88b837a2
KH
446 (((str)[0] < 0x80 || (str)[0] >= 0xA0) \
447 ? (bytes) = 1 \
448 : (((bytes) = BYTES_BY_CHAR_HEAD ((str)[0])), \
449 ((str)[0] != LEADING_CODE_8_BIT_CONTROL \
6e4dc3e1
KH
450 && (bytes) <= (length) \
451 && !CHAR_HEAD_P ((str)[1]) \
452 && ((bytes) == 2 \
453 || (!CHAR_HEAD_P ((str)[2]) \
454 && ((bytes) == 3 \
88b837a2 455 || !CHAR_HEAD_P ((str)[3])))))))
6e4dc3e1
KH
456
457/* Return 1 iff the byte sequence at multibyte string STR is valid as
458 a unibyte form. By a side effect, BYTES is set to the byte length
459 of one character at STR. */
460
461#define MULTIBYTE_STR_AS_UNIBYTE_P(str, bytes) \
462 ((bytes) = BYTES_BY_CHAR_HEAD ((str)[0]), \
463 (str)[0] != LEADING_CODE_8_BIT_CONTROL)
4ed46869 464
feb453fa
RS
465/* The charset of character C is stored in CHARSET, and the
466 position-codes of C are stored in C1 and C2.
9dd2aa1a 467 We store -1 in C2 if the dimension of the charset is 1. */
feb453fa 468
6e4dc3e1
KH
469#define SPLIT_CHAR(c, charset, c1, c2) \
470 (SINGLE_BYTE_CHAR_P (c) \
471 ? ((charset = ASCII_BYTE_P (c) \
472 ? CHARSET_ASCII \
473 : (c) < 0xA0 ? CHARSET_8_BIT_CONTROL : CHARSET_8_BIT_GRAPHIC), \
474 c1 = (c), c2 = -1) \
475 : ((c) & CHAR_FIELD1_MASK \
476 ? (charset = (CHAR_FIELD1 (c) \
477 + ((c) < MIN_CHAR_PRIVATE_DIMENSION2 ? 0x8F : 0xE0)), \
478 c1 = CHAR_FIELD2 (c), \
479 c2 = CHAR_FIELD3 (c)) \
480 : (charset = CHAR_FIELD2 (c) + 0x70, \
481 c1 = CHAR_FIELD3 (c), \
482 c2 = -1)))
4ed46869 483
384107f2 484/* Return 1 iff character C has valid printable glyph. */
6e4dc3e1 485#define CHAR_PRINTABLE_P(c) (ASCII_BYTE_P (c) || char_printable_p (c))
384107f2 486
feb453fa
RS
487/* The charset of the character at STR is stored in CHARSET, and the
488 position-codes are stored in C1 and C2.
f49b37c9 489 We store -1 in C2 if the character is just 2 bytes. */
feb453fa 490
f49b37c9
KH
491#define SPLIT_STRING(str, len, charset, c1, c2) \
492 ((BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) < 2 \
493 || BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) > len \
494 || split_string (str, len, &charset, &c1, &c2) < 0) \
495 ? c1 = *(str), charset = CHARSET_ASCII \
4ed46869
KH
496 : charset)
497
4ed46869
KH
498/* Mapping table from ISO2022's charset (specified by DIMENSION,
499 CHARS, and FINAL_CHAR) to Emacs' charset. Should be accessed by
500 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
501extern int iso_charset_table[2][2][128];
502
503#define ISO_CHARSET_TABLE(dimension, chars, final_char) \
504 iso_charset_table[XINT (dimension) - 1][XINT (chars) > 94][XINT (final_char)]
505
506#define BASE_LEADING_CODE_P(c) (BYTES_BY_CHAR_HEAD ((unsigned char) (c)) > 1)
507
3124bc0e 508/* Return how many bytes C will occupy in a multibyte buffer. */
6e4dc3e1
KH
509#define CHAR_BYTES(c) \
510 (SINGLE_BYTE_CHAR_P (c) \
511 ? ((ASCII_BYTE_P (c) || (c) >= 0xA0) ? 1 : 2) \
512 : char_bytes (c))
3124bc0e 513
4ed46869
KH
514/* The following two macros CHAR_STRING and STRING_CHAR are the main
515 entry points to convert between Emacs two types of character
516 representations: multi-byte form and single-word form (character
517 code). */
518
f49b37c9 519/* Store multi-byte form of the character C in STR. The caller should
6e4dc3e1
KH
520 allocate at least MAX_MULTIBYTE_LENGTH bytes area at STR in
521 advance. Returns the length of the multi-byte form. If C is an
522 invalid character code, signal an error. */
4ed46869 523
f49b37c9 524#define CHAR_STRING(c, str) \
6e4dc3e1
KH
525 (ASCII_BYTE_P (c) \
526 ? (*(str) = (unsigned char)(c), 1) \
527 : char_to_string (c, (unsigned char *) str))
4ed46869
KH
528
529/* Return a character code of the character of which multi-byte form
530 is at STR and the length is LEN. If STR doesn't contain valid
531 multi-byte form, only the first byte in STR is returned. */
532
dfd57aa7
KH
533#define STRING_CHAR(str, len) \
534 (BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
535 ? (unsigned char) *(str) \
f49b37c9 536 : string_to_char (str, len, 0))
4ed46869 537
384107f2
KH
538/* This is like STRING_CHAR but the third arg ACTUAL_LEN is set to the
539 length of the multi-byte form. Just to know the length, use
4ed46869
KH
540 MULTIBYTE_FORM_LENGTH. */
541
384107f2
KH
542#define STRING_CHAR_AND_LENGTH(str, len, actual_len) \
543 (BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
544 ? ((actual_len) = 1), (unsigned char) *(str) \
f49b37c9 545 : string_to_char (str, len, &(actual_len)))
d74d0ca5 546
6e4dc3e1
KH
547/* Fetch the "next" character from Lisp string STRING at byte position
548 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
520afed1
RS
549
550 All the args must be side-effect-free.
551 BYTEIDX and CHARIDX must be lvalues;
552 we increment them past the character fetched. */
553
6e4dc3e1
KH
554#define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
555if (1) \
556 { \
557 CHARIDX++; \
558 if (STRING_MULTIBYTE (STRING)) \
559 { \
560 unsigned char *ptr = &XSTRING (STRING)->data[BYTEIDX]; \
561 int space_left = XSTRING (STRING)->size_byte - BYTEIDX; \
562 int actual_len; \
563 \
564 OUTPUT = STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
565 BYTEIDX += actual_len; \
566 } \
567 else \
568 OUTPUT = XSTRING (STRING)->data[BYTEIDX++]; \
569 } \
570else
571
572/* Like FETCH_STRING_CHAR_ADVANCE but assume STRING is multibyte. */
573
574#define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
520afed1
RS
575if (1) \
576 { \
577 unsigned char *fetch_string_char_ptr = &XSTRING (STRING)->data[BYTEIDX]; \
578 int fetch_string_char_space_left = XSTRING (STRING)->size_byte - BYTEIDX; \
579 int actual_len; \
580 \
581 OUTPUT \
582 = STRING_CHAR_AND_LENGTH (fetch_string_char_ptr, \
583 fetch_string_char_space_left, actual_len); \
584 \
585 BYTEIDX += actual_len; \
586 CHARIDX++; \
587 } \
588else
589
6e4dc3e1
KH
590/* Like FETCH_STRING_CHAR_ADVANCE but fetch character from the current
591 buffer. */
f49b37c9
KH
592
593#define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
594if (1) \
595 { \
f49b37c9 596 CHARIDX++; \
6e4dc3e1
KH
597 if (!NILP (current_buffer->enable_multibyte_characters)) \
598 { \
599 unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \
600 int space_left = ((CHARIDX < GPT ? GPT_BYTE : Z_BYTE) - BYTEIDX); \
601 int actual_len; \
602 \
603 OUTPUT= STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
604 BYTEIDX += actual_len; \
605 } \
606 else \
607 { \
608 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
609 BYTEIDX++; \
610 } \
f49b37c9
KH
611 } \
612else
613
4ed46869
KH
614/* Return the length of the multi-byte form at string STR of length LEN. */
615
d121936f
KH
616#define MULTIBYTE_FORM_LENGTH(str, len) \
617 (BYTES_BY_CHAR_HEAD (*(unsigned char *)(str)) == 1 \
618 ? 1 \
4ed46869
KH
619 : multibyte_form_length (str, len))
620
4ed46869
KH
621#ifdef emacs
622
384107f2
KH
623/* Increase the buffer byte position POS_BYTE of the current buffer to
624 the next character boundary. This macro relies on the fact that
625 *GPT_ADDR and *Z_ADDR are always accessible and the values are
626 '\0'. No range checking of POS. */
6e4dc3e1
KH
627
628#ifdef BYTE_COMBINING_DEBUG
629
384107f2
KH
630#define INC_POS(pos_byte) \
631 do { \
632 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
633 if (BASE_LEADING_CODE_P (*p)) \
634 { \
635 int len, bytes; \
636 len = Z_BYTE - pos_byte; \
637 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
638 pos_byte += bytes; \
639 } \
640 else \
641 pos_byte++; \
4ed46869
KH
642 } while (0)
643
6e4dc3e1
KH
644#else /* not BYTE_COMBINING_DEBUG */
645
646#define INC_POS(pos_byte) \
647 do { \
648 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
649 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
650 } while (0)
651
652#endif /* not BYTE_COMBINING_DEBUG */
653
384107f2
KH
654/* Decrease the buffer byte position POS_BYTE of the current buffer to
655 the previous character boundary. No range checking of POS. */
656#define DEC_POS(pos_byte) \
657 do { \
658 unsigned char *p, *p_min; \
659 \
660 pos_byte--; \
661 if (pos_byte < GPT_BYTE) \
662 p = BEG_ADDR + pos_byte - 1, p_min = BEG_ADDR; \
663 else \
664 p = BEG_ADDR + GAP_SIZE + pos_byte - 1, p_min = GAP_END_ADDR; \
665 if (p > p_min && !CHAR_HEAD_P (*p)) \
666 { \
667 unsigned char *pend = p--; \
668 int len, bytes; \
669 while (p > p_min && !CHAR_HEAD_P (*p)) p--; \
670 len = pend + 1 - p; \
671 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
672 if (bytes == len) \
673 pos_byte -= len - 1; \
674 } \
c399b461
RS
675 } while (0)
676
677/* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
678
d619ee06
RS
679#define INC_BOTH(charpos, bytepos) \
680do \
681 { \
682 (charpos)++; \
683 if (NILP (current_buffer->enable_multibyte_characters)) \
684 (bytepos)++; \
685 else \
686 INC_POS ((bytepos)); \
687 } \
c399b461
RS
688while (0)
689
690/* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
691
d619ee06
RS
692#define DEC_BOTH(charpos, bytepos) \
693do \
694 { \
695 (charpos)--; \
696 if (NILP (current_buffer->enable_multibyte_characters)) \
697 (bytepos)--; \
698 else \
699 DEC_POS ((bytepos)); \
700 } \
c399b461
RS
701while (0)
702
384107f2
KH
703/* Increase the buffer byte position POS_BYTE of the current buffer to
704 the next character boundary. This macro relies on the fact that
705 *GPT_ADDR and *Z_ADDR are always accessible and the values are
706 '\0'. No range checking of POS_BYTE. */
6e4dc3e1
KH
707
708#ifdef BYTE_COMBINING_DEBUG
709
384107f2
KH
710#define BUF_INC_POS(buf, pos_byte) \
711 do { \
712 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \
713 if (BASE_LEADING_CODE_P (*p)) \
bb5999ae 714 { \
384107f2
KH
715 int len, bytes; \
716 len = BUF_Z_BYTE (buf) - pos_byte; \
717 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
718 pos_byte += bytes; \
bb5999ae 719 } \
384107f2
KH
720 else \
721 pos_byte++; \
722 } while (0)
723
6e4dc3e1
KH
724#else /* not BYTE_COMBINING_DEBUG */
725
726#define BUF_INC_POS(buf, pos_byte) \
727 do { \
728 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos_byte); \
729 pos_byte += BYTES_BY_CHAR_HEAD (*p); \
730 } while (0)
731
732#endif /* not BYTE_COMBINING_DEBUG */
733
384107f2
KH
734/* Decrease the buffer byte position POS_BYTE of the current buffer to
735 the previous character boundary. No range checking of POS_BYTE. */
736#define BUF_DEC_POS(buf, pos_byte) \
737 do { \
738 unsigned char *p, *p_min; \
739 pos_byte--; \
740 if (pos_byte < BUF_GPT_BYTE (buf)) \
741 { \
742 p = BUF_BEG_ADDR (buf) + pos_byte - 1; \
743 p_min = BUF_BEG_ADDR (buf); \
744 } \
745 else \
746 { \
747 p = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - 1; \
748 p_min = BUF_GAP_END_ADDR (buf); \
749 } \
750 if (p > p_min && !CHAR_HEAD_P (*p)) \
751 { \
752 unsigned char *pend = p--; \
753 int len, bytes; \
754 while (p > p_min && !CHAR_HEAD_P (*p)) p--; \
755 len = pend + 1 - p; \
756 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
757 if (bytes == len) \
758 pos_byte -= len - 1; \
759 } \
4ed46869
KH
760 } while (0)
761
762#endif /* emacs */
763
f49b37c9
KH
764/* This is the maximum byte length of multi-byte sequence. */
765#define MAX_MULTIBYTE_LENGTH 4
4ed46869 766
bb5999ae
KH
767extern void invalid_character P_ ((int));
768
62c6686f 769extern int translate_char P_ ((Lisp_Object, int, int, int, int));
f49b37c9 770extern int split_string P_ ((const unsigned char *, int, int *,
c04809fb 771 unsigned char *, unsigned char *));
f49b37c9
KH
772extern int char_to_string P_ ((int, unsigned char *));
773extern int string_to_char P_ ((const unsigned char *, int, int *));
384107f2 774extern int char_printable_p P_ ((int c));
8867de67 775extern int multibyte_form_length P_ ((const unsigned char *, int));
6e4dc3e1
KH
776extern void parse_str_as_multibyte P_ ((unsigned char *, int, int *, int *));
777extern int str_as_multibyte P_ ((unsigned char *, int, int, int *));
778extern int str_to_multibyte P_ ((unsigned char *, int, int));
779extern int str_as_unibyte P_ ((unsigned char *, int));
c04809fb 780extern int get_charset_id P_ ((Lisp_Object));
6e4dc3e1
KH
781extern int find_charset_in_text P_ ((unsigned char *, int, int, int *,
782 Lisp_Object));
c04809fb 783extern int strwidth P_ ((unsigned char *, int));
3124bc0e 784extern int char_bytes P_ ((int));
c2d1e590 785extern int char_valid_p P_ ((int, int));
2db95897 786
d74d0ca5 787extern Lisp_Object Vtranslation_table_vector;
62c6686f 788
d74d0ca5 789/* Return a translation table of id number ID. */
62c6686f 790#define GET_TRANSLATION_TABLE(id) \
e35efe89 791 (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)]))
c1f6608b 792
69535a4b
KH
793/* A char-table for characters which may invoke auto-filling. */
794extern Lisp_Object Vauto_fill_chars;
795
c1f6608b
KH
796/* Copy LEN bytes from FROM to TO. This macro should be used only
797 when a caller knows that LEN is short and the obvious copy loop is
6e4dc3e1
KH
798 faster than calling bcopy which has some overhead. Copying a
799 multibyte sequence of a multibyte character is the typical case. */
c1f6608b
KH
800
801#define BCOPY_SHORT(from, to, len) \
802 do { \
803 int i = len; \
ae53401a 804 unsigned char *from_p = from, *to_p = to; \
6e4dc3e1 805 while (i--) *to_p++ = *from_p++; \
c1f6608b
KH
806 } while (0)
807
aa01a892 808#endif /* EMACS_CHARSET_H */