Replace `iff' in comments.
[bpt/emacs.git] / src / charset.h
CommitLineData
f49b37c9 1/* Header for multibyte character handler.
aaef169d 2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
4e6835db 3 2006, 2007 Free Software Foundation, Inc.
7976eda0 4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4e6835db 5 2005, 2006, 2007
ce03bf76
KH
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
4ed46869 8
369314dc
KH
9This file is part of GNU Emacs.
10
11GNU Emacs is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
1427aa65 13the Free Software Foundation; either version 3, or (at your option)
369314dc 14any later version.
4ed46869 15
369314dc
KH
16GNU Emacs is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
4ed46869 20
369314dc
KH
21You should have received a copy of the GNU General Public License
22along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
23the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24Boston, MA 02110-1301, USA. */
4ed46869 25
aa01a892
KH
26#ifndef EMACS_CHARSET_H
27#define EMACS_CHARSET_H
4ed46869 28
6e4dc3e1
KH
29/* #define BYTE_COMBINING_DEBUG */
30
4ed46869
KH
31/*** GENERAL NOTE on CHARACTER SET (CHARSET) ***
32
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
d8e4f486 36 of the ISO charsets. Emacs identifies a charset by a unique
f49b37c9
KH
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).
4ed46869 40
d8e4f486 41 The value range of charsets is 0x00, 0x81..0xFE. There are four
4ed46869
KH
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
f49b37c9 44 characters.
4ed46869
KH
45
46 Within Emacs Lisp, a charset is treated as a symbol which has a
47 property `charset'. The property value is a vector containing
d8e4f486 48 various information about the charset. For readability of C code,
f49b37c9 49 we use the following convention for C variable names:
4ed46869
KH
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
53
6e4dc3e1
KH
54 Each charset (except for ascii) is assigned a base leading-code
55 (range 0x80..0x9E). In addition, a charset of greater than 0xA0
4ed46869
KH
56 (whose base leading-code is 0x9A..0x9D) is assigned an extended
57 leading-code (range 0xA0..0xFE). In this case, each base
d8e4f486
DL
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
4ed46869
KH
60 character in Emacs' buffer and string.
61
d8e4f486 62 We call a charset which has extended leading-code a "private
f49b37c9 63 charset" because those are mainly for a charset which is not yet
4ed46869 64 registered by ISO. On the contrary, we call a charset which does
d8e4f486 65 not have extended leading-code an "official charset".
4ed46869
KH
66
67 ---------------------------------------------------------------------------
68 charset dimension base leading-code extended leading-code
69 ---------------------------------------------------------------------------
70 0x00 official dim1 -- none -- -- none --
71 (ASCII)
72 0x01..0x7F --never used--
88b837a2
KH
73 0x80 official dim1 -- none -- -- none --
74 (eight-bit-graphic)
4ed46869
KH
75 0x81..0x8F official dim1 same as charset -- none --
76 0x90..0x99 official dim2 same as charset -- none --
6e4dc3e1
KH
77 0x9A..0x9D --never used--
78 0x9E official dim1 same as charset -- none --
79 (eight-bit-control)
88b837a2 80 0x9F --never used--
4ed46869
KH
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 ---------------------------------------------------------------------------
91
4ed46869
KH
92*/
93
94/* Definition of special leading-codes. */
4ed46869
KH
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 */
5c6257e5 99#define LEADING_CODE_PRIVATE_22 0x9D /* for private DIMENSION2 of 2-column */
4ed46869 100
6e4dc3e1
KH
101#define LEADING_CODE_8_BIT_CONTROL 0x9E /* for `eight-bit-control' */
102
4ed46869
KH
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
111
112/* Definition of minimum/maximum charset of each DIMENSION. */
2b9eeded 113#define MIN_CHARSET_OFFICIAL_DIMENSION1 0x80
4ed46869
KH
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
119
03e66c53
KH
120/* Maximum value of overall charset identification number. */
121#define MAX_CHARSET 0xFE
122
4ed46869 123/* Definition of special charsets. */
6e4dc3e1
KH
124#define CHARSET_ASCII 0 /* 0x00..0x7F */
125#define CHARSET_8_BIT_CONTROL 0x9E /* 0x80..0x9F */
88b837a2 126#define CHARSET_8_BIT_GRAPHIC 0x80 /* 0xA0..0xFF */
4ed46869 127
4ed46869
KH
128extern int charset_latin_iso8859_1; /* ISO8859-1 (Latin-1) */
129extern int charset_jisx0208_1978; /* JISX0208.1978 (Japanese Kanji old set) */
130extern int charset_jisx0208; /* JISX0208.1983 (Japanese Kanji) */
131extern int charset_katakana_jisx0201; /* JISX0201.Kana (Japanese Katakana) */
132extern int charset_latin_jisx0201; /* JISX0201.Roman (Japanese Roman) */
133extern int charset_big5_1; /* Big5 Level 1 (Chinese Traditional) */
134extern int charset_big5_2; /* Big5 Level 2 (Chinese Traditional) */
bde7944b
KH
135extern int charset_mule_unicode_0100_24ff;
136extern int charset_mule_unicode_2500_33ff;
137extern int charset_mule_unicode_e000_ffff;
4ed46869 138
6e4dc3e1
KH
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. */
c399b461 142#define CHAR_HEAD_P(ch) ((unsigned char) (ch) < 0xA0)
4ed46869
KH
143
144/*** GENERAL NOTE on CHARACTER REPRESENTATION ***
145
d8e4f486
DL
146 Firstly, the term "character" or "char" is used for a multilingual
147 character (of course, including ASCII characters), not for a byte in
4ed46869
KH
148 computer memory. We use the term "code" or "byte" for the latter
149 case.
150
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.
157
158 Emacs has two kinds of representation of a character: multi-byte
d8e4f486
DL
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
4ed46869
KH
163 POSITION-CODE is set in multi-byte form).
164
d8e4f486
DL
165 For details of the multi-byte form, see the section "2. Emacs
166 internal format handlers" of `coding.c'.
4ed46869
KH
167
168 Emacs uses 19 bits for a character code. The bits are divided into
169 3 fields: FIELD1(5bits):FIELD2(7bits):FIELD3(7bits).
170
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.
175
176 More precisely...
177
6e4dc3e1
KH
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.
4ed46869
KH
183
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.
187
6e4dc3e1
KH
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 -----------------------------------------------------------------------------
4ed46869 198 "(o)": official, "(p)": private
6e4dc3e1 199 -----------------------------------------------------------------------------
4ed46869
KH
200*/
201
4ed46869
KH
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
206
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)
211
212/* Minimum character code of character of each DIMENSION. */
213#define MIN_CHAR_OFFICIAL_DIMENSION1 \
2b9eeded 214 ((0x81 - 0x70) << 7)
4ed46869
KH
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)
f49b37c9
KH
221/* Maximum character code currently used plus 1. */
222#define MAX_CHAR (0x1F << 14)
4ed46869 223
6e4dc3e1 224/* 1 if C is a single byte character, else 0. */
6f1a3d53 225#define SINGLE_BYTE_CHAR_P(c) (((unsigned)(c) & 0xFF) == (c))
4ed46869 226
6e4dc3e1 227/* 1 if BYTE is an ASCII character in itself, in multibyte mode. */
7614f779
KH
228#define ASCII_BYTE_P(byte) ((byte) < 0x80)
229
d8e4f486 230/* A char-table containing information on each character set.
4ed46869 231
d8e4f486 232 Unlike ordinary char-tables, this doesn't contain any nested tables.
4ed46869
KH
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.
240
241 CHARSET-ID (integer) is the identification number of the charset.
242
d8e4f486
DL
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.
4ed46869
KH
245
246 DIMENSION (integer) is the number of bytes to represent a character: 1 or 2.
247
248 CHARS (integer) is the number of characters in a dimension: 94 or 96.
249
250 WIDTH (integer) is the number of columns a character in the charset
6e4dc3e1 251 occupies on the screen: one of 0, 1, and 2..
4ed46869
KH
252
253 DIRECTION (integer) is the rendering direction of characters in the
28d0c844
KH
254 charset when rendering. If 0, render from left to right, else
255 render from right to left.
4ed46869
KH
256
257 LEADING-CODE-BASE (integer) is the base leading-code for the
258 charset.
259
260 LEADING-CODE-EXT (integer) is the extended leading-code for the
d8e4f486 261 charset. All charsets of less than 0xA0 have the value 0.
4ed46869
KH
262
263 ISO-FINAL-CHAR (character) is the final character of the
6e4dc3e1
KH
264 corresponding ISO 2022 charset. It is -1 for such a character
265 that is used only internally (e.g. `eight-bit-control').
4ed46869
KH
266
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
6e4dc3e1
KH
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').
4ed46869
KH
272
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.
d8e4f486 276
4ed46869
KH
277 SHORT-NAME (string) is the short name to refer to the charset.
278
279 LONG-NAME (string) is the long name to refer to the charset.
280
281 DESCRIPTION (string) is the description string of the charset.
282
283 PLIST (property list) may contain any type of information a user
d8e4f486 284 wants to put and get by functions `put-charset-property' and
4ed46869
KH
285 `get-charset-property' respectively. */
286extern Lisp_Object Vcharset_table;
287
288/* Macros to access various information of CHARSET in Vcharset_table.
289 We provide these macros for efficiency. No range check of CHARSET. */
290
a3f4e3d9 291/* Return entry of CHARSET (C integer) in Vcharset_table. */
d6f92c04
KH
292#define CHARSET_TABLE_ENTRY(charset) \
293 XCHAR_TABLE (Vcharset_table)->contents[((charset) == CHARSET_ASCII \
294 ? 0 : (charset) + 128)]
4ed46869
KH
295
296/* Return information INFO-IDX of CHARSET. */
297#define CHARSET_TABLE_INFO(charset, info_idx) \
298 XVECTOR (CHARSET_TABLE_ENTRY (charset))->contents[info_idx]
299
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)
317
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) \
6e4dc3e1 334 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_FINAL_CHAR_IDX))
4ed46869 335#define CHARSET_ISO_GRAPHIC_PLANE(charset) \
6e4dc3e1 336 XINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_GRAPHIC_PLANE_IDX))
4ed46869
KH
337#define CHARSET_REVERSE_CHARSET(charset) \
338 XINT (CHARSET_TABLE_INFO (charset, CHARSET_REVERSE_CHARSET_IDX))
339
340/* Macros to specify direction of a charset. */
341#define CHARSET_DIRECTION_LEFT_TO_RIGHT 0
342#define CHARSET_DIRECTION_RIGHT_TO_LEFT 1
343
344/* A vector of charset symbol indexed by charset-id. This is used
345 only for returning charset symbol from C functions. */
346extern Lisp_Object Vcharset_symbol_table;
347
348/* Return symbol of CHARSET. */
349#define CHARSET_SYMBOL(charset) \
350 XVECTOR (Vcharset_symbol_table)->contents[charset]
351
384107f2 352/* 1 if CHARSET is in valid value range, else 0. */
4ed46869
KH
353#define CHARSET_VALID_P(charset) \
354 ((charset) == 0 \
f49b37c9 355 || ((charset) > 0x80 && (charset) <= MAX_CHARSET_OFFICIAL_DIMENSION2) \
6e4dc3e1
KH
356 || ((charset) >= MIN_CHARSET_PRIVATE_DIMENSION1 \
357 && (charset) <= MAX_CHARSET) \
358 || ((charset) == CHARSET_8_BIT_CONTROL) \
359 || ((charset) == CHARSET_8_BIT_GRAPHIC))
4ed46869 360
f49b37c9 361/* 1 if CHARSET is already defined, else 0. */
4ed46869 362#define CHARSET_DEFINED_P(charset) \
03e66c53 363 (((charset) >= 0) && ((charset) <= MAX_CHARSET) \
4ed46869
KH
364 && !NILP (CHARSET_TABLE_ENTRY (charset)))
365
366/* Since the information CHARSET-BYTES and CHARSET-WIDTH of
6e4dc3e1 367 Vcharset_table can be retrieved only by the first byte of
4ed46869
KH
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. */
371extern int bytes_by_char_head[256];
372extern int width_by_char_head[256];
373
6e4dc3e1
KH
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])
4ed46869
KH
378
379/* Charset of the character C. */
6e4dc3e1
KH
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 \
f49b37c9 389 : CHAR_FIELD1 (c) + 0xE0)))
4ed46869 390
f49b37c9 391/* Check if two characters C1 and C2 belong to the same charset. */
6e4dc3e1
KH
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))
4ed46869
KH
396
397/* Return a character of which charset is CHARSET and position-codes
398 are C1 and C2. DIMENSION1 character ignores C2. */
6e4dc3e1
KH
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 \
f164d9b5
KH
405 : ((CHARSET_DEFINED_P (charset) \
406 ? CHARSET_DIMENSION (charset) == 1 \
407 : (charset) < MIN_CHARSET_PRIVATE_DIMENSION2) \
2cd31e75 408 ? (((charset) - 0x70) << 7) | ((c1) <= 0 ? 0 : ((c1) & 0x7F)) \
6e4dc3e1
KH
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))))))
414
4ed46869 415
e0f24100
GM
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
15979e9e
KH
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)))
c6112b99 422
d74d0ca5 423/* This default value is used when nonascii-translation-table or
54e15bb9
KH
424 nonascii-insert-offset fail to convert unibyte character to a valid
425 multibyte character. This makes a Latin-1 character. */
426
427#define DEFAULT_NONASCII_INSERT_OFFSET 0x800
428
6e4dc3e1
KH
429/* Parse multibyte string STR of length LENGTH and set BYTES to the
430 byte length of a character at STR. */
431
432#ifdef BYTE_COMBINING_DEBUG
384107f2
KH
433
434#define PARSE_MULTIBYTE_SEQ(str, length, bytes) \
435 do { \
436 int i = 1; \
f49b37c9 437 while (i < (length) && ! CHAR_HEAD_P ((str)[i])) i++; \
6e4dc3e1
KH
438 (bytes) = BYTES_BY_CHAR_HEAD ((str)[0]); \
439 if ((bytes) > i) \
440 abort (); \
384107f2 441 } while (0)
54e15bb9 442
6e4dc3e1 443#else /* not BYTE_COMBINING_DEBUG */
feb453fa 444
6e4dc3e1 445#define PARSE_MULTIBYTE_SEQ(str, length, bytes) \
304f9d2a 446 ((void)(length), (bytes) = BYTES_BY_CHAR_HEAD ((str)[0]))
feb453fa 447
6e4dc3e1
KH
448#endif /* not BYTE_COMBINING_DEBUG */
449
1efcd78f
KH
450#define VALID_LEADING_CODE_P(code) \
451 (! NILP (CHARSET_TABLE_ENTRY (code)))
452
e0f24100 453/* Return 1 if the byte sequence at unibyte string STR (LENGTH bytes)
6e4dc3e1
KH
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. */
456
1efcd78f
KH
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]))))))))
472
6e4dc3e1 473
e0f24100 474/* Return 1 if the byte sequence at multibyte string STR is valid as
6e4dc3e1
KH
475 a unibyte form. By a side effect, BYTES is set to the byte length
476 of one character at STR. */
477
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)
4ed46869 481
feb453fa
RS
482/* The charset of character C is stored in CHARSET, and the
483 position-codes of C are stored in C1 and C2.
9dd2aa1a 484 We store -1 in C2 if the dimension of the charset is 1. */
feb453fa 485
f164d9b5
KH
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), \
6e4dc3e1 500 c2 = -1)))
4ed46869 501
e0f24100 502/* Return 1 if character C has valid printable glyph. */
6e4dc3e1 503#define CHAR_PRINTABLE_P(c) (ASCII_BYTE_P (c) || char_printable_p (c))
384107f2 504
feb453fa
RS
505/* The charset of the character at STR is stored in CHARSET, and the
506 position-codes are stored in C1 and C2.
f49b37c9 507 We store -1 in C2 if the character is just 2 bytes. */
feb453fa 508
f49b37c9
KH
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 \
4ed46869
KH
514 : charset)
515
4ed46869
KH
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). */
519extern int iso_charset_table[2][2][128];
520
521#define ISO_CHARSET_TABLE(dimension, chars, final_char) \
522 iso_charset_table[XINT (dimension) - 1][XINT (chars) > 94][XINT (final_char)]
523
524#define BASE_LEADING_CODE_P(c) (BYTES_BY_CHAR_HEAD ((unsigned char) (c)) > 1)
525
3124bc0e 526/* Return how many bytes C will occupy in a multibyte buffer. */
6e4dc3e1
KH
527#define CHAR_BYTES(c) \
528 (SINGLE_BYTE_CHAR_P (c) \
529 ? ((ASCII_BYTE_P (c) || (c) >= 0xA0) ? 1 : 2) \
530 : char_bytes (c))
3124bc0e 531
4ed46869 532/* The following two macros CHAR_STRING and STRING_CHAR are the main
d8e4f486 533 entry points to convert between Emacs's two types of character
4ed46869
KH
534 representations: multi-byte form and single-word form (character
535 code). */
536
f49b37c9 537/* Store multi-byte form of the character C in STR. The caller should
6e4dc3e1
KH
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. */
4ed46869 541
e10ae834
KH
542#define CHAR_STRING(c, str) \
543 (SINGLE_BYTE_CHAR_P (c) \
6f1a3d53 544 ? ((ASCII_BYTE_P (c) || c >= 0xA0) \
e10ae834
KH
545 ? (*(str) = (unsigned char)(c), 1) \
546 : (*(str) = LEADING_CODE_8_BIT_CONTROL, *((str)+ 1) = c + 0x20, 2)) \
6e4dc3e1 547 : char_to_string (c, (unsigned char *) str))
4ed46869 548
e06aa1f9
GM
549/* Like CHAR_STRING but don't signal an error if C is invalid.
550 Value is -1 in this case. */
551
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))
558
4ed46869
KH
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. */
562
dfd57aa7
KH
563#define STRING_CHAR(str, len) \
564 (BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
565 ? (unsigned char) *(str) \
f49b37c9 566 : string_to_char (str, len, 0))
4ed46869 567
384107f2
KH
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
4ed46869
KH
570 MULTIBYTE_FORM_LENGTH. */
571
384107f2
KH
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) \
f49b37c9 575 : string_to_char (str, len, &(actual_len)))
d74d0ca5 576
6e4dc3e1
KH
577/* Fetch the "next" character from Lisp string STRING at byte position
578 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
520afed1
RS
579
580 All the args must be side-effect-free.
581 BYTEIDX and CHARIDX must be lvalues;
582 we increment them past the character fetched. */
583
6e4dc3e1
KH
584#define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
585if (1) \
586 { \
587 CHARIDX++; \
588 if (STRING_MULTIBYTE (STRING)) \
589 { \
8a25217a 590 const unsigned char *ptr = SDATA (STRING) + BYTEIDX; \
9f5348e3 591 int space_left = SBYTES (STRING) - BYTEIDX; \
6e4dc3e1
KH
592 int actual_len; \
593 \
594 OUTPUT = STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
595 BYTEIDX += actual_len; \
596 } \
597 else \
d5db4077 598 OUTPUT = SREF (STRING, BYTEIDX++); \
6e4dc3e1
KH
599 } \
600else
601
602/* Like FETCH_STRING_CHAR_ADVANCE but assume STRING is multibyte. */
603
604#define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
520afed1
RS
605if (1) \
606 { \
8a25217a 607 const unsigned char *fetch_string_char_ptr = SDATA (STRING) + BYTEIDX; \
9f5348e3 608 int fetch_string_char_space_left = SBYTES (STRING) - BYTEIDX; \
520afed1
RS
609 int actual_len; \
610 \
611 OUTPUT \
612 = STRING_CHAR_AND_LENGTH (fetch_string_char_ptr, \
613 fetch_string_char_space_left, actual_len); \
614 \
615 BYTEIDX += actual_len; \
616 CHARIDX++; \
617 } \
618else
619
6e4dc3e1
KH
620/* Like FETCH_STRING_CHAR_ADVANCE but fetch character from the current
621 buffer. */
f49b37c9
KH
622
623#define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
624if (1) \
625 { \
f49b37c9 626 CHARIDX++; \
6e4dc3e1
KH
627 if (!NILP (current_buffer->enable_multibyte_characters)) \
628 { \
629 unsigned char *ptr = BYTE_POS_ADDR (BYTEIDX); \
630 int space_left = ((CHARIDX < GPT ? GPT_BYTE : Z_BYTE) - BYTEIDX); \
631 int actual_len; \
632 \
633 OUTPUT= STRING_CHAR_AND_LENGTH (ptr, space_left, actual_len); \
634 BYTEIDX += actual_len; \
635 } \
636 else \
637 { \
638 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
639 BYTEIDX++; \
640 } \
f49b37c9
KH
641 } \
642else
643
4ed46869
KH
644/* Return the length of the multi-byte form at string STR of length LEN. */
645
d121936f
KH
646#define MULTIBYTE_FORM_LENGTH(str, len) \
647 (BYTES_BY_CHAR_HEAD (*(unsigned char *)(str)) == 1 \
648 ? 1 \
4ed46869
KH
649 : multibyte_form_length (str, len))
650
d3985b78
KH
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. */
654
655#define NEXT_CHAR_BOUNDARY(p, limit) \
656 do { \
657 if ((p) < (limit)) \
658 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
659 } while (0)
660
661
3ffcda54 662/* If P is after LIMIT, advance P to the previous character boundary. */
d3985b78
KH
663
664#define PREV_CHAR_BOUNDARY(p, limit) \
665 do { \
666 if ((p) > (limit)) \
667 { \
668 const unsigned char *p0 = (p); \
3ffcda54 669 const unsigned char *p_limit = max (limit, p0 - MAX_MULTIBYTE_LENGTH);\
d3985b78
KH
670 do { \
671 p0--; \
3ffcda54
SM
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; \
d3985b78
KH
675 } \
676 } while (0)
677
3ffcda54
SM
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 \
684 { \
685 const unsigned char *p_aux = (p)+1; \
686 PREV_CHAR_BOUNDARY (p_aux, limit); \
687 (result) = (p_aux == (p)); \
688 } \
689} while (0)
d3985b78 690
4ed46869
KH
691#ifdef emacs
692
384107f2
KH
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. */
6e4dc3e1
KH
697
698#ifdef BYTE_COMBINING_DEBUG
699
384107f2
KH
700#define INC_POS(pos_byte) \
701 do { \
702 unsigned char *p = BYTE_POS_ADDR (pos_byte); \
703 if (BASE_LEADING_CODE_P (*p)) \
704 { \
705 int len, bytes; \
706 len = Z_BYTE - pos_byte; \
707 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
708 pos_byte += bytes; \
709 } \
710 else \
711 pos_byte++; \
4ed46869
KH
712 } while (0)
713
6e4dc3e1
KH
714#else /* not BYTE_COMBINING_DEBUG */
715
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)
721
722#endif /* not BYTE_COMBINING_DEBUG */
723
384107f2
KH
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; \
729 \
730 pos_byte--; \
731 if (pos_byte < GPT_BYTE) \
02dcfd84 732 p = BEG_ADDR + pos_byte - BEG_BYTE, p_min = BEG_ADDR; \
384107f2 733 else \
02dcfd84 734 p = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE, p_min = GAP_END_ADDR;\
384107f2
KH
735 if (p > p_min && !CHAR_HEAD_P (*p)) \
736 { \
737 unsigned char *pend = p--; \
738 int len, bytes; \
02dcfd84
SM
739 if (p_min < p - MAX_MULTIBYTE_LENGTH) \
740 p_min = p - MAX_MULTIBYTE_LENGTH; \
384107f2
KH
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; \
746 } \
c399b461
RS
747 } while (0)
748
749/* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
750
d619ee06
RS
751#define INC_BOTH(charpos, bytepos) \
752do \
753 { \
754 (charpos)++; \
755 if (NILP (current_buffer->enable_multibyte_characters)) \
756 (bytepos)++; \
757 else \
758 INC_POS ((bytepos)); \
759 } \
c399b461
RS
760while (0)
761
762/* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
763
d619ee06
RS
764#define DEC_BOTH(charpos, bytepos) \
765do \
766 { \
767 (charpos)--; \
768 if (NILP (current_buffer->enable_multibyte_characters)) \
769 (bytepos)--; \
770 else \
771 DEC_POS ((bytepos)); \
772 } \
c399b461
RS
773while (0)
774
384107f2
KH
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. */
6e4dc3e1
KH
779
780#ifdef BYTE_COMBINING_DEBUG
781
384107f2
KH
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)) \
bb5999ae 786 { \
384107f2
KH
787 int len, bytes; \
788 len = BUF_Z_BYTE (buf) - pos_byte; \
789 PARSE_MULTIBYTE_SEQ (p, len, bytes); \
790 pos_byte += bytes; \
bb5999ae 791 } \
384107f2
KH
792 else \
793 pos_byte++; \
794 } while (0)
795
6e4dc3e1
KH
796#else /* not BYTE_COMBINING_DEBUG */
797
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)
803
804#endif /* not BYTE_COMBINING_DEBUG */
805
384107f2
KH
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)) \
813 { \
02dcfd84 814 p = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
384107f2
KH
815 p_min = BUF_BEG_ADDR (buf); \
816 } \
817 else \
818 { \
02dcfd84 819 p = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
384107f2
KH
820 p_min = BUF_GAP_END_ADDR (buf); \
821 } \
822 if (p > p_min && !CHAR_HEAD_P (*p)) \
823 { \
824 unsigned char *pend = p--; \
825 int len, bytes; \
02dcfd84
SM
826 if (p_min < p - MAX_MULTIBYTE_LENGTH) \
827 p_min = p - MAX_MULTIBYTE_LENGTH; \
384107f2
KH
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; \
833 } \
4ed46869
KH
834 } while (0)
835
836#endif /* emacs */
837
f49b37c9
KH
838/* This is the maximum byte length of multi-byte sequence. */
839#define MAX_MULTIBYTE_LENGTH 4
4ed46869 840
345b3483 841extern void invalid_character P_ ((int)) NO_RETURN;
bb5999ae 842
62c6686f 843extern int translate_char P_ ((Lisp_Object, int, int, int, int));
f49b37c9 844extern int split_string P_ ((const unsigned char *, int, int *,
c04809fb 845 unsigned char *, unsigned char *));
f49b37c9 846extern int char_to_string P_ ((int, unsigned char *));
e06aa1f9 847extern int char_to_string_1 P_ ((int, unsigned char *));
f49b37c9 848extern int string_to_char P_ ((const unsigned char *, int, int *));
384107f2 849extern int char_printable_p P_ ((int c));
8867de67 850extern int multibyte_form_length P_ ((const unsigned char *, int));
8a25217a
KR
851extern void parse_str_as_multibyte P_ ((const unsigned char *, int, int *,
852 int *));
6e4dc3e1 853extern int str_as_multibyte P_ ((unsigned char *, int, int, int *));
347abaf7 854extern int parse_str_to_multibyte P_ ((unsigned char *, int));
6e4dc3e1
KH
855extern int str_to_multibyte P_ ((unsigned char *, int, int));
856extern int str_as_unibyte P_ ((unsigned char *, int));
c04809fb 857extern int get_charset_id P_ ((Lisp_Object));
8a25217a 858extern int find_charset_in_text P_ ((const unsigned char *, int, int, int *,
6e4dc3e1 859 Lisp_Object));
c04809fb 860extern int strwidth P_ ((unsigned char *, int));
8a25217a 861extern int c_string_width P_ ((const unsigned char *, int, int, int *, int *));
417ef8f6 862extern int lisp_string_width P_ ((Lisp_Object, int, int *, int *));
3124bc0e 863extern int char_bytes P_ ((int));
c2d1e590 864extern int char_valid_p P_ ((int, int));
2db95897 865
9028b39e
SM
866EXFUN (Funibyte_char_to_multibyte, 1);
867
d74d0ca5 868extern Lisp_Object Vtranslation_table_vector;
62c6686f 869
d74d0ca5 870/* Return a translation table of id number ID. */
62c6686f 871#define GET_TRANSLATION_TABLE(id) \
e35efe89 872 (XCDR(XVECTOR(Vtranslation_table_vector)->contents[(id)]))
c1f6608b 873
69535a4b
KH
874/* A char-table for characters which may invoke auto-filling. */
875extern Lisp_Object Vauto_fill_chars;
876
c1f6608b
KH
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
6e4dc3e1
KH
879 faster than calling bcopy which has some overhead. Copying a
880 multibyte sequence of a multibyte character is the typical case. */
c1f6608b
KH
881
882#define BCOPY_SHORT(from, to, len) \
883 do { \
884 int i = len; \
a9fb257e
KR
885 const unsigned char *from_p = from; \
886 unsigned char *to_p = to; \
6e4dc3e1 887 while (i--) *to_p++ = *from_p++; \
c1f6608b
KH
888 } while (0)
889
aa01a892 890#endif /* EMACS_CHARSET_H */
ab5796a9
MB
891
892/* arch-tag: 3b96db55-4961-481d-ac3e-219f46a2b3aa
893 (do not change this comment) */