* lisp/subr.el (decode-char, encode-char): Use advertised-calling-convention
[bpt/emacs.git] / src / charset.c
CommitLineData
3263d5a2 1/* Basic character set support.
ab422c4d 2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
7976eda0 3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5df4f04c 4 2005, 2006, 2007, 2008, 2009, 2010, 2011
ce03bf76
KH
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
4ed46869 7
327719ee 8 Copyright (C) 2003, 2004
3263d5a2
KH
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
4ed46869 11
369314dc
KH
12This file is part of GNU Emacs.
13
9ec0b715 14GNU Emacs is free software: you can redistribute it and/or modify
369314dc 15it under the terms of the GNU General Public License as published by
9ec0b715
GM
16the Free Software Foundation, either version 3 of the License, or
17(at your option) any later version.
4ed46869 18
369314dc
KH
19GNU Emacs is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
4ed46869 23
369314dc 24You should have received a copy of the GNU General Public License
9ec0b715 25along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
4ed46869 26
68c45bf0 27#include <config.h>
68c45bf0 28
f4b1eb36 29#include <errno.h>
4ed46869 30#include <stdio.h>
3263d5a2 31#include <unistd.h>
726929c4 32#include <limits.h>
4ed46869 33#include <sys/types.h>
620f13b0 34#include <c-ctype.h>
4ed46869 35#include "lisp.h"
3263d5a2 36#include "character.h"
4ed46869
KH
37#include "charset.h"
38#include "coding.h"
fc6b09bf 39#include "disptab.h"
3263d5a2 40#include "buffer.h"
4ed46869 41
04c2f2c5 42/*** GENERAL NOTES on CODED CHARACTER SETS (CHARSETS) ***
4ed46869 43
3263d5a2 44 A coded character set ("charset" hereafter) is a meaningful
04c2f2c5 45 collection (i.e. language, culture, functionality, etc.) of
3263d5a2 46 characters. Emacs handles multiple charsets at once. In Emacs Lisp
04c2f2c5
DL
47 code, a charset is represented by a symbol. In C code, a charset is
48 represented by its ID number or by a pointer to a struct charset.
4ed46869 49
3263d5a2
KH
50 The actual information about each charset is stored in two places.
51 Lispy information is stored in the hash table Vcharset_hash_table as
52 a vector (charset attributes). The other information is stored in
04c2f2c5 53 charset_table as a struct charset.
4ed46869 54
3263d5a2 55*/
4ed46869 56
3263d5a2
KH
57/* Hash table that contains attributes of each charset. Keys are
58 charset symbols, and values are vectors of charset attributes. */
59Lisp_Object Vcharset_hash_table;
4ed46869 60
3263d5a2
KH
61/* Table of struct charset. */
62struct charset *charset_table;
4ed46869 63
0065d054 64static ptrdiff_t charset_table_size;
5af5dd92 65static int charset_table_used;
4ed46869 66
3263d5a2 67Lisp_Object Qcharsetp;
4ed46869 68
3263d5a2
KH
69/* Special charset symbols. */
70Lisp_Object Qascii;
955cbe7b
PE
71static Lisp_Object Qeight_bit;
72static Lisp_Object Qiso_8859_1;
73static Lisp_Object Qunicode;
74static Lisp_Object Qemacs;
4ed46869 75
3263d5a2
KH
76/* The corresponding charsets. */
77int charset_ascii;
2fe1edd1 78int charset_eight_bit;
a2cb4e63 79static int charset_iso_8859_1;
3263d5a2 80int charset_unicode;
a2cb4e63 81static int charset_emacs;
b0e3cf2b 82
7c7dceee
KH
83/* The other special charsets. */
84int charset_jisx0201_roman;
85int charset_jisx0208_1978;
86int charset_jisx0208;
d32320c4 87int charset_ksc5601;
c1a08b4c 88
3263d5a2 89/* Value of charset attribute `charset-iso-plane'. */
955cbe7b 90static Lisp_Object Qgl, Qgr;
c1a08b4c 91
d1a04588
KH
92/* Charset of unibyte characters. */
93int charset_unibyte;
4ed46869 94
3263d5a2
KH
95/* List of charsets ordered by the priority. */
96Lisp_Object Vcharset_ordered_list;
4ed46869 97
6a9c90ec
KH
98/* Sub-list of Vcharset_ordered_list that contains all non-preferred
99 charsets. */
100Lisp_Object Vcharset_non_preferred_head;
101
dbbb237d 102/* Incremented everytime we change Vcharset_ordered_list. This is
64165ae2 103 unsigned short so that it fits in Lisp_Int and never matches
dbbb237d
KH
104 -1. */
105unsigned short charset_ordered_list_tick;
4ed46869 106
3263d5a2
KH
107/* List of iso-2022 charsets. */
108Lisp_Object Viso_2022_charset_list;
35e623fb 109
3263d5a2
KH
110/* List of emacs-mule charsets. */
111Lisp_Object Vemacs_mule_charset_list;
112
b84ae584 113int emacs_mule_charset[256];
4ed46869
KH
114
115/* Mapping table from ISO2022's charset (specified by DIMENSION,
116 CHARS, and FINAL-CHAR) to Emacs' charset. */
3263d5a2
KH
117int iso_charset_table[ISO_MAX_DIMENSION][ISO_MAX_CHARS][ISO_MAX_FINAL];
118
69f8de5b
KH
119#define CODE_POINT_TO_INDEX(charset, code) \
120 ((charset)->code_linear_p \
3c7649c1 121 ? (int) ((code) - (charset)->min_code) \
69f8de5b
KH
122 : (((charset)->code_space_mask[(code) >> 24] & 0x8) \
123 && ((charset)->code_space_mask[((code) >> 16) & 0xFF] & 0x4) \
124 && ((charset)->code_space_mask[((code) >> 8) & 0xFF] & 0x2) \
125 && ((charset)->code_space_mask[(code) & 0xFF] & 0x1)) \
3c7649c1
PE
126 ? (int) (((((code) >> 24) - (charset)->code_space[12]) \
127 * (charset)->code_space[11]) \
128 + (((((code) >> 16) & 0xFF) - (charset)->code_space[8]) \
129 * (charset)->code_space[7]) \
130 + (((((code) >> 8) & 0xFF) - (charset)->code_space[4]) \
131 * (charset)->code_space[3]) \
132 + (((code) & 0xFF) - (charset)->code_space[0]) \
133 - ((charset)->char_index_offset)) \
3263d5a2
KH
134 : -1)
135
136
3c7649c1
PE
137/* Return the code-point for the character index IDX in CHARSET.
138 IDX should be an unsigned int variable in a valid range (which is
139 always in nonnegative int range too). IDX contains garbage afterwards. */
3263d5a2 140
820ee249
KH
141#define INDEX_TO_CODE_POINT(charset, idx) \
142 ((charset)->code_linear_p \
143 ? (idx) + (charset)->min_code \
144 : (idx += (charset)->char_index_offset, \
145 (((charset)->code_space[0] + (idx) % (charset)->code_space[2]) \
146 | (((charset)->code_space[4] \
147 + ((idx) / (charset)->code_space[3] % (charset)->code_space[6])) \
148 << 8) \
149 | (((charset)->code_space[8] \
150 + ((idx) / (charset)->code_space[7] % (charset)->code_space[10])) \
151 << 16) \
152 | (((charset)->code_space[12] + ((idx) / (charset)->code_space[11])) \
153 << 24))))
4cf9710d 154
ecca2aad
KH
155/* Structure to hold mapping tables for a charset. Used by temacs
156 invoked for dumping. */
8a73a704 157
ecca2aad
KH
158static struct
159{
160 /* The current charset for which the following tables are setup. */
161 struct charset *current;
162
163 /* 1 iff the following table is used for encoder. */
164 short for_encoder;
165
0b381c7e
JB
166 /* When the following table is used for encoding, minimum and
167 maximum character of the current charset. */
ecca2aad
KH
168 int min_char, max_char;
169
e4769531 170 /* A Unicode character corresponding to the code index 0 (i.e. the
ecca2aad 171 minimum code-point) of the current charset, or -1 if the code
e4769531 172 index 0 is not a Unicode character. This is checked when
ecca2aad
KH
173 table.encoder[CHAR] is zero. */
174 int zero_index_char;
175
176 union {
177 /* Table mapping code-indices (not code-points) of the current
178 charset to Unicode characters. If decoder[CHAR] is -1, CHAR
179 doesn't belong to the current charset. */
180 int decoder[0x10000];
181 /* Table mapping Unicode characters to code-indices of the current
182 charset. The first 0x10000 elements are for BMP (0..0xFFFF),
183 and the last 0x10000 are for SMP (0x10000..0x1FFFF) or SIP
184 (0x20000..0x2FFFF). Note that there is no charset map that
185 uses both SMP and SIP. */
186 unsigned short encoder[0x20000];
187 } table;
188} *temp_charset_work;
189
190#define SET_TEMP_CHARSET_WORK_ENCODER(C, CODE) \
191 do { \
192 if ((CODE) == 0) \
193 temp_charset_work->zero_index_char = (C); \
194 else if ((C) < 0x20000) \
195 temp_charset_work->table.encoder[(C)] = (CODE); \
196 else \
197 temp_charset_work->table.encoder[(C) - 0x10000] = (CODE); \
198 } while (0)
199
200#define GET_TEMP_CHARSET_WORK_ENCODER(C) \
201 ((C) == temp_charset_work->zero_index_char ? 0 \
202 : (C) < 0x20000 ? (temp_charset_work->table.encoder[(C)] \
203 ? (int) temp_charset_work->table.encoder[(C)] : -1) \
204 : temp_charset_work->table.encoder[(C) - 0x10000] \
205 ? temp_charset_work->table.encoder[(C) - 0x10000] : -1)
206
207#define SET_TEMP_CHARSET_WORK_DECODER(C, CODE) \
208 (temp_charset_work->table.decoder[(CODE)] = (C))
209
210#define GET_TEMP_CHARSET_WORK_DECODER(CODE) \
211 (temp_charset_work->table.decoder[(CODE)])
046b1f03 212\f
93bcb785 213
e9ce014c
KH
214/* Set to 1 to warn that a charset map is loaded and thus a buffer
215 text and a string data may be relocated. */
d5172d4f 216bool charset_map_loaded;
4ed46869 217
e9ce014c 218struct charset_map_entries
4ed46869 219{
e9ce014c
KH
220 struct {
221 unsigned from, to;
222 int c;
223 } entry[0x10000];
224 struct charset_map_entries *next;
225};
226
ecca2aad
KH
227/* Load the mapping information of CHARSET from ENTRIES for
228 initializing (CONTROL_FLAG == 0), decoding (CONTROL_FLAG == 1), and
229 encoding (CONTROL_FLAG == 2).
230
231 If CONTROL_FLAG is 0, setup CHARSET->min_char, CHARSET->max_char,
232 and CHARSET->fast_map.
233
234 If CONTROL_FLAG is 1, setup the following tables according to
235 CHARSET->method and inhibit_load_charset_map.
4cf9710d 236
ecca2aad
KH
237 CHARSET->method | inhibit_lcm == 0 | inhibit_lcm == 1
238 ----------------------+--------------------+---------------------------
239 CHARSET_METHOD_MAP | CHARSET->decoder | temp_charset_work->decoder
240 ----------------------+--------------------+---------------------------
241 CHARSET_METHOD_OFFSET | Vchar_unify_table | temp_charset_work->decoder
8a73a704 242
ecca2aad 243 If CONTROL_FLAG is 2, setup the following tables.
93bcb785 244
ecca2aad
KH
245 CHARSET->method | inhibit_lcm == 0 | inhibit_lcm == 1
246 ----------------------+--------------------+---------------------------
247 CHARSET_METHOD_MAP | CHARSET->encoder | temp_charset_work->encoder
248 ----------------------+--------------------+--------------------------
249 CHARSET_METHOD_OFFSET | CHARSET->deunifier | temp_charset_work->encoder
250*/
4ed46869 251
3263d5a2 252static void
971de7fb 253load_charset_map (struct charset *charset, struct charset_map_entries *entries, int n_entries, int control_flag)
4ed46869 254{
89ef49df 255 Lisp_Object vec IF_LINT (= Qnil), table IF_LINT (= Qnil);
3263d5a2 256 unsigned max_code = CHARSET_MAX_CODE (charset);
d5172d4f 257 bool ascii_compatible_p = charset->ascii_compatible_p;
3263d5a2 258 int min_char, max_char, nonascii_min_char;
3263d5a2 259 int i;
3263d5a2 260 unsigned char *fast_map = charset->fast_map;
99529c2c 261
e9ce014c
KH
262 if (n_entries <= 0)
263 return;
264
ecca2aad 265 if (control_flag)
8ac5a9cc 266 {
ecca2aad
KH
267 if (! inhibit_load_charset_map)
268 {
269 if (control_flag == 1)
270 {
271 if (charset->method == CHARSET_METHOD_MAP)
272 {
273 int n = CODE_POINT_TO_INDEX (charset, max_code) + 1;
6662e69b 274
4939150c
PE
275 vec = Fmake_vector (make_number (n), make_number (-1));
276 set_charset_attr (charset, charset_decoder, vec);
ecca2aad
KH
277 }
278 else
279 {
280 char_table_set_range (Vchar_unify_table,
281 charset->min_char, charset->max_char,
282 Qnil);
283 }
284 }
285 else
286 {
287 table = Fmake_char_table (Qnil, Qnil);
4939150c
PE
288 set_charset_attr (charset,
289 (charset->method == CHARSET_METHOD_MAP
290 ? charset_encoder : charset_deunifier),
291 table);
ecca2aad
KH
292 }
293 }
294 else
295 {
296 if (! temp_charset_work)
38182d90 297 temp_charset_work = xmalloc (sizeof *temp_charset_work);
ecca2aad
KH
298 if (control_flag == 1)
299 {
300 memset (temp_charset_work->table.decoder, -1,
301 sizeof (int) * 0x10000);
ecca2aad
KH
302 }
303 else
304 {
305 memset (temp_charset_work->table.encoder, 0,
306 sizeof (unsigned short) * 0x20000);
307 temp_charset_work->zero_index_char = -1;
308 }
309 temp_charset_work->current = charset;
310 temp_charset_work->for_encoder = (control_flag == 2);
311 control_flag += 2;
312 }
3263d5a2 313 charset_map_loaded = 1;
2e344af3 314 }
6662e69b 315
e9ce014c 316 min_char = max_char = entries->entry[0].c;
3263d5a2 317 nonascii_min_char = MAX_CHAR;
e9ce014c 318 for (i = 0; i < n_entries; i++)
2e344af3 319 {
e9ce014c 320 unsigned from, to;
3befa583 321 int from_index, to_index, lim_index;
3b4f4446 322 int from_c, to_c;
e9ce014c 323 int idx = i % 0x10000;
3263d5a2 324
e9ce014c
KH
325 if (i > 0 && idx == 0)
326 entries = entries->next;
327 from = entries->entry[idx].from;
328 to = entries->entry[idx].to;
3b4f4446
KH
329 from_c = entries->entry[idx].c;
330 from_index = CODE_POINT_TO_INDEX (charset, from);
331 if (from == to)
6662e69b 332 {
3b4f4446
KH
333 to_index = from_index;
334 to_c = from_c;
6662e69b 335 }
3b4f4446 336 else
6662e69b 337 {
3b4f4446
KH
338 to_index = CODE_POINT_TO_INDEX (charset, to);
339 to_c = from_c + (to_index - from_index);
6662e69b 340 }
3b4f4446
KH
341 if (from_index < 0 || to_index < 0)
342 continue;
3befa583 343 lim_index = to_index + 1;
3263d5a2 344
ecca2aad
KH
345 if (to_c > max_char)
346 max_char = to_c;
347 else if (from_c < min_char)
348 min_char = from_c;
6662e69b 349
ecca2aad
KH
350 if (control_flag == 1)
351 {
352 if (charset->method == CHARSET_METHOD_MAP)
3befa583 353 for (; from_index < lim_index; from_index++, from_c++)
ecca2aad
KH
354 ASET (vec, from_index, make_number (from_c));
355 else
3befa583 356 for (; from_index < lim_index; from_index++, from_c++)
ecca2aad
KH
357 CHAR_TABLE_SET (Vchar_unify_table,
358 CHARSET_CODE_OFFSET (charset) + from_index,
359 make_number (from_c));
360 }
361 else if (control_flag == 2)
362 {
363 if (charset->method == CHARSET_METHOD_MAP
364 && CHARSET_COMPACT_CODES_P (charset))
3befa583 365 for (; from_index < lim_index; from_index++, from_c++)
ecca2aad 366 {
3c7649c1
PE
367 unsigned code = from_index;
368 code = INDEX_TO_CODE_POINT (charset, code);
ecca2aad
KH
369
370 if (NILP (CHAR_TABLE_REF (table, from_c)))
371 CHAR_TABLE_SET (table, from_c, make_number (code));
372 }
373 else
3befa583 374 for (; from_index < lim_index; from_index++, from_c++)
ecca2aad
KH
375 {
376 if (NILP (CHAR_TABLE_REF (table, from_c)))
377 CHAR_TABLE_SET (table, from_c, make_number (from_index));
378 }
379 }
380 else if (control_flag == 3)
3befa583 381 for (; from_index < lim_index; from_index++, from_c++)
ecca2aad
KH
382 SET_TEMP_CHARSET_WORK_DECODER (from_c, from_index);
383 else if (control_flag == 4)
3befa583 384 for (; from_index < lim_index; from_index++, from_c++)
ecca2aad
KH
385 SET_TEMP_CHARSET_WORK_ENCODER (from_c, from_index);
386 else /* control_flag == 0 */
387 {
3b4f4446
KH
388 if (ascii_compatible_p)
389 {
390 if (! ASCII_BYTE_P (from_c))
391 {
392 if (from_c < nonascii_min_char)
393 nonascii_min_char = from_c;
394 }
395 else if (! ASCII_BYTE_P (to_c))
396 {
397 nonascii_min_char = 0x80;
398 }
399 }
177c0ea7 400
ecca2aad
KH
401 for (; from_c <= to_c; from_c++)
402 CHARSET_FAST_MAP_SET (from_c, fast_map);
2e344af3 403 }
8ac5a9cc 404 }
3263d5a2 405
ecca2aad 406 if (control_flag == 0)
4ed46869 407 {
3263d5a2
KH
408 CHARSET_MIN_CHAR (charset) = (ascii_compatible_p
409 ? nonascii_min_char : min_char);
410 CHARSET_MAX_CHAR (charset) = max_char;
4ed46869 411 }
ecca2aad
KH
412 else if (control_flag == 4)
413 {
414 temp_charset_work->min_char = min_char;
415 temp_charset_work->max_char = max_char;
416 }
4ed46869
KH
417}
418
12bcae05 419
3263d5a2 420/* Read a hexadecimal number (preceded by "0x") from the file FP while
3b59c351 421 paying attention to comment character '#'. */
12bcae05 422
b0ab8123 423static unsigned
d5172d4f 424read_hex (FILE *fp, bool *eof, bool *overflow)
12bcae05 425{
3263d5a2
KH
426 int c;
427 unsigned n;
12bcae05 428
3263d5a2
KH
429 while ((c = getc (fp)) != EOF)
430 {
69f8de5b 431 if (c == '#')
3263d5a2
KH
432 {
433 while ((c = getc (fp)) != EOF && c != '\n');
434 }
435 else if (c == '0')
436 {
437 if ((c = getc (fp)) == EOF || c == 'x')
438 break;
439 }
8f924df7 440 }
3263d5a2
KH
441 if (c == EOF)
442 {
443 *eof = 1;
444 return 0;
445 }
3263d5a2 446 n = 0;
620f13b0 447 while (c_isxdigit (c = getc (fp)))
caeeedc1
PE
448 {
449 if (UINT_MAX >> 4 < n)
450 *overflow = 1;
3263d5a2 451 n = ((n << 4)
caeeedc1
PE
452 | (c - ('0' <= c && c <= '9' ? '0'
453 : 'A' <= c && c <= 'F' ? 'A' - 10
454 : 'a' - 10)));
455 }
e9ce014c
KH
456 if (c != EOF)
457 ungetc (c, fp);
3263d5a2
KH
458 return n;
459}
12bcae05 460
3263d5a2 461/* Return a mapping vector for CHARSET loaded from MAPFILE.
e9ce014c
KH
462 Each line of MAPFILE has this form
463 0xAAAA 0xCCCC
464 where 0xAAAA is a code-point and 0xCCCC is the corresponding
465 character code, or this form
466 0xAAAA-0xBBBB 0xCCCC
467 where 0xAAAA and 0xBBBB are code-points specifying a range, and
468 0xCCCC is the first character code of the range.
4ed46869 469
3263d5a2
KH
470 The returned vector has this form:
471 [ CODE1 CHAR1 CODE2 CHAR2 .... ]
e9ce014c 472 where CODE1 is a code-point or a cons of code-points specifying a
dde2559c
KH
473 range.
474
18a10a21
JB
475 Note that this function uses `openp' to open MAPFILE but ignores
476 `file-name-handler-alist' to avoid running any Lisp code. */
4ed46869 477
e9ce014c 478static void
f4b1eb36
PE
479load_charset_map_from_file (struct charset *charset, Lisp_Object mapfile,
480 int control_flag)
4ed46869 481{
e9ce014c
KH
482 unsigned min_code = CHARSET_MIN_CODE (charset);
483 unsigned max_code = CHARSET_MAX_CODE (charset);
3263d5a2
KH
484 int fd;
485 FILE *fp;
3263d5a2 486 Lisp_Object suffixes;
e9ce014c 487 struct charset_map_entries *head, *entries;
d311d28c
PE
488 int n_entries;
489 ptrdiff_t count;
4ed46869 490
6c6f1994 491 suffixes = list2 (build_string (".map"), build_string (".TXT"));
4ed46869 492
a2f3eb19 493 count = SPECPDL_INDEX ();
f4b1eb36 494 record_unwind_protect_nothing ();
dde2559c 495 specbind (Qfile_name_handler_alist, Qnil);
4beef065 496 fd = openp (Vcharset_map_path, mapfile, suffixes, NULL, Qnil);
f4b1eb36
PE
497 fp = fd < 0 ? 0 : fdopen (fd, "r");
498 if (!fp)
499 {
500 int open_errno = errno;
501 emacs_close (fd);
502 report_file_errno ("Loading charset map", mapfile, open_errno);
503 }
504 set_unwind_protect_ptr (count, fclose_unwind, fp);
505 unbind_to (count + 1, Qnil);
4ed46869 506
f4b1eb36 507 /* Use record_xmalloc, as `charset_map_entries' is
a2f3eb19 508 large (larger than MAX_ALLOCA). */
f4b1eb36 509 head = record_xmalloc (sizeof *head);
a2f3eb19 510 entries = head;
72af86bd 511 memset (entries, 0, sizeof (struct charset_map_entries));
a2f3eb19 512
e9ce014c 513 n_entries = 0;
3263d5a2
KH
514 while (1)
515 {
caeeedc1 516 unsigned from, to, c;
e9ce014c 517 int idx;
d5172d4f 518 bool eof = 0, overflow = 0;
4ed46869 519
caeeedc1 520 from = read_hex (fp, &eof, &overflow);
3263d5a2
KH
521 if (eof)
522 break;
e9ce014c 523 if (getc (fp) == '-')
caeeedc1 524 to = read_hex (fp, &eof, &overflow);
e9ce014c
KH
525 else
526 to = from;
caeeedc1
PE
527 if (eof)
528 break;
529 c = read_hex (fp, &eof, &overflow);
530 if (eof)
531 break;
ac4137cc 532
caeeedc1
PE
533 if (overflow)
534 continue;
e9ce014c
KH
535 if (from < min_code || to > max_code || from > to || c > MAX_CHAR)
536 continue;
ac4137cc 537
f4b1eb36 538 if (n_entries == 0x10000)
3263d5a2 539 {
f4b1eb36 540 entries->next = record_xmalloc (sizeof *entries->next);
e9ce014c 541 entries = entries->next;
72af86bd 542 memset (entries, 0, sizeof (struct charset_map_entries));
d311d28c 543 n_entries = 0;
3263d5a2 544 }
d311d28c 545 idx = n_entries;
e9ce014c
KH
546 entries->entry[idx].from = from;
547 entries->entry[idx].to = to;
548 entries->entry[idx].c = c;
549 n_entries++;
3263d5a2
KH
550 }
551 fclose (fp);
f4b1eb36 552 clear_unwind_protect (count);
177c0ea7 553
e9ce014c 554 load_charset_map (charset, head, n_entries, control_flag);
f4b1eb36 555 unbind_to (count, Qnil);
4ed46869
KH
556}
557
e9ce014c 558static void
971de7fb 559load_charset_map_from_vector (struct charset *charset, Lisp_Object vec, int control_flag)
23d2a7f1 560{
e9ce014c
KH
561 unsigned min_code = CHARSET_MIN_CODE (charset);
562 unsigned max_code = CHARSET_MAX_CODE (charset);
563 struct charset_map_entries *head, *entries;
564 int n_entries;
565 int len = ASIZE (vec);
566 int i;
a2f3eb19 567 USE_SAFE_ALLOCA;
23d2a7f1 568
e9ce014c 569 if (len % 2 == 1)
3263d5a2 570 {
e9ce014c
KH
571 add_to_log ("Failure in loading charset map: %V", vec, Qnil);
572 return;
3263d5a2 573 }
35e623fb 574
a2f3eb19
CY
575 /* Use SAFE_ALLOCA instead of alloca, as `charset_map_entries' is
576 large (larger than MAX_ALLOCA). */
98c6f1e3 577 head = SAFE_ALLOCA (sizeof *head);
a2f3eb19 578 entries = head;
72af86bd 579 memset (entries, 0, sizeof (struct charset_map_entries));
a2f3eb19 580
e9ce014c
KH
581 n_entries = 0;
582 for (i = 0; i < len; i += 2)
35e623fb 583 {
e9ce014c
KH
584 Lisp_Object val, val2;
585 unsigned from, to;
d311d28c 586 EMACS_INT c;
e9ce014c 587 int idx;
d2665018 588
e9ce014c
KH
589 val = AREF (vec, i);
590 if (CONSP (val))
bbf12bb3 591 {
e9ce014c
KH
592 val2 = XCDR (val);
593 val = XCAR (val);
e9ce014c
KH
594 from = XFASTINT (val);
595 to = XFASTINT (val2);
bbf12bb3 596 }
e9ce014c 597 else
d311d28c 598 from = to = XFASTINT (val);
e9ce014c
KH
599 val = AREF (vec, i + 1);
600 CHECK_NATNUM (val);
601 c = XFASTINT (val);
76d7b829 602
e9ce014c
KH
603 if (from < min_code || to > max_code || from > to || c > MAX_CHAR)
604 continue;
76d7b829 605
dcc694d7 606 if (n_entries > 0 && (n_entries % 0x10000) == 0)
e9ce014c 607 {
98c6f1e3 608 entries->next = SAFE_ALLOCA (sizeof *entries->next);
e9ce014c 609 entries = entries->next;
72af86bd 610 memset (entries, 0, sizeof (struct charset_map_entries));
e9ce014c
KH
611 }
612 idx = n_entries % 0x10000;
613 entries->entry[idx].from = from;
614 entries->entry[idx].to = to;
615 entries->entry[idx].c = c;
616 n_entries++;
617 }
76d7b829 618
e9ce014c 619 load_charset_map (charset, head, n_entries, control_flag);
a2f3eb19 620 SAFE_FREE ();
ac4137cc
KH
621}
622
ecca2aad
KH
623
624/* Load a mapping table for CHARSET. CONTROL-FLAG tells what kind of
625 map it is (see the comment of load_charset_map for the detail). */
626
3263d5a2 627static void
971de7fb 628load_charset (struct charset *charset, int control_flag)
76d7b829 629{
ecca2aad 630 Lisp_Object map;
76d7b829 631
ecca2aad
KH
632 if (inhibit_load_charset_map
633 && temp_charset_work
634 && charset == temp_charset_work->current
56f00ed2 635 && ((control_flag == 2) == temp_charset_work->for_encoder))
ecca2aad
KH
636 return;
637
638 if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP)
639 map = CHARSET_MAP (charset);
0ac2c299
PE
640 else
641 {
642 if (! CHARSET_UNIFIED_P (charset))
1088b922 643 emacs_abort ();
0ac2c299
PE
644 map = CHARSET_UNIFY_MAP (charset);
645 }
ecca2aad
KH
646 if (STRINGP (map))
647 load_charset_map_from_file (charset, map, control_flag);
648 else
649 load_charset_map_from_vector (charset, map, control_flag);
4ed46869 650}
76d7b829 651
3263d5a2
KH
652
653DEFUN ("charsetp", Fcharsetp, Scharsetp, 1, 1, 0,
654 doc: /* Return non-nil if and only if OBJECT is a charset.*/)
5842a27b 655 (Lisp_Object object)
23d2a7f1 656{
3263d5a2 657 return (CHARSETP (object) ? Qt : Qnil);
76d7b829
KH
658}
659
4ed46869 660
a2cb4e63
PE
661static void
662map_charset_for_dump (void (*c_function) (Lisp_Object, Lisp_Object),
663 Lisp_Object function, Lisp_Object arg,
664 unsigned int from, unsigned int to)
ecca2aad
KH
665{
666 int from_idx = CODE_POINT_TO_INDEX (temp_charset_work->current, from);
667 int to_idx = CODE_POINT_TO_INDEX (temp_charset_work->current, to);
668 Lisp_Object range;
669 int c, stop;
670 struct gcpro gcpro1;
671
672 range = Fcons (Qnil, Qnil);
673 GCPRO1 (range);
674
675 c = temp_charset_work->min_char;
676 stop = (temp_charset_work->max_char < 0x20000
677 ? temp_charset_work->max_char : 0xFFFF);
51b59d79 678
ecca2aad
KH
679 while (1)
680 {
f6095868 681 int idx = GET_TEMP_CHARSET_WORK_ENCODER (c);
ecca2aad 682
f6095868 683 if (idx >= from_idx && idx <= to_idx)
ecca2aad
KH
684 {
685 if (NILP (XCAR (range)))
686 XSETCAR (range, make_number (c));
687 }
688 else if (! NILP (XCAR (range)))
689 {
690 XSETCDR (range, make_number (c - 1));
691 if (c_function)
692 (*c_function) (arg, range);
693 else
694 call2 (function, range, arg);
695 XSETCAR (range, Qnil);
696 }
697 if (c == stop)
698 {
699 if (c == temp_charset_work->max_char)
700 {
701 if (! NILP (XCAR (range)))
702 {
703 XSETCDR (range, make_number (c));
704 if (c_function)
705 (*c_function) (arg, range);
706 else
707 call2 (function, range, arg);
708 }
709 break;
710 }
711 c = 0x1FFFF;
712 stop = temp_charset_work->max_char;
713 }
714 c++;
715 }
c542407d 716 UNGCPRO;
ecca2aad
KH
717}
718
4ed46869 719void
6f704c76
DN
720map_charset_chars (void (*c_function)(Lisp_Object, Lisp_Object), Lisp_Object function,
721 Lisp_Object arg, struct charset *charset, unsigned from, unsigned to)
4ed46869 722{
3263d5a2 723 Lisp_Object range;
d5172d4f
PE
724 bool partial = (from > CHARSET_MIN_CODE (charset)
725 || to < CHARSET_MAX_CODE (charset));
374c5cfd 726
3263d5a2 727 if (CHARSET_METHOD (charset) == CHARSET_METHOD_OFFSET)
4ed46869 728 {
374c5cfd
KH
729 int from_idx = CODE_POINT_TO_INDEX (charset, from);
730 int to_idx = CODE_POINT_TO_INDEX (charset, to);
731 int from_c = from_idx + CHARSET_CODE_OFFSET (charset);
732 int to_c = to_idx + CHARSET_CODE_OFFSET (charset);
733
ecca2aad
KH
734 if (CHARSET_UNIFIED_P (charset))
735 {
736 if (! CHAR_TABLE_P (CHARSET_DEUNIFIER (charset)))
737 load_charset (charset, 2);
738 if (CHAR_TABLE_P (CHARSET_DEUNIFIER (charset)))
739 map_char_table_for_charset (c_function, function,
740 CHARSET_DEUNIFIER (charset), arg,
741 partial ? charset : NULL, from, to);
742 else
743 map_charset_for_dump (c_function, function, arg, from, to);
744 }
745
374c5cfd 746 range = Fcons (make_number (from_c), make_number (to_c));
3263d5a2 747 if (NILP (function))
5af5dd92 748 (*c_function) (arg, range);
3263d5a2
KH
749 else
750 call2 (function, range, arg);
c83ef371 751 }
3263d5a2
KH
752 else if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP)
753 {
754 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset)))
ecca2aad
KH
755 load_charset (charset, 2);
756 if (CHAR_TABLE_P (CHARSET_ENCODER (charset)))
757 map_char_table_for_charset (c_function, function,
758 CHARSET_ENCODER (charset), arg,
759 partial ? charset : NULL, from, to);
760 else
761 map_charset_for_dump (c_function, function, arg, from, to);
3263d5a2 762 }
374c5cfd 763 else if (CHARSET_METHOD (charset) == CHARSET_METHOD_SUBSET)
4ed46869 764 {
374c5cfd
KH
765 Lisp_Object subset_info;
766 int offset;
767
768 subset_info = CHARSET_SUBSET (charset);
769 charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
770 offset = XINT (AREF (subset_info, 3));
771 from -= offset;
772 if (from < XFASTINT (AREF (subset_info, 1)))
773 from = XFASTINT (AREF (subset_info, 1));
774 to -= offset;
775 if (to > XFASTINT (AREF (subset_info, 2)))
776 to = XFASTINT (AREF (subset_info, 2));
777 map_charset_chars (c_function, function, arg, charset, from, to);
4ed46869 778 }
374c5cfd
KH
779 else /* i.e. CHARSET_METHOD_SUPERSET */
780 {
781 Lisp_Object parents;
4ed46869 782
374c5cfd
KH
783 for (parents = CHARSET_SUPERSET (charset); CONSP (parents);
784 parents = XCDR (parents))
bbf12bb3 785 {
374c5cfd
KH
786 int offset;
787 unsigned this_from, this_to;
788
789 charset = CHARSET_FROM_ID (XFASTINT (XCAR (XCAR (parents))));
790 offset = XINT (XCDR (XCAR (parents)));
12b55765
KH
791 this_from = from > offset ? from - offset : 0;
792 this_to = to > offset ? to - offset : 0;
374c5cfd
KH
793 if (this_from < CHARSET_MIN_CODE (charset))
794 this_from = CHARSET_MIN_CODE (charset);
795 if (this_to > CHARSET_MAX_CODE (charset))
796 this_to = CHARSET_MAX_CODE (charset);
111daccf
KH
797 map_charset_chars (c_function, function, arg, charset,
798 this_from, this_to);
bbf12bb3 799 }
35e623fb 800 }
4ed46869
KH
801}
802
374c5cfd 803DEFUN ("map-charset-chars", Fmap_charset_chars, Smap_charset_chars, 2, 5, 0,
04c2f2c5 804 doc: /* Call FUNCTION for all characters in CHARSET.
374c5cfd 805FUNCTION is called with an argument RANGE and the optional 3rd
3263d5a2 806argument ARG.
4ed46869 807
374c5cfd
KH
808RANGE is a cons (FROM . TO), where FROM and TO indicate a range of
809characters contained in CHARSET.
4ed46869 810
374c5cfd 811The optional 4th and 5th arguments FROM-CODE and TO-CODE specify the
72d51285 812range of code points (in CHARSET) of target characters. */)
5842a27b 813 (Lisp_Object function, Lisp_Object charset, Lisp_Object arg, Lisp_Object from_code, Lisp_Object to_code)
4ed46869 814{
374c5cfd 815 struct charset *cs;
16fed1fc 816 unsigned from, to;
4ed46869 817
374c5cfd
KH
818 CHECK_CHARSET_GET_CHARSET (charset, cs);
819 if (NILP (from_code))
16fed1fc 820 from = CHARSET_MIN_CODE (cs);
970b7474 821 else
4ed46869 822 {
970b7474
KH
823 from = XINT (from_code);
824 if (from < CHARSET_MIN_CODE (cs))
825 from = CHARSET_MIN_CODE (cs);
4ed46869 826 }
374c5cfd 827 if (NILP (to_code))
970b7474 828 to = CHARSET_MAX_CODE (cs);
4ed46869
KH
829 else
830 {
970b7474
KH
831 to = XINT (to_code);
832 if (to > CHARSET_MAX_CODE (cs))
833 to = CHARSET_MAX_CODE (cs);
4ed46869 834 }
16fed1fc 835 map_charset_chars (NULL, function, arg, cs, from, to);
3263d5a2 836 return Qnil;
35e623fb 837}
4ed46869 838
4ed46869 839
3263d5a2
KH
840/* Define a charset according to the arguments. The Nth argument is
841 the Nth attribute of the charset (the last attribute `charset-id'
842 is not included). See the docstring of `define-charset' for the
843 detail. */
4ed46869 844
3263d5a2
KH
845DEFUN ("define-charset-internal", Fdefine_charset_internal,
846 Sdefine_charset_internal, charset_arg_max, MANY, 0,
04c2f2c5
DL
847 doc: /* For internal use only.
848usage: (define-charset-internal ...) */)
f66c7cf8 849 (ptrdiff_t nargs, Lisp_Object *args)
4ed46869 850{
3263d5a2
KH
851 /* Charset attr vector. */
852 Lisp_Object attrs;
853 Lisp_Object val;
0de4bb68 854 EMACS_UINT hash_code;
3263d5a2 855 struct Lisp_Hash_Table *hash_table = XHASH_TABLE (Vcharset_hash_table);
69f8de5b 856 int i, j;
3263d5a2
KH
857 struct charset charset;
858 int id;
859 int dimension;
d5172d4f 860 bool new_definition_p;
3263d5a2
KH
861 int nchars;
862
863 if (nargs != charset_arg_max)
864 return Fsignal (Qwrong_number_of_arguments,
865 Fcons (intern ("define-charset-internal"),
866 make_number (nargs)));
867
868 attrs = Fmake_vector (make_number (charset_attr_max), Qnil);
869
870 CHECK_SYMBOL (args[charset_arg_name]);
871 ASET (attrs, charset_name, args[charset_arg_name]);
872
873 val = args[charset_arg_code_space];
c032b5f8 874 for (i = 0, dimension = 0, nchars = 1; ; i++)
76d7b829 875 {
d311d28c 876 Lisp_Object min_byte_obj, max_byte_obj;
3263d5a2
KH
877 int min_byte, max_byte;
878
d311d28c
PE
879 min_byte_obj = Faref (val, make_number (i * 2));
880 max_byte_obj = Faref (val, make_number (i * 2 + 1));
af5a5a98 881 CHECK_RANGED_INTEGER (min_byte_obj, 0, 255);
d311d28c 882 min_byte = XINT (min_byte_obj);
af5a5a98 883 CHECK_RANGED_INTEGER (max_byte_obj, min_byte, 255);
d311d28c 884 max_byte = XINT (max_byte_obj);
3263d5a2
KH
885 charset.code_space[i * 4] = min_byte;
886 charset.code_space[i * 4 + 1] = max_byte;
887 charset.code_space[i * 4 + 2] = max_byte - min_byte + 1;
3263d5a2
KH
888 if (max_byte > 0)
889 dimension = i + 1;
c032b5f8
PE
890 if (i == 3)
891 break;
892 nchars *= charset.code_space[i * 4 + 2];
893 charset.code_space[i * 4 + 3] = nchars;
3263d5a2 894 }
4ed46869 895
3263d5a2
KH
896 val = args[charset_arg_dimension];
897 if (NILP (val))
898 charset.dimension = dimension;
899 else
4ed46869 900 {
af5a5a98 901 CHECK_RANGED_INTEGER (val, 1, 4);
3263d5a2 902 charset.dimension = XINT (val);
4ed46869
KH
903 }
904
3263d5a2
KH
905 charset.code_linear_p
906 = (charset.dimension == 1
907 || (charset.code_space[2] == 256
908 && (charset.dimension == 2
909 || (charset.code_space[6] == 256
910 && (charset.dimension == 3
911 || charset.code_space[10] == 256)))));
912
69f8de5b 913 if (! charset.code_linear_p)
4ed46869 914 {
23f86fce 915 charset.code_space_mask = xzalloc (256);
69f8de5b
KH
916 for (i = 0; i < 4; i++)
917 for (j = charset.code_space[i * 4]; j <= charset.code_space[i * 4 + 1];
918 j++)
919 charset.code_space_mask[j] |= (1 << i);
4ed46869
KH
920 }
921
3263d5a2 922 charset.iso_chars_96 = charset.code_space[2] == 96;
4ed46869 923
3263d5a2
KH
924 charset.min_code = (charset.code_space[0]
925 | (charset.code_space[4] << 8)
926 | (charset.code_space[8] << 16)
3c7649c1 927 | ((unsigned) charset.code_space[12] << 24));
3263d5a2
KH
928 charset.max_code = (charset.code_space[1]
929 | (charset.code_space[5] << 8)
930 | (charset.code_space[9] << 16)
3c7649c1 931 | ((unsigned) charset.code_space[13] << 24));
820ee249 932 charset.char_index_offset = 0;
8a73a704 933
820ee249
KH
934 val = args[charset_arg_min_code];
935 if (! NILP (val))
936 {
be44ca6c 937 unsigned code = cons_to_unsigned (val, UINT_MAX);
fdb82f93 938
820ee249
KH
939 if (code < charset.min_code
940 || code > charset.max_code)
3c7649c1
PE
941 args_out_of_range_3 (make_fixnum_or_float (charset.min_code),
942 make_fixnum_or_float (charset.max_code), val);
820ee249
KH
943 charset.char_index_offset = CODE_POINT_TO_INDEX (&charset, code);
944 charset.min_code = code;
945 }
3fac5a51 946
820ee249
KH
947 val = args[charset_arg_max_code];
948 if (! NILP (val))
3fac5a51 949 {
be44ca6c 950 unsigned code = cons_to_unsigned (val, UINT_MAX);
820ee249 951
820ee249
KH
952 if (code < charset.min_code
953 || code > charset.max_code)
3c7649c1
PE
954 args_out_of_range_3 (make_fixnum_or_float (charset.min_code),
955 make_fixnum_or_float (charset.max_code), val);
820ee249 956 charset.max_code = code;
3fac5a51 957 }
3fac5a51 958
ecca2aad 959 charset.compact_codes_p = charset.max_code < 0x10000;
4ed46869 960
3263d5a2
KH
961 val = args[charset_arg_invalid_code];
962 if (NILP (val))
963 {
964 if (charset.min_code > 0)
965 charset.invalid_code = 0;
bbf12bb3
KH
966 else
967 {
3c7649c1 968 if (charset.max_code < UINT_MAX)
3263d5a2
KH
969 charset.invalid_code = charset.max_code + 1;
970 else
971 error ("Attribute :invalid-code must be specified");
76d7b829 972 }
76d7b829 973 }
3263d5a2 974 else
3c7649c1 975 charset.invalid_code = cons_to_unsigned (val, UINT_MAX);
4ed46869 976
3263d5a2
KH
977 val = args[charset_arg_iso_final];
978 if (NILP (val))
979 charset.iso_final = -1;
980 else
981 {
982 CHECK_NUMBER (val);
983 if (XINT (val) < '0' || XINT (val) > 127)
c2982e87 984 error ("Invalid iso-final-char: %"pI"d", XINT (val));
3263d5a2
KH
985 charset.iso_final = XINT (val);
986 }
4ed46869 987
3263d5a2
KH
988 val = args[charset_arg_iso_revision];
989 if (NILP (val))
990 charset.iso_revision = -1;
991 else
4ed46869 992 {
af5a5a98 993 CHECK_RANGED_INTEGER (val, -1, 63);
3263d5a2 994 charset.iso_revision = XINT (val);
4ed46869 995 }
4ed46869 996
3263d5a2
KH
997 val = args[charset_arg_emacs_mule_id];
998 if (NILP (val))
999 charset.emacs_mule_id = -1;
4ed46869
KH
1000 else
1001 {
3263d5a2
KH
1002 CHECK_NATNUM (val);
1003 if ((XINT (val) > 0 && XINT (val) <= 128) || XINT (val) >= 256)
c2982e87 1004 error ("Invalid emacs-mule-id: %"pI"d", XINT (val));
3263d5a2 1005 charset.emacs_mule_id = XINT (val);
c83ef371 1006 }
f6302ac9 1007
3263d5a2 1008 charset.ascii_compatible_p = ! NILP (args[charset_arg_ascii_compatible_p]);
1d67c29b 1009
3263d5a2 1010 charset.supplementary_p = ! NILP (args[charset_arg_supplementary_p]);
4ed46869 1011
3263d5a2
KH
1012 charset.unified_p = 0;
1013
72af86bd 1014 memset (charset.fast_map, 0, sizeof (charset.fast_map));
3263d5a2
KH
1015
1016 if (! NILP (args[charset_arg_code_offset]))
1017 {
1018 val = args[charset_arg_code_offset];
3c7649c1 1019 CHECK_CHARACTER (val);
3263d5a2
KH
1020
1021 charset.method = CHARSET_METHOD_OFFSET;
1022 charset.code_offset = XINT (val);
1023
3263d5a2 1024 i = CODE_POINT_TO_INDEX (&charset, charset.max_code);
3c7649c1 1025 if (MAX_CHAR - charset.code_offset < i)
3263d5a2 1026 error ("Unsupported max char: %d", charset.max_char);
3c7649c1
PE
1027 charset.max_char = i + charset.code_offset;
1028 i = CODE_POINT_TO_INDEX (&charset, charset.min_code);
1029 charset.min_char = i + charset.code_offset;
3263d5a2 1030
f148205f
KH
1031 i = (charset.min_char >> 7) << 7;
1032 for (; i < 0x10000 && i <= charset.max_char; i += 128)
3263d5a2 1033 CHARSET_FAST_MAP_SET (i, charset.fast_map);
f148205f 1034 i = (i >> 12) << 12;
3263d5a2
KH
1035 for (; i <= charset.max_char; i += 0x1000)
1036 CHARSET_FAST_MAP_SET (i, charset.fast_map);
3620330b
KH
1037 if (charset.code_offset == 0 && charset.max_char >= 0x80)
1038 charset.ascii_compatible_p = 1;
3263d5a2
KH
1039 }
1040 else if (! NILP (args[charset_arg_map]))
1041 {
1042 val = args[charset_arg_map];
1043 ASET (attrs, charset_map, val);
ecca2aad 1044 charset.method = CHARSET_METHOD_MAP;
3263d5a2 1045 }
374c5cfd 1046 else if (! NILP (args[charset_arg_subset]))
3263d5a2 1047 {
374c5cfd
KH
1048 Lisp_Object parent;
1049 Lisp_Object parent_min_code, parent_max_code, parent_code_offset;
1050 struct charset *parent_charset;
1051
1052 val = args[charset_arg_subset];
1053 parent = Fcar (val);
1054 CHECK_CHARSET_GET_CHARSET (parent, parent_charset);
1055 parent_min_code = Fnth (make_number (1), val);
1056 CHECK_NATNUM (parent_min_code);
1057 parent_max_code = Fnth (make_number (2), val);
1058 CHECK_NATNUM (parent_max_code);
1059 parent_code_offset = Fnth (make_number (3), val);
1060 CHECK_NUMBER (parent_code_offset);
25721f5b 1061 val = make_uninit_vector (4);
374c5cfd
KH
1062 ASET (val, 0, make_number (parent_charset->id));
1063 ASET (val, 1, parent_min_code);
1064 ASET (val, 2, parent_max_code);
1065 ASET (val, 3, parent_code_offset);
1066 ASET (attrs, charset_subset, val);
1067
1068 charset.method = CHARSET_METHOD_SUBSET;
1069 /* Here, we just copy the parent's fast_map. It's not accurate,
1070 but at least it works for quickly detecting which character
1071 DOESN'T belong to this charset. */
1072 for (i = 0; i < 190; i++)
1073 charset.fast_map[i] = parent_charset->fast_map[i];
1074
1075 /* We also copy these for parents. */
1076 charset.min_char = parent_charset->min_char;
1077 charset.max_char = parent_charset->max_char;
1078 }
1079 else if (! NILP (args[charset_arg_superset]))
0282eb69 1080 {
374c5cfd
KH
1081 val = args[charset_arg_superset];
1082 charset.method = CHARSET_METHOD_SUPERSET;
3263d5a2 1083 val = Fcopy_sequence (val);
374c5cfd 1084 ASET (attrs, charset_superset, val);
3263d5a2
KH
1085
1086 charset.min_char = MAX_CHAR;
1087 charset.max_char = 0;
1088 for (; ! NILP (val); val = Fcdr (val))
0282eb69 1089 {
3263d5a2
KH
1090 Lisp_Object elt, car_part, cdr_part;
1091 int this_id, offset;
1092 struct charset *this_charset;
2e344af3 1093
3263d5a2
KH
1094 elt = Fcar (val);
1095 if (CONSP (elt))
2e344af3 1096 {
3263d5a2
KH
1097 car_part = XCAR (elt);
1098 cdr_part = XCDR (elt);
1099 CHECK_CHARSET_GET_ID (car_part, this_id);
d311d28c 1100 CHECK_TYPE_RANGED_INTEGER (int, cdr_part);
3263d5a2 1101 offset = XINT (cdr_part);
177c0ea7 1102 }
3263d5a2 1103 else
4ed46869 1104 {
3263d5a2
KH
1105 CHECK_CHARSET_GET_ID (elt, this_id);
1106 offset = 0;
4ed46869 1107 }
3263d5a2
KH
1108 XSETCAR (val, Fcons (make_number (this_id), make_number (offset)));
1109
1110 this_charset = CHARSET_FROM_ID (this_id);
1111 if (charset.min_char > this_charset->min_char)
1112 charset.min_char = this_charset->min_char;
1113 if (charset.max_char < this_charset->max_char)
1114 charset.max_char = this_charset->max_char;
1115 for (i = 0; i < 190; i++)
1116 charset.fast_map[i] |= this_charset->fast_map[i];
0282eb69 1117 }
0282eb69 1118 }
2e344af3 1119 else
3263d5a2 1120 error ("None of :code-offset, :map, :parents are specified");
05505664 1121
3263d5a2
KH
1122 val = args[charset_arg_unify_map];
1123 if (! NILP (val) && !STRINGP (val))
1124 CHECK_VECTOR (val);
1125 ASET (attrs, charset_unify_map, val);
05505664 1126
3263d5a2
KH
1127 CHECK_LIST (args[charset_arg_plist]);
1128 ASET (attrs, charset_plist, args[charset_arg_plist]);
4ed46869 1129
3263d5a2
KH
1130 charset.hash_index = hash_lookup (hash_table, args[charset_arg_name],
1131 &hash_code);
1132 if (charset.hash_index >= 0)
1133 {
1134 new_definition_p = 0;
4f65af01 1135 id = XFASTINT (CHARSET_SYMBOL_ID (args[charset_arg_name]));
e83064be 1136 set_hash_value_slot (hash_table, charset.hash_index, attrs);
3263d5a2 1137 }
1a45ff10 1138 else
3263d5a2
KH
1139 {
1140 charset.hash_index = hash_put (hash_table, args[charset_arg_name], attrs,
1141 hash_code);
1142 if (charset_table_used == charset_table_size)
1143 {
e097a6fa 1144 /* Ensure that charset IDs fit into 'int' as well as into the
0065d054
PE
1145 restriction imposed by fixnums. Although the 'int' restriction
1146 could be removed, too much other code would need altering; for
1147 example, the IDs are stuffed into struct
1148 coding_system.charbuf[i] entries, which are 'int'. */
1149 int old_size = charset_table_size;
2dd2e622 1150 ptrdiff_t new_size = old_size;
0065d054 1151 struct charset *new_table =
2dd2e622 1152 xpalloc (0, &new_size, 1,
0065d054
PE
1153 min (INT_MAX, MOST_POSITIVE_FIXNUM),
1154 sizeof *charset_table);
1155 memcpy (new_table, charset_table, old_size * sizeof *new_table);
2fe1edd1 1156 charset_table = new_table;
2dd2e622 1157 charset_table_size = new_size;
f701dc2a
PE
1158 /* FIXME: This leaks memory, as the old charset_table becomes
1159 unreachable. If the old charset table is charset_table_init
1160 then this leak is intentional; otherwise, it's unclear.
1161 If the latter memory leak is intentional, a
0065d054
PE
1162 comment should be added to explain this. If not, the old
1163 charset_table should be freed, by passing it as the 1st argument
1164 to xpalloc and removing the memcpy. */
3263d5a2
KH
1165 }
1166 id = charset_table_used++;
3263d5a2
KH
1167 new_definition_p = 1;
1168 }
2e344af3 1169
4f65af01 1170 ASET (attrs, charset_id, make_number (id));
3263d5a2
KH
1171 charset.id = id;
1172 charset_table[id] = charset;
2e344af3 1173
ecca2aad 1174 if (charset.method == CHARSET_METHOD_MAP)
b8ebe9dd
KH
1175 {
1176 load_charset (&charset, 0);
1177 charset_table[id] = charset;
1178 }
ecca2aad 1179
3263d5a2 1180 if (charset.iso_final >= 0)
4ed46869 1181 {
3263d5a2
KH
1182 ISO_CHARSET_TABLE (charset.dimension, charset.iso_chars_96,
1183 charset.iso_final) = id;
1184 if (new_definition_p)
1185 Viso_2022_charset_list = nconc2 (Viso_2022_charset_list,
6c6f1994 1186 list1 (make_number (id)));
7c7dceee
KH
1187 if (ISO_CHARSET_TABLE (1, 0, 'J') == id)
1188 charset_jisx0201_roman = id;
1189 else if (ISO_CHARSET_TABLE (2, 0, '@') == id)
1190 charset_jisx0208_1978 = id;
1191 else if (ISO_CHARSET_TABLE (2, 0, 'B') == id)
1192 charset_jisx0208 = id;
d32320c4
KH
1193 else if (ISO_CHARSET_TABLE (2, 0, 'C') == id)
1194 charset_ksc5601 = id;
4ed46869 1195 }
d0cf2d48 1196
3263d5a2 1197 if (charset.emacs_mule_id >= 0)
4ed46869 1198 {
b84ae584 1199 emacs_mule_charset[charset.emacs_mule_id] = id;
4f65af01
KH
1200 if (charset.emacs_mule_id < 0xA0)
1201 emacs_mule_bytes[charset.emacs_mule_id] = charset.dimension + 1;
3b1ae89b
KH
1202 else
1203 emacs_mule_bytes[charset.emacs_mule_id] = charset.dimension + 2;
3263d5a2
KH
1204 if (new_definition_p)
1205 Vemacs_mule_charset_list = nconc2 (Vemacs_mule_charset_list,
6c6f1994 1206 list1 (make_number (id)));
4ed46869
KH
1207 }
1208
3263d5a2
KH
1209 if (new_definition_p)
1210 {
1211 Vcharset_list = Fcons (args[charset_arg_name], Vcharset_list);
8055c66a
KH
1212 if (charset.supplementary_p)
1213 Vcharset_ordered_list = nconc2 (Vcharset_ordered_list,
6c6f1994 1214 list1 (make_number (id)));
8055c66a 1215 else
880820fe
KH
1216 {
1217 Lisp_Object tail;
1218
1219 for (tail = Vcharset_ordered_list; CONSP (tail); tail = XCDR (tail))
1220 {
1221 struct charset *cs = CHARSET_FROM_ID (XINT (XCAR (tail)));
1222
1223 if (cs->supplementary_p)
1224 break;
1225 }
1226 if (EQ (tail, Vcharset_ordered_list))
1227 Vcharset_ordered_list = Fcons (make_number (id),
1228 Vcharset_ordered_list);
1229 else if (NILP (tail))
1230 Vcharset_ordered_list = nconc2 (Vcharset_ordered_list,
6c6f1994 1231 list1 (make_number (id)));
880820fe
KH
1232 else
1233 {
1234 val = Fcons (XCAR (tail), XCDR (tail));
1235 XSETCDR (tail, val);
1236 XSETCAR (tail, make_number (id));
1237 }
1238 }
dbbb237d 1239 charset_ordered_list_tick++;
3263d5a2 1240 }
4ed46869 1241
3263d5a2 1242 return Qnil;
4ed46869
KH
1243}
1244
2fe1edd1
KH
1245
1246/* Same as Fdefine_charset_internal but arguments are more convenient
1247 to call from C (typically in syms_of_charset). This can define a
1248 charset of `offset' method only. Return the ID of the new
1249 charset. */
1250
1251static int
dd4c5104
DN
1252define_charset_internal (Lisp_Object name,
1253 int dimension,
dfb6afda 1254 const char *code_space_chars,
dd4c5104
DN
1255 unsigned min_code, unsigned max_code,
1256 int iso_final, int iso_revision, int emacs_mule_id,
d5172d4f 1257 bool ascii_compatible, bool supplementary,
dd4c5104 1258 int code_offset)
2fe1edd1 1259{
dfb6afda 1260 const unsigned char *code_space = (const unsigned char *) code_space_chars;
2fe1edd1 1261 Lisp_Object args[charset_arg_max];
2fe1edd1
KH
1262 Lisp_Object val;
1263 int i;
1264
1265 args[charset_arg_name] = name;
1266 args[charset_arg_dimension] = make_number (dimension);
25721f5b 1267 val = make_uninit_vector (8);
2fe1edd1
KH
1268 for (i = 0; i < 8; i++)
1269 ASET (val, i, make_number (code_space[i]));
1270 args[charset_arg_code_space] = val;
1271 args[charset_arg_min_code] = make_number (min_code);
1272 args[charset_arg_max_code] = make_number (max_code);
1273 args[charset_arg_iso_final]
1274 = (iso_final < 0 ? Qnil : make_number (iso_final));
1275 args[charset_arg_iso_revision] = make_number (iso_revision);
1276 args[charset_arg_emacs_mule_id]
1277 = (emacs_mule_id < 0 ? Qnil : make_number (emacs_mule_id));
1278 args[charset_arg_ascii_compatible_p] = ascii_compatible ? Qt : Qnil;
7acf89e6 1279 args[charset_arg_supplementary_p] = supplementary ? Qt : Qnil;
2fe1edd1
KH
1280 args[charset_arg_invalid_code] = Qnil;
1281 args[charset_arg_code_offset] = make_number (code_offset);
1282 args[charset_arg_map] = Qnil;
1283 args[charset_arg_subset] = Qnil;
1284 args[charset_arg_superset] = Qnil;
1285 args[charset_arg_unify_map] = Qnil;
1286
694b6c97 1287 args[charset_arg_plist] =
3438fe21 1288 listn (CONSTYPE_HEAP, 14,
694b6c97
DA
1289 intern_c_string (":name"),
1290 args[charset_arg_name],
1291 intern_c_string (":dimension"),
1292 args[charset_arg_dimension],
1293 intern_c_string (":code-space"),
1294 args[charset_arg_code_space],
1295 intern_c_string (":iso-final-char"),
1296 args[charset_arg_iso_final],
1297 intern_c_string (":emacs-mule-id"),
1298 args[charset_arg_emacs_mule_id],
1299 intern_c_string (":ascii-compatible-p"),
1300 args[charset_arg_ascii_compatible_p],
1301 intern_c_string (":code-offset"),
1302 args[charset_arg_code_offset]);
2fe1edd1
KH
1303 Fdefine_charset_internal (charset_arg_max, args);
1304
1305 return XINT (CHARSET_SYMBOL_ID (name));
1306}
1307
1308
3263d5a2
KH
1309DEFUN ("define-charset-alias", Fdefine_charset_alias,
1310 Sdefine_charset_alias, 2, 2, 0,
1311 doc: /* Define ALIAS as an alias for charset CHARSET. */)
5842a27b 1312 (Lisp_Object alias, Lisp_Object charset)
4ed46869 1313{
3263d5a2
KH
1314 Lisp_Object attr;
1315
1316 CHECK_CHARSET_GET_ATTR (charset, attr);
1317 Fputhash (alias, attr, Vcharset_hash_table);
528623a0 1318 Vcharset_list = Fcons (alias, Vcharset_list);
3263d5a2
KH
1319 return Qnil;
1320}
4ed46869 1321
4ed46869 1322
3263d5a2 1323DEFUN ("charset-plist", Fcharset_plist, Scharset_plist, 1, 1, 0,
56a46d1d 1324 doc: /* Return the property list of CHARSET. */)
5842a27b 1325 (Lisp_Object charset)
3263d5a2
KH
1326{
1327 Lisp_Object attrs;
1328
1329 CHECK_CHARSET_GET_ATTR (charset, attrs);
1330 return CHARSET_ATTR_PLIST (attrs);
1331}
1332
1333
1334DEFUN ("set-charset-plist", Fset_charset_plist, Sset_charset_plist, 2, 2, 0,
1335 doc: /* Set CHARSET's property list to PLIST. */)
5842a27b 1336 (Lisp_Object charset, Lisp_Object plist)
3263d5a2
KH
1337{
1338 Lisp_Object attrs;
1339
1340 CHECK_CHARSET_GET_ATTR (charset, attrs);
4939150c 1341 ASET (attrs, charset_plist, plist);
3263d5a2
KH
1342 return plist;
1343}
1344
1345
dbbb237d 1346DEFUN ("unify-charset", Funify_charset, Sunify_charset, 1, 3, 0,
56a46d1d
DL
1347 doc: /* Unify characters of CHARSET with Unicode.
1348This means reading the relevant file and installing the table defined
dbbb237d
KH
1349by CHARSET's `:unify-map' property.
1350
64165ae2
DL
1351Optional second arg UNIFY-MAP is a file name string or a vector. It has
1352the same meaning as the `:unify-map' attribute in the function
dbbb237d
KH
1353`define-charset' (which see).
1354
1355Optional third argument DEUNIFY, if non-nil, means to de-unify CHARSET. */)
5842a27b 1356 (Lisp_Object charset, Lisp_Object unify_map, Lisp_Object deunify)
8a73a704 1357{
3263d5a2
KH
1358 int id;
1359 struct charset *cs;
8f924df7 1360
3263d5a2
KH
1361 CHECK_CHARSET_GET_ID (charset, id);
1362 cs = CHARSET_FROM_ID (id);
dbbb237d
KH
1363 if (NILP (deunify)
1364 ? CHARSET_UNIFIED_P (cs) && ! NILP (CHARSET_DEUNIFIER (cs))
1365 : ! CHARSET_UNIFIED_P (cs))
3263d5a2 1366 return Qnil;
dbbb237d 1367
3263d5a2 1368 CHARSET_UNIFIED_P (cs) = 0;
dbbb237d
KH
1369 if (NILP (deunify))
1370 {
ecca2aad
KH
1371 if (CHARSET_METHOD (cs) != CHARSET_METHOD_OFFSET
1372 || CHARSET_CODE_OFFSET (cs) < 0x110000)
8f924df7 1373 error ("Can't unify charset: %s", SDATA (SYMBOL_NAME (charset)));
dbbb237d
KH
1374 if (NILP (unify_map))
1375 unify_map = CHARSET_UNIFY_MAP (cs);
dbbb237d 1376 else
ecca2aad
KH
1377 {
1378 if (! STRINGP (unify_map) && ! VECTORP (unify_map))
1379 signal_error ("Bad unify-map", unify_map);
4939150c 1380 set_charset_attr (cs, charset_unify_map, unify_map);
ecca2aad
KH
1381 }
1382 if (NILP (Vchar_unify_table))
1383 Vchar_unify_table = Fmake_char_table (Qnil, Qnil);
1384 char_table_set_range (Vchar_unify_table,
1385 cs->min_char, cs->max_char, charset);
dbbb237d
KH
1386 CHARSET_UNIFIED_P (cs) = 1;
1387 }
1388 else if (CHAR_TABLE_P (Vchar_unify_table))
1389 {
3c7649c1
PE
1390 unsigned min_code = CHARSET_MIN_CODE (cs);
1391 unsigned max_code = CHARSET_MAX_CODE (cs);
dbbb237d
KH
1392 int min_char = DECODE_CHAR (cs, min_code);
1393 int max_char = DECODE_CHAR (cs, max_code);
8f924df7 1394
dbbb237d
KH
1395 char_table_set_range (Vchar_unify_table, min_char, max_char, Qnil);
1396 }
8f924df7 1397
3263d5a2 1398 return Qnil;
8a73a704
KH
1399}
1400
3fac5a51
KH
1401DEFUN ("get-unused-iso-final-char", Fget_unused_iso_final_char,
1402 Sget_unused_iso_final_char, 2, 2, 0,
3263d5a2 1403 doc: /*
d0cf2d48 1404Return an unused ISO final char for a charset of DIMENSION and CHARS.
fdb82f93
PJ
1405DIMENSION is the number of bytes to represent a character: 1 or 2.
1406CHARS is the number of characters in a dimension: 94 or 96.
1407
1408This final char is for private use, thus the range is `0' (48) .. `?' (63).
1721b6af 1409If there's no unused final char for the specified kind of charset,
fdb82f93 1410return nil. */)
5842a27b 1411 (Lisp_Object dimension, Lisp_Object chars)
3fac5a51
KH
1412{
1413 int final_char;
1414
b7826503
PJ
1415 CHECK_NUMBER (dimension);
1416 CHECK_NUMBER (chars);
3263d5a2
KH
1417 if (XINT (dimension) != 1 && XINT (dimension) != 2 && XINT (dimension) != 3)
1418 args_out_of_range_3 (dimension, make_number (1), make_number (3));
3fac5a51 1419 if (XINT (chars) != 94 && XINT (chars) != 96)
3263d5a2 1420 args_out_of_range_3 (chars, make_number (94), make_number (96));
3fac5a51 1421 for (final_char = '0'; final_char <= '?'; final_char++)
3263d5a2
KH
1422 if (ISO_CHARSET_TABLE (XINT (dimension), XINT (chars), final_char) < 0)
1423 break;
3fac5a51
KH
1424 return (final_char <= '?' ? make_number (final_char) : Qnil);
1425}
1426
3263d5a2 1427static void
971de7fb 1428check_iso_charset_parameter (Lisp_Object dimension, Lisp_Object chars, Lisp_Object final_char)
4ed46869 1429{
3263d5a2
KH
1430 CHECK_NATNUM (dimension);
1431 CHECK_NATNUM (chars);
e6c3da20 1432 CHECK_CHARACTER (final_char);
4ed46869 1433
3263d5a2 1434 if (XINT (dimension) > 3)
c2982e87 1435 error ("Invalid DIMENSION %"pI"d, it should be 1, 2, or 3",
5fdb398c 1436 XINT (dimension));
4ed46869 1437 if (XINT (chars) != 94 && XINT (chars) != 96)
c2982e87 1438 error ("Invalid CHARS %"pI"d, it should be 94 or 96", XINT (chars));
3263d5a2 1439 if (XINT (final_char) < '0' || XINT (final_char) > '~')
e6c3da20
EZ
1440 error ("Invalid FINAL-CHAR %c, it should be `0'..`~'",
1441 (int)XINT (final_char));
3263d5a2
KH
1442}
1443
1444
1445DEFUN ("declare-equiv-charset", Fdeclare_equiv_charset, Sdeclare_equiv_charset,
1446 4, 4, 0,
cefd8c4f
KH
1447 doc: /* Declare an equivalent charset for ISO-2022 decoding.
1448
1449On decoding by an ISO-2022 base coding system, when a charset
1450specified by DIMENSION, CHARS, and FINAL-CHAR is designated, behave as
1451if CHARSET is designated instead. */)
5842a27b 1452 (Lisp_Object dimension, Lisp_Object chars, Lisp_Object final_char, Lisp_Object charset)
3263d5a2
KH
1453{
1454 int id;
d5172d4f 1455 bool chars_flag;
4ed46869 1456
3263d5a2
KH
1457 CHECK_CHARSET_GET_ID (charset, id);
1458 check_iso_charset_parameter (dimension, chars, final_char);
82215ce9
KH
1459 chars_flag = XINT (chars) == 96;
1460 ISO_CHARSET_TABLE (XINT (dimension), chars_flag, XINT (final_char)) = id;
4ed46869
KH
1461 return Qnil;
1462}
1463
3263d5a2 1464
2e344af3
KH
1465/* Return information about charsets in the text at PTR of NBYTES
1466 bytes, which are NCHARS characters. The value is:
f6302ac9 1467
cfe34140 1468 0: Each character is represented by one byte. This is always
3263d5a2
KH
1469 true for a unibyte string. For a multibyte string, true if
1470 it contains only ASCII characters.
1471
28c026cd
DL
1472 1: No charsets other than ascii, control-1, and latin-1 are
1473 found.
1d67c29b 1474
3263d5a2
KH
1475 2: Otherwise.
1476*/
4ed46869
KH
1477
1478int
971de7fb 1479string_xstring_p (Lisp_Object string)
4ed46869 1480{
8f924df7
KH
1481 const unsigned char *p = SDATA (string);
1482 const unsigned char *endp = p + SBYTES (string);
3263d5a2 1483
8f924df7 1484 if (SCHARS (string) == SBYTES (string))
3263d5a2
KH
1485 return 0;
1486
3263d5a2 1487 while (p < endp)
0282eb69 1488 {
3263d5a2 1489 int c = STRING_CHAR_ADVANCE (p);
2e344af3 1490
3cc67a4d 1491 if (c >= 0x100)
3263d5a2 1492 return 2;
0282eb69 1493 }
3263d5a2
KH
1494 return 1;
1495}
05505664 1496
05505664 1497
3263d5a2 1498/* Find charsets in the string at PTR of NCHARS and NBYTES.
4ed46869 1499
3cc67a4d
KH
1500 CHARSETS is a vector. If Nth element is non-nil, it means the
1501 charset whose id is N is already found.
2e344af3 1502
3263d5a2 1503 It may lookup a translation table TABLE if supplied. */
2e344af3 1504
3263d5a2 1505static void
d5172d4f
PE
1506find_charsets_in_text (const unsigned char *ptr, ptrdiff_t nchars,
1507 ptrdiff_t nbytes, Lisp_Object charsets,
1508 Lisp_Object table, bool multibyte)
3263d5a2 1509{
dbbb237d 1510 const unsigned char *pend = ptr + nbytes;
3263d5a2
KH
1511
1512 if (nchars == nbytes)
3263d5a2 1513 {
3cc67a4d
KH
1514 if (multibyte)
1515 ASET (charsets, charset_ascii, Qt);
1516 else
1517 while (ptr < pend)
1518 {
1519 int c = *ptr++;
1520
1521 if (!NILP (table))
1522 c = translate_char (table, c);
1523 if (ASCII_BYTE_P (c))
1524 ASET (charsets, charset_ascii, Qt);
1525 else
1526 ASET (charsets, charset_eight_bit, Qt);
1527 }
1528 }
1529 else
1530 {
1531 while (ptr < pend)
3263d5a2 1532 {
3cc67a4d
KH
1533 int c = STRING_CHAR_ADVANCE (ptr);
1534 struct charset *charset;
3263d5a2 1535
3cc67a4d
KH
1536 if (!NILP (table))
1537 c = translate_char (table, c);
1538 charset = CHAR_CHARSET (c);
1539 ASET (charsets, CHARSET_ID (charset), Qt);
4ed46869 1540 }
4ed46869 1541 }
4ed46869
KH
1542}
1543
1544DEFUN ("find-charset-region", Ffind_charset_region, Sfind_charset_region,
23d2a7f1 1545 2, 3, 0,
fdb82f93
PJ
1546 doc: /* Return a list of charsets in the region between BEG and END.
1547BEG and END are buffer positions.
1548Optional arg TABLE if non-nil is a translation table to look up.
1549
fdb82f93
PJ
1550If the current buffer is unibyte, the returned list may contain
1551only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
5842a27b 1552 (Lisp_Object beg, Lisp_Object end, Lisp_Object table)
4ed46869 1553{
3263d5a2 1554 Lisp_Object charsets;
d311d28c 1555 ptrdiff_t from, from_byte, to, stop, stop_byte;
42ca828e 1556 int i;
4ed46869 1557 Lisp_Object val;
d5172d4f 1558 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
4ed46869
KH
1559
1560 validate_region (&beg, &end);
1561 from = XFASTINT (beg);
1562 stop = to = XFASTINT (end);
6ae1f27e 1563
4ed46869 1564 if (from < GPT && GPT < to)
6ae1f27e
RS
1565 {
1566 stop = GPT;
1567 stop_byte = GPT_BYTE;
1568 }
1569 else
1570 stop_byte = CHAR_TO_BYTE (stop);
1571
1572 from_byte = CHAR_TO_BYTE (from);
1573
3263d5a2 1574 charsets = Fmake_vector (make_number (charset_table_used), Qnil);
4ed46869
KH
1575 while (1)
1576 {
3263d5a2 1577 find_charsets_in_text (BYTE_POS_ADDR (from_byte), stop - from,
3cc67a4d
KH
1578 stop_byte - from_byte, charsets, table,
1579 multibyte);
4ed46869 1580 if (stop < to)
6ae1f27e
RS
1581 {
1582 from = stop, from_byte = stop_byte;
1583 stop = to, stop_byte = CHAR_TO_BYTE (stop);
1584 }
4ed46869
KH
1585 else
1586 break;
1587 }
6ae1f27e 1588
4ed46869 1589 val = Qnil;
3263d5a2 1590 for (i = charset_table_used - 1; i >= 0; i--)
3cc67a4d 1591 if (!NILP (AREF (charsets, i)))
3263d5a2 1592 val = Fcons (CHARSET_NAME (charset_table + i), val);
4ed46869
KH
1593 return val;
1594}
1595
1596DEFUN ("find-charset-string", Ffind_charset_string, Sfind_charset_string,
23d2a7f1 1597 1, 2, 0,
fdb82f93
PJ
1598 doc: /* Return a list of charsets in STR.
1599Optional arg TABLE if non-nil is a translation table to look up.
1600
fdb82f93 1601If STR is unibyte, the returned list may contain
3263d5a2 1602only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
5842a27b 1603 (Lisp_Object str, Lisp_Object table)
4ed46869 1604{
3263d5a2 1605 Lisp_Object charsets;
4ed46869
KH
1606 int i;
1607 Lisp_Object val;
1608
b7826503 1609 CHECK_STRING (str);
87b089ad 1610
3263d5a2 1611 charsets = Fmake_vector (make_number (charset_table_used), Qnil);
8f924df7 1612 find_charsets_in_text (SDATA (str), SCHARS (str), SBYTES (str),
3cc67a4d
KH
1613 charsets, table,
1614 STRING_MULTIBYTE (str));
4ed46869 1615 val = Qnil;
3263d5a2 1616 for (i = charset_table_used - 1; i >= 0; i--)
3cc67a4d 1617 if (!NILP (AREF (charsets, i)))
3263d5a2 1618 val = Fcons (CHARSET_NAME (charset_table + i), val);
4ed46869
KH
1619 return val;
1620}
2e344af3 1621
4ed46869 1622\f
3263d5a2 1623
ecca2aad
KH
1624/* Return a unified character code for C (>= 0x110000). VAL is a
1625 value of Vchar_unify_table for C; i.e. it is nil, an integer, or a
1626 charset symbol. */
41c8bfcf 1627static int
971de7fb 1628maybe_unify_char (int c, Lisp_Object val)
ecca2aad
KH
1629{
1630 struct charset *charset;
1631
1632 if (INTEGERP (val))
ccd9a01a 1633 return XFASTINT (val);
ecca2aad
KH
1634 if (NILP (val))
1635 return c;
1636
1637 CHECK_CHARSET_GET_CHARSET (val, charset);
291d430f 1638#ifdef REL_ALLOC
efc00ab1 1639 /* The call to load_charset below can allocate memory, which screws
291d430f
EZ
1640 callers of this function through STRING_CHAR_* macros that hold C
1641 pointers to buffer text, if REL_ALLOC is used. */
1642 r_alloc_inhibit_buffer_relocation (1);
1643#endif
ecca2aad
KH
1644 load_charset (charset, 1);
1645 if (! inhibit_load_charset_map)
1646 {
1647 val = CHAR_TABLE_REF (Vchar_unify_table, c);
1648 if (! NILP (val))
ccd9a01a 1649 c = XFASTINT (val);
ecca2aad
KH
1650 }
1651 else
1652 {
1653 int code_index = c - CHARSET_CODE_OFFSET (charset);
1654 int unified = GET_TEMP_CHARSET_WORK_DECODER (code_index);
1655
1656 if (unified > 0)
1657 c = unified;
1658 }
291d430f
EZ
1659#ifdef REL_ALLOC
1660 r_alloc_inhibit_buffer_relocation (0);
1661#endif
ecca2aad
KH
1662 return c;
1663}
1664
1665
bbd240ce 1666/* Return a character corresponding to the code-point CODE of
3263d5a2
KH
1667 CHARSET. */
1668
1669int
971de7fb 1670decode_char (struct charset *charset, unsigned int code)
4ed46869 1671{
3263d5a2
KH
1672 int c, char_index;
1673 enum charset_method method = CHARSET_METHOD (charset);
ac4137cc 1674
3263d5a2
KH
1675 if (code < CHARSET_MIN_CODE (charset) || code > CHARSET_MAX_CODE (charset))
1676 return -1;
4ed46869 1677
374c5cfd 1678 if (method == CHARSET_METHOD_SUBSET)
2e344af3 1679 {
374c5cfd
KH
1680 Lisp_Object subset_info;
1681
1682 subset_info = CHARSET_SUBSET (charset);
1683 charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
1684 code -= XINT (AREF (subset_info, 3));
1685 if (code < XFASTINT (AREF (subset_info, 1))
1686 || code > XFASTINT (AREF (subset_info, 2)))
1687 c = -1;
1688 else
1689 c = DECODE_CHAR (charset, code);
2e344af3 1690 }
374c5cfd 1691 else if (method == CHARSET_METHOD_SUPERSET)
2e344af3 1692 {
3263d5a2 1693 Lisp_Object parents;
4ed46869 1694
374c5cfd 1695 parents = CHARSET_SUPERSET (charset);
3263d5a2
KH
1696 c = -1;
1697 for (; CONSP (parents); parents = XCDR (parents))
1698 {
1699 int id = XINT (XCAR (XCAR (parents)));
1700 int code_offset = XINT (XCDR (XCAR (parents)));
374c5cfd 1701 unsigned this_code = code - code_offset;
4ed46869 1702
3263d5a2
KH
1703 charset = CHARSET_FROM_ID (id);
1704 if ((c = DECODE_CHAR (charset, this_code)) >= 0)
1705 break;
1706 }
1707 }
1708 else
ac4137cc 1709 {
3263d5a2 1710 char_index = CODE_POINT_TO_INDEX (charset, code);
69f8de5b
KH
1711 if (char_index < 0)
1712 return -1;
4ed46869 1713
3263d5a2 1714 if (method == CHARSET_METHOD_MAP)
ac4137cc 1715 {
3263d5a2 1716 Lisp_Object decoder;
4ed46869 1717
3263d5a2
KH
1718 decoder = CHARSET_DECODER (charset);
1719 if (! VECTORP (decoder))
ecca2aad
KH
1720 {
1721 load_charset (charset, 1);
1722 decoder = CHARSET_DECODER (charset);
1723 }
1724 if (VECTORP (decoder))
1725 c = XINT (AREF (decoder, char_index));
1726 else
1727 c = GET_TEMP_CHARSET_WORK_DECODER (char_index);
ac4137cc 1728 }
ecca2aad 1729 else /* method == CHARSET_METHOD_OFFSET */
ac4137cc 1730 {
3263d5a2 1731 c = char_index + CHARSET_CODE_OFFSET (charset);
ecca2aad 1732 if (CHARSET_UNIFIED_P (charset)
41c8bfcf
PE
1733 && MAX_UNICODE_CHAR < c && c <= MAX_5_BYTE_CHAR)
1734 {
1735 /* Unify C with a Unicode character if possible. */
1736 Lisp_Object val = CHAR_TABLE_REF (Vchar_unify_table, c);
1737 c = maybe_unify_char (c, val);
1738 }
ac4137cc
KH
1739 }
1740 }
4ed46869 1741
3263d5a2 1742 return c;
90d7b74e
KH
1743}
1744
374c5cfd
KH
1745/* Variable used temporarily by the macro ENCODE_CHAR. */
1746Lisp_Object charset_work;
4ed46869 1747
d5172d4f 1748/* Return a code-point of C in CHARSET. If C doesn't belong to
28c026cd
DL
1749 CHARSET, return CHARSET_INVALID_CODE (CHARSET). If STRICT is true,
1750 use CHARSET's strict_max_char instead of max_char. */
4ed46869 1751
3263d5a2 1752unsigned
971de7fb 1753encode_char (struct charset *charset, int c)
9d3d8cba 1754{
3263d5a2
KH
1755 unsigned code;
1756 enum charset_method method = CHARSET_METHOD (charset);
9d3d8cba 1757
3263d5a2 1758 if (CHARSET_UNIFIED_P (charset))
ac4137cc 1759 {
6809ca75 1760 Lisp_Object deunifier;
ecca2aad 1761 int code_index = -1;
4ed46869 1762
3263d5a2
KH
1763 deunifier = CHARSET_DEUNIFIER (charset);
1764 if (! CHAR_TABLE_P (deunifier))
ac4137cc 1765 {
ecca2aad 1766 load_charset (charset, 2);
3263d5a2 1767 deunifier = CHARSET_DEUNIFIER (charset);
ac4137cc 1768 }
ecca2aad
KH
1769 if (CHAR_TABLE_P (deunifier))
1770 {
1771 Lisp_Object deunified = CHAR_TABLE_REF (deunifier, c);
1772
1773 if (INTEGERP (deunified))
1774 code_index = XINT (deunified);
1775 }
1776 else
1777 {
1778 code_index = GET_TEMP_CHARSET_WORK_ENCODER (c);
1779 }
1780 if (code_index >= 0)
1781 c = CHARSET_CODE_OFFSET (charset) + code_index;
ac4137cc 1782 }
9d3d8cba 1783
374c5cfd
KH
1784 if (method == CHARSET_METHOD_SUBSET)
1785 {
1786 Lisp_Object subset_info;
1787 struct charset *this_charset;
1788
1789 subset_info = CHARSET_SUBSET (charset);
1790 this_charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
1791 code = ENCODE_CHAR (this_charset, c);
1792 if (code == CHARSET_INVALID_CODE (this_charset)
1793 || code < XFASTINT (AREF (subset_info, 1))
1794 || code > XFASTINT (AREF (subset_info, 2)))
1795 return CHARSET_INVALID_CODE (charset);
1796 code += XINT (AREF (subset_info, 3));
1797 return code;
1798 }
9d3d8cba 1799
374c5cfd 1800 if (method == CHARSET_METHOD_SUPERSET)
859f2b3c 1801 {
3263d5a2 1802 Lisp_Object parents;
d2665018 1803
374c5cfd 1804 parents = CHARSET_SUPERSET (charset);
3263d5a2 1805 for (; CONSP (parents); parents = XCDR (parents))
beeedaad 1806 {
3263d5a2
KH
1807 int id = XINT (XCAR (XCAR (parents)));
1808 int code_offset = XINT (XCDR (XCAR (parents)));
1809 struct charset *this_charset = CHARSET_FROM_ID (id);
d2665018 1810
3263d5a2 1811 code = ENCODE_CHAR (this_charset, c);
dbbb237d
KH
1812 if (code != CHARSET_INVALID_CODE (this_charset))
1813 return code + code_offset;
beeedaad 1814 }
3263d5a2
KH
1815 return CHARSET_INVALID_CODE (charset);
1816 }
1bcc1567 1817
15c85a88
KH
1818 if (! CHARSET_FAST_MAP_REF ((c), charset->fast_map)
1819 || c < CHARSET_MIN_CHAR (charset) || c > CHARSET_MAX_CHAR (charset))
1820 return CHARSET_INVALID_CODE (charset);
1bcc1567 1821
3263d5a2 1822 if (method == CHARSET_METHOD_MAP)
3f62427c 1823 {
3263d5a2 1824 Lisp_Object encoder;
beeedaad 1825 Lisp_Object val;
9b6a601f 1826
3263d5a2
KH
1827 encoder = CHARSET_ENCODER (charset);
1828 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset)))
b8ebe9dd
KH
1829 {
1830 load_charset (charset, 2);
1831 encoder = CHARSET_ENCODER (charset);
1832 }
1833 if (CHAR_TABLE_P (encoder))
ecca2aad
KH
1834 {
1835 val = CHAR_TABLE_REF (encoder, c);
1836 if (NILP (val))
1837 return CHARSET_INVALID_CODE (charset);
1838 code = XINT (val);
1839 if (! CHARSET_COMPACT_CODES_P (charset))
1840 code = INDEX_TO_CODE_POINT (charset, code);
1841 }
1842 else
1843 {
1844 code = GET_TEMP_CHARSET_WORK_ENCODER (c);
51b59d79 1845 code = INDEX_TO_CODE_POINT (charset, code);
ecca2aad 1846 }
3263d5a2 1847 }
820ee249 1848 else /* method == CHARSET_METHOD_OFFSET */
beeedaad 1849 {
58c8a77d
PE
1850 unsigned code_index = c - CHARSET_CODE_OFFSET (charset);
1851
1852 code = INDEX_TO_CODE_POINT (charset, code_index);
3f62427c 1853 }
8ac5a9cc 1854
3263d5a2 1855 return code;
4ed46869
KH
1856}
1857
4ed46869 1858
3263d5a2
KH
1859DEFUN ("decode-char", Fdecode_char, Sdecode_char, 2, 3, 0,
1860 doc: /* Decode the pair of CHARSET and CODE-POINT into a character.
1861Return nil if CODE-POINT is not valid in CHARSET.
4ed46869 1862
328a8179 1863CODE-POINT may be a cons (HIGHER-16-BIT-VALUE . LOWER-16-BIT-VALUE). */)
5842a27b 1864 (Lisp_Object charset, Lisp_Object code_point, Lisp_Object restriction)
4ed46869 1865{
3263d5a2
KH
1866 int c, id;
1867 unsigned code;
1868 struct charset *charsetp;
859f2b3c 1869
3263d5a2 1870 CHECK_CHARSET_GET_ID (charset, id);
be44ca6c 1871 code = cons_to_unsigned (code_point, UINT_MAX);
3263d5a2
KH
1872 charsetp = CHARSET_FROM_ID (id);
1873 c = DECODE_CHAR (charsetp, code);
1874 return (c >= 0 ? make_number (c) : Qnil);
4ed46869
KH
1875}
1876
859f2b3c 1877
3263d5a2
KH
1878DEFUN ("encode-char", Fencode_char, Sencode_char, 2, 3, 0,
1879 doc: /* Encode the character CH into a code-point of CHARSET.
328a8179 1880Return nil if CHARSET doesn't include CH. */)
5842a27b 1881 (Lisp_Object ch, Lisp_Object charset, Lisp_Object restriction)
4ed46869 1882{
34206dd2 1883 int c, id;
3263d5a2
KH
1884 unsigned code;
1885 struct charset *charsetp;
046b1f03 1886
3263d5a2 1887 CHECK_CHARSET_GET_ID (charset, id);
34206dd2
PE
1888 CHECK_CHARACTER (ch);
1889 c = XFASTINT (ch);
3263d5a2 1890 charsetp = CHARSET_FROM_ID (id);
34206dd2 1891 code = ENCODE_CHAR (charsetp, c);
3263d5a2
KH
1892 if (code == CHARSET_INVALID_CODE (charsetp))
1893 return Qnil;
be44ca6c 1894 return INTEGER_TO_CONS (code);
beeedaad
KH
1895}
1896
beeedaad 1897
b121a744
KH
1898DEFUN ("make-char", Fmake_char, Smake_char, 1, 5, 0,
1899 doc:
1900 /* Return a character of CHARSET whose position codes are CODEn.
1901
1902CODE1 through CODE4 are optional, but if you don't supply sufficient
1903position codes, it is assumed that the minimum code in each dimension
04c2f2c5 1904is specified. */)
5842a27b 1905 (Lisp_Object charset, Lisp_Object code1, Lisp_Object code2, Lisp_Object code3, Lisp_Object code4)
beeedaad 1906{
3263d5a2
KH
1907 int id, dimension;
1908 struct charset *charsetp;
b121a744
KH
1909 unsigned code;
1910 int c;
87b089ad 1911
3263d5a2
KH
1912 CHECK_CHARSET_GET_ID (charset, id);
1913 charsetp = CHARSET_FROM_ID (id);
4ed46869 1914
b121a744
KH
1915 dimension = CHARSET_DIMENSION (charsetp);
1916 if (NILP (code1))
d47073ca
KH
1917 code = (CHARSET_ASCII_COMPATIBLE_P (charsetp)
1918 ? 0 : CHARSET_MIN_CODE (charsetp));
3263d5a2 1919 else
859f2b3c 1920 {
b121a744
KH
1921 CHECK_NATNUM (code1);
1922 if (XFASTINT (code1) >= 0x100)
1923 args_out_of_range (make_number (0xFF), code1);
1924 code = XFASTINT (code1);
859f2b3c 1925
b0a1e45e 1926 if (dimension > 1)
beeedaad 1927 {
b121a744 1928 code <<= 8;
b0a1e45e
KH
1929 if (NILP (code2))
1930 code |= charsetp->code_space[(dimension - 2) * 4];
beeedaad 1931 else
b121a744 1932 {
b0a1e45e
KH
1933 CHECK_NATNUM (code2);
1934 if (XFASTINT (code2) >= 0x100)
1935 args_out_of_range (make_number (0xFF), code2);
1936 code |= XFASTINT (code2);
b121a744 1937 }
99529c2c 1938
b0a1e45e 1939 if (dimension > 2)
b121a744
KH
1940 {
1941 code <<= 8;
b0a1e45e
KH
1942 if (NILP (code3))
1943 code |= charsetp->code_space[(dimension - 3) * 4];
b121a744
KH
1944 else
1945 {
b0a1e45e
KH
1946 CHECK_NATNUM (code3);
1947 if (XFASTINT (code3) >= 0x100)
1948 args_out_of_range (make_number (0xFF), code3);
1949 code |= XFASTINT (code3);
1950 }
1951
1952 if (dimension > 3)
1953 {
1954 code <<= 8;
1955 if (NILP (code4))
1956 code |= charsetp->code_space[0];
1957 else
1958 {
1959 CHECK_NATNUM (code4);
1960 if (XFASTINT (code4) >= 0x100)
1961 args_out_of_range (make_number (0xFF), code4);
1962 code |= XFASTINT (code4);
1963 }
b121a744
KH
1964 }
1965 }
beeedaad 1966 }
859f2b3c 1967 }
beeedaad 1968
b121a744
KH
1969 if (CHARSET_ISO_FINAL (charsetp) >= 0)
1970 code &= 0x7F7F7F7F;
1971 c = DECODE_CHAR (charsetp, code);
1972 if (c < 0)
1973 error ("Invalid code(s)");
3263d5a2 1974 return make_number (c);
4ed46869
KH
1975}
1976
beeedaad 1977
3263d5a2
KH
1978/* Return the first charset in CHARSET_LIST that contains C.
1979 CHARSET_LIST is a list of charset IDs. If it is nil, use
1980 Vcharset_ordered_list. */
beeedaad 1981
3263d5a2 1982struct charset *
971de7fb 1983char_charset (int c, Lisp_Object charset_list, unsigned int *code_return)
2e344af3 1984{
d5172d4f 1985 bool maybe_null = 0;
8a1816bb 1986
3263d5a2
KH
1987 if (NILP (charset_list))
1988 charset_list = Vcharset_ordered_list;
8a1816bb
KH
1989 else
1990 maybe_null = 1;
beeedaad 1991
6c652beb 1992 while (CONSP (charset_list))
2e344af3 1993 {
3263d5a2
KH
1994 struct charset *charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
1995 unsigned code = ENCODE_CHAR (charset, c);
beeedaad 1996
3263d5a2 1997 if (code != CHARSET_INVALID_CODE (charset))
beeedaad 1998 {
3263d5a2
KH
1999 if (code_return)
2000 *code_return = code;
2001 return charset;
3f62427c 2002 }
3263d5a2 2003 charset_list = XCDR (charset_list);
c0be27fd
KH
2004 if (! maybe_null
2005 && c <= MAX_UNICODE_CHAR
2006 && EQ (charset_list, Vcharset_non_preferred_head))
6c652beb 2007 return CHARSET_FROM_ID (charset_unicode);
3f62427c 2008 }
8a1816bb
KH
2009 return (maybe_null ? NULL
2010 : c <= MAX_5_BYTE_CHAR ? CHARSET_FROM_ID (charset_emacs)
6a9c90ec 2011 : CHARSET_FROM_ID (charset_eight_bit));
3f62427c
KH
2012}
2013
2e344af3 2014
3263d5a2 2015DEFUN ("split-char", Fsplit_char, Ssplit_char, 1, 1, 0,
3cc67a4d 2016 doc:
d0cf2d48 2017 /*Return list of charset and one to four position-codes of CH.
3cc67a4d
KH
2018The charset is decided by the current priority order of charsets.
2019A position-code is a byte value of each dimension of the code-point of
d0cf2d48 2020CH in the charset. */)
5842a27b 2021 (Lisp_Object ch)
4ed46869 2022{
3263d5a2
KH
2023 struct charset *charset;
2024 int c, dimension;
2025 unsigned code;
4ed46869
KH
2026 Lisp_Object val;
2027
3263d5a2
KH
2028 CHECK_CHARACTER (ch);
2029 c = XFASTINT (ch);
2030 charset = CHAR_CHARSET (c);
2031 if (! charset)
1088b922 2032 emacs_abort ();
3263d5a2
KH
2033 code = ENCODE_CHAR (charset, c);
2034 if (code == CHARSET_INVALID_CODE (charset))
1088b922 2035 emacs_abort ();
3263d5a2 2036 dimension = CHARSET_DIMENSION (charset);
3cc67a4d
KH
2037 for (val = Qnil; dimension > 0; dimension--)
2038 {
2039 val = Fcons (make_number (code & 0xFF), val);
2040 code >>= 8;
2041 }
3263d5a2 2042 return Fcons (CHARSET_NAME (charset), val);
4ed46869
KH
2043}
2044
740f080d 2045
4cb75c4b
KH
2046DEFUN ("char-charset", Fchar_charset, Schar_charset, 1, 2, 0,
2047 doc: /* Return the charset of highest priority that contains CH.
15c6d837
XF
2048ASCII characters are an exception: for them, this function always
2049returns `ascii'.
4cb75c4b
KH
2050If optional 2nd arg RESTRICTION is non-nil, it is a list of charsets
2051from which to find the charset. It may also be a coding system. In
2052that case, find the charset from what supported by that coding system. */)
5842a27b 2053 (Lisp_Object ch, Lisp_Object restriction)
4ed46869 2054{
3263d5a2 2055 struct charset *charset;
4ed46869 2056
3263d5a2 2057 CHECK_CHARACTER (ch);
4cb75c4b
KH
2058 if (NILP (restriction))
2059 charset = CHAR_CHARSET (XINT (ch));
2060 else
2061 {
4cb75c4b
KH
2062 if (CONSP (restriction))
2063 {
7a84eee5
KH
2064 int c = XFASTINT (ch);
2065
2066 for (; CONSP (restriction); restriction = XCDR (restriction))
4cb75c4b 2067 {
f6095868 2068 struct charset *rcharset;
4cb75c4b 2069
f6095868
PE
2070 CHECK_CHARSET_GET_CHARSET (XCAR (restriction), rcharset);
2071 if (ENCODE_CHAR (rcharset, c) != CHARSET_INVALID_CODE (rcharset))
7a84eee5 2072 return XCAR (restriction);
4cb75c4b 2073 }
7a84eee5 2074 return Qnil;
4cb75c4b 2075 }
7a84eee5
KH
2076 restriction = coding_system_charset_list (restriction);
2077 charset = char_charset (XINT (ch), restriction, NULL);
4cb75c4b
KH
2078 if (! charset)
2079 return Qnil;
2080 }
3263d5a2 2081 return (CHARSET_NAME (charset));
4ed46869
KH
2082}
2083
17e7ef1b 2084
3263d5a2
KH
2085DEFUN ("charset-after", Fcharset_after, Scharset_after, 0, 1, 0,
2086 doc: /*
2087Return charset of a character in the current buffer at position POS.
cd1181db 2088If POS is nil, it defaults to the current point.
3263d5a2 2089If POS is out of range, the value is nil. */)
5842a27b 2090 (Lisp_Object pos)
2e344af3 2091{
3263d5a2
KH
2092 Lisp_Object ch;
2093 struct charset *charset;
046b1f03 2094
3263d5a2
KH
2095 ch = Fchar_after (pos);
2096 if (! INTEGERP (ch))
2097 return ch;
2098 charset = CHAR_CHARSET (XINT (ch));
2099 return (CHARSET_NAME (charset));
6ae1f27e 2100}
9036eb45 2101
87b089ad 2102
3263d5a2
KH
2103DEFUN ("iso-charset", Fiso_charset, Siso_charset, 3, 3, 0,
2104 doc: /*
2105Return charset of ISO's specification DIMENSION, CHARS, and FINAL-CHAR.
2106
2107ISO 2022's designation sequence (escape sequence) distinguishes charsets
2108by their DIMENSION, CHARS, and FINAL-CHAR,
d0cf2d48 2109whereas Emacs distinguishes them by charset symbol.
3263d5a2
KH
2110See the documentation of the function `charset-info' for the meanings of
2111DIMENSION, CHARS, and FINAL-CHAR. */)
5842a27b 2112 (Lisp_Object dimension, Lisp_Object chars, Lisp_Object final_char)
6ae1f27e 2113{
3263d5a2 2114 int id;
d5172d4f 2115 bool chars_flag;
a8a35e61 2116
3263d5a2 2117 check_iso_charset_parameter (dimension, chars, final_char);
82215ce9
KH
2118 chars_flag = XFASTINT (chars) == 96;
2119 id = ISO_CHARSET_TABLE (XFASTINT (dimension), chars_flag,
3263d5a2
KH
2120 XFASTINT (final_char));
2121 return (id >= 0 ? CHARSET_NAME (CHARSET_FROM_ID (id)) : Qnil);
046b1f03
RS
2122}
2123
87b089ad 2124
3263d5a2
KH
2125DEFUN ("clear-charset-maps", Fclear_charset_maps, Sclear_charset_maps,
2126 0, 0, 0,
2127 doc: /*
ecca2aad
KH
2128Internal use only.
2129Clear temporary charset mapping tables.
2130It should be called only from temacs invoked for dumping. */)
5842a27b 2131 (void)
87b089ad 2132{
ecca2aad 2133 if (temp_charset_work)
87b089ad 2134 {
baad03f0 2135 xfree (temp_charset_work);
ecca2aad 2136 temp_charset_work = NULL;
2e344af3 2137 }
2e344af3 2138
ecca2aad
KH
2139 if (CHAR_TABLE_P (Vchar_unify_table))
2140 Foptimize_char_table (Vchar_unify_table, Qnil);
740f080d 2141
3263d5a2 2142 return Qnil;
740f080d
KH
2143}
2144
8ddf5e57
DL
2145DEFUN ("charset-priority-list", Fcharset_priority_list,
2146 Scharset_priority_list, 0, 1, 0,
2147 doc: /* Return the list of charsets ordered by priority.
2148HIGHESTP non-nil means just return the highest priority one. */)
5842a27b 2149 (Lisp_Object highestp)
2e344af3 2150{
8ddf5e57 2151 Lisp_Object val = Qnil, list = Vcharset_ordered_list;
2e344af3 2152
8ddf5e57 2153 if (!NILP (highestp))
16fed1fc 2154 return CHARSET_NAME (CHARSET_FROM_ID (XINT (Fcar (list))));
2e344af3 2155
8ddf5e57 2156 while (!NILP (list))
2e344af3 2157 {
16fed1fc 2158 val = Fcons (CHARSET_NAME (CHARSET_FROM_ID (XINT (XCAR (list)))), val);
8ddf5e57 2159 list = XCDR (list);
2e344af3 2160 }
8ddf5e57 2161 return Fnreverse (val);
2e344af3
KH
2162}
2163
8ddf5e57
DL
2164DEFUN ("set-charset-priority", Fset_charset_priority, Sset_charset_priority,
2165 1, MANY, 0,
2166 doc: /* Assign higher priority to the charsets given as arguments.
2167usage: (set-charset-priority &rest charsets) */)
f66c7cf8 2168 (ptrdiff_t nargs, Lisp_Object *args)
4ed46869 2169{
af7c60ca 2170 Lisp_Object new_head, old_list, arglist[2];
321c819c 2171 Lisp_Object list_2022, list_emacs_mule;
f66c7cf8 2172 ptrdiff_t i;
c5101a77 2173 int id;
4ed46869 2174
8ddf5e57 2175 old_list = Fcopy_sequence (Vcharset_ordered_list);
af7c60ca 2176 new_head = Qnil;
8ddf5e57 2177 for (i = 0; i < nargs; i++)
4ed46869 2178 {
8ddf5e57 2179 CHECK_CHARSET_GET_ID (args[i], id);
af7c60ca
KH
2180 if (! NILP (Fmemq (make_number (id), old_list)))
2181 {
2182 old_list = Fdelq (make_number (id), old_list);
2183 new_head = Fcons (make_number (id), new_head);
2184 }
5729c92f 2185 }
8ddf5e57 2186 arglist[0] = Fnreverse (new_head);
6a9c90ec 2187 arglist[1] = Vcharset_non_preferred_head = old_list;
8ddf5e57 2188 Vcharset_ordered_list = Fnconc (2, arglist);
dbbb237d 2189 charset_ordered_list_tick++;
5729c92f 2190
6809ca75 2191 charset_unibyte = -1;
321c819c 2192 for (old_list = Vcharset_ordered_list, list_2022 = list_emacs_mule = Qnil;
d017b41e 2193 CONSP (old_list); old_list = XCDR (old_list))
5729c92f 2194 {
e77415b0 2195 if (! NILP (Fmemq (XCAR (old_list), Viso_2022_charset_list)))
321c819c
KH
2196 list_2022 = Fcons (XCAR (old_list), list_2022);
2197 if (! NILP (Fmemq (XCAR (old_list), Vemacs_mule_charset_list)))
2198 list_emacs_mule = Fcons (XCAR (old_list), list_emacs_mule);
6809ca75
KH
2199 if (charset_unibyte < 0)
2200 {
2201 struct charset *charset = CHARSET_FROM_ID (XINT (XCAR (old_list)));
2202
2203 if (CHARSET_DIMENSION (charset) == 1
2204 && CHARSET_ASCII_COMPATIBLE_P (charset)
2205 && CHARSET_MAX_CHAR (charset) >= 0x80)
2206 charset_unibyte = CHARSET_ID (charset);
2207 }
4ed46869 2208 }
321c819c
KH
2209 Viso_2022_charset_list = Fnreverse (list_2022);
2210 Vemacs_mule_charset_list = Fnreverse (list_emacs_mule);
6809ca75
KH
2211 if (charset_unibyte < 0)
2212 charset_unibyte = charset_iso_8859_1;
4ed46869 2213
8ddf5e57 2214 return Qnil;
4ed46869
KH
2215}
2216
d5b33309
KH
2217DEFUN ("charset-id-internal", Fcharset_id_internal, Scharset_id_internal,
2218 0, 1, 0,
2219 doc: /* Internal use only.
2220Return charset identification number of CHARSET. */)
5842a27b 2221 (Lisp_Object charset)
4ed46869 2222{
d5b33309 2223 int id;
4ed46869 2224
d5b33309
KH
2225 CHECK_CHARSET_GET_ID (charset, id);
2226 return make_number (id);
4ed46869
KH
2227}
2228
7a84eee5
KH
2229struct charset_sort_data
2230{
2231 Lisp_Object charset;
2232 int id;
0065d054 2233 ptrdiff_t priority;
7a84eee5
KH
2234};
2235
2236static int
2237charset_compare (const void *d1, const void *d2)
2238{
2239 const struct charset_sort_data *data1 = d1, *data2 = d2;
0065d054
PE
2240 if (data1->priority != data2->priority)
2241 return data1->priority < data2->priority ? -1 : 1;
2242 return 0;
7a84eee5
KH
2243}
2244
2245DEFUN ("sort-charsets", Fsort_charsets, Ssort_charsets, 1, 1, 0,
2246 doc: /* Sort charset list CHARSETS by a priority of each charset.
2247Return the sorted list. CHARSETS is modified by side effects.
2248See also `charset-priority-list' and `set-charset-priority'. */)
2249 (Lisp_Object charsets)
2250{
2251 Lisp_Object len = Flength (charsets);
0065d054
PE
2252 ptrdiff_t n = XFASTINT (len), i, j;
2253 int done;
7a84eee5
KH
2254 Lisp_Object tail, elt, attrs;
2255 struct charset_sort_data *sort_data;
726929c4 2256 int id, min_id = INT_MAX, max_id = INT_MIN;
7a84eee5
KH
2257 USE_SAFE_ALLOCA;
2258
2259 if (n == 0)
2260 return Qnil;
0065d054 2261 SAFE_NALLOCA (sort_data, 1, n);
7a84eee5
KH
2262 for (tail = charsets, i = 0; CONSP (tail); tail = XCDR (tail), i++)
2263 {
2264 elt = XCAR (tail);
2265 CHECK_CHARSET_GET_ATTR (elt, attrs);
2266 sort_data[i].charset = elt;
2267 sort_data[i].id = id = XINT (CHARSET_ATTR_ID (attrs));
726929c4 2268 if (id < min_id)
7a84eee5 2269 min_id = id;
726929c4 2270 if (id > max_id)
7a84eee5
KH
2271 max_id = id;
2272 }
2273 for (done = 0, tail = Vcharset_ordered_list, i = 0;
2274 done < n && CONSP (tail); tail = XCDR (tail), i++)
2275 {
2276 elt = XCAR (tail);
2277 id = XFASTINT (elt);
2278 if (id >= min_id && id <= max_id)
2279 for (j = 0; j < n; j++)
2280 if (sort_data[j].id == id)
2281 {
2282 sort_data[j].priority = i;
2283 done++;
2284 }
2285 }
2286 qsort (sort_data, n, sizeof *sort_data, charset_compare);
2287 for (i = 0, tail = charsets; CONSP (tail); tail = XCDR (tail), i++)
2288 XSETCAR (tail, sort_data[i].charset);
2289 SAFE_FREE ();
2290 return charsets;
2291}
2292
4ed46869 2293\f
3263d5a2 2294void
971de7fb 2295init_charset (void)
4ed46869 2296{
c8f94403
GM
2297 Lisp_Object tempdir;
2298 tempdir = Fexpand_file_name (build_string ("charsets"), Vdata_directory);
73dcdb9f 2299 if (! file_accessible_directory_p (SSDATA (tempdir)))
c8f94403 2300 {
63541ed8
GM
2301 /* This used to be non-fatal (dir_warning), but it should not
2302 happen, and if it does sooner or later it will cause some
16905bec 2303 obscure problem (eg bug#6401), so better abort. */
63541ed8
GM
2304 fprintf (stderr, "Error: charsets directory not found:\n\
2305%s\n\
c8f94403
GM
2306Emacs will not function correctly without the character map files.\n\
2307Please check your installation!\n",
63541ed8
GM
2308 SDATA (tempdir));
2309 exit (1);
c8f94403
GM
2310 }
2311
6c6f1994 2312 Vcharset_map_path = list1 (tempdir);
4ed46869
KH
2313}
2314
4ed46869 2315
dfcf069d 2316void
971de7fb 2317init_charset_once (void)
4ed46869
KH
2318{
2319 int i, j, k;
2320
3263d5a2
KH
2321 for (i = 0; i < ISO_MAX_DIMENSION; i++)
2322 for (j = 0; j < ISO_MAX_CHARS; j++)
2323 for (k = 0; k < ISO_MAX_FINAL; k++)
2324 iso_charset_table[i][j][k] = -1;
4ed46869 2325
60383934 2326 for (i = 0; i < 256; i++)
b84ae584 2327 emacs_mule_charset[i] = -1;
4ed46869 2328
7c7dceee
KH
2329 charset_jisx0201_roman = -1;
2330 charset_jisx0208_1978 = -1;
2331 charset_jisx0208 = -1;
d32320c4 2332 charset_ksc5601 = -1;
4ed46869
KH
2333}
2334
2335#ifdef emacs
2336
f701dc2a
PE
2337/* Allocate an initial charset table that is large enough to handle
2338 Emacs while it is bootstrapping. As of September 2011, the size
2339 needs to be at least 166; make it a bit bigger to allow for future
2340 expansion.
2341
2342 Don't make the value so small that the table is reallocated during
2343 bootstrapping, as glibc malloc calls larger than just under 64 KiB
2344 during an initial bootstrap wreak havoc after dumping; see the
2345 M_MMAP_THRESHOLD value in alloc.c, plus there is a extra overhead
2346 internal to glibc malloc and perhaps to Emacs malloc debugging. */
2347static struct charset charset_table_init[180];
2348
dfcf069d 2349void
971de7fb 2350syms_of_charset (void)
4ed46869 2351{
3263d5a2
KH
2352 DEFSYM (Qcharsetp, "charsetp");
2353
2354 DEFSYM (Qascii, "ascii");
2355 DEFSYM (Qunicode, "unicode");
6c652beb 2356 DEFSYM (Qemacs, "emacs");
2fe1edd1 2357 DEFSYM (Qeight_bit, "eight-bit");
3263d5a2
KH
2358 DEFSYM (Qiso_8859_1, "iso-8859-1");
2359
2360 DEFSYM (Qgl, "gl");
2361 DEFSYM (Qgr, "gr");
2362
3263d5a2
KH
2363 staticpro (&Vcharset_ordered_list);
2364 Vcharset_ordered_list = Qnil;
2365
2366 staticpro (&Viso_2022_charset_list);
2367 Viso_2022_charset_list = Qnil;
2368
2369 staticpro (&Vemacs_mule_charset_list);
2370 Vemacs_mule_charset_list = Qnil;
2371
3943ed76 2372 /* Don't staticpro them here. It's done in syms_of_fns. */
088dcc3e
DN
2373 QCtest = intern_c_string (":test");
2374 Qeq = intern_c_string ("eq");
3943ed76 2375
3263d5a2 2376 staticpro (&Vcharset_hash_table);
8f924df7
KH
2377 {
2378 Lisp_Object args[2];
2379 args[0] = QCtest;
2380 args[1] = Qeq;
2381 Vcharset_hash_table = Fmake_hash_table (2, args);
2382 }
3263d5a2 2383
f701dc2a
PE
2384 charset_table = charset_table_init;
2385 charset_table_size = sizeof charset_table_init / sizeof *charset_table_init;
3263d5a2
KH
2386 charset_table_used = 0;
2387
3263d5a2
KH
2388 defsubr (&Scharsetp);
2389 defsubr (&Smap_charset_chars);
2390 defsubr (&Sdefine_charset_internal);
2391 defsubr (&Sdefine_charset_alias);
3263d5a2
KH
2392 defsubr (&Scharset_plist);
2393 defsubr (&Sset_charset_plist);
2394 defsubr (&Sunify_charset);
3fac5a51 2395 defsubr (&Sget_unused_iso_final_char);
4ed46869
KH
2396 defsubr (&Sdeclare_equiv_charset);
2397 defsubr (&Sfind_charset_region);
2398 defsubr (&Sfind_charset_string);
3263d5a2
KH
2399 defsubr (&Sdecode_char);
2400 defsubr (&Sencode_char);
4ed46869 2401 defsubr (&Ssplit_char);
3263d5a2 2402 defsubr (&Smake_char);
4ed46869 2403 defsubr (&Schar_charset);
90d7b74e 2404 defsubr (&Scharset_after);
4ed46869 2405 defsubr (&Siso_charset);
3263d5a2 2406 defsubr (&Sclear_charset_maps);
8ddf5e57
DL
2407 defsubr (&Scharset_priority_list);
2408 defsubr (&Sset_charset_priority);
d5b33309 2409 defsubr (&Scharset_id_internal);
7a84eee5 2410 defsubr (&Ssort_charsets);
3263d5a2 2411
29208e82 2412 DEFVAR_LISP ("charset-map-path", Vcharset_map_path,
fb7ada5f 2413 doc: /* List of directories to search for charset map files. */);
4beef065 2414 Vcharset_map_path = Qnil;
4ed46869 2415
29208e82 2416 DEFVAR_BOOL ("inhibit-load-charset-map", inhibit_load_charset_map,
ecca2aad
KH
2417 doc: /* Inhibit loading of charset maps. Used when dumping Emacs. */);
2418 inhibit_load_charset_map = 0;
2419
29208e82 2420 DEFVAR_LISP ("charset-list", Vcharset_list,
528623a0 2421 doc: /* List of all charsets ever defined. */);
3263d5a2
KH
2422 Vcharset_list = Qnil;
2423
29208e82 2424 DEFVAR_LISP ("current-iso639-language", Vcurrent_iso639_language,
6a9c90ec
KH
2425 doc: /* ISO639 language mnemonic symbol for the current language environment.
2426If the current language environment is for multiple languages (e.g. "Latin-1"),
2427the value may be a list of mnemonics. */);
2428 Vcurrent_iso639_language = Qnil;
2429
2fe1edd1
KH
2430 charset_ascii
2431 = define_charset_internal (Qascii, 1, "\x00\x7F\x00\x00\x00\x00",
2432 0, 127, 'B', -1, 0, 1, 0, 0);
14e3d523
KH
2433 charset_iso_8859_1
2434 = define_charset_internal (Qiso_8859_1, 1, "\x00\xFF\x00\x00\x00\x00",
2435 0, 255, -1, -1, -1, 1, 0, 0);
2fe1edd1 2436 charset_unicode
73fbf2d9 2437 = define_charset_internal (Qunicode, 3, "\x00\xFF\x00\xFF\x00\x10",
2fe1edd1 2438 0, MAX_UNICODE_CHAR, -1, 0, -1, 1, 0, 0);
6c652beb
KH
2439 charset_emacs
2440 = define_charset_internal (Qemacs, 3, "\x00\xFF\x00\xFF\x00\x3F",
2441 0, MAX_5_BYTE_CHAR, -1, 0, -1, 1, 1, 0);
2fe1edd1
KH
2442 charset_eight_bit
2443 = define_charset_internal (Qeight_bit, 1, "\x80\xFF\x00\x00\x00\x00",
880820fe 2444 128, 255, -1, 0, -1, 0, 1,
2fe1edd1 2445 MAX_5_BYTE_CHAR + 1);
6809ca75 2446 charset_unibyte = charset_iso_8859_1;
4ed46869
KH
2447}
2448
2449#endif /* emacs */