]> code.delx.au - gnu-emacs/blob - src/character.h
; Merge branch 'fix/no-undo-boundary-on-secondary-buffer-change'
[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 XSETCAR ((x), tmp); \
139 } while (false)
140
141 #define CHECK_CHARACTER_CDR(x) \
142 do { \
143 Lisp_Object tmp = XCDR (x); \
144 CHECK_CHARACTER (tmp); \
145 XSETCDR ((x), tmp); \
146 } while (false)
147
148 /* Nonzero iff C is a character of code less than 0x100. */
149 #define SINGLE_BYTE_CHAR_P(c) UNSIGNED_CMP (c, <, 0x100)
150
151 /* Nonzero if character C has a printable glyph. */
152 #define CHAR_PRINTABLE_P(c) \
153 (((c) >= 32 && (c) < 127) \
154 || ! NILP (CHAR_TABLE_REF (Vprintable_chars, (c))))
155
156 /* Return byte length of multibyte form for character C. */
157 #define CHAR_BYTES(c) \
158 ( (c) <= MAX_1_BYTE_CHAR ? 1 \
159 : (c) <= MAX_2_BYTE_CHAR ? 2 \
160 : (c) <= MAX_3_BYTE_CHAR ? 3 \
161 : (c) <= MAX_4_BYTE_CHAR ? 4 \
162 : (c) <= MAX_5_BYTE_CHAR ? 5 \
163 : 2)
164
165
166 /* Return the leading code of multibyte form of C. */
167 #define CHAR_LEADING_CODE(c) \
168 ((c) <= MAX_1_BYTE_CHAR ? c \
169 : (c) <= MAX_2_BYTE_CHAR ? (0xC0 | ((c) >> 6)) \
170 : (c) <= MAX_3_BYTE_CHAR ? (0xE0 | ((c) >> 12)) \
171 : (c) <= MAX_4_BYTE_CHAR ? (0xF0 | ((c) >> 18)) \
172 : (c) <= MAX_5_BYTE_CHAR ? 0xF8 \
173 : (0xC0 | (((c) >> 6) & 0x01)))
174
175
176 /* Store multibyte form of the character C in P. The caller should
177 allocate at least MAX_MULTIBYTE_LENGTH bytes area at P in advance.
178 Returns the length of the multibyte form. */
179
180 #define CHAR_STRING(c, p) \
181 (UNSIGNED_CMP (c, <=, MAX_1_BYTE_CHAR) \
182 ? ((p)[0] = (c), \
183 1) \
184 : UNSIGNED_CMP (c, <=, MAX_2_BYTE_CHAR) \
185 ? ((p)[0] = (0xC0 | ((c) >> 6)), \
186 (p)[1] = (0x80 | ((c) & 0x3F)), \
187 2) \
188 : UNSIGNED_CMP (c, <=, MAX_3_BYTE_CHAR) \
189 ? ((p)[0] = (0xE0 | ((c) >> 12)), \
190 (p)[1] = (0x80 | (((c) >> 6) & 0x3F)), \
191 (p)[2] = (0x80 | ((c) & 0x3F)), \
192 3) \
193 : verify_expr (sizeof (c) <= sizeof (unsigned), char_string (c, p)))
194
195 /* Store multibyte form of byte B in P. The caller should allocate at
196 least MAX_MULTIBYTE_LENGTH bytes area at P in advance. Returns the
197 length of the multibyte form. */
198
199 #define BYTE8_STRING(b, p) \
200 ((p)[0] = (0xC0 | (((b) >> 6) & 0x01)), \
201 (p)[1] = (0x80 | ((b) & 0x3F)), \
202 2)
203
204
205 /* Store multibyte form of the character C in P and advance P to the
206 end of the multibyte form. The caller should allocate at least
207 MAX_MULTIBYTE_LENGTH bytes area at P in advance. */
208
209 #define CHAR_STRING_ADVANCE(c, p) \
210 do { \
211 if ((c) <= MAX_1_BYTE_CHAR) \
212 *(p)++ = (c); \
213 else if ((c) <= MAX_2_BYTE_CHAR) \
214 *(p)++ = (0xC0 | ((c) >> 6)), \
215 *(p)++ = (0x80 | ((c) & 0x3F)); \
216 else if ((c) <= MAX_3_BYTE_CHAR) \
217 *(p)++ = (0xE0 | ((c) >> 12)), \
218 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
219 *(p)++ = (0x80 | ((c) & 0x3F)); \
220 else \
221 { \
222 verify (sizeof (c) <= sizeof (unsigned)); \
223 (p) += char_string (c, p); \
224 } \
225 } while (false)
226
227
228 /* Nonzero iff BYTE starts a non-ASCII character in a multibyte
229 form. */
230 #define LEADING_CODE_P(byte) (((byte) & 0xC0) == 0xC0)
231
232 /* Nonzero iff BYTE is a trailing code of a non-ASCII character in a
233 multibyte form. */
234 #define TRAILING_CODE_P(byte) (((byte) & 0xC0) == 0x80)
235
236 /* Nonzero iff BYTE starts a character in a multibyte form.
237 This is equivalent to:
238 (ASCII_CHAR_P (byte) || LEADING_CODE_P (byte)) */
239 #define CHAR_HEAD_P(byte) (((byte) & 0xC0) != 0x80)
240
241 /* How many bytes a character that starts with BYTE occupies in a
242 multibyte form. */
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 STR doesn't point to a valid multibyte form, return 0. */
253
254 #define MULTIBYTE_LENGTH(p, pend) \
255 (p >= pend ? 0 \
256 : !((p)[0] & 0x80) ? 1 \
257 : ((p + 1 >= pend) || (((p)[1] & 0xC0) != 0x80)) ? 0 \
258 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
259 : ((p + 2 >= pend) || (((p)[2] & 0xC0) != 0x80)) ? 0 \
260 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
261 : ((p + 3 >= pend) || (((p)[3] & 0xC0) != 0x80)) ? 0 \
262 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
263 : ((p + 4 >= pend) || (((p)[4] & 0xC0) != 0x80)) ? 0 \
264 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
265 : 0)
266
267
268 /* Like MULTIBYTE_LENGTH, but don't check the ending address. */
269
270 #define MULTIBYTE_LENGTH_NO_CHECK(p) \
271 (!((p)[0] & 0x80) ? 1 \
272 : ((p)[1] & 0xC0) != 0x80 ? 0 \
273 : ((p)[0] & 0xE0) == 0xC0 ? 2 \
274 : ((p)[2] & 0xC0) != 0x80 ? 0 \
275 : ((p)[0] & 0xF0) == 0xE0 ? 3 \
276 : ((p)[3] & 0xC0) != 0x80 ? 0 \
277 : ((p)[0] & 0xF8) == 0xF0 ? 4 \
278 : ((p)[4] & 0xC0) != 0x80 ? 0 \
279 : (p)[0] == 0xF8 && ((p)[1] & 0xF0) == 0x80 ? 5 \
280 : 0)
281
282 /* If P is before LIMIT, advance P to the next character boundary.
283 Assumes that P is already at a character boundary of the same
284 multibyte form whose end address is LIMIT. */
285
286 #define NEXT_CHAR_BOUNDARY(p, limit) \
287 do { \
288 if ((p) < (limit)) \
289 (p) += BYTES_BY_CHAR_HEAD (*(p)); \
290 } while (false)
291
292
293 /* If P is after LIMIT, advance P to the previous character boundary.
294 Assumes that P is already at a character boundary of the same
295 multibyte form whose beginning address is LIMIT. */
296
297 #define PREV_CHAR_BOUNDARY(p, limit) \
298 do { \
299 if ((p) > (limit)) \
300 { \
301 const unsigned char *chp = (p); \
302 do { \
303 chp--; \
304 } while (chp >= limit && ! CHAR_HEAD_P (*chp)); \
305 (p) = (BYTES_BY_CHAR_HEAD (*chp) == (p) - chp) ? chp : (p) - 1; \
306 } \
307 } while (false)
308
309 /* Return the character code of character whose multibyte form is at
310 P. Note that this macro unifies CJK characters whose codepoints
311 are in the Private Use Areas (PUAs), so it might return a different
312 codepoint from the one actually stored 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 Note: This macro returns the actual length of the character's
332 multibyte sequence as it is stored in a buffer or string. The
333 character it returns might have a different codepoint that has a
334 different multibyte sequence of a different length, due to possible
335 unification of CJK characters inside string_char. Therefore do NOT
336 assume that the length returned by this macro is identical to the
337 length of the multibyte sequence of the character it returns. */
338
339 #define STRING_CHAR_AND_LENGTH(p, actual_len) \
340 (!((p)[0] & 0x80) \
341 ? ((actual_len) = 1, (p)[0]) \
342 : ! ((p)[0] & 0x20) \
343 ? ((actual_len) = 2, \
344 (((((p)[0] & 0x1F) << 6) \
345 | ((p)[1] & 0x3F)) \
346 + (((unsigned char) (p)[0]) < 0xC2 ? 0x3FFF80 : 0))) \
347 : ! ((p)[0] & 0x10) \
348 ? ((actual_len) = 3, \
349 ((((p)[0] & 0x0F) << 12) \
350 | (((p)[1] & 0x3F) << 6) \
351 | ((p)[2] & 0x3F))) \
352 : string_char ((p), NULL, &actual_len))
353
354
355 /* Like STRING_CHAR, but advance P to the end of multibyte form. */
356
357 #define STRING_CHAR_ADVANCE(p) \
358 (!((p)[0] & 0x80) \
359 ? *(p)++ \
360 : ! ((p)[0] & 0x20) \
361 ? ((p) += 2, \
362 ((((p)[-2] & 0x1F) << 6) \
363 | ((p)[-1] & 0x3F) \
364 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
365 : ! ((p)[0] & 0x10) \
366 ? ((p) += 3, \
367 ((((p)[-3] & 0x0F) << 12) \
368 | (((p)[-2] & 0x3F) << 6) \
369 | ((p)[-1] & 0x3F))) \
370 : string_char ((p), &(p), NULL))
371
372
373 /* Fetch the "next" character from Lisp string STRING at byte position
374 BYTEIDX, character position CHARIDX. Store it into OUTPUT.
375
376 All the args must be side-effect-free.
377 BYTEIDX and CHARIDX must be lvalues;
378 we increment them past the character fetched. */
379
380 #define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
381 do \
382 { \
383 CHARIDX++; \
384 if (STRING_MULTIBYTE (STRING)) \
385 { \
386 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
387 int chlen; \
388 \
389 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
390 BYTEIDX += chlen; \
391 } \
392 else \
393 { \
394 OUTPUT = SREF (STRING, BYTEIDX); \
395 BYTEIDX++; \
396 } \
397 } \
398 while (false)
399
400 /* Like FETCH_STRING_CHAR_ADVANCE, but return a multibyte character
401 even if STRING is unibyte. */
402
403 #define FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
404 do \
405 { \
406 CHARIDX++; \
407 if (STRING_MULTIBYTE (STRING)) \
408 { \
409 unsigned char *chp = &SDATA (STRING)[BYTEIDX]; \
410 int chlen; \
411 \
412 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
413 BYTEIDX += chlen; \
414 } \
415 else \
416 { \
417 OUTPUT = SREF (STRING, BYTEIDX); \
418 BYTEIDX++; \
419 MAKE_CHAR_MULTIBYTE (OUTPUT); \
420 } \
421 } \
422 while (false)
423
424
425 /* Like FETCH_STRING_CHAR_ADVANCE, but assumes STRING is multibyte. */
426
427 #define FETCH_STRING_CHAR_ADVANCE_NO_CHECK(OUTPUT, STRING, CHARIDX, BYTEIDX) \
428 do \
429 { \
430 unsigned char *fetch_ptr = &SDATA (STRING)[BYTEIDX]; \
431 int fetch_len; \
432 \
433 OUTPUT = STRING_CHAR_AND_LENGTH (fetch_ptr, fetch_len); \
434 BYTEIDX += fetch_len; \
435 CHARIDX++; \
436 } \
437 while (false)
438
439
440 /* Like FETCH_STRING_CHAR_ADVANCE, but fetch character from the current
441 buffer. */
442
443 #define FETCH_CHAR_ADVANCE(OUTPUT, CHARIDX, BYTEIDX) \
444 do \
445 { \
446 CHARIDX++; \
447 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))) \
448 { \
449 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
450 int chlen; \
451 \
452 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
453 BYTEIDX += chlen; \
454 } \
455 else \
456 { \
457 OUTPUT = *(BYTE_POS_ADDR (BYTEIDX)); \
458 BYTEIDX++; \
459 } \
460 } \
461 while (false)
462
463
464 /* Like FETCH_CHAR_ADVANCE, but assumes the current buffer is multibyte. */
465
466 #define FETCH_CHAR_ADVANCE_NO_CHECK(OUTPUT, CHARIDX, BYTEIDX) \
467 do \
468 { \
469 unsigned char *chp = BYTE_POS_ADDR (BYTEIDX); \
470 int chlen; \
471 \
472 OUTPUT = STRING_CHAR_AND_LENGTH (chp, chlen); \
473 BYTEIDX += chlen; \
474 CHARIDX++; \
475 } \
476 while (false)
477
478
479 /* Increment the buffer byte position POS_BYTE of the current buffer to
480 the next character boundary. No range checking of POS. */
481
482 #define INC_POS(pos_byte) \
483 do { \
484 unsigned char *chp = BYTE_POS_ADDR (pos_byte); \
485 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
486 } while (false)
487
488
489 /* Decrement the buffer byte position POS_BYTE of the current buffer to
490 the previous character boundary. No range checking of POS. */
491
492 #define DEC_POS(pos_byte) \
493 do { \
494 unsigned char *chp; \
495 \
496 pos_byte--; \
497 if (pos_byte < GPT_BYTE) \
498 chp = BEG_ADDR + pos_byte - BEG_BYTE; \
499 else \
500 chp = BEG_ADDR + GAP_SIZE + pos_byte - BEG_BYTE; \
501 while (!CHAR_HEAD_P (*chp)) \
502 { \
503 chp--; \
504 pos_byte--; \
505 } \
506 } while (false)
507
508 /* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
509
510 #define INC_BOTH(charpos, bytepos) \
511 do \
512 { \
513 (charpos)++; \
514 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
515 (bytepos)++; \
516 else \
517 INC_POS ((bytepos)); \
518 } \
519 while (false)
520
521
522 /* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
523
524 #define DEC_BOTH(charpos, bytepos) \
525 do \
526 { \
527 (charpos)--; \
528 if (NILP (BVAR (current_buffer, enable_multibyte_characters))) \
529 (bytepos)--; \
530 else \
531 DEC_POS ((bytepos)); \
532 } \
533 while (false)
534
535
536 /* Increment the buffer byte position POS_BYTE of the current buffer to
537 the next character boundary. This macro relies on the fact that
538 *GPT_ADDR and *Z_ADDR are always accessible and the values are
539 '\0'. No range checking of POS_BYTE. */
540
541 #define BUF_INC_POS(buf, pos_byte) \
542 do { \
543 unsigned char *chp = BUF_BYTE_ADDRESS (buf, pos_byte); \
544 pos_byte += BYTES_BY_CHAR_HEAD (*chp); \
545 } while (false)
546
547
548 /* Decrement the buffer byte position POS_BYTE of the current buffer to
549 the previous character boundary. No range checking of POS_BYTE. */
550
551 #define BUF_DEC_POS(buf, pos_byte) \
552 do { \
553 unsigned char *chp; \
554 pos_byte--; \
555 if (pos_byte < BUF_GPT_BYTE (buf)) \
556 chp = BUF_BEG_ADDR (buf) + pos_byte - BEG_BYTE; \
557 else \
558 chp = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos_byte - BEG_BYTE;\
559 while (!CHAR_HEAD_P (*chp)) \
560 { \
561 chp--; \
562 pos_byte--; \
563 } \
564 } while (false)
565
566
567 /* Return a non-outlandish value for the tab width. */
568
569 #define SANE_TAB_WIDTH(buf) \
570 sanitize_tab_width (XFASTINT (BVAR (buf, tab_width)))
571 INLINE int
572 sanitize_tab_width (EMACS_INT width)
573 {
574 return 0 < width && width <= 1000 ? width : 8;
575 }
576
577 /* Return the width of ASCII character C. The width is measured by
578 how many columns C will occupy on the screen when displayed in the
579 current buffer. */
580
581 #define ASCII_CHAR_WIDTH(c) \
582 (c < 0x20 \
583 ? (c == '\t' \
584 ? SANE_TAB_WIDTH (current_buffer) \
585 : (c == '\n' ? 0 : (NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))) \
586 : (c < 0x7f \
587 ? 1 \
588 : ((NILP (BVAR (current_buffer, ctl_arrow)) ? 4 : 2))))
589
590 /* Return a non-outlandish value for a character width. */
591
592 INLINE int
593 sanitize_char_width (EMACS_INT width)
594 {
595 return 0 <= width && width <= 1000 ? width : 1000;
596 }
597
598 /* Return the width of character C. The width is measured by how many
599 columns C will occupy on the screen when displayed in the current
600 buffer. */
601
602 #define CHAR_WIDTH(c) \
603 (ASCII_CHAR_P (c) \
604 ? ASCII_CHAR_WIDTH (c) \
605 : sanitize_char_width (XINT (CHAR_TABLE_REF (Vchar_width_table, c))))
606
607 /* If C is a variation selector, return the index of the
608 variation selector (1..256). Otherwise, return 0. */
609
610 #define CHAR_VARIATION_SELECTOR_P(c) \
611 ((c) < 0xFE00 ? 0 \
612 : (c) <= 0xFE0F ? (c) - 0xFE00 + 1 \
613 : (c) < 0xE0100 ? 0 \
614 : (c) <= 0xE01EF ? (c) - 0xE0100 + 17 \
615 : 0)
616
617 /* If C is a high surrogate, return 1. If C is a low surrogate,
618 return 2. Otherwise, return 0. */
619
620 #define CHAR_SURROGATE_PAIR_P(c) \
621 ((c) < 0xD800 ? 0 \
622 : (c) <= 0xDBFF ? 1 \
623 : (c) <= 0xDFFF ? 2 \
624 : 0)
625
626 /* Data type for Unicode general category.
627
628 The order of members must be in sync with the 8th element of the
629 member of unidata-prop-alist (in admin/unidata/unidata-gen.el) for
630 Unicode character property `general-category'. */
631
632 typedef enum {
633 UNICODE_CATEGORY_UNKNOWN = 0,
634 UNICODE_CATEGORY_Lu,
635 UNICODE_CATEGORY_Ll,
636 UNICODE_CATEGORY_Lt,
637 UNICODE_CATEGORY_Lm,
638 UNICODE_CATEGORY_Lo,
639 UNICODE_CATEGORY_Mn,
640 UNICODE_CATEGORY_Mc,
641 UNICODE_CATEGORY_Me,
642 UNICODE_CATEGORY_Nd,
643 UNICODE_CATEGORY_Nl,
644 UNICODE_CATEGORY_No,
645 UNICODE_CATEGORY_Pc,
646 UNICODE_CATEGORY_Pd,
647 UNICODE_CATEGORY_Ps,
648 UNICODE_CATEGORY_Pe,
649 UNICODE_CATEGORY_Pi,
650 UNICODE_CATEGORY_Pf,
651 UNICODE_CATEGORY_Po,
652 UNICODE_CATEGORY_Sm,
653 UNICODE_CATEGORY_Sc,
654 UNICODE_CATEGORY_Sk,
655 UNICODE_CATEGORY_So,
656 UNICODE_CATEGORY_Zs,
657 UNICODE_CATEGORY_Zl,
658 UNICODE_CATEGORY_Zp,
659 UNICODE_CATEGORY_Cc,
660 UNICODE_CATEGORY_Cf,
661 UNICODE_CATEGORY_Cs,
662 UNICODE_CATEGORY_Co,
663 UNICODE_CATEGORY_Cn
664 } unicode_category_t;
665
666 extern EMACS_INT char_resolve_modifier_mask (EMACS_INT) ATTRIBUTE_CONST;
667 extern int char_string (unsigned, unsigned char *);
668 extern int string_char (const unsigned char *,
669 const unsigned char **, int *);
670
671 extern int translate_char (Lisp_Object, int c);
672 extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t);
673 extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t,
674 ptrdiff_t *);
675 extern ptrdiff_t str_to_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t);
676 extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t);
677 extern ptrdiff_t str_to_unibyte (const unsigned char *, unsigned char *,
678 ptrdiff_t);
679 extern ptrdiff_t strwidth (const char *, ptrdiff_t);
680 extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int,
681 ptrdiff_t *, ptrdiff_t *);
682 extern ptrdiff_t lisp_string_width (Lisp_Object, ptrdiff_t,
683 ptrdiff_t *, ptrdiff_t *);
684
685 extern Lisp_Object Vchar_unify_table;
686 extern Lisp_Object string_escape_byte8 (Lisp_Object);
687
688 extern bool alphabeticp (int);
689 extern bool decimalnump (int);
690 extern bool graphicp (int);
691 extern bool printablep (int);
692
693 /* Return a translation table of id number ID. */
694 #define GET_TRANSLATION_TABLE(id) \
695 (XCDR (XVECTOR (Vtranslation_table_vector)->contents[(id)]))
696
697 /* Look up the element in char table OBJ at index CH, and return it as
698 an integer. If the element is not a character, return CH itself. */
699
700 INLINE int
701 char_table_translate (Lisp_Object obj, int ch)
702 {
703 /* This internal function is expected to be called with valid arguments,
704 so there is a eassert instead of CHECK_xxx for the sake of speed. */
705 eassert (CHAR_VALID_P (ch));
706 eassert (CHAR_TABLE_P (obj));
707 obj = CHAR_TABLE_REF (obj, ch);
708 return CHARACTERP (obj) ? XINT (obj) : ch;
709 }
710
711 INLINE_HEADER_END
712
713 #endif /* EMACS_CHARACTER_H */