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