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