]> code.delx.au - gnu-emacs/blob - src/character.h
440e78147d1d5002146364d64b0957b29992bc3c
[gnu-emacs] / src / character.h
1 /* Header for multibyte character handler.
2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
7
8 This file is part of GNU Emacs.
9
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22
23 #ifndef EMACS_CHARACTER_H
24 #define EMACS_CHARACTER_H
25
26 #include <verify.h>
27 #include "lisp.h"
28
29 INLINE_HEADER_BEGIN
30
31 /* character code 1st byte byte sequence
32 -------------- -------- -------------
33 0-7F 00..7F 0xxxxxxx
34 80-7FF C2..DF 110xxxxx 10xxxxxx
35 800-FFFF E0..EF 1110xxxx 10xxxxxx 10xxxxxx
36 10000-1FFFFF F0..F7 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
37 200000-3FFF7F F8 11111000 1000xxxx 10xxxxxx 10xxxxxx 10xxxxxx
38 3FFF80-3FFFFF C0..C1 1100000x 10xxxxxx (for eight-bit-char)
39 400000-... invalid
40
41 invalid 1st byte 80..BF 10xxxxxx
42 F9..FF 11111xxx (xxx != 000)
43 */
44
45 /* Maximum character code ((1 << CHARACTERBITS) - 1). */
46 #define MAX_CHAR 0x3FFFFF
47
48 /* Maximum Unicode character code. */
49 #define MAX_UNICODE_CHAR 0x10FFFF
50
51 /* Maximum N-byte character codes. */
52 #define MAX_1_BYTE_CHAR 0x7F
53 #define MAX_2_BYTE_CHAR 0x7FF
54 #define MAX_3_BYTE_CHAR 0xFFFF
55 #define MAX_4_BYTE_CHAR 0x1FFFFF
56 #define MAX_5_BYTE_CHAR 0x3FFF7F
57
58 /* Minimum leading code of multibyte characters. */
59 #define MIN_MULTIBYTE_LEADING_CODE 0xC0
60 /* Maximum leading code of multibyte characters. */
61 #define MAX_MULTIBYTE_LEADING_CODE 0xF8
62
63 /* Unicode character values. */
64 enum
65 {
66 NO_BREAK_SPACE = 0x00A0,
67 SOFT_HYPHEN = 0x00AD,
68 ZERO_WIDTH_NON_JOINER = 0x200C,
69 ZERO_WIDTH_JOINER = 0x200D,
70 HYPHEN = 0x2010,
71 NON_BREAKING_HYPHEN = 0x2011,
72 LEFT_SINGLE_QUOTATION_MARK = 0x2018,
73 RIGHT_SINGLE_QUOTATION_MARK = 0x2019,
74 PARAGRAPH_SEPARATOR = 0x2029,
75 LEFT_POINTING_ANGLE_BRACKET = 0x2329,
76 RIGHT_POINTING_ANGLE_BRACKET = 0x232A,
77 LEFT_ANGLE_BRACKET = 0x3008,
78 RIGHT_ANGLE_BRACKET = 0x3009,
79 OBJECT_REPLACEMENT_CHARACTER = 0xFFFC,
80 };
81
82 /* UTF-8 encodings. Use \x escapes, so they are portable to pre-C11
83 compilers and can be concatenated with ordinary string literals. */
84 #define uLSQM "\xE2\x80\x98" /* U+2018 LEFT SINGLE QUOTATION MARK */
85 #define uRSQM "\xE2\x80\x99" /* U+2019 RIGHT SINGLE QUOTATION MARK */
86
87 /* Nonzero iff C is a character that corresponds to a raw 8-bit
88 byte. */
89 #define CHAR_BYTE8_P(c) ((c) > MAX_5_BYTE_CHAR)
90
91 /* Return the character code for raw 8-bit byte BYTE. */
92 #define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00)
93
94 #define UNIBYTE_TO_CHAR(byte) \
95 (ASCII_CHAR_P (byte) ? (byte) : BYTE8_TO_CHAR (byte))
96
97 /* Return the raw 8-bit byte for character C. */
98 #define CHAR_TO_BYTE8(c) (CHAR_BYTE8_P (c) ? (c) - 0x3FFF00 : (c & 0xFF))
99
100 /* Return the raw 8-bit byte for character C,
101 or -1 if C doesn't correspond to a byte. */
102 #define CHAR_TO_BYTE_SAFE(c) \
103 (ASCII_CHAR_P (c) ? c : (CHAR_BYTE8_P (c) ? (c) - 0x3FFF00 : -1))
104
105 /* Nonzero iff BYTE is the 1st byte of a multibyte form of a character
106 that corresponds to a raw 8-bit byte. */
107 #define CHAR_BYTE8_HEAD_P(byte) ((byte) == 0xC0 || (byte) == 0xC1)
108
109 /* If C is not ASCII, make it unibyte. */
110 #define MAKE_CHAR_UNIBYTE(c) \
111 do { \
112 if (! ASCII_CHAR_P (c)) \
113 c = CHAR_TO_BYTE8 (c); \
114 } while (false)
115
116
117 /* If C is not ASCII, make it multibyte. Assumes C < 256. */
118 #define MAKE_CHAR_MULTIBYTE(c) \
119 (eassert ((c) >= 0 && (c) < 256), (c) = UNIBYTE_TO_CHAR (c))
120
121 /* This is the maximum byte length of multibyte form. */
122 #define MAX_MULTIBYTE_LENGTH 5
123
124 /* Nonzero iff X is a character. */
125 #define CHARACTERP(x) (NATNUMP (x) && XFASTINT (x) <= MAX_CHAR)
126
127 /* Nonzero iff C is valid as a character code. */
128 #define CHAR_VALID_P(c) UNSIGNED_CMP (c, <=, MAX_CHAR)
129
130 /* Check if Lisp object X is a character or not. */
131 #define CHECK_CHARACTER(x) \
132 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
133
134 #define CHECK_CHARACTER_CAR(x) \
135 do { \
136 Lisp_Object tmp = XCAR (x); \
137 CHECK_CHARACTER (tmp); \
138 } while (false)
139
140 #define CHECK_CHARACTER_CDR(x) \
141 do { \
142 Lisp_Object tmp = XCDR (x); \
143 CHECK_CHARACTER (tmp); \
144 } while (false)
145
146 /* Nonzero iff C is a character of code less than 0x100. */
147 #define SINGLE_BYTE_CHAR_P(c) UNSIGNED_CMP (c, <, 0x100)
148
149 /* Nonzero if character C has a printable glyph. */
150 #define CHAR_PRINTABLE_P(c) \
151 (((c) >= 32 && (c) < 127) \
152 || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c))))
153
154 /* Return byte length of multibyte form for character C. */
155 #define CHAR_BYTES(c) \
156 ( (c) <= MAX_1_BYTE_CHAR ? 1 \
157 : (c) <= MAX_2_BYTE_CHAR ? 2 \
158 : (c) <= MAX_3_BYTE_CHAR ? 3 \
159 : (c) <= MAX_4_BYTE_CHAR ? 4 \
160 : (c) <= MAX_5_BYTE_CHAR ? 5 \
161 : 2)
162
163
164 /* Return the leading code of multibyte form of C. */
165 #define CHAR_LEADING_CODE(c) \
166 ((c) <= MAX_1_BYTE_CHAR ? c \
167 : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \
168 : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \
169 : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \
170 : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \
171 : (0xC0 | (((c) >> 6) & 0x01)))
172
173
174 /* Store multibyte form of the character C in P. The caller should
175 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
176 Returns the length of the multibyte form. */
177
178 #define CHAR_STRING(c, p) \
179 (UNSIGNED_CMP (c, <=, MAX_1_BYTE_CHAR) \
180 ? ((p)[0] = (c), \
181 1) \
182 : UNSIGNED_CMP (c, <=, MAX_2_BYTE_CHAR) \
183 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
184 (p)[1] = (0x80 | ((c) & 0x3F)), \
185 2) \
186 : UNSIGNED_CMP (c, <=, MAX_3_BYTE_CHAR) \
187 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
188 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
189 (p)[2] = (0x80 | ((c) & 0x3F)), \
190 3) \
191 : verify_expr (sizeof (c) <= sizeof (unsigned), char_string (c, p)))
192
193 /* Store multibyte form of byte B in P. The caller should allocate at
194 least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the
195 length of the multibyte form. */
196
197 #define BYTE8_STRING(b, p) \
198 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
199 (p)[1] = (0x80 | ((b) & 0x3F)), \
200 2)
201
202
203 /* Store multibyte form of the character C in P and advance P to the
204 end of the multibyte form. The caller should allocate at least
205 MAX_MULTIBYTE_LENGTH bytes area at P in advance. */
206
207 #define CHAR_STRING_ADVANCE(c, p) \
208 do { \
209 if ((c) <= MAX_1_BYTE_CHAR) \
210 *(p)++ = (c); \
211 else if ((c) <= MAX_2_BYTE_CHAR) \
212 *(p)++ = (0xC0 | ((c) >> 6)), \
213 *(p)++ = (0x80 | ((c) & 0x3F)); \
214 else if ((c) <= MAX_3_BYTE_CHAR) \
215 *(p)++ = (0xE0 | ((c) >> 12)), \
216 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
217 *(p)++ = (0x80 | ((c) & 0x3F)); \
218 else \
219 { \
220 verify (sizeof (c) <= sizeof (unsigned)); \
221 (p) += char_string (c, p); \
222 } \
223 } while (false)
224
225
226 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
227 form. */
228 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
229
230 /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
231 multibyte form. */
232 #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
233
234 /* Nonzero iff BYTE starts a character in a multibyte form.
235 This is equivalent to:
236 (ASCII_CHAR_P (byte) || LEADING_CODE_P (byte)) */
237 #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
238
239 /* How many bytes a character that starts with BYTE occupies in a
240 multibyte form. */
241 #define BYTES_BY_CHAR_HEAD(byte) \
242 (!((byte) & 0x80) ? 1 \
243 : !((byte) & 0x20) ? 2 \
244 : !((byte) & 0x10) ? 3 \
245 : !((byte) & 0x08) ? 4 \
246 : 5)
247
248
249 /* The byte length of multibyte form at unibyte string P ending at
250 PEND. If STR doesn't point to a valid multibyte form, return 0. */
251
252 #define MULTIBYTE_LENGTH(p, pend) \
253 (p >= pend ? 0 \
254 : !((p)[0] & 0x80) ? 1 \
255 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
256 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
257 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
258 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
259 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
260 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
261 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
262 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
263 : 0)
264
265
266 /* Like MULTIBYTE_LENGTH, but don't check the ending address. */
267
268 #define MULTIBYTE_LENGTH_NO_CHECK(p) \
269 (!((p)[0] & 0x80) ? 1 \
270 : ((p)[1] & 0xC0) != 0x80 ? 0 \
271 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
272 : ((p)[2] & 0xC0) != 0x80 ? 0 \
273 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
274 : ((p)[3] & 0xC0) != 0x80 ? 0 \
275 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
276 : ((p)[4] & 0xC0) != 0x80 ? 0 \
277 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
278 : 0)
279
280 /* If P is before LIMIT, advance P to the next character boundary.
281 Assumes that P is already at a character boundary of the same
282 multibyte form whose end address is LIMIT. */
283
284 #define NEXT_CHAR_BOUNDARY(p, limit) \
285 do { \
286 if ((p) < (limit)) \
287 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
288 } while (false)
289
290
291 /* If P is after LIMIT, advance P to the previous character boundary.
292 Assumes that P is already at a character boundary of the same
293 multibyte form whose beginning address is LIMIT. */
294
295 #define PREV_CHAR_BOUNDARY(p, limit) \
296 do { \
297 if ((p) > (limit)) \
298 { \
299 const unsigned char *chp = (p); \
300 do { \
301 chp--; \
302 } while (chp >= limit && ! CHAR_HEAD_P (*chp)); \
303 (p) = (BYTES_BY_CHAR_HEAD (*chp) == (p) - chp) ? chp : (p) - 1; \
304 } \
305 } while (false)
306
307 /* Return the character code of character whose multibyte form is at
308 P. Note that this macro unifies CJK characters whose codepoints
309 are in the Private Use Areas (PUAs), so it might return a different
310 codepoint from the one actually stored at P. */
311
312 #define STRING_CHAR(p) \
313 (!((p)[0] & 0x80) \
314 ? (p)[0] \
315 : ! ((p)[0] & 0x20) \
316 ? (((((p)[0] & 0x1F) << 6) \
317 | ((p)[1] & 0x3F)) \
318 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0)) \
319 : ! ((p)[0] & 0x10) \
320 ? ((((p)[0] & 0x0F) << 12) \
321 | (((p)[1] & 0x3F) << 6) \
322 | ((p)[2] & 0x3F)) \
323 : string_char ((p), NULL, NULL))
324
325
326 /* Like STRING_CHAR, but set ACTUAL_LEN to the length of multibyte
327 form.
328
329 Note: This macro returns the actual length of the character's
330 multibyte sequence as it is stored in a buffer or string. The
331 character it returns might have a different codepoint that has a
332 different multibyte sequence of a different length, due to possible
333 unification of CJK characters inside string_char. Therefore do NOT
334 assume that the length returned by this macro is identical to the
335 length of the multibyte sequence of the character it returns. */
336
337 #define STRING_CHAR_AND_LENGTH(p, actual_len) \
338 (!((p)[0] & 0x80) \
339 ? ((actual_len) = 1, (p)[0]) \
340 : ! ((p)[0] & 0x20) \
341 ? ((actual_len) = 2, \
342 (((((p)[0] & 0x1F) << 6) \
343 | ((p)[1] & 0x3F)) \
344 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \
345 : ! ((p)[0] & 0x10) \
346 ? ((actual_len) = 3, \
347 ((((p)[0] & 0x0F) << 12) \
348 | (((p)[1] & 0x3F) << 6) \
349 | ((p)[2] & 0x3F))) \
350 : string_char ((p), NULL, &actual_len))
351
352
353 /* Like STRING_CHAR, but advance P to the end of multibyte form. */
354
355 #define STRING_CHAR_ADVANCE(p) \
356 (!((p)[0] & 0x80) \
357 ? *(p)++ \
358 : ! ((p)[0] & 0x20) \
359 ? ((p) += 2, \
360 ((((p)[-2] & 0x1F) << 6) \
361 | ((p)[-1] & 0x3F) \
362 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
363 : ! ((p)[0] & 0x10) \
364 ? ((p) += 3, \
365 ((((p)[-3] & 0x0F) << 12) \
366 | (((p)[-2] & 0x3F) << 6) \
367 | ((p)[-1] & 0x3F))) \
368 : string_char ((p), &(p), NULL))
369
370
371 /* Fetch the "next" character from Lisp string STRING at byte position
372 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
373
374 All the args must be side-effect-free.
375 BYTEIDX and CHARIDX must be lvalues;
376 we increment them past the character fetched. */
377
378 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
379 do \
380 { \
381 CHARIDX++; \
382 if (STRING_MULTIBYTE (STRING)) \
383 { \
384 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
385 int chlen; \
386 \
387 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
388 BYTEIDX += chlen; \
389 } \
390 else \
391 { \
392 OUTPUT = SREF (STRING, BYTEIDX); \
393 BYTEIDX++; \
394 } \
395 } \
396 while (false)
397
398 /* Like FETCH_STRING_CHAR_ADVANCE, but return a multibyte character
399 even if STRING is unibyte. */
400
401 #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
402 do \
403 { \
404 CHARIDX++; \
405 if (STRING_MULTIBYTE (STRING)) \
406 { \
407 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
408 int chlen; \
409 \
410 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
411 BYTEIDX += chlen; \
412 } \
413 else \
414 { \
415 OUTPUT = SREF (STRING, BYTEIDX); \
416 BYTEIDX++; \
417 MAKE_CHAR_MULTIBYTE (OUTPUT); \
418 } \
419 } \
420 while (false)
421
422
423 /* Like FETCH_STRING_CHAR_ADVANCE, but assumes STRING is multibyte. */
424
425 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
426 do \
427 { \
428 unsigned char *fetch_ptr = &SDATA (STRING)[BYTEIDX]; \
429 int fetch_len; \
430 \
431 OUTPUT = STRING_CHAR_AND_LENGTH (fetch_ptr, fetch_len); \
432 BYTEIDX += fetch_len; \
433 CHARIDX++; \
434 } \
435 while (false)
436
437
438 /* Like FETCH_STRING_CHAR_ADVANCE, but fetch character from the current
439 buffer. */
440
441 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
442 do \
443 { \
444 CHARIDX++; \
445 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) \
446 { \
447 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
448 int chlen; \
449 \
450 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
451 BYTEIDX += chlen; \
452 } \
453 else \
454 { \
455 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
456 BYTEIDX++; \
457 } \
458 } \
459 while (false)
460
461
462 /* Like FETCH_CHAR_ADVANCE, but assumes the current buffer is multibyte. */
463
464 #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
465 do \
466 { \
467 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
468 int chlen; \
469 \
470 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
471 BYTEIDX += chlen; \
472 CHARIDX++; \
473 } \
474 while (false)
475
476
477 /* Increment the buffer byte position POS_BYTE of the current buffer to
478 the next character boundary. No range checking of POS. */
479
480 #define INC_POS(pos_byte) \
481 do { \
482 unsigned char *chp = BYTE_POS_ADDR (pos_byte); \
483 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
484 } while (false)
485
486
487 /* Decrement the buffer byte position POS_BYTE of the current buffer to
488 the previous character boundary. No range checking of POS. */
489
490 #define DEC_POS(pos_byte) \
491 do { \
492 unsigned char *chp; \
493 \
494 pos_byte--; \
495 if (pos_byte < GPT_BYTE) \
496 chp = BEG_ADDR + pos_byte - BEG_BYTE; \
497 else \
498 chp = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE; \
499 while (!CHAR_HEAD_P (*chp)) \
500 { \
501 chp--; \
502 pos_byte--; \
503 } \
504 } while (false)
505
506 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
507
508 #define INC_BOTH(charpos, bytepos) \
509 do \
510 { \
511 (charpos)++; \
512 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
513 (bytepos)++; \
514 else \
515 INC_POS ((bytepos)); \
516 } \
517 while (false)
518
519
520 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
521
522 #define DEC_BOTH(charpos, bytepos) \
523 do \
524 { \
525 (charpos)--; \
526 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
527 (bytepos)--; \
528 else \
529 DEC_POS ((bytepos)); \
530 } \
531 while (false)
532
533
534 /* Increment the buffer byte position POS_BYTE of the current buffer to
535 the next character boundary. This macro relies on the fact that
536 *GPT_ADDR and *Z_ADDR are always accessible and the values are
537 '\0'. No range checking of POS_BYTE. */
538
539 #define BUF_INC_POS(buf, pos_byte) \
540 do { \
541 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte); \
542 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
543 } while (false)
544
545
546 /* Decrement the buffer byte position POS_BYTE of the current buffer to
547 the previous character boundary. No range checking of POS_BYTE. */
548
549 #define BUF_DEC_POS(buf, pos_byte) \
550 do { \
551 unsigned char *chp; \
552 pos_byte--; \
553 if (pos_byte < BUF_GPT_BYTE (buf)) \
554 chp = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
555 else \
556 chp = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
557 while (!CHAR_HEAD_P (*chp)) \
558 { \
559 chp--; \
560 pos_byte--; \
561 } \
562 } while (false)
563
564
565 /* Return a non-outlandish value for the tab width. */
566
567 #define SANE_TAB_WIDTH(buf) \
568 sanitize_tab_width (XFASTINT (BVAR (buf, tab_width)))
569 INLINE int
570 sanitize_tab_width (EMACS_INT width)
571 {
572 return 0 < width && width <= 1000 ? width : 8;
573 }
574
575 /* Return the width of ASCII character C. The width is measured by
576 how many columns C will occupy on the screen when displayed in the
577 current buffer. */
578
579 #define ASCII_CHAR_WIDTH(c) \
580 (c < 0x20 \
581 ? (c == '\t' \
582 ? SANE_TAB_WIDTH (current_buffer) \
583 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
584 : (c < 0x7f \
585 ? 1 \
586 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
587
588 /* Return a non-outlandish value for a character width. */
589
590 INLINE int
591 sanitize_char_width (EMACS_INT width)
592 {
593 return 0 <= width && width <= 1000 ? width : 1000;
594 }
595
596 /* Return the width of character C. The width is measured by how many
597 columns C will occupy on the screen when displayed in the current
598 buffer. */
599
600 #define CHAR_WIDTH(c) \
601 (ASCII_CHAR_P (c) \
602 ? ASCII_CHAR_WIDTH (c) \
603 : sanitize_char_width (XINT (CHAR_TABLE_REF (Vchar_width_table, c))))
604
605 /* If C is a variation selector, return the index of the
606 variation selector (1..256). Otherwise, return 0. */
607
608 #define CHAR_VARIATION_SELECTOR_P(c) \
609 ((c) < 0xFE00 ? 0 \
610 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
611 : (c) < 0xE0100 ? 0 \
612 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
613 : 0)
614
615 /* If C is a high surrogate, return 1. If C is a low surrogate,
616 return 2. Otherwise, return 0. */
617
618 #define CHAR_SURROGATE_PAIR_P(c) \
619 ((c) < 0xD800 ? 0 \
620 : (c) <= 0xDBFF ? 1 \
621 : (c) <= 0xDFFF ? 2 \
622 : 0)
623
624 /* Data type for Unicode general category.
625
626 The order of members must be in sync with the 8th element of the
627 member of unidata-prop-alist (in admin/unidata/unidata-gen.el) for
628 Unicode character property `general-category'. */
629
630 typedef enum {
631 UNICODE_CATEGORY_UNKNOWN = 0,
632 UNICODE_CATEGORY_Lu,
633 UNICODE_CATEGORY_Ll,
634 UNICODE_CATEGORY_Lt,
635 UNICODE_CATEGORY_Lm,
636 UNICODE_CATEGORY_Lo,
637 UNICODE_CATEGORY_Mn,
638 UNICODE_CATEGORY_Mc,
639 UNICODE_CATEGORY_Me,
640 UNICODE_CATEGORY_Nd,
641 UNICODE_CATEGORY_Nl,
642 UNICODE_CATEGORY_No,
643 UNICODE_CATEGORY_Pc,
644 UNICODE_CATEGORY_Pd,
645 UNICODE_CATEGORY_Ps,
646 UNICODE_CATEGORY_Pe,
647 UNICODE_CATEGORY_Pi,
648 UNICODE_CATEGORY_Pf,
649 UNICODE_CATEGORY_Po,
650 UNICODE_CATEGORY_Sm,
651 UNICODE_CATEGORY_Sc,
652 UNICODE_CATEGORY_Sk,
653 UNICODE_CATEGORY_So,
654 UNICODE_CATEGORY_Zs,
655 UNICODE_CATEGORY_Zl,
656 UNICODE_CATEGORY_Zp,
657 UNICODE_CATEGORY_Cc,
658 UNICODE_CATEGORY_Cf,
659 UNICODE_CATEGORY_Cs,
660 UNICODE_CATEGORY_Co,
661 UNICODE_CATEGORY_Cn
662 } unicode_category_t;
663
664 extern EMACS_INT char_resolve_modifier_mask (EMACS_INT) ATTRIBUTE_CONST;
665 extern int char_string (unsigned, unsigned char *);
666 extern int string_char (const unsigned char *,
667 const unsigned char **, int *);
668
669 extern int translate_char (Lisp_Object, int c);
670 extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t);
671 extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t,
672 ptrdiff_t *);
673 extern ptrdiff_t str_to_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t);
674 extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t);
675 extern ptrdiff_t str_to_unibyte (const unsigned char *, unsigned char *,
676 ptrdiff_t);
677 extern ptrdiff_t strwidth (const char *, ptrdiff_t);
678 extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int,
679 ptrdiff_t *, ptrdiff_t *);
680 extern ptrdiff_t lisp_string_width (Lisp_Object, ptrdiff_t,
681 ptrdiff_t *, ptrdiff_t *);
682
683 extern Lisp_Object Vchar_unify_table;
684 extern Lisp_Object string_escape_byte8 (Lisp_Object);
685
686 extern bool alphabeticp (int);
687 extern bool decimalnump (int);
688 extern bool graphicp (int);
689 extern bool printablep (int);
690
691 /* Return a translation table of id number ID. */
692 #define GET_TRANSLATION_TABLE(id) \
693 (XCDR (XVECTOR (Vtranslation_table_vector)->contents[(id)]))
694
695 /* Look up the element in char table OBJ at index CH, and return it as
696 an integer. If the element is not a character, return CH itself. */
697
698 INLINE int
699 char_table_translate (Lisp_Object obj, int ch)
700 {
701 /* This internal function is expected to be called with valid arguments,
702 so there is a eassert instead of CHECK_xxx for the sake of speed. */
703 eassert (CHAR_VALID_P (ch));
704 eassert (CHAR_TABLE_P (obj));
705 obj = CHAR_TABLE_REF (obj, ch);
706 return CHARACTERP (obj) ? XINT (obj) : ch;
707 }
708
709 INLINE_HEADER_END
710
711 #endif /* EMACS_CHARACTER_H */