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