Merge from emacs-24; up to 2012-12-22T02:59:08Z!cyd@gnu.org
[bpt/emacs.git] / src / character.c
CommitLineData
0168c3d8 1/* Basic character support.
73b0cd50 2
ab422c4d 3Copyright (C) 2001-2013 Free Software Foundation, Inc.
73b0cd50
GM
4Copyright (C) 1995, 1997, 1998, 2001 Electrotechnical Laboratory, JAPAN.
5 Licensed to the Free Software Foundation.
6Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
7 National Institute of Advanced Industrial Science and Technology (AIST)
8 Registration Number H13PRO009
0168c3d8
KH
9
10This file is part of GNU Emacs.
11
9ec0b715 12GNU Emacs is free software: you can redistribute it and/or modify
0168c3d8 13it under the terms of the GNU General Public License as published by
9ec0b715
GM
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
0168c3d8
KH
16
17GNU Emacs is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
9ec0b715 23along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
0168c3d8
KH
24
25/* At first, see the document in `character.h' to understand the code
26 in this file. */
27
28#ifdef emacs
29#include <config.h>
30#endif
31
f162bcc3
PE
32#define CHARACTER_INLINE EXTERN_INLINE
33
0168c3d8
KH
34#include <stdio.h>
35
36#ifdef emacs
37
38#include <sys/types.h>
2b4560a8 39#include <intprops.h>
0168c3d8
KH
40#include "lisp.h"
41#include "character.h"
42#include "buffer.h"
43#include "charset.h"
44#include "composite.h"
45#include "disptab.h"
46
47#else /* not emacs */
48
49#include "mulelib.h"
50
51#endif /* emacs */
52
53Lisp_Object Qcharacterp;
54
955cbe7b 55static Lisp_Object Qauto_fill_chars;
0168c3d8 56
33f91981 57/* Char-table of information about which character to unify to which
6f1b43a0 58 Unicode character. Mainly used by the macro MAYBE_UNIFY_CHAR. */
0168c3d8
KH
59Lisp_Object Vchar_unify_table;
60
c57f3328
KH
61static Lisp_Object Qchar_script_table;
62
0168c3d8
KH
63\f
64
2bde7652
KH
65/* If character code C has modifier masks, reflect them to the
66 character code if possible. Return the resulting code. */
67
d311d28c
PE
68EMACS_INT
69char_resolve_modifier_mask (EMACS_INT c)
2bde7652 70{
d0363d44 71 /* A non-ASCII character can't reflect modifier bits to the code. */
2bde7652
KH
72 if (! ASCII_CHAR_P ((c & ~CHAR_MODIFIER_MASK)))
73 return c;
74
75 /* For Meta, Shift, and Control modifiers, we need special care. */
2bde7652
KH
76 if (c & CHAR_SHIFT)
77 {
78 /* Shift modifier is valid only with [A-Za-z]. */
79 if ((c & 0377) >= 'A' && (c & 0377) <= 'Z')
80 c &= ~CHAR_SHIFT;
81 else if ((c & 0377) >= 'a' && (c & 0377) <= 'z')
82 c = (c & ~CHAR_SHIFT) - ('a' - 'A');
03365d0e
KH
83 /* Shift modifier for control characters and SPC is ignored. */
84 else if ((c & ~CHAR_MODIFIER_MASK) <= 0x20)
d0363d44
KH
85 c &= ~CHAR_SHIFT;
86 }
2bde7652
KH
87 if (c & CHAR_CTL)
88 {
89 /* Simulate the code in lread.c. */
90 /* Allow `\C- ' and `\C-?'. */
03365d0e
KH
91 if ((c & 0377) == ' ')
92 c &= ~0177 & ~ CHAR_CTL;
93 else if ((c & 0377) == '?')
94 c = 0177 | (c & ~0177 & ~CHAR_CTL);
2bde7652
KH
95 /* ASCII control chars are made from letters (both cases),
96 as well as the non-letters within 0100...0137. */
97 else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
98 c &= (037 | (~0177 & ~CHAR_CTL));
99 else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
100 c &= (037 | (~0177 & ~CHAR_CTL));
101 }
e9c1637d 102#if 0 /* This is outside the scope of this function. (bug#4751) */
03365d0e
KH
103 if (c & CHAR_META)
104 {
105 /* Move the meta bit to the right place for a string. */
106 c = (c & ~CHAR_META) | 0x80;
107 }
e9c1637d 108#endif
2bde7652
KH
109
110 return c;
111}
112
113
33f91981
KH
114/* Store multibyte form of character C at P. If C has modifier bits,
115 handle them appropriately. */
116
0168c3d8 117int
971de7fb 118char_string (unsigned int c, unsigned char *p)
0168c3d8
KH
119{
120 int bytes;
121
e3d8eb8c
KH
122 if (c & CHAR_MODIFIER_MASK)
123 {
c5958d4c 124 c = char_resolve_modifier_mask (c);
e3d8eb8c
KH
125 /* If C still has any modifier bits, just ignore it. */
126 c &= ~CHAR_MODIFIER_MASK;
127 }
128
e3d8eb8c 129 if (c <= MAX_3_BYTE_CHAR)
0168c3d8
KH
130 {
131 bytes = CHAR_STRING (c, p);
132 }
133 else if (c <= MAX_4_BYTE_CHAR)
134 {
135 p[0] = (0xF0 | (c >> 18));
136 p[1] = (0x80 | ((c >> 12) & 0x3F));
137 p[2] = (0x80 | ((c >> 6) & 0x3F));
138 p[3] = (0x80 | (c & 0x3F));
139 bytes = 4;
140 }
e3d8eb8c 141 else if (c <= MAX_5_BYTE_CHAR)
0168c3d8
KH
142 {
143 p[0] = 0xF8;
144 p[1] = (0x80 | ((c >> 18) & 0x0F));
145 p[2] = (0x80 | ((c >> 12) & 0x3F));
146 p[3] = (0x80 | ((c >> 6) & 0x3F));
147 p[4] = (0x80 | (c & 0x3F));
148 bytes = 5;
149 }
5aa91c9b 150 else if (c <= MAX_CHAR)
e3d8eb8c
KH
151 {
152 c = CHAR_TO_BYTE8 (c);
153 bytes = BYTE8_STRING (c, p);
154 }
5aa91c9b 155 else
e6c3da20 156 error ("Invalid character: %x", c);
1889b238 157
0168c3d8
KH
158 return bytes;
159}
160
161
224a3131 162/* Return a character whose multibyte form is at P. If LEN is not
33f91981 163 NULL, it must be a pointer to integer. In that case, set *LEN to
224a3131 164 the byte length of the multibyte form. If ADVANCED is not NULL, it
33f91981 165 must be a pointer to unsigned char. In that case, set *ADVANCED to
224a3131 166 the ending address (i.e., the starting address of the next
33f91981
KH
167 character) of the multibyte form. */
168
0168c3d8 169int
971de7fb 170string_char (const unsigned char *p, const unsigned char **advanced, int *len)
0168c3d8 171{
1889b238 172 int c;
15843e6f 173 const unsigned char *saved_p = p;
0168c3d8
KH
174
175 if (*p < 0x80 || ! (*p & 0x20) || ! (*p & 0x10))
176 {
177 c = STRING_CHAR_ADVANCE (p);
178 }
179 else if (! (*p & 0x08))
180 {
181 c = ((((p)[0] & 0xF) << 18)
182 | (((p)[1] & 0x3F) << 12)
183 | (((p)[2] & 0x3F) << 6)
184 | ((p)[3] & 0x3F));
185 p += 4;
186 }
187 else
188 {
189 c = ((((p)[1] & 0x3F) << 18)
190 | (((p)[2] & 0x3F) << 12)
191 | (((p)[3] & 0x3F) << 6)
192 | ((p)[4] & 0x3F));
193 p += 5;
194 }
195
0168c3d8
KH
196 if (len)
197 *len = p - saved_p;
198 if (advanced)
199 *advanced = p;
200 return c;
201}
202
203
224a3131
EZ
204/* Translate character C by translation table TABLE. If no translation is
205 found in TABLE, return the untranslated character. If TABLE is a list,
206 elements are char tables. In that case, recursively translate C by all the
207 tables in the list. */
0168c3d8
KH
208
209int
971de7fb 210translate_char (Lisp_Object table, int c)
0168c3d8 211{
10453be9
KH
212 if (CHAR_TABLE_P (table))
213 {
214 Lisp_Object ch;
215
216 ch = CHAR_TABLE_REF (table, c);
217 if (CHARACTERP (ch))
218 c = XINT (ch);
219 }
220 else
221 {
222 for (; CONSP (table); table = XCDR (table))
223 c = translate_char (XCAR (table), c);
224 }
225 return c;
0168c3d8
KH
226}
227
2e5db15c 228/* Convert ASCII or 8-bit character C to unibyte. If C is none of
461c2ab9 229 them, return (C & 0xFF). */
0168c3d8
KH
230
231int
461c2ab9 232multibyte_char_to_unibyte (int c)
0168c3d8 233{
2e5db15c
KH
234 if (c < 0x80)
235 return c;
b672c5ae
KH
236 if (CHAR_BYTE8_P (c))
237 return CHAR_TO_BYTE8 (c);
2e5db15c 238 return (c & 0xFF);
0168c3d8
KH
239}
240
935d5b02
KH
241/* Like multibyte_char_to_unibyte, but return -1 if C is not supported
242 by charset_unibyte. */
243
244int
971de7fb 245multibyte_char_to_unibyte_safe (int c)
935d5b02 246{
2e5db15c
KH
247 if (c < 0x80)
248 return c;
935d5b02
KH
249 if (CHAR_BYTE8_P (c))
250 return CHAR_TO_BYTE8 (c);
2e5db15c 251 return -1;
935d5b02 252}
0168c3d8
KH
253
254DEFUN ("characterp", Fcharacterp, Scharacterp, 1, 2, 0,
bc985c87 255 doc: /* Return non-nil if OBJECT is a character.
4abcdac8
CY
256In Emacs Lisp, characters are represented by character codes, which
257are non-negative integers. The function `max-char' returns the
258maximum character code.
bc985c87 259usage: (characterp OBJECT) */)
5842a27b 260 (Lisp_Object object, Lisp_Object ignore)
0168c3d8
KH
261{
262 return (CHARACTERP (object) ? Qt : Qnil);
263}
264
265DEFUN ("max-char", Fmax_char, Smax_char, 0, 0, 0,
266 doc: /* Return the character of the maximum code. */)
5842a27b 267 (void)
0168c3d8
KH
268{
269 return make_number (MAX_CHAR);
270}
271
272DEFUN ("unibyte-char-to-multibyte", Funibyte_char_to_multibyte,
273 Sunibyte_char_to_multibyte, 1, 1, 0,
5556875b 274 doc: /* Convert the byte CH to multibyte character. */)
5842a27b 275 (Lisp_Object ch)
0168c3d8
KH
276{
277 int c;
0168c3d8
KH
278
279 CHECK_CHARACTER (ch);
280 c = XFASTINT (ch);
2e5db15c
KH
281 if (c >= 0x100)
282 error ("Not a unibyte character: %d", c);
4c0354d7 283 MAKE_CHAR_MULTIBYTE (c);
0168c3d8
KH
284 return make_number (c);
285}
286
287DEFUN ("multibyte-char-to-unibyte", Fmultibyte_char_to_unibyte,
288 Smultibyte_char_to_unibyte, 1, 1, 0,
5556875b
SM
289 doc: /* Convert the multibyte character CH to a byte.
290If the multibyte character does not represent a byte, return -1. */)
5842a27b 291 (Lisp_Object ch)
0168c3d8 292{
5556875b 293 int cm;
0168c3d8
KH
294
295 CHECK_CHARACTER (ch);
5556875b
SM
296 cm = XFASTINT (ch);
297 if (cm < 256)
298 /* Can't distinguish a byte read from a unibyte buffer from
299 a latin1 char, so let's let it slide. */
300 return ch;
301 else
302 {
2afc21f5 303 int cu = CHAR_TO_BYTE_SAFE (cm);
5556875b
SM
304 return make_number (cu);
305 }
0168c3d8
KH
306}
307
25ed9e61
KH
308
309/* Return width (columns) of C considering the buffer display table DP. */
310
6e6c82a4 311static ptrdiff_t
25ed9e61
KH
312char_width (int c, struct Lisp_Char_Table *dp)
313{
6e6c82a4 314 ptrdiff_t width = CHAR_WIDTH (c);
25ed9e61
KH
315
316 if (dp)
317 {
318 Lisp_Object disp = DISP_CHAR_VECTOR (dp, c), ch;
319 int i;
320
321 if (VECTORP (disp))
322 for (i = 0, width = 0; i < ASIZE (disp); i++)
323 {
324 ch = AREF (disp, i);
325 if (CHARACTERP (ch))
df0b2940
PE
326 {
327 int w = CHAR_WIDTH (XFASTINT (ch));
328 if (INT_ADD_OVERFLOW (width, w))
329 string_overflow ();
330 width += w;
331 }
25ed9e61
KH
332 }
333 }
334 return width;
335}
336
337
a7ca3326 338DEFUN ("char-width", Fchar_width, Schar_width, 1, 1, 0,
0168c3d8
KH
339 doc: /* Return width of CHAR when displayed in the current buffer.
340The width is measured by how many columns it occupies on the screen.
add553ac
JB
341Tab is taken to occupy `tab-width' columns.
342usage: (char-width CHAR) */)
5842a27b 343 (Lisp_Object ch)
0168c3d8 344{
df0b2940 345 int c;
6e6c82a4 346 ptrdiff_t width;
0168c3d8
KH
347
348 CHECK_CHARACTER (ch);
349 c = XINT (ch);
25ed9e61 350 width = char_width (c, buffer_display_table ());
0168c3d8
KH
351 return make_number (width);
352}
353
0168c3d8
KH
354/* Return width of string STR of length LEN when displayed in the
355 current buffer. The width is measured by how many columns it
356 occupies on the screen. If PRECISION > 0, return the width of
357 longest substring that doesn't exceed PRECISION, and set number of
358 characters and bytes of the substring in *NCHARS and *NBYTES
359 respectively. */
360
d311d28c
PE
361ptrdiff_t
362c_string_width (const unsigned char *str, ptrdiff_t len, int precision,
363 ptrdiff_t *nchars, ptrdiff_t *nbytes)
0168c3d8 364{
d311d28c
PE
365 ptrdiff_t i = 0, i_byte = 0;
366 ptrdiff_t width = 0;
0168c3d8
KH
367 struct Lisp_Char_Table *dp = buffer_display_table ();
368
369 while (i_byte < len)
370 {
25ed9e61 371 int bytes;
62a6e103 372 int c = STRING_CHAR_AND_LENGTH (str + i_byte, bytes);
6e6c82a4 373 ptrdiff_t thiswidth = char_width (c, dp);
0168c3d8 374
df0b2940 375 if (precision <= 0)
0168c3d8 376 {
df0b2940
PE
377 if (INT_ADD_OVERFLOW (width, thiswidth))
378 string_overflow ();
0168c3d8 379 }
df0b2940 380 else if (precision - width < thiswidth)
0168c3d8
KH
381 {
382 *nchars = i;
383 *nbytes = i_byte;
384 return width;
385 }
386 i++;
387 i_byte += bytes;
388 width += thiswidth;
389 }
390
391 if (precision > 0)
392 {
393 *nchars = i;
394 *nbytes = i_byte;
395 }
396
397 return width;
398}
399
1889b238
KH
400/* Return width of string STR of length LEN when displayed in the
401 current buffer. The width is measured by how many columns it
402 occupies on the screen. */
403
d311d28c
PE
404ptrdiff_t
405strwidth (const char *str, ptrdiff_t len)
1889b238 406{
7469ef5d 407 return c_string_width ((const unsigned char *) str, len, -1, NULL, NULL);
1889b238
KH
408}
409
0168c3d8
KH
410/* Return width of Lisp string STRING when displayed in the current
411 buffer. The width is measured by how many columns it occupies on
412 the screen while paying attention to compositions. If PRECISION >
413 0, return the width of longest substring that doesn't exceed
414 PRECISION, and set number of characters and bytes of the substring
415 in *NCHARS and *NBYTES respectively. */
416
d311d28c
PE
417ptrdiff_t
418lisp_string_width (Lisp_Object string, ptrdiff_t precision,
419 ptrdiff_t *nchars, ptrdiff_t *nbytes)
0168c3d8 420{
d311d28c 421 ptrdiff_t len = SCHARS (string);
0aee65b9
KH
422 /* This set multibyte to 0 even if STRING is multibyte when it
423 contains only ascii and eight-bit-graphic, but that's
424 intentional. */
d5172d4f 425 bool multibyte = len < SBYTES (string);
8f924df7 426 unsigned char *str = SDATA (string);
d311d28c
PE
427 ptrdiff_t i = 0, i_byte = 0;
428 ptrdiff_t width = 0;
0168c3d8
KH
429 struct Lisp_Char_Table *dp = buffer_display_table ();
430
431 while (i < len)
432 {
d311d28c 433 ptrdiff_t chars, bytes, thiswidth;
0168c3d8 434 Lisp_Object val;
ebfa62c0 435 ptrdiff_t cmp_id;
d311d28c 436 ptrdiff_t ignore, end;
0168c3d8
KH
437
438 if (find_composition (i, -1, &ignore, &end, &val, string)
439 && ((cmp_id = get_composition_id (i, i_byte, end - i, val, string))
440 >= 0))
441 {
442 thiswidth = composition_table[cmp_id]->width;
443 chars = end - i;
444 bytes = string_char_to_byte (string, end) - i_byte;
445 }
0168c3d8
KH
446 else
447 {
0aee65b9 448 int c;
0168c3d8 449
0aee65b9 450 if (multibyte)
2b4560a8
PE
451 {
452 int cbytes;
453 c = STRING_CHAR_AND_LENGTH (str + i_byte, cbytes);
454 bytes = cbytes;
455 }
0aee65b9
KH
456 else
457 c = str[i_byte], bytes = 1;
0168c3d8 458 chars = 1;
25ed9e61 459 thiswidth = char_width (c, dp);
0168c3d8
KH
460 }
461
2b4560a8
PE
462 if (precision <= 0)
463 {
464#ifdef emacs
465 if (INT_ADD_OVERFLOW (width, thiswidth))
466 string_overflow ();
467#endif
468 }
469 else if (precision - width < thiswidth)
0168c3d8
KH
470 {
471 *nchars = i;
472 *nbytes = i_byte;
473 return width;
474 }
475 i += chars;
476 i_byte += bytes;
477 width += thiswidth;
ef3ff036 478 }
0168c3d8
KH
479
480 if (precision > 0)
481 {
482 *nchars = i;
483 *nbytes = i_byte;
484 }
485
486 return width;
487}
488
489DEFUN ("string-width", Fstring_width, Sstring_width, 1, 1, 0,
490 doc: /* Return width of STRING when displayed in the current buffer.
491Width is measured by how many columns it occupies on the screen.
492When calculating width of a multibyte character in STRING,
493only the base leading-code is considered; the validity of
494the following bytes is not checked. Tabs in STRING are always
add553ac
JB
495taken to occupy `tab-width' columns.
496usage: (string-width STRING) */)
5842a27b 497 (Lisp_Object str)
0168c3d8
KH
498{
499 Lisp_Object val;
500
501 CHECK_STRING (str);
502 XSETFASTINT (val, lisp_string_width (str, -1, NULL, NULL));
503 return val;
504}
505
0168c3d8
KH
506/* Return the number of characters in the NBYTES bytes at PTR.
507 This works by looking at the contents and checking for multibyte
508 sequences while assuming that there's no invalid sequence.
509 However, if the current buffer has enable-multibyte-characters =
510 nil, we treat each byte as a character. */
511
d311d28c
PE
512ptrdiff_t
513chars_in_text (const unsigned char *ptr, ptrdiff_t nbytes)
0168c3d8
KH
514{
515 /* current_buffer is null at early stages of Emacs initialization. */
516 if (current_buffer == 0
4b4deea2 517 || NILP (BVAR (current_buffer, enable_multibyte_characters)))
0168c3d8
KH
518 return nbytes;
519
520 return multibyte_chars_in_text (ptr, nbytes);
521}
522
523/* Return the number of characters in the NBYTES bytes at PTR.
524 This works by looking at the contents and checking for multibyte
525 sequences while assuming that there's no invalid sequence. It
526 ignores enable-multibyte-characters. */
527
d311d28c
PE
528ptrdiff_t
529multibyte_chars_in_text (const unsigned char *ptr, ptrdiff_t nbytes)
0168c3d8 530{
8f924df7 531 const unsigned char *endp = ptr + nbytes;
d311d28c 532 ptrdiff_t chars = 0;
0168c3d8
KH
533
534 while (ptr < endp)
535 {
d311d28c 536 int len = MULTIBYTE_LENGTH (ptr, endp);
0168c3d8
KH
537
538 if (len == 0)
1088b922 539 emacs_abort ();
0168c3d8
KH
540 ptr += len;
541 chars++;
542 }
543
544 return chars;
545}
546
547/* Parse unibyte text at STR of LEN bytes as a multibyte text, count
548 characters and bytes in it, and store them in *NCHARS and *NBYTES
549 respectively. On counting bytes, pay attention to that 8-bit
550 characters not constructing a valid multibyte sequence are
551 represented by 2-byte in a multibyte text. */
552
553void
d311d28c
PE
554parse_str_as_multibyte (const unsigned char *str, ptrdiff_t len,
555 ptrdiff_t *nchars, ptrdiff_t *nbytes)
0168c3d8 556{
8f924df7 557 const unsigned char *endp = str + len;
d311d28c
PE
558 int n;
559 ptrdiff_t chars = 0, bytes = 0;
0168c3d8
KH
560
561 if (len >= MAX_MULTIBYTE_LENGTH)
562 {
8f924df7 563 const unsigned char *adjusted_endp = endp - MAX_MULTIBYTE_LENGTH;
0168c3d8
KH
564 while (str < adjusted_endp)
565 {
fc9a17bc
KH
566 if (! CHAR_BYTE8_HEAD_P (*str)
567 && (n = MULTIBYTE_LENGTH_NO_CHECK (str)) > 0)
0168c3d8
KH
568 str += n, bytes += n;
569 else
570 str++, bytes += 2;
571 chars++;
572 }
573 }
574 while (str < endp)
575 {
fc9a17bc
KH
576 if (! CHAR_BYTE8_HEAD_P (*str)
577 && (n = MULTIBYTE_LENGTH (str, endp)) > 0)
0168c3d8
KH
578 str += n, bytes += n;
579 else
580 str++, bytes += 2;
581 chars++;
582 }
583
584 *nchars = chars;
585 *nbytes = bytes;
586 return;
587}
588
589/* Arrange unibyte text at STR of NBYTES bytes as a multibyte text.
da6062e6 590 It actually converts only such 8-bit characters that don't construct
0168c3d8
KH
591 a multibyte sequence to multibyte forms of Latin-1 characters. If
592 NCHARS is nonzero, set *NCHARS to the number of characters in the
593 text. It is assured that we can use LEN bytes at STR as a work
594 area and that is enough. Return the number of bytes of the
595 resulting text. */
596
d311d28c
PE
597ptrdiff_t
598str_as_multibyte (unsigned char *str, ptrdiff_t len, ptrdiff_t nbytes,
599 ptrdiff_t *nchars)
0168c3d8
KH
600{
601 unsigned char *p = str, *endp = str + nbytes;
602 unsigned char *to;
d311d28c 603 ptrdiff_t chars = 0;
0168c3d8
KH
604 int n;
605
606 if (nbytes >= MAX_MULTIBYTE_LENGTH)
607 {
608 unsigned char *adjusted_endp = endp - MAX_MULTIBYTE_LENGTH;
609 while (p < adjusted_endp
fc9a17bc 610 && ! CHAR_BYTE8_HEAD_P (*p)
0168c3d8
KH
611 && (n = MULTIBYTE_LENGTH_NO_CHECK (p)) > 0)
612 p += n, chars++;
613 }
fc9a17bc
KH
614 while (p < endp
615 && ! CHAR_BYTE8_HEAD_P (*p)
616 && (n = MULTIBYTE_LENGTH (p, endp)) > 0)
0168c3d8
KH
617 p += n, chars++;
618 if (nchars)
619 *nchars = chars;
620 if (p == endp)
621 return nbytes;
622
623 to = p;
624 nbytes = endp - p;
625 endp = str + len;
72af86bd 626 memmove (endp - nbytes, p, nbytes);
0168c3d8
KH
627 p = endp - nbytes;
628
629 if (nbytes >= MAX_MULTIBYTE_LENGTH)
630 {
631 unsigned char *adjusted_endp = endp - MAX_MULTIBYTE_LENGTH;
632 while (p < adjusted_endp)
633 {
fc9a17bc
KH
634 if (! CHAR_BYTE8_HEAD_P (*p)
635 && (n = MULTIBYTE_LENGTH_NO_CHECK (p)) > 0)
0168c3d8
KH
636 {
637 while (n--)
638 *to++ = *p++;
639 }
640 else
641 {
642 int c = *p++;
643 c = BYTE8_TO_CHAR (c);
644 to += CHAR_STRING (c, to);
645 }
646 }
647 chars++;
648 }
649 while (p < endp)
650 {
fc9a17bc
KH
651 if (! CHAR_BYTE8_HEAD_P (*p)
652 && (n = MULTIBYTE_LENGTH (p, endp)) > 0)
0168c3d8
KH
653 {
654 while (n--)
655 *to++ = *p++;
8f924df7 656 }
0168c3d8
KH
657 else
658 {
659 int c = *p++;
660 c = BYTE8_TO_CHAR (c);
661 to += CHAR_STRING (c, to);
662 }
663 chars++;
664 }
665 if (nchars)
666 *nchars = chars;
667 return (to - str);
668}
669
670/* Parse unibyte string at STR of LEN bytes, and return the number of
e1dbe924 671 bytes it may occupy when converted to multibyte string by
0168c3d8
KH
672 `str_to_multibyte'. */
673
d311d28c
PE
674ptrdiff_t
675count_size_as_multibyte (const unsigned char *str, ptrdiff_t len)
0168c3d8 676{
eec47d6b 677 const unsigned char *endp = str + len;
d311d28c 678 ptrdiff_t bytes;
0168c3d8
KH
679
680 for (bytes = 0; str < endp; str++)
de883a70
PE
681 {
682 int n = *str < 0x80 ? 1 : 2;
683 if (INT_ADD_OVERFLOW (bytes, n))
684 string_overflow ();
685 bytes += n;
686 }
0168c3d8
KH
687 return bytes;
688}
689
690
ef3ff036 691/* Convert unibyte text at STR of BYTES bytes to a multibyte text
0168c3d8
KH
692 that contains the same single-byte characters. It actually
693 converts all 8-bit characters to multibyte forms. It is assured
694 that we can use LEN bytes at STR as a work area and that is
695 enough. */
696
d311d28c
PE
697ptrdiff_t
698str_to_multibyte (unsigned char *str, ptrdiff_t len, ptrdiff_t bytes)
0168c3d8
KH
699{
700 unsigned char *p = str, *endp = str + bytes;
701 unsigned char *to;
702
703 while (p < endp && *p < 0x80) p++;
704 if (p == endp)
705 return bytes;
706 to = p;
707 bytes = endp - p;
708 endp = str + len;
72af86bd 709 memmove (endp - bytes, p, bytes);
0168c3d8 710 p = endp - bytes;
8f924df7 711 while (p < endp)
0168c3d8
KH
712 {
713 int c = *p++;
714
715 if (c >= 0x80)
716 c = BYTE8_TO_CHAR (c);
717 to += CHAR_STRING (c, to);
718 }
719 return (to - str);
720}
721
722/* Arrange multibyte text at STR of LEN bytes as a unibyte text. It
723 actually converts characters in the range 0x80..0xFF to
724 unibyte. */
725
d311d28c
PE
726ptrdiff_t
727str_as_unibyte (unsigned char *str, ptrdiff_t bytes)
0168c3d8 728{
15843e6f
KH
729 const unsigned char *p = str, *endp = str + bytes;
730 unsigned char *to;
0168c3d8
KH
731 int c, len;
732
733 while (p < endp)
734 {
735 c = *p;
736 len = BYTES_BY_CHAR_HEAD (c);
737 if (CHAR_BYTE8_HEAD_P (c))
738 break;
739 p += len;
740 }
15843e6f 741 to = str + (p - str);
8f924df7 742 while (p < endp)
0168c3d8
KH
743 {
744 c = *p;
745 len = BYTES_BY_CHAR_HEAD (c);
746 if (CHAR_BYTE8_HEAD_P (c))
747 {
748 c = STRING_CHAR_ADVANCE (p);
749 *to++ = CHAR_TO_BYTE8 (c);
750 }
751 else
752 {
753 while (len--) *to++ = *p++;
754 }
755 }
756 return (to - str);
757}
758
4aa40bb8
KH
759/* Convert eight-bit chars in SRC (in multibyte form) to the
760 corresponding byte and store in DST. CHARS is the number of
761 characters in SRC. The value is the number of bytes stored in DST.
762 Usually, the value is the same as CHARS, but is less than it if SRC
d5172d4f 763 contains a non-ASCII, non-eight-bit character. */
4aa40bb8 764
d311d28c 765ptrdiff_t
d5172d4f 766str_to_unibyte (const unsigned char *src, unsigned char *dst, ptrdiff_t chars)
4aa40bb8 767{
d311d28c 768 ptrdiff_t i;
4aa40bb8
KH
769
770 for (i = 0; i < chars; i++)
771 {
772 int c = STRING_CHAR_ADVANCE (src);
773
774 if (CHAR_BYTE8_P (c))
775 c = CHAR_TO_BYTE8 (c);
d5172d4f 776 else if (! ASCII_CHAR_P (c))
4aa40bb8
KH
777 return i;
778 *dst++ = c;
779 }
780 return i;
781}
782
783
d311d28c 784static ptrdiff_t
971de7fb 785string_count_byte8 (Lisp_Object string)
0168c3d8 786{
d5172d4f 787 bool multibyte = STRING_MULTIBYTE (string);
d311d28c 788 ptrdiff_t nbytes = SBYTES (string);
8f924df7 789 unsigned char *p = SDATA (string);
0168c3d8 790 unsigned char *pend = p + nbytes;
d311d28c 791 ptrdiff_t count = 0;
0168c3d8
KH
792 int c, len;
793
794 if (multibyte)
795 while (p < pend)
796 {
797 c = *p;
798 len = BYTES_BY_CHAR_HEAD (c);
799
800 if (CHAR_BYTE8_HEAD_P (c))
801 count++;
802 p += len;
803 }
804 else
805 while (p < pend)
806 {
807 if (*p++ >= 0x80)
808 count++;
809 }
810 return count;
811}
812
813
814Lisp_Object
971de7fb 815string_escape_byte8 (Lisp_Object string)
0168c3d8 816{
d311d28c
PE
817 ptrdiff_t nchars = SCHARS (string);
818 ptrdiff_t nbytes = SBYTES (string);
d5172d4f 819 bool multibyte = STRING_MULTIBYTE (string);
d311d28c 820 ptrdiff_t byte8_count;
15843e6f
KH
821 const unsigned char *src, *src_end;
822 unsigned char *dst;
0168c3d8
KH
823 Lisp_Object val;
824 int c, len;
825
826 if (multibyte && nchars == nbytes)
827 return string;
828
829 byte8_count = string_count_byte8 (string);
830
831 if (byte8_count == 0)
832 return string;
833
834 if (multibyte)
14162469
EZ
835 {
836 if ((MOST_POSITIVE_FIXNUM - nchars) / 3 < byte8_count
c9d624c6 837 || (STRING_BYTES_BOUND - nbytes) / 2 < byte8_count)
cb93f9be 838 string_overflow ();
14162469
EZ
839
840 /* Convert 2-byte sequence of byte8 chars to 4-byte octal. */
841 val = make_uninit_multibyte_string (nchars + byte8_count * 3,
842 nbytes + byte8_count * 2);
843 }
0168c3d8 844 else
14162469 845 {
c9d624c6 846 if ((STRING_BYTES_BOUND - nbytes) / 3 < byte8_count)
cb93f9be 847 string_overflow ();
ef3ff036 848
14162469
EZ
849 /* Convert 1-byte sequence of byte8 chars to 4-byte octal. */
850 val = make_uninit_string (nbytes + byte8_count * 3);
851 }
0168c3d8 852
8f924df7 853 src = SDATA (string);
0168c3d8 854 src_end = src + nbytes;
8f924df7 855 dst = SDATA (val);
0168c3d8
KH
856 if (multibyte)
857 while (src < src_end)
858 {
859 c = *src;
860 len = BYTES_BY_CHAR_HEAD (c);
861
862 if (CHAR_BYTE8_HEAD_P (c))
863 {
864 c = STRING_CHAR_ADVANCE (src);
865 c = CHAR_TO_BYTE8 (c);
99027bdd 866 dst += sprintf ((char *) dst, "\\%03o", c);
0168c3d8
KH
867 }
868 else
869 while (len--) *dst++ = *src++;
870 }
871 else
872 while (src < src_end)
873 {
874 c = *src++;
875 if (c >= 0x80)
99027bdd 876 dst += sprintf ((char *) dst, "\\%03o", c);
0168c3d8
KH
877 else
878 *dst++ = c;
879 }
880 return val;
881}
882
883\f
a7ca3326 884DEFUN ("string", Fstring, Sstring, 0, MANY, 0,
0168c3d8 885 doc: /*
d2e83296
DL
886Concatenate all the argument characters and make the result a string.
887usage: (string &rest CHARACTERS) */)
f66c7cf8 888 (ptrdiff_t n, Lisp_Object *args)
0168c3d8 889{
f66c7cf8 890 ptrdiff_t i;
c5101a77 891 int c;
754790b6
CY
892 unsigned char *buf, *p;
893 Lisp_Object str;
894 USE_SAFE_ALLOCA;
895
0065d054 896 SAFE_NALLOCA (buf, MAX_MULTIBYTE_LENGTH, n);
754790b6 897 p = buf;
0168c3d8
KH
898
899 for (i = 0; i < n; i++)
900 {
901 CHECK_CHARACTER (args[i]);
902 c = XINT (args[i]);
903 p += CHAR_STRING (c, p);
904 }
905
754790b6
CY
906 str = make_string_from_bytes ((char *) buf, n, p - buf);
907 SAFE_FREE ();
908 return str;
0168c3d8
KH
909}
910
70b4969d 911DEFUN ("unibyte-string", Funibyte_string, Sunibyte_string, 0, MANY, 0,
87d6f965
KH
912 doc: /* Concatenate all the argument bytes and make the result a unibyte string.
913usage: (unibyte-string &rest BYTES) */)
f66c7cf8 914 (ptrdiff_t n, Lisp_Object *args)
70b4969d 915{
f66c7cf8 916 ptrdiff_t i;
754790b6
CY
917 Lisp_Object str;
918 USE_SAFE_ALLOCA;
98c6f1e3
PE
919 unsigned char *buf = SAFE_ALLOCA (n);
920 unsigned char *p = buf;
70b4969d
KH
921
922 for (i = 0; i < n; i++)
923 {
af5a5a98 924 CHECK_RANGED_INTEGER (args[i], 0, 255);
a14e1568 925 *p++ = XINT (args[i]);
70b4969d
KH
926 }
927
754790b6
CY
928 str = make_string_from_bytes ((char *) buf, n, p - buf);
929 SAFE_FREE ();
930 return str;
70b4969d
KH
931}
932
c73ae4ae 933DEFUN ("char-resolve-modifiers", Fchar_resolve_modifiers,
d0363d44
KH
934 Schar_resolve_modifiers, 1, 1, 0,
935 doc: /* Resolve modifiers in the character CHAR.
936The value is a character with modifiers resolved into the character
937code. Unresolved modifiers are kept in the value.
c73ae4ae 938usage: (char-resolve-modifiers CHAR) */)
5842a27b 939 (Lisp_Object character)
d0363d44 940{
d311d28c 941 EMACS_INT c;
d0363d44
KH
942
943 CHECK_NUMBER (character);
944 c = XINT (character);
945 return make_number (char_resolve_modifier_mask (c));
946}
947
ee107a89
KH
948DEFUN ("get-byte", Fget_byte, Sget_byte, 0, 2, 0,
949 doc: /* Return a byte value of a character at point.
950Optional 1st arg POSITION, if non-nil, is a position of a character to get
951a byte value.
952Optional 2nd arg STRING, if non-nil, is a string of which first
953character is a target to get a byte value. In this case, POSITION, if
954non-nil, is an index of a target character in the string.
955
956If the current buffer (or STRING) is multibyte, and the target
8350f087 957character is not ASCII nor 8-bit character, an error is signaled. */)
5842a27b 958 (Lisp_Object position, Lisp_Object string)
ee107a89
KH
959{
960 int c;
d311d28c 961 ptrdiff_t pos;
ee107a89
KH
962 unsigned char *p;
963
964 if (NILP (string))
965 {
966 if (NILP (position))
967 {
968 p = PT_ADDR;
4d8e170e 969 }
ee107a89
KH
970 else
971 {
972 CHECK_NUMBER_COERCE_MARKER (position);
973 if (XINT (position) < BEGV || XINT (position) >= ZV)
974 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
975 pos = XFASTINT (position);
976 p = CHAR_POS_ADDR (pos);
977 }
4b4deea2 978 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
d5998e03 979 return make_number (*p);
ee107a89
KH
980 }
981 else
982 {
983 CHECK_STRING (string);
984 if (NILP (position))
985 {
986 p = SDATA (string);
987 }
988 else
989 {
990 CHECK_NATNUM (position);
991 if (XINT (position) >= SCHARS (string))
992 args_out_of_range (string, position);
993 pos = XFASTINT (position);
994 p = SDATA (string) + string_char_to_byte (string, pos);
995 }
d5998e03
KH
996 if (! STRING_MULTIBYTE (string))
997 return make_number (*p);
ee107a89 998 }
62a6e103 999 c = STRING_CHAR (p);
ee107a89
KH
1000 if (CHAR_BYTE8_P (c))
1001 c = CHAR_TO_BYTE8 (c);
1002 else if (! ASCII_CHAR_P (c))
1003 error ("Not an ASCII nor an 8-bit character: %d", c);
1004 return make_number (c);
1005}
1006
0168c3d8
KH
1007#ifdef emacs
1008
1009void
971de7fb 1010syms_of_character (void)
0168c3d8
KH
1011{
1012 DEFSYM (Qcharacterp, "characterp");
1013 DEFSYM (Qauto_fill_chars, "auto-fill-chars");
1014
1015 staticpro (&Vchar_unify_table);
1016 Vchar_unify_table = Qnil;
1017
1018 defsubr (&Smax_char);
1019 defsubr (&Scharacterp);
1020 defsubr (&Sunibyte_char_to_multibyte);
1021 defsubr (&Smultibyte_char_to_unibyte);
0168c3d8
KH
1022 defsubr (&Schar_width);
1023 defsubr (&Sstring_width);
0168c3d8 1024 defsubr (&Sstring);
70b4969d 1025 defsubr (&Sunibyte_string);
d0363d44 1026 defsubr (&Schar_resolve_modifiers);
ee107a89 1027 defsubr (&Sget_byte);
0168c3d8 1028
29208e82 1029 DEFVAR_LISP ("translation-table-vector", Vtranslation_table_vector,
0168c3d8 1030 doc: /*
68978cf0
DL
1031Vector recording all translation tables ever defined.
1032Each element is a pair (SYMBOL . TABLE) relating the table to the
1033symbol naming it. The ID of a translation table is an index into this vector. */);
0168c3d8
KH
1034 Vtranslation_table_vector = Fmake_vector (make_number (16), Qnil);
1035
29208e82 1036 DEFVAR_LISP ("auto-fill-chars", Vauto_fill_chars,
0168c3d8
KH
1037 doc: /*
1038A char-table for characters which invoke auto-filling.
1039Such characters have value t in this table. */);
1040 Vauto_fill_chars = Fmake_char_table (Qauto_fill_chars, Qnil);
6cc0e1ca
DL
1041 CHAR_TABLE_SET (Vauto_fill_chars, ' ', Qt);
1042 CHAR_TABLE_SET (Vauto_fill_chars, '\n', Qt);
0168c3d8 1043
29208e82 1044 DEFVAR_LISP ("char-width-table", Vchar_width_table,
0168c3d8
KH
1045 doc: /*
1046A char-table for width (columns) of each character. */);
1047 Vchar_width_table = Fmake_char_table (Qnil, make_number (1));
be8b50bc
KH
1048 char_table_set_range (Vchar_width_table, 0x80, 0x9F, make_number (4));
1049 char_table_set_range (Vchar_width_table, MAX_5_BYTE_CHAR + 1, MAX_CHAR,
1050 make_number (4));
0168c3d8 1051
29208e82 1052 DEFVAR_LISP ("printable-chars", Vprintable_chars,
0168c3d8 1053 doc: /* A char-table for each printable character. */);
db6d4189 1054 Vprintable_chars = Fmake_char_table (Qnil, Qnil);
67dde660
KH
1055 Fset_char_table_range (Vprintable_chars,
1056 Fcons (make_number (32), make_number (126)), Qt);
1057 Fset_char_table_range (Vprintable_chars,
1058 Fcons (make_number (160),
1059 make_number (MAX_5_BYTE_CHAR)), Qt);
15843e6f 1060
29208e82 1061 DEFVAR_LISP ("char-script-table", Vchar_script_table,
c57f3328
KH
1062 doc: /* Char table of script symbols.
1063It has one extra slot whose value is a list of script symbols. */);
1064
1065 /* Intern this now in case it isn't already done.
1066 Setting this variable twice is harmless.
1067 But don't staticpro it here--that is done in alloc.c. */
d67b4f80 1068 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
c57f3328
KH
1069 DEFSYM (Qchar_script_table, "char-script-table");
1070 Fput (Qchar_script_table, Qchar_table_extra_slots, make_number (1));
1071 Vchar_script_table = Fmake_char_table (Qchar_script_table, Qnil);
c7e14352 1072
29208e82 1073 DEFVAR_LISP ("script-representative-chars", Vscript_representative_chars,
c3bb7671 1074 doc: /* Alist of scripts vs the representative characters.
f4427a54 1075Each element is a cons (SCRIPT . CHARS).
4d8e170e 1076SCRIPT is a symbol representing a script or a subgroup of a script.
c3bb7671 1077CHARS is a list or a vector of characters.
472a4dc9 1078If it is a list, all characters in the list are necessary for supporting SCRIPT.
c3bb7671
KH
1079If it is a vector, one of the characters in the vector is necessary.
1080This variable is used to find a font for a specific script. */);
c7e14352 1081 Vscript_representative_chars = Qnil;
a3cbb631 1082
29208e82 1083 DEFVAR_LISP ("unicode-category-table", Vunicode_category_table,
a3cbb631 1084 doc: /* Char table of Unicode's "General Category".
472a4dc9
JB
1085All Unicode characters have one of the following values (symbol):
1086 Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
a3cbb631
KH
1087 Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn
1088See The Unicode Standard for the meaning of those values. */);
1089 /* The correct char-table is setup in characters.el. */
1090 Vunicode_category_table = Qnil;
0168c3d8
KH
1091}
1092
1093#endif /* emacs */