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