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