Improve responsiveness while in 'replace-buffer-contents'
[emacs.git] / src / character.h
blobbc65759aa2a99145f1e4441f82189062c81bc5c9
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
8 This file is part of GNU Emacs.
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.
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.
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/>. */
23 #ifndef EMACS_CHARACTER_H
24 #define EMACS_CHARACTER_H
26 #include <verify.h>
27 #include "lisp.h"
29 INLINE_HEADER_BEGIN
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
41 invalid 1st byte 80..BF 10xxxxxx
42 F9..FF 11111xxx (xxx != 000)
45 /* Maximum character code ((1 << CHARACTERBITS) - 1). */
46 #define MAX_CHAR 0x3FFFFF
48 /* Maximum Unicode character code. */
49 #define MAX_UNICODE_CHAR 0x10FFFF
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
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
64 /* Unicode character values. */
65 enum
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,
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 */
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)
92 /* Return the character code for raw 8-bit byte BYTE. */
93 #define BYTE8_TO_CHAR(byte) ((byte) + 0x3FFF00)
95 #define UNIBYTE_TO_CHAR(byte) \
96 (ASCII_CHAR_P (byte) ? (byte) : BYTE8_TO_CHAR (byte))
98 /* Return the raw 8-bit byte for character C. */
99 #define CHAR_TO_BYTE8(c) (CHAR_BYTE8_P (c) ? (c) - 0x3FFF00 : (c & 0xFF))
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))
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)
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)
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))
122 /* This is the maximum byte length of multibyte form. */
123 #define MAX_MULTIBYTE_LENGTH 5
125 /* Nonzero iff X is a character. */
126 #define CHARACTERP(x) (NATNUMP (x) && XFASTINT (x) <= MAX_CHAR)
128 /* Nonzero iff C is valid as a character code. */
129 #define CHAR_VALID_P(c) UNSIGNED_CMP (c, <=, MAX_CHAR)
131 /* Check if Lisp object X is a character or not. */
132 #define CHECK_CHARACTER(x) \
133 CHECK_TYPE (CHARACTERP (x), Qcharacterp, x)
135 #define CHECK_CHARACTER_CAR(x) \
136 do { \
137 Lisp_Object tmp = XCAR (x); \
138 CHECK_CHARACTER (tmp); \
139 } while (false)
141 #define CHECK_CHARACTER_CDR(x) \
142 do { \
143 Lisp_Object tmp = XCDR (x); \
144 CHECK_CHARACTER (tmp); \
145 } while (false)
147 /* Nonzero iff C is a character of code less than 0x100. */
148 #define SINGLE_BYTE_CHAR_P(c) UNSIGNED_CMP (c, <, 0x100)
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))))
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)
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)))
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. */
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)))
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. */
198 #define BYTE8_STRING(b, p) \
199 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
200 (p)[1] = (0x80 | ((b) & 0x3F)), \
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. */
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 \
221 verify (sizeof (c) <= sizeof (unsigned)); \
222 (p) += char_string (c, p); \
224 } while (false)
227 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
228 form. */
229 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
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)
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)
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)
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. */
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)
270 /* Like MULTIBYTE_LENGTH, but don't check the ending address. The
271 multibyte form is still validated, unlike BYTES_BY_CHAR_HEAD. */
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)
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. */
289 #define NEXT_CHAR_BOUNDARY(p, limit) \
290 do { \
291 if ((p) < (limit)) \
292 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
293 } while (false)
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. */
300 #define PREV_CHAR_BOUNDARY(p, limit) \
301 do { \
302 if ((p) > (limit)) \
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; \
310 } while (false)
312 /* Return the character code of character whose multibyte form is at P. */
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))
328 /* Like STRING_CHAR, but set ACTUAL_LEN to the length of multibyte
329 form. */
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))
347 /* Like STRING_CHAR, but advance P to the end of multibyte form. */
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))
365 /* Fetch the "next" character from Lisp string STRING at byte position
366 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
368 All the args must be side-effect-free.
369 BYTEIDX and CHARIDX must be lvalues;
370 we increment them past the character fetched. */
372 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
373 do \
375 CHARIDX++; \
376 if (STRING_MULTIBYTE (STRING)) \
378 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
379 int chlen; \
381 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
382 BYTEIDX += chlen; \
384 else \
386 OUTPUT = SREF (STRING, BYTEIDX); \
387 BYTEIDX++; \
390 while (false)
392 /* Like FETCH_STRING_CHAR_ADVANCE, but return a multibyte character
393 even if STRING is unibyte. */
395 #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
396 do \
398 CHARIDX++; \
399 if (STRING_MULTIBYTE (STRING)) \
401 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
402 int chlen; \
404 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
405 BYTEIDX += chlen; \
407 else \
409 OUTPUT = SREF (STRING, BYTEIDX); \
410 BYTEIDX++; \
411 MAKE_CHAR_MULTIBYTE (OUTPUT); \
414 while (false)
417 /* Like FETCH_STRING_CHAR_ADVANCE, but assumes STRING is multibyte. */
419 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
420 do \
422 unsigned char *fetch_ptr = &SDATA (STRING)[BYTEIDX]; \
423 int fetch_len; \
425 OUTPUT = STRING_CHAR_AND_LENGTH (fetch_ptr, fetch_len); \
426 BYTEIDX += fetch_len; \
427 CHARIDX++; \
429 while (false)
432 /* Like FETCH_STRING_CHAR_ADVANCE, but fetch character from the current
433 buffer. */
435 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
436 do \
438 CHARIDX++; \
439 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) \
441 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
442 int chlen; \
444 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
445 BYTEIDX += chlen; \
447 else \
449 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
450 BYTEIDX++; \
453 while (false)
456 /* Like FETCH_CHAR_ADVANCE, but assumes the current buffer is multibyte. */
458 #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
459 do \
461 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
462 int chlen; \
464 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
465 BYTEIDX += chlen; \
466 CHARIDX++; \
468 while (false)
471 /* Increment the buffer byte position POS_BYTE of the current buffer to
472 the next character boundary. No range checking of POS. */
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)
481 /* Decrement the buffer byte position POS_BYTE of the current buffer to
482 the previous character boundary. No range checking of POS. */
484 #define DEC_POS(pos_byte) \
485 do { \
486 unsigned char *chp; \
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)) \
495 chp--; \
496 pos_byte--; \
498 } while (false)
500 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
502 #define INC_BOTH(charpos, bytepos) \
503 do \
505 (charpos)++; \
506 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
507 (bytepos)++; \
508 else \
509 INC_POS ((bytepos)); \
511 while (false)
514 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
516 #define DEC_BOTH(charpos, bytepos) \
517 do \
519 (charpos)--; \
520 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
521 (bytepos)--; \
522 else \
523 DEC_POS ((bytepos)); \
525 while (false)
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. */
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)
540 /* Decrement the buffer byte position POS_BYTE of the current buffer to
541 the previous character boundary. No range checking of POS_BYTE. */
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)) \
553 chp--; \
554 pos_byte--; \
556 } while (false)
559 /* Return a non-outlandish value for the tab width. */
561 #define SANE_TAB_WIDTH(buf) \
562 sanitize_tab_width (XFASTINT (BVAR (buf, tab_width)))
563 INLINE int
564 sanitize_tab_width (EMACS_INT width)
566 return 0 < width && width <= 1000 ? width : 8;
569 /* Return the width of ASCII character C. The width is measured by
570 how many columns C will occupy on the screen when displayed in the
571 current buffer. */
573 #define ASCII_CHAR_WIDTH(c) \
574 (c < 0x20 \
575 ? (c == '\t' \
576 ? SANE_TAB_WIDTH (current_buffer) \
577 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
578 : (c < 0x7f \
579 ? 1 \
580 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
582 /* Return a non-outlandish value for a character width. */
584 INLINE int
585 sanitize_char_width (EMACS_INT width)
587 return 0 <= width && width <= 1000 ? width : 1000;
590 /* Return the width of character C. The width is measured by how many
591 columns C will occupy on the screen when displayed in the current
592 buffer. The name CHARACTER_WIDTH avoids a collision with <limits.h>
593 CHAR_WIDTH when enabled; see ISO/IEC TS 18661-1:2014. */
595 #define CHARACTER_WIDTH(c) \
596 (ASCII_CHAR_P (c) \
597 ? ASCII_CHAR_WIDTH (c) \
598 : sanitize_char_width (XINT (CHAR_TABLE_REF (Vchar_width_table, c))))
600 /* If C is a variation selector, return the index of the
601 variation selector (1..256). Otherwise, return 0. */
603 #define CHAR_VARIATION_SELECTOR_P(c) \
604 ((c) < 0xFE00 ? 0 \
605 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
606 : (c) < 0xE0100 ? 0 \
607 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
608 : 0)
610 /* Return true if C is a surrogate. */
612 INLINE bool
613 char_surrogate_p (int c)
615 return 0xD800 <= c && c <= 0xDFFF;
618 /* Data type for Unicode general category.
620 The order of members must be in sync with the 8th element of the
621 member of unidata-prop-alist (in admin/unidata/unidata-gen.el) for
622 Unicode character property `general-category'. */
624 typedef enum {
625 UNICODE_CATEGORY_UNKNOWN = 0,
626 UNICODE_CATEGORY_Lu,
627 UNICODE_CATEGORY_Ll,
628 UNICODE_CATEGORY_Lt,
629 UNICODE_CATEGORY_Lm,
630 UNICODE_CATEGORY_Lo,
631 UNICODE_CATEGORY_Mn,
632 UNICODE_CATEGORY_Mc,
633 UNICODE_CATEGORY_Me,
634 UNICODE_CATEGORY_Nd,
635 UNICODE_CATEGORY_Nl,
636 UNICODE_CATEGORY_No,
637 UNICODE_CATEGORY_Pc,
638 UNICODE_CATEGORY_Pd,
639 UNICODE_CATEGORY_Ps,
640 UNICODE_CATEGORY_Pe,
641 UNICODE_CATEGORY_Pi,
642 UNICODE_CATEGORY_Pf,
643 UNICODE_CATEGORY_Po,
644 UNICODE_CATEGORY_Sm,
645 UNICODE_CATEGORY_Sc,
646 UNICODE_CATEGORY_Sk,
647 UNICODE_CATEGORY_So,
648 UNICODE_CATEGORY_Zs,
649 UNICODE_CATEGORY_Zl,
650 UNICODE_CATEGORY_Zp,
651 UNICODE_CATEGORY_Cc,
652 UNICODE_CATEGORY_Cf,
653 UNICODE_CATEGORY_Cs,
654 UNICODE_CATEGORY_Co,
655 UNICODE_CATEGORY_Cn
656 } unicode_category_t;
658 extern EMACS_INT char_resolve_modifier_mask (EMACS_INT) ATTRIBUTE_CONST;
659 extern int char_string (unsigned, unsigned char *);
660 extern int string_char (const unsigned char *,
661 const unsigned char **, int *);
663 extern int translate_char (Lisp_Object, int c);
664 extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t);
665 extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t,
666 ptrdiff_t *);
667 extern ptrdiff_t str_to_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t);
668 extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t);
669 extern ptrdiff_t str_to_unibyte (const unsigned char *, unsigned char *,
670 ptrdiff_t);
671 extern ptrdiff_t strwidth (const char *, ptrdiff_t);
672 extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int,
673 ptrdiff_t *, ptrdiff_t *);
674 extern ptrdiff_t lisp_string_width (Lisp_Object, ptrdiff_t,
675 ptrdiff_t *, ptrdiff_t *);
677 extern Lisp_Object Vchar_unify_table;
678 extern Lisp_Object string_escape_byte8 (Lisp_Object);
680 extern bool alphabeticp (int);
681 extern bool alphanumericp (int);
682 extern bool graphicp (int);
683 extern bool printablep (int);
684 extern bool blankp (int);
686 /* Return a translation table of id number ID. */
687 #define GET_TRANSLATION_TABLE(id) \
688 (XCDR (XVECTOR (Vtranslation_table_vector)->contents[(id)]))
690 /* Look up the element in char table OBJ at index CH, and return it as
691 an integer. If the element is not a character, return CH itself. */
693 INLINE int
694 char_table_translate (Lisp_Object obj, int ch)
696 /* This internal function is expected to be called with valid arguments,
697 so there is an eassert instead of CHECK_xxx for the sake of speed. */
698 eassert (CHAR_VALID_P (ch));
699 eassert (CHAR_TABLE_P (obj));
700 obj = CHAR_TABLE_REF (obj, ch);
701 return CHARACTERP (obj) ? XINT (obj) : ch;
704 #if defined __GNUC__ && !defined __STRICT_ANSI__
705 # define HEXDIGIT_CONST const
706 # define HEXDIGIT_IS_CONST true
707 #else
708 # define HEXDIGIT_CONST
709 # define HEXDIGIT_IS_CONST false
710 #endif
711 extern signed char HEXDIGIT_CONST hexdigit[];
713 /* If C is a hexadecimal digit ('0'-'9', 'a'-'f', 'A'-'F'), return its
714 value (0-15). Otherwise return -1. */
716 INLINE int
717 char_hexdigit (int c)
719 return 0 <= c && c <= UCHAR_MAX ? hexdigit[c] : -1;
722 INLINE_HEADER_END
724 #endif /* EMACS_CHARACTER_H */