(charset_unibyte): Extern it instead of
[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.
75c8c592 3 Licensed to the Free Software Foundation.
12bcae05 4 Copyright (C) 2001 Free Software Foundation, Inc.
3263d5a2
KH
5 Copyright (C) 2001, 2002
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
KH
56Lisp_Object Vcharset_list;
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;
61
62/* Table of struct charset. */
63struct charset *charset_table;
64
65static int charset_table_size;
66int charset_table_used;
67
68Lisp_Object Qcharsetp;
69
70/* Special charset symbols. */
71Lisp_Object Qascii;
2fe1edd1 72Lisp_Object Qeight_bit;
3263d5a2
KH
73Lisp_Object Qiso_8859_1;
74Lisp_Object Qunicode;
b0e3cf2b 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;
c1a08b4c 81
7c7dceee
KH
82/* The other special charsets. */
83int charset_jisx0201_roman;
84int charset_jisx0208_1978;
85int charset_jisx0208;
86
3263d5a2
KH
87/* Value of charset attribute `charset-iso-plane'. */
88Lisp_Object Qgl, Qgr;
c1a08b4c 89
3263d5a2
KH
90/* The primary charset. It is a charset of unibyte characters. */
91int charset_primary;
92
93/* List of charsets ordered by the priority. */
94Lisp_Object Vcharset_ordered_list;
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;
100
3263d5a2
KH
101/* List of iso-2022 charsets. */
102Lisp_Object Viso_2022_charset_list;
103
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
113Lisp_Object Vcharset_map_directory;
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))))
158
4ed46869 159
3263d5a2 160\f
4ed46869 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;
35e623fb 165
e9ce014c
KH
166struct charset_map_entries
167{
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 }
3263d5a2 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)
232 {
233 to_index = from_index;
234 to_c = from_c;
235 }
236 else
237 {
238 to_index = CODE_POINT_TO_INDEX (charset, to);
239 to_c = from_c + (to_index - from_index);
240 }
241 if (from_index < 0 || to_index < 0)
242 continue;
3263d5a2 243
3263d5a2
KH
244 if (control_flag < 2)
245 {
3b4f4446
KH
246 int c;
247
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 }
264
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
KH
275 ASET (vec, from_index, make_number (from_c));
276 CHAR_TABLE_SET (table, from_c, make_number (code));
e9ce014c
KH
277 if (from_index == to_index)
278 break;
3b4f4446 279 from_index++, from_c++;
e9ce014c
KH
280 code = INDEX_TO_CODE_POINT (charset, from_index);
281 }
282 else
3b4f4446 283 for (; from_index <= to_index; from_index++, from_c++)
e9ce014c 284 {
3b4f4446
KH
285 ASET (vec, from_index, make_number (from_c));
286 CHAR_TABLE_SET (table, from_c, make_number (from_index));
e9ce014c 287 }
3263d5a2 288 }
3263d5a2 289 }
e9ce014c 290 else
2e344af3 291 {
69f8de5b 292 unsigned code = from;
3b4f4446 293
69f8de5b
KH
294 while (1)
295 {
296 int c1 = DECODE_CHAR (charset, code);
297
3263d5a2
KH
298 if (c1 >= 0)
299 {
3b4f4446 300 CHAR_TABLE_SET (table, from_c, make_number (c1));
16fed1fc 301 CHAR_TABLE_SET (Vchar_unify_table, c1, make_number (from_c));
3263d5a2
KH
302 if (CHAR_TABLE_P (Vchar_unified_charset_table))
303 CHAR_TABLE_SET (Vchar_unified_charset_table, c1,
304 CHARSET_NAME (charset));
305 }
69f8de5b
KH
306 if (from_index == to_index)
307 break;
3b4f4446 308 from_index++, from_c++;
69f8de5b 309 code = INDEX_TO_CODE_POINT (charset, from_index);
3263d5a2 310 }
2e344af3 311 }
8ac5a9cc 312 }
3263d5a2
KH
313
314 if (control_flag < 2)
4ed46869 315 {
3263d5a2
KH
316 CHARSET_MIN_CHAR (charset) = (ascii_compatible_p
317 ? nonascii_min_char : min_char);
318 CHARSET_MAX_CHAR (charset) = max_char;
e9ce014c 319 if (control_flag == 1)
4ed46869 320 {
3263d5a2
KH
321 CHARSET_DECODER (charset) = vec;
322 CHARSET_ENCODER (charset) = table;
4ed46869
KH
323 }
324 }
2e344af3 325 else
3263d5a2 326 CHARSET_DEUNIFIER (charset) = table;
4ed46869
KH
327}
328
12bcae05 329
3263d5a2
KH
330/* Read a hexadecimal number (preceded by "0x") from the file FP while
331 paying attention to comment charcter '#'. */
12bcae05 332
3263d5a2
KH
333static INLINE unsigned
334read_hex (fp, eof)
335 FILE *fp;
336 int *eof;
12bcae05 337{
3263d5a2
KH
338 int c;
339 unsigned n;
12bcae05 340
3263d5a2
KH
341 while ((c = getc (fp)) != EOF)
342 {
69f8de5b 343 if (c == '#')
3263d5a2
KH
344 {
345 while ((c = getc (fp)) != EOF && c != '\n');
346 }
347 else if (c == '0')
348 {
349 if ((c = getc (fp)) == EOF || c == 'x')
350 break;
351 }
352 }
353 if (c == EOF)
354 {
355 *eof = 1;
356 return 0;
357 }
358 *eof = 0;
359 n = 0;
360 if (c == 'x')
361 while ((c = getc (fp)) != EOF && isxdigit (c))
362 n = ((n << 4)
363 | (c <= '9' ? c - '0' : c <= 'F' ? c - 'A' + 10 : c - 'a' + 10));
364 else
365 while ((c = getc (fp)) != EOF && isdigit (c))
366 n = (n * 10) + c - '0';
e9ce014c
KH
367 if (c != EOF)
368 ungetc (c, fp);
3263d5a2
KH
369 return n;
370}
12bcae05 371
537efd8d 372
3263d5a2 373/* Return a mapping vector for CHARSET loaded from MAPFILE.
e9ce014c
KH
374 Each line of MAPFILE has this form
375 0xAAAA 0xCCCC
376 where 0xAAAA is a code-point and 0xCCCC is the corresponding
377 character code, or this form
378 0xAAAA-0xBBBB 0xCCCC
379 where 0xAAAA and 0xBBBB are code-points specifying a range, and
380 0xCCCC is the first character code of the range.
381
3263d5a2
KH
382 The returned vector has this form:
383 [ CODE1 CHAR1 CODE2 CHAR2 .... ]
e9ce014c
KH
384 where CODE1 is a code-point or a cons of code-points specifying a
385 range. */
4ed46869 386
c449997d
KH
387extern void add_to_log P_ ((char *, Lisp_Object, Lisp_Object));
388
e9ce014c
KH
389static void
390load_charset_map_from_file (charset, mapfile, control_flag)
3263d5a2
KH
391 struct charset *charset;
392 Lisp_Object mapfile;
e9ce014c 393 int control_flag;
4ed46869 394{
e9ce014c
KH
395 unsigned min_code = CHARSET_MIN_CODE (charset);
396 unsigned max_code = CHARSET_MAX_CODE (charset);
3263d5a2
KH
397 int fd;
398 FILE *fp;
3263d5a2
KH
399 int eof;
400 Lisp_Object suffixes;
e9ce014c
KH
401 struct charset_map_entries *head, *entries;
402 int n_entries;
4ed46869 403
3263d5a2
KH
404 suffixes = Fcons (build_string (".map"),
405 Fcons (build_string (".TXT"), Qnil));
4ed46869 406
3263d5a2
KH
407 fd = openp (Fcons (Vcharset_map_directory, Qnil), mapfile, suffixes,
408 NULL, 0);
409 if (fd < 0
410 || ! (fp = fdopen (fd, "r")))
411 {
412 add_to_log ("Failure in loading charset map: %S", mapfile, Qnil);
e9ce014c 413 return;
3263d5a2 414 }
4ed46869 415
e9ce014c
KH
416 head = entries = ((struct charset_map_entries *)
417 alloca (sizeof (struct charset_map_entries)));
418 n_entries = 0;
3263d5a2
KH
419 eof = 0;
420 while (1)
421 {
e9ce014c
KH
422 unsigned from, to;
423 int c;
424 int idx;
4ed46869 425
e9ce014c 426 from = read_hex (fp, &eof);
3263d5a2
KH
427 if (eof)
428 break;
e9ce014c
KH
429 if (getc (fp) == '-')
430 to = read_hex (fp, &eof);
431 else
432 to = from;
433 c = (int) read_hex (fp, &eof);
434
435 if (from < min_code || to > max_code || from > to || c > MAX_CHAR)
436 continue;
437
438 if (n_entries > 0 && (n_entries % 0x10000) == 0)
3263d5a2 439 {
e9ce014c
KH
440 entries->next = ((struct charset_map_entries *)
441 alloca (sizeof (struct charset_map_entries)));
442 entries = entries->next;
3263d5a2 443 }
e9ce014c
KH
444 idx = n_entries % 0x10000;
445 entries->entry[idx].from = from;
446 entries->entry[idx].to = to;
447 entries->entry[idx].c = c;
448 n_entries++;
3263d5a2
KH
449 }
450 fclose (fp);
451 close (fd);
4ed46869 452
e9ce014c
KH
453 load_charset_map (charset, head, n_entries, control_flag);
454}
455
456static void
457load_charset_map_from_vector (charset, vec, control_flag)
458 struct charset *charset;
459 Lisp_Object vec;
460 int control_flag;
461{
462 unsigned min_code = CHARSET_MIN_CODE (charset);
463 unsigned max_code = CHARSET_MAX_CODE (charset);
464 struct charset_map_entries *head, *entries;
465 int n_entries;
466 int len = ASIZE (vec);
467 int i;
468
469 if (len % 2 == 1)
3263d5a2 470 {
e9ce014c
KH
471 add_to_log ("Failure in loading charset map: %V", vec, Qnil);
472 return;
3263d5a2 473 }
4ed46869 474
e9ce014c
KH
475 head = entries = ((struct charset_map_entries *)
476 alloca (sizeof (struct charset_map_entries)));
477 n_entries = 0;
478 for (i = 0; i < len; i += 2)
479 {
480 Lisp_Object val, val2;
481 unsigned from, to;
482 int c;
483 int idx;
3263d5a2 484
e9ce014c
KH
485 val = AREF (vec, i);
486 if (CONSP (val))
487 {
488 val2 = XCDR (val);
489 val = XCAR (val);
490 CHECK_NATNUM (val);
491 CHECK_NATNUM (val2);
492 from = XFASTINT (val);
493 to = XFASTINT (val2);
494 }
495 else
496 {
497 CHECK_NATNUM (val);
498 from = to = XFASTINT (val);
499 }
500 val = AREF (vec, i + 1);
501 CHECK_NATNUM (val);
502 c = XFASTINT (val);
503
504 if (from < min_code || to > max_code || from > to || c > MAX_CHAR)
505 continue;
506
507 if ((n_entries % 0x10000) == 0)
508 {
509 entries->next = ((struct charset_map_entries *)
510 alloca (sizeof (struct charset_map_entries)));
511 entries = entries->next;
512 }
513 idx = n_entries % 0x10000;
514 entries->entry[idx].from = from;
515 entries->entry[idx].to = to;
516 entries->entry[idx].c = c;
517 n_entries++;
518 }
519
520 load_charset_map (charset, head, n_entries, control_flag);
ac4137cc
KH
521}
522
3263d5a2
KH
523static void
524load_charset (charset)
525 struct charset *charset;
ac4137cc 526{
3263d5a2
KH
527 if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP_DEFERRED)
528 {
529 Lisp_Object map;
ac4137cc 530
3263d5a2
KH
531 map = CHARSET_MAP (charset);
532 if (STRINGP (map))
e9ce014c
KH
533 load_charset_map_from_file (charset, map, 1);
534 else
535 load_charset_map_from_vector (charset, map, 1);
3263d5a2
KH
536 CHARSET_METHOD (charset) = CHARSET_METHOD_MAP;
537 }
4ed46869
KH
538}
539
3263d5a2
KH
540
541DEFUN ("charsetp", Fcharsetp, Scharsetp, 1, 1, 0,
542 doc: /* Return non-nil if and only if OBJECT is a charset.*/)
543 (object)
544 Lisp_Object object;
23d2a7f1 545{
3263d5a2 546 return (CHARSETP (object) ? Qt : Qnil);
23d2a7f1
KH
547}
548
35e623fb 549
3263d5a2 550void
374c5cfd
KH
551map_charset_chars (c_function, function, arg,
552 charset, from, to)
553 void (*c_function) P_ ((Lisp_Object, Lisp_Object));
554 Lisp_Object function, arg;
555 struct charset *charset;
556 unsigned from, to;
557
35e623fb 558{
3263d5a2 559 Lisp_Object range;
374c5cfd 560 int partial;
3263d5a2
KH
561
562 if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP_DEFERRED)
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)))
570 {
571 map_char_table_for_charset (c_function, function,
572 CHARSET_DEUNIFIER (charset), arg,
573 partial ? charset : NULL, from, to);
574 }
575
3263d5a2
KH
576 if (CHARSET_METHOD (charset) == CHARSET_METHOD_OFFSET)
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))
374c5cfd 585 (*c_function) (range, arg);
3263d5a2
KH
586 else
587 call2 (function, range, arg);
588 }
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)
bbf12bb3 594 {
374c5cfd
KH
595 range = Fcons (make_number (from), make_number (to));
596 if (to >= 128)
597 XSETCAR (range, make_number (127));
598
3263d5a2 599 if (NILP (function))
374c5cfd 600 (*c_function) (range, arg);
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)
3263d5a2 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);
623 }
624 else /* i.e. CHARSET_METHOD_SUPERSET */
625 {
626 Lisp_Object parents;
3263d5a2 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);
642 map_charset_chars (c_function, function, arg, charset, from, to);
bbf12bb3 643 }
35e623fb 644 }
3263d5a2
KH
645}
646
374c5cfd
KH
647
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.
6abd9323 652
374c5cfd
KH
653RANGE is a cons (FROM . TO), where FROM and TO indicate a range of
654characters contained in CHARSET.
655
656The optional 4th and 5th arguments FROM-CODE and TO-CODE specify the
657range of code points of targer characters. */)
658 (function, charset, arg, from_code, to_code)
659 Lisp_Object function, charset, arg, from_code, to_code;
3263d5a2 660{
374c5cfd 661 struct charset *cs;
16fed1fc 662 unsigned from, to;
374c5cfd
KH
663
664 CHECK_CHARSET_GET_CHARSET (charset, cs);
665 if (NILP (from_code))
16fed1fc 666 from = CHARSET_MIN_CODE (cs);
970b7474
KH
667 else
668 {
669 CHECK_NATNUM (from_code);
670 from = XINT (from_code);
671 if (from < CHARSET_MIN_CODE (cs))
672 from = CHARSET_MIN_CODE (cs);
673 }
374c5cfd 674 if (NILP (to_code))
970b7474
KH
675 to = CHARSET_MAX_CODE (cs);
676 else
677 {
678 CHECK_NATNUM (to_code);
679 to = XINT (to_code);
680 if (to > CHARSET_MAX_CODE (cs))
681 to = CHARSET_MAX_CODE (cs);
682 }
16fed1fc 683 map_charset_chars (NULL, function, arg, cs, from, to);
3263d5a2 684 return Qnil;
35e623fb 685}
76d7b829
KH
686
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. */
76d7b829 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;
76d7b829 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 }
76d7b829 740
3263d5a2
KH
741 val = args[charset_arg_dimension];
742 if (NILP (val))
743 charset.dimension = dimension;
744 else
745 {
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));
750 }
751
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
KH
760 if (! charset.code_linear_p)
761 {
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);
768 }
769
3263d5a2
KH
770 charset.iso_chars_96 = charset.code_space[2] == 96;
771
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
KH
780 charset.char_index_offset = 0;
781
782 val = args[charset_arg_min_code];
783 if (! NILP (val))
784 {
785 unsigned code;
786
787 if (INTEGERP (val))
788 code = XINT (val);
789 else
790 {
791 CHECK_CONS (val);
792 CHECK_NUMBER (XCAR (val));
793 CHECK_NUMBER (XCDR (val));
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 }
803
804 val = args[charset_arg_max_code];
805 if (! NILP (val))
806 {
807 unsigned code;
808
809 if (INTEGERP (val))
810 code = XINT (val);
811 else
812 {
813 CHECK_CONS (val);
814 CHECK_NUMBER (XCAR (val));
815 CHECK_NUMBER (XCDR (val));
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;
823 }
3263d5a2 824
e9ce014c
KH
825 charset.compact_codes_p = charset.max_code < 0x1000000;
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 }
76d7b829 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 }
857
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 }
3263d5a2
KH
868
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 }
6ef23ebb 879
3263d5a2 880 charset.ascii_compatible_p = ! NILP (args[charset_arg_ascii_compatible_p]);
4ed46869 881
3263d5a2
KH
882 charset.supplementary_p = ! NILP (args[charset_arg_supplementary_p]);
883
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
903 for (i = charset.min_char; i < 0x10000 && i <= charset.max_char;
904 i += 128)
905 CHARSET_FAST_MAP_SET (i, charset.fast_map);
906 for (; i <= charset.max_char; i += 0x1000)
907 CHARSET_FAST_MAP_SET (i, charset.fast_map);
908 }
909 else if (! NILP (args[charset_arg_map]))
910 {
911 val = args[charset_arg_map];
912 ASET (attrs, charset_map, val);
913 if (STRINGP (val))
e9ce014c
KH
914 load_charset_map_from_file (&charset, val, 0);
915 else
916 load_charset_map_from_vector (&charset, val, 0);
3263d5a2
KH
917 charset.method = CHARSET_METHOD_MAP_DEFERRED;
918 }
374c5cfd 919 else if (! NILP (args[charset_arg_subset]))
3263d5a2 920 {
374c5cfd
KH
921 Lisp_Object parent;
922 Lisp_Object parent_min_code, parent_max_code, parent_code_offset;
923 struct charset *parent_charset;
924
925 val = args[charset_arg_subset];
926 parent = Fcar (val);
927 CHECK_CHARSET_GET_CHARSET (parent, parent_charset);
928 parent_min_code = Fnth (make_number (1), val);
929 CHECK_NATNUM (parent_min_code);
930 parent_max_code = Fnth (make_number (2), val);
931 CHECK_NATNUM (parent_max_code);
932 parent_code_offset = Fnth (make_number (3), val);
933 CHECK_NUMBER (parent_code_offset);
934 val = Fmake_vector (make_number (4), Qnil);
935 ASET (val, 0, make_number (parent_charset->id));
936 ASET (val, 1, parent_min_code);
937 ASET (val, 2, parent_max_code);
938 ASET (val, 3, parent_code_offset);
939 ASET (attrs, charset_subset, val);
940
941 charset.method = CHARSET_METHOD_SUBSET;
942 /* Here, we just copy the parent's fast_map. It's not accurate,
943 but at least it works for quickly detecting which character
944 DOESN'T belong to this charset. */
945 for (i = 0; i < 190; i++)
946 charset.fast_map[i] = parent_charset->fast_map[i];
947
948 /* We also copy these for parents. */
949 charset.min_char = parent_charset->min_char;
950 charset.max_char = parent_charset->max_char;
951 }
952 else if (! NILP (args[charset_arg_superset]))
953 {
954 val = args[charset_arg_superset];
955 charset.method = CHARSET_METHOD_SUPERSET;
3263d5a2 956 val = Fcopy_sequence (val);
374c5cfd 957 ASET (attrs, charset_superset, val);
3263d5a2
KH
958
959 charset.min_char = MAX_CHAR;
960 charset.max_char = 0;
961 for (; ! NILP (val); val = Fcdr (val))
4ed46869 962 {
3263d5a2
KH
963 Lisp_Object elt, car_part, cdr_part;
964 int this_id, offset;
965 struct charset *this_charset;
966
967 elt = Fcar (val);
968 if (CONSP (elt))
969 {
970 car_part = XCAR (elt);
971 cdr_part = XCDR (elt);
972 CHECK_CHARSET_GET_ID (car_part, this_id);
973 CHECK_NUMBER (cdr_part);
974 offset = XINT (cdr_part);
975 }
976 else
4ed46869 977 {
3263d5a2
KH
978 CHECK_CHARSET_GET_ID (elt, this_id);
979 offset = 0;
4ed46869 980 }
3263d5a2
KH
981 XSETCAR (val, Fcons (make_number (this_id), make_number (offset)));
982
983 this_charset = CHARSET_FROM_ID (this_id);
984 if (charset.min_char > this_charset->min_char)
985 charset.min_char = this_charset->min_char;
986 if (charset.max_char < this_charset->max_char)
987 charset.max_char = this_charset->max_char;
988 for (i = 0; i < 190; i++)
989 charset.fast_map[i] |= this_charset->fast_map[i];
4ed46869 990 }
4ed46869 991 }
3263d5a2
KH
992 else
993 error ("None of :code-offset, :map, :parents are specified");
4ed46869 994
3263d5a2
KH
995 val = args[charset_arg_unify_map];
996 if (! NILP (val) && !STRINGP (val))
997 CHECK_VECTOR (val);
998 ASET (attrs, charset_unify_map, val);
4ed46869 999
3263d5a2
KH
1000 CHECK_LIST (args[charset_arg_plist]);
1001 ASET (attrs, charset_plist, args[charset_arg_plist]);
4ed46869 1002
3263d5a2
KH
1003 charset.hash_index = hash_lookup (hash_table, args[charset_arg_name],
1004 &hash_code);
1005 if (charset.hash_index >= 0)
1006 {
1007 new_definition_p = 0;
4f65af01 1008 id = XFASTINT (CHARSET_SYMBOL_ID (args[charset_arg_name]));
3263d5a2
KH
1009 HASH_VALUE (hash_table, charset.hash_index) = attrs;
1010 }
1a45ff10 1011 else
3263d5a2
KH
1012 {
1013 charset.hash_index = hash_put (hash_table, args[charset_arg_name], attrs,
1014 hash_code);
1015 if (charset_table_used == charset_table_size)
1016 {
2fe1edd1
KH
1017 struct charset *new_table
1018 = (struct charset *) xmalloc (sizeof (struct charset)
1019 * (charset_table_size + 16));
1020 bcopy (charset_table, new_table,
1021 sizeof (struct charset) * charset_table_size);
1022 charset_table_size += 16;
1023 charset_table = new_table;
3263d5a2
KH
1024 }
1025 id = charset_table_used++;
3263d5a2
KH
1026 new_definition_p = 1;
1027 }
4ed46869 1028
4f65af01 1029 ASET (attrs, charset_id, make_number (id));
3263d5a2
KH
1030 charset.id = id;
1031 charset_table[id] = charset;
1032
1033 if (charset.iso_final >= 0)
4ed46869 1034 {
3263d5a2
KH
1035 ISO_CHARSET_TABLE (charset.dimension, charset.iso_chars_96,
1036 charset.iso_final) = id;
1037 if (new_definition_p)
1038 Viso_2022_charset_list = nconc2 (Viso_2022_charset_list,
1039 Fcons (make_number (id), Qnil));
7c7dceee
KH
1040 if (ISO_CHARSET_TABLE (1, 0, 'J') == id)
1041 charset_jisx0201_roman = id;
1042 else if (ISO_CHARSET_TABLE (2, 0, '@') == id)
1043 charset_jisx0208_1978 = id;
1044 else if (ISO_CHARSET_TABLE (2, 0, 'B') == id)
1045 charset_jisx0208 = id;
4ed46869 1046 }
3263d5a2
KH
1047
1048 if (charset.emacs_mule_id >= 0)
4ed46869 1049 {
3263d5a2 1050 emacs_mule_charset[charset.emacs_mule_id] = CHARSET_FROM_ID (id);
4f65af01
KH
1051 if (charset.emacs_mule_id < 0xA0)
1052 emacs_mule_bytes[charset.emacs_mule_id] = charset.dimension + 1;
3263d5a2
KH
1053 if (new_definition_p)
1054 Vemacs_mule_charset_list = nconc2 (Vemacs_mule_charset_list,
1055 Fcons (make_number (id), Qnil));
4ed46869
KH
1056 }
1057
3263d5a2
KH
1058 if (new_definition_p)
1059 {
1060 Vcharset_list = Fcons (args[charset_arg_name], Vcharset_list);
1061 Vcharset_ordered_list = nconc2 (Vcharset_ordered_list,
1062 Fcons (make_number (id), Qnil));
dbbb237d 1063 charset_ordered_list_tick++;
3263d5a2 1064 }
4ed46869 1065
3263d5a2 1066 return Qnil;
4ed46869
KH
1067}
1068
2fe1edd1
KH
1069
1070/* Same as Fdefine_charset_internal but arguments are more convenient
1071 to call from C (typically in syms_of_charset). This can define a
1072 charset of `offset' method only. Return the ID of the new
1073 charset. */
1074
1075static int
1076define_charset_internal (name, dimension, code_space, min_code, max_code,
1077 iso_final, iso_revision, emacs_mule_id,
1078 ascii_compatible, supprementary,
1079 code_offset)
1080 Lisp_Object name;
1081 int dimension;
1082 unsigned char *code_space;
1083 unsigned min_code, max_code;
1084 int iso_final, iso_revision, emacs_mule_id;
1085 int ascii_compatible, supprementary;
1086 int code_offset;
1087{
1088 Lisp_Object args[charset_arg_max];
1089 Lisp_Object plist[14];
1090 Lisp_Object val;
1091 int i;
1092
1093 args[charset_arg_name] = name;
1094 args[charset_arg_dimension] = make_number (dimension);
1095 val = Fmake_vector (make_number (8), make_number (0));
1096 for (i = 0; i < 8; i++)
1097 ASET (val, i, make_number (code_space[i]));
1098 args[charset_arg_code_space] = val;
1099 args[charset_arg_min_code] = make_number (min_code);
1100 args[charset_arg_max_code] = make_number (max_code);
1101 args[charset_arg_iso_final]
1102 = (iso_final < 0 ? Qnil : make_number (iso_final));
1103 args[charset_arg_iso_revision] = make_number (iso_revision);
1104 args[charset_arg_emacs_mule_id]
1105 = (emacs_mule_id < 0 ? Qnil : make_number (emacs_mule_id));
1106 args[charset_arg_ascii_compatible_p] = ascii_compatible ? Qt : Qnil;
1107 args[charset_arg_supplementary_p] = supprementary ? Qt : Qnil;
1108 args[charset_arg_invalid_code] = Qnil;
1109 args[charset_arg_code_offset] = make_number (code_offset);
1110 args[charset_arg_map] = Qnil;
1111 args[charset_arg_subset] = Qnil;
1112 args[charset_arg_superset] = Qnil;
1113 args[charset_arg_unify_map] = Qnil;
1114
1115 plist[0] = intern (":name");
1116 plist[1] = args[charset_arg_name];
1117 plist[2] = intern (":dimension");
1118 plist[3] = args[charset_arg_dimension];
1119 plist[4] = intern (":code-space");
1120 plist[5] = args[charset_arg_code_space];
1121 plist[6] = intern (":iso-final-char");
1122 plist[7] = args[charset_arg_iso_final];
1123 plist[8] = intern (":emacs-mule-id");
1124 plist[9] = args[charset_arg_emacs_mule_id];
1125 plist[10] = intern (":ascii-compatible-p");
1126 plist[11] = args[charset_arg_ascii_compatible_p];
1127 plist[12] = intern (":code-offset");
1128 plist[13] = args[charset_arg_code_offset];
1129
1130 args[charset_arg_plist] = Flist (14, plist);
1131 Fdefine_charset_internal (charset_arg_max, args);
1132
1133 return XINT (CHARSET_SYMBOL_ID (name));
1134}
1135
1136
3263d5a2
KH
1137DEFUN ("define-charset-alias", Fdefine_charset_alias,
1138 Sdefine_charset_alias, 2, 2, 0,
1139 doc: /* Define ALIAS as an alias for charset CHARSET. */)
1140 (alias, charset)
1141 Lisp_Object alias, charset;
4ed46869 1142{
3263d5a2
KH
1143 Lisp_Object attr;
1144
1145 CHECK_CHARSET_GET_ATTR (charset, attr);
1146 Fputhash (alias, attr, Vcharset_hash_table);
528623a0 1147 Vcharset_list = Fcons (alias, Vcharset_list);
3263d5a2
KH
1148 return Qnil;
1149}
4ed46869 1150
4ed46869 1151
3263d5a2 1152DEFUN ("primary-charset", Fprimary_charset, Sprimary_charset, 0, 0, 0,
56a46d1d 1153 doc: /* Return the primary charset (set by `set-primary-charset'). */)
3263d5a2
KH
1154 ()
1155{
1156 return CHARSET_NAME (CHARSET_FROM_ID (charset_primary));
1157}
4ed46869 1158
4ed46869 1159
3263d5a2
KH
1160DEFUN ("set-primary-charset", Fset_primary_charset, Sset_primary_charset,
1161 1, 1, 0,
56a46d1d
DL
1162 doc: /* Set the primary charset to CHARSET.
1163This determines how unibyte/multibyte conversion is done. See also
1164function `primary-charset'. */)
3263d5a2
KH
1165 (charset)
1166 Lisp_Object charset;
1167{
1168 int id;
1169
1170 CHECK_CHARSET_GET_ID (charset, id);
1171 charset_primary = id;
4ed46869
KH
1172 return Qnil;
1173}
1174
3263d5a2
KH
1175
1176DEFUN ("charset-plist", Fcharset_plist, Scharset_plist, 1, 1, 0,
56a46d1d 1177 doc: /* Return the property list of CHARSET. */)
3263d5a2
KH
1178 (charset)
1179 Lisp_Object charset;
1180{
1181 Lisp_Object attrs;
1182
1183 CHECK_CHARSET_GET_ATTR (charset, attrs);
1184 return CHARSET_ATTR_PLIST (attrs);
1185}
1186
1187
1188DEFUN ("set-charset-plist", Fset_charset_plist, Sset_charset_plist, 2, 2, 0,
1189 doc: /* Set CHARSET's property list to PLIST. */)
1190 (charset, plist)
1191 Lisp_Object charset, plist;
1192{
1193 Lisp_Object attrs;
1194
1195 CHECK_CHARSET_GET_ATTR (charset, attrs);
1196 CHARSET_ATTR_PLIST (attrs) = plist;
1197 return plist;
1198}
1199
1200
dbbb237d 1201DEFUN ("unify-charset", Funify_charset, Sunify_charset, 1, 3, 0,
56a46d1d
DL
1202 doc: /* Unify characters of CHARSET with Unicode.
1203This means reading the relevant file and installing the table defined
dbbb237d
KH
1204by CHARSET's `:unify-map' property.
1205
64165ae2
DL
1206Optional second arg UNIFY-MAP is a file name string or a vector. It has
1207the same meaning as the `:unify-map' attribute in the function
dbbb237d
KH
1208`define-charset' (which see).
1209
1210Optional third argument DEUNIFY, if non-nil, means to de-unify CHARSET. */)
1211 (charset, unify_map, deunify)
1212 Lisp_Object charset, unify_map, deunify;
8a73a704 1213{
3263d5a2
KH
1214 int id;
1215 struct charset *cs;
1216
1217 CHECK_CHARSET_GET_ID (charset, id);
1218 cs = CHARSET_FROM_ID (id);
1219 if (CHARSET_METHOD (cs) == CHARSET_METHOD_MAP_DEFERRED)
1220 load_charset (cs);
dbbb237d
KH
1221 if (NILP (deunify)
1222 ? CHARSET_UNIFIED_P (cs) && ! NILP (CHARSET_DEUNIFIER (cs))
1223 : ! CHARSET_UNIFIED_P (cs))
3263d5a2 1224 return Qnil;
dbbb237d 1225
3263d5a2 1226 CHARSET_UNIFIED_P (cs) = 0;
dbbb237d
KH
1227 if (NILP (deunify))
1228 {
1229 if (CHARSET_METHOD (cs) != CHARSET_METHOD_OFFSET)
1230 error ("Can't unify charset: %s", XSYMBOL (charset)->name->data);
1231 if (NILP (unify_map))
1232 unify_map = CHARSET_UNIFY_MAP (cs);
1233 if (STRINGP (unify_map))
1234 load_charset_map_from_file (cs, unify_map, 2);
1235 else if (VECTORP (unify_map))
1236 load_charset_map_from_vector (cs, unify_map, 2);
1237 else if (NILP (unify_map))
1238 error ("No unify-map for charset");
1239 else
1240 error ("Bad unify-map arg");
1241 CHARSET_UNIFIED_P (cs) = 1;
1242 }
1243 else if (CHAR_TABLE_P (Vchar_unify_table))
1244 {
1245 int min_code = CHARSET_MIN_CODE (cs);
1246 int max_code = CHARSET_MAX_CODE (cs);
1247 int min_char = DECODE_CHAR (cs, min_code);
1248 int max_char = DECODE_CHAR (cs, max_code);
1249
1250 char_table_set_range (Vchar_unify_table, min_char, max_char, Qnil);
1251 }
1252
3263d5a2 1253 return Qnil;
8a73a704
KH
1254}
1255
3fac5a51
KH
1256DEFUN ("get-unused-iso-final-char", Fget_unused_iso_final_char,
1257 Sget_unused_iso_final_char, 2, 2, 0,
3263d5a2 1258 doc: /*
1721b6af 1259Return an unsed ISO final char for a charset of DIMENISION and CHARS.
fdb82f93
PJ
1260DIMENSION is the number of bytes to represent a character: 1 or 2.
1261CHARS is the number of characters in a dimension: 94 or 96.
1262
1263This final char is for private use, thus the range is `0' (48) .. `?' (63).
1721b6af 1264If there's no unused final char for the specified kind of charset,
fdb82f93
PJ
1265return nil. */)
1266 (dimension, chars)
3fac5a51
KH
1267 Lisp_Object dimension, chars;
1268{
1269 int final_char;
1270
b7826503
PJ
1271 CHECK_NUMBER (dimension);
1272 CHECK_NUMBER (chars);
3263d5a2
KH
1273 if (XINT (dimension) != 1 && XINT (dimension) != 2 && XINT (dimension) != 3)
1274 args_out_of_range_3 (dimension, make_number (1), make_number (3));
3fac5a51 1275 if (XINT (chars) != 94 && XINT (chars) != 96)
3263d5a2 1276 args_out_of_range_3 (chars, make_number (94), make_number (96));
3fac5a51 1277 for (final_char = '0'; final_char <= '?'; final_char++)
3263d5a2
KH
1278 if (ISO_CHARSET_TABLE (XINT (dimension), XINT (chars), final_char) < 0)
1279 break;
3fac5a51
KH
1280 return (final_char <= '?' ? make_number (final_char) : Qnil);
1281}
1282
3263d5a2
KH
1283static void
1284check_iso_charset_parameter (dimension, chars, final_char)
1285 Lisp_Object dimension, chars, final_char;
4ed46869 1286{
3263d5a2
KH
1287 CHECK_NATNUM (dimension);
1288 CHECK_NATNUM (chars);
1289 CHECK_NATNUM (final_char);
4ed46869 1290
3263d5a2
KH
1291 if (XINT (dimension) > 3)
1292 error ("Invalid DIMENSION %d, it should be 1, 2, or 3", XINT (dimension));
4ed46869
KH
1293 if (XINT (chars) != 94 && XINT (chars) != 96)
1294 error ("Invalid CHARS %d, it should be 94 or 96", XINT (chars));
3263d5a2 1295 if (XINT (final_char) < '0' || XINT (final_char) > '~')
4ed46869 1296 error ("Invalid FINAL-CHAR %c, it should be `0'..`~'", XINT (chars));
3263d5a2
KH
1297}
1298
1299
1300DEFUN ("declare-equiv-charset", Fdeclare_equiv_charset, Sdeclare_equiv_charset,
1301 4, 4, 0,
1302 doc: /*
1303Declare a charset of DIMENSION, CHARS, FINAL-CHAR is the same as CHARSET.
56a46d1d 1304CHARSET should be defined by `define-charset' in advance. */)
3263d5a2
KH
1305 (dimension, chars, final_char, charset)
1306 Lisp_Object dimension, chars, final_char, charset;
1307{
1308 int id;
4ed46869 1309
3263d5a2
KH
1310 CHECK_CHARSET_GET_ID (charset, id);
1311 check_iso_charset_parameter (dimension, chars, final_char);
1312
16fed1fc 1313 ISO_CHARSET_TABLE (XINT (dimension), XINT (chars), XINT (final_char)) = id;
4ed46869
KH
1314 return Qnil;
1315}
1316
3263d5a2 1317
2e344af3
KH
1318/* Return information about charsets in the text at PTR of NBYTES
1319 bytes, which are NCHARS characters. The value is:
f6302ac9 1320
cfe34140 1321 0: Each character is represented by one byte. This is always
3263d5a2
KH
1322 true for a unibyte string. For a multibyte string, true if
1323 it contains only ASCII characters.
1324
28c026cd
DL
1325 1: No charsets other than ascii, control-1, and latin-1 are
1326 found.
1d67c29b 1327
3263d5a2
KH
1328 2: Otherwise.
1329*/
4ed46869
KH
1330
1331int
3263d5a2
KH
1332string_xstring_p (string)
1333 Lisp_Object string;
4ed46869 1334{
dbbb237d
KH
1335 const unsigned char *p = XSTRING (string)->data;
1336 const unsigned char *endp = p + STRING_BYTES (XSTRING (string));
3263d5a2
KH
1337 struct charset *charset;
1338
1339 if (XSTRING (string)->size == STRING_BYTES (XSTRING (string)))
1340 return 0;
1341
1342 charset = CHARSET_FROM_ID (charset_iso_8859_1);
1343 while (p < endp)
0282eb69 1344 {
3263d5a2 1345 int c = STRING_CHAR_ADVANCE (p);
2e344af3 1346
64165ae2 1347 /* Fixme: comparison of unsigned expression < 0 is always false */
3263d5a2
KH
1348 if (ENCODE_CHAR (charset, c) < 0)
1349 return 2;
0282eb69 1350 }
3263d5a2
KH
1351 return 1;
1352}
05505664 1353
05505664 1354
3263d5a2 1355/* Find charsets in the string at PTR of NCHARS and NBYTES.
4ed46869 1356
3263d5a2
KH
1357 CHARSETS is a vector. Each element is a cons of CHARSET and
1358 FOUND-FLAG. CHARSET is a charset id, and FOUND-FLAG is nil or t.
1359 FOUND-FLAG t (or nil) means that the corresponding charset is
1360 already found (or not yet found).
2e344af3 1361
3263d5a2 1362 It may lookup a translation table TABLE if supplied. */
2e344af3 1363
3263d5a2
KH
1364static void
1365find_charsets_in_text (ptr, nchars, nbytes, charsets, table)
dbbb237d 1366 const unsigned char *ptr;
3263d5a2
KH
1367 int nchars, nbytes;
1368 Lisp_Object charsets, table;
1369{
dbbb237d 1370 const unsigned char *pend = ptr + nbytes;
3263d5a2
KH
1371 int ncharsets = ASIZE (charsets);
1372
1373 if (nchars == nbytes)
1374 return;
1375
1376 while (ptr < pend)
1377 {
1378 int c = STRING_CHAR_ADVANCE (ptr);
1379 int i;
1380 int all_found = 1;
1381 Lisp_Object elt;
1382
1383 if (!NILP (table))
1384 c = translate_char (table, c);
1385 for (i = 0; i < ncharsets; i++)
1386 {
1387 elt = AREF (charsets, i);
1388 if (NILP (XCDR (elt)))
1389 {
1390 struct charset *charset = CHARSET_FROM_ID (XINT (XCAR (elt)));
1391
1392 if (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset))
1393 XCDR (elt) = Qt;
1394 else
1395 all_found = 0;
1396 }
4ed46869 1397 }
3263d5a2
KH
1398 if (all_found)
1399 break;
4ed46869 1400 }
4ed46869
KH
1401}
1402
28c026cd 1403/* Fixme: returns nil for unibyte. */
4ed46869 1404DEFUN ("find-charset-region", Ffind_charset_region, Sfind_charset_region,
23d2a7f1 1405 2, 3, 0,
fdb82f93
PJ
1406 doc: /* Return a list of charsets in the region between BEG and END.
1407BEG and END are buffer positions.
1408Optional arg TABLE if non-nil is a translation table to look up.
1409
fdb82f93
PJ
1410If the current buffer is unibyte, the returned list may contain
1411only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
1412 (beg, end, table)
23d2a7f1 1413 Lisp_Object beg, end, table;
4ed46869 1414{
3263d5a2 1415 Lisp_Object charsets;
6ae1f27e 1416 int from, from_byte, to, stop, stop_byte, i;
4ed46869
KH
1417 Lisp_Object val;
1418
1419 validate_region (&beg, &end);
1420 from = XFASTINT (beg);
1421 stop = to = XFASTINT (end);
6ae1f27e 1422
4ed46869 1423 if (from < GPT && GPT < to)
6ae1f27e
RS
1424 {
1425 stop = GPT;
1426 stop_byte = GPT_BYTE;
1427 }
1428 else
1429 stop_byte = CHAR_TO_BYTE (stop);
1430
1431 from_byte = CHAR_TO_BYTE (from);
1432
3263d5a2
KH
1433 charsets = Fmake_vector (make_number (charset_table_used), Qnil);
1434 for (i = 0; i < charset_table_used; i++)
1435 ASET (charsets, i, Fcons (make_number (i), Qnil));
1436
4ed46869
KH
1437 while (1)
1438 {
3263d5a2
KH
1439 find_charsets_in_text (BYTE_POS_ADDR (from_byte), stop - from,
1440 stop_byte - from_byte, charsets, table);
4ed46869 1441 if (stop < to)
6ae1f27e
RS
1442 {
1443 from = stop, from_byte = stop_byte;
1444 stop = to, stop_byte = CHAR_TO_BYTE (stop);
1445 }
4ed46869
KH
1446 else
1447 break;
1448 }
6ae1f27e 1449
4ed46869 1450 val = Qnil;
3263d5a2
KH
1451 for (i = charset_table_used - 1; i >= 0; i--)
1452 if (!NILP (XCDR (AREF (charsets, i))))
1453 val = Fcons (CHARSET_NAME (charset_table + i), val);
4ed46869
KH
1454 return val;
1455}
1456
28c026cd 1457/* Fixme: returns nil for unibyte. */
4ed46869 1458DEFUN ("find-charset-string", Ffind_charset_string, Sfind_charset_string,
23d2a7f1 1459 1, 2, 0,
fdb82f93
PJ
1460 doc: /* Return a list of charsets in STR.
1461Optional arg TABLE if non-nil is a translation table to look up.
1462
fdb82f93 1463If STR is unibyte, the returned list may contain
3263d5a2 1464only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
fdb82f93 1465 (str, table)
23d2a7f1 1466 Lisp_Object str, table;
4ed46869 1467{
3263d5a2 1468 Lisp_Object charsets;
4ed46869
KH
1469 int i;
1470 Lisp_Object val;
1471
b7826503 1472 CHECK_STRING (str);
87b089ad 1473
3263d5a2 1474 charsets = Fmake_vector (make_number (charset_table_used), Qnil);
dcd50550
KH
1475 for (i = 0; i < charset_table_used; i++)
1476 ASET (charsets, i, Fcons (make_number (i), Qnil));
3263d5a2
KH
1477 find_charsets_in_text (XSTRING (str)->data, XSTRING (str)->size,
1478 STRING_BYTES (XSTRING (str)), charsets, table);
2e344af3 1479
4ed46869 1480 val = Qnil;
3263d5a2
KH
1481 for (i = charset_table_used - 1; i >= 0; i--)
1482 if (!NILP (XCDR (AREF (charsets, i))))
1483 val = Fcons (CHARSET_NAME (charset_table + i), val);
4ed46869
KH
1484 return val;
1485}
2e344af3 1486
4ed46869 1487\f
3263d5a2
KH
1488
1489/* Return a character correponding to the code-point CODE of
1490 CHARSET. */
1491
1492int
1493decode_char (charset, code)
1494 struct charset *charset;
1495 unsigned code;
4ed46869 1496{
3263d5a2
KH
1497 int c, char_index;
1498 enum charset_method method = CHARSET_METHOD (charset);
ac4137cc 1499
3263d5a2
KH
1500 if (code < CHARSET_MIN_CODE (charset) || code > CHARSET_MAX_CODE (charset))
1501 return -1;
4ed46869 1502
3263d5a2 1503 if (method == CHARSET_METHOD_MAP_DEFERRED)
ac4137cc 1504 {
3263d5a2
KH
1505 load_charset (charset);
1506 method = CHARSET_METHOD (charset);
ac4137cc 1507 }
4ed46869 1508
374c5cfd
KH
1509 if (method == CHARSET_METHOD_SUBSET)
1510 {
1511 Lisp_Object subset_info;
1512
1513 subset_info = CHARSET_SUBSET (charset);
1514 charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
1515 code -= XINT (AREF (subset_info, 3));
1516 if (code < XFASTINT (AREF (subset_info, 1))
1517 || code > XFASTINT (AREF (subset_info, 2)))
1518 c = -1;
1519 else
1520 c = DECODE_CHAR (charset, code);
1521 }
1522 else if (method == CHARSET_METHOD_SUPERSET)
2e344af3 1523 {
3263d5a2 1524 Lisp_Object parents;
4ed46869 1525
374c5cfd 1526 parents = CHARSET_SUPERSET (charset);
3263d5a2
KH
1527 c = -1;
1528 for (; CONSP (parents); parents = XCDR (parents))
1529 {
1530 int id = XINT (XCAR (XCAR (parents)));
1531 int code_offset = XINT (XCDR (XCAR (parents)));
374c5cfd 1532 unsigned this_code = code - code_offset;
9d3d8cba 1533
3263d5a2
KH
1534 charset = CHARSET_FROM_ID (id);
1535 if ((c = DECODE_CHAR (charset, this_code)) >= 0)
1536 break;
1537 }
1538 }
1539 else
ac4137cc 1540 {
3263d5a2 1541 char_index = CODE_POINT_TO_INDEX (charset, code);
69f8de5b
KH
1542 if (char_index < 0)
1543 return -1;
3263d5a2
KH
1544
1545 if (method == CHARSET_METHOD_MAP)
ac4137cc 1546 {
3263d5a2
KH
1547 Lisp_Object decoder;
1548
1549 decoder = CHARSET_DECODER (charset);
1550 if (! VECTORP (decoder))
1551 return -1;
1552 c = XINT (AREF (decoder, char_index));
ac4137cc
KH
1553 }
1554 else
1555 {
3263d5a2 1556 c = char_index + CHARSET_CODE_OFFSET (charset);
ac4137cc
KH
1557 }
1558 }
9d3d8cba 1559
3263d5a2
KH
1560 if (CHARSET_UNIFIED_P (charset)
1561 && c >= 0)
c449997d
KH
1562 {
1563 MAYBE_UNIFY_CHAR (c);
1564 }
d2665018 1565
3263d5a2 1566 return c;
d2665018
KH
1567}
1568
374c5cfd
KH
1569/* Variable used temporarily by the macro ENCODE_CHAR. */
1570Lisp_Object charset_work;
1bcc1567 1571
3263d5a2 1572/* Return a code-point of CHAR in CHARSET. If CHAR doesn't belong to
28c026cd
DL
1573 CHARSET, return CHARSET_INVALID_CODE (CHARSET). If STRICT is true,
1574 use CHARSET's strict_max_char instead of max_char. */
1bcc1567 1575
3263d5a2
KH
1576unsigned
1577encode_char (charset, c)
1578 struct charset *charset;
9b6a601f
KH
1579 int c;
1580{
3263d5a2
KH
1581 unsigned code;
1582 enum charset_method method = CHARSET_METHOD (charset);
8ac5a9cc 1583
3263d5a2 1584 if (CHARSET_UNIFIED_P (charset))
4ed46869 1585 {
374c5cfd 1586 Lisp_Object deunifier, deunified;
4ed46869 1587
3263d5a2
KH
1588 deunifier = CHARSET_DEUNIFIER (charset);
1589 if (! CHAR_TABLE_P (deunifier))
1590 {
dbbb237d 1591 Funify_charset (CHARSET_NAME (charset), Qnil, Qnil);
3263d5a2
KH
1592 deunifier = CHARSET_DEUNIFIER (charset);
1593 }
374c5cfd
KH
1594 deunified = CHAR_TABLE_REF (deunifier, c);
1595 if (! NILP (deunified))
1596 c = XINT (deunified);
4ed46869 1597 }
beeedaad 1598
3263d5a2
KH
1599 if (! CHARSET_FAST_MAP_REF ((c), charset->fast_map)
1600 || c < CHARSET_MIN_CHAR (charset) || c > CHARSET_MAX_CHAR (charset))
1601 return CHARSET_INVALID_CODE (charset);
beeedaad 1602
374c5cfd
KH
1603 if (method == CHARSET_METHOD_SUBSET)
1604 {
1605 Lisp_Object subset_info;
1606 struct charset *this_charset;
1607
1608 subset_info = CHARSET_SUBSET (charset);
1609 this_charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
1610 code = ENCODE_CHAR (this_charset, c);
1611 if (code == CHARSET_INVALID_CODE (this_charset)
1612 || code < XFASTINT (AREF (subset_info, 1))
1613 || code > XFASTINT (AREF (subset_info, 2)))
1614 return CHARSET_INVALID_CODE (charset);
1615 code += XINT (AREF (subset_info, 3));
1616 return code;
1617 }
1618
1619 if (method == CHARSET_METHOD_SUPERSET)
859f2b3c 1620 {
3263d5a2 1621 Lisp_Object parents;
859f2b3c 1622
374c5cfd 1623 parents = CHARSET_SUPERSET (charset);
3263d5a2 1624 for (; CONSP (parents); parents = XCDR (parents))
beeedaad 1625 {
3263d5a2
KH
1626 int id = XINT (XCAR (XCAR (parents)));
1627 int code_offset = XINT (XCDR (XCAR (parents)));
1628 struct charset *this_charset = CHARSET_FROM_ID (id);
beeedaad 1629
3263d5a2 1630 code = ENCODE_CHAR (this_charset, c);
dbbb237d
KH
1631 if (code != CHARSET_INVALID_CODE (this_charset))
1632 return code + code_offset;
beeedaad 1633 }
3263d5a2
KH
1634 return CHARSET_INVALID_CODE (charset);
1635 }
99529c2c 1636
3263d5a2 1637 if (method == CHARSET_METHOD_MAP_DEFERRED)
beeedaad 1638 {
3263d5a2
KH
1639 load_charset (charset);
1640 method = CHARSET_METHOD (charset);
859f2b3c 1641 }
beeedaad 1642
3263d5a2 1643 if (method == CHARSET_METHOD_MAP)
3f62427c 1644 {
3263d5a2 1645 Lisp_Object encoder;
beeedaad 1646 Lisp_Object val;
beeedaad 1647
3263d5a2
KH
1648 encoder = CHARSET_ENCODER (charset);
1649 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset)))
1650 return CHARSET_INVALID_CODE (charset);
1651 val = CHAR_TABLE_REF (encoder, c);
374c5cfd
KH
1652 if (NILP (val))
1653 return CHARSET_INVALID_CODE (charset);
e9ce014c
KH
1654 code = XINT (val);
1655 if (! CHARSET_COMPACT_CODES_P (charset))
1656 code = INDEX_TO_CODE_POINT (charset, code);
3263d5a2 1657 }
820ee249 1658 else /* method == CHARSET_METHOD_OFFSET */
beeedaad 1659 {
3263d5a2
KH
1660 code = c - CHARSET_CODE_OFFSET (charset);
1661 code = INDEX_TO_CODE_POINT (charset, code);
3f62427c 1662 }
beeedaad 1663
3263d5a2 1664 return code;
3f62427c
KH
1665}
1666
4ed46869 1667
3263d5a2
KH
1668DEFUN ("decode-char", Fdecode_char, Sdecode_char, 2, 3, 0,
1669 doc: /* Decode the pair of CHARSET and CODE-POINT into a character.
1670Return nil if CODE-POINT is not valid in CHARSET.
4ed46869 1671
3263d5a2
KH
1672CODE-POINT may be a cons (HIGHER-16-BIT-VALUE . LOWER-16-BIT-VALUE).
1673
1674Optional argument RESTRICTION specifies a way to map the pair of CCS
1675and CODE-POINT to a chracter. Currently not supported and just ignored. */)
1676 (charset, code_point, restriction)
1677 Lisp_Object charset, code_point, restriction;
4ed46869 1678{
3263d5a2
KH
1679 int c, id;
1680 unsigned code;
1681 struct charset *charsetp;
4ed46869 1682
3263d5a2
KH
1683 CHECK_CHARSET_GET_ID (charset, id);
1684 if (CONSP (code_point))
1685 {
1686 CHECK_NATNUM (XCAR (code_point));
1687 CHECK_NATNUM (XCDR (code_point));
69f8de5b 1688 code = (XINT (XCAR (code_point)) << 16) | (XINT (XCDR (code_point)));
3263d5a2
KH
1689 }
1690 else
1691 {
1692 CHECK_NATNUM (code_point);
1693 code = XINT (code_point);
1694 }
1695 charsetp = CHARSET_FROM_ID (id);
1696 c = DECODE_CHAR (charsetp, code);
1697 return (c >= 0 ? make_number (c) : Qnil);
4ed46869
KH
1698}
1699
046b1f03 1700
3263d5a2
KH
1701DEFUN ("encode-char", Fencode_char, Sencode_char, 2, 3, 0,
1702 doc: /* Encode the character CH into a code-point of CHARSET.
1703Return nil if CHARSET doesn't include CH.
17e7ef1b 1704
3263d5a2
KH
1705Optional argument RESTRICTION specifies a way to map CHAR to a
1706code-point in CCS. Currently not supported and just ignored. */)
1707 (ch, charset, restriction)
1708 Lisp_Object ch, charset, restriction;
1709{
16fed1fc 1710 int id;
3263d5a2
KH
1711 unsigned code;
1712 struct charset *charsetp;
046b1f03 1713
3263d5a2
KH
1714 CHECK_CHARSET_GET_ID (charset, id);
1715 CHECK_NATNUM (ch);
3263d5a2 1716 charsetp = CHARSET_FROM_ID (id);
16fed1fc 1717 code = ENCODE_CHAR (charsetp, XINT (ch));
3263d5a2
KH
1718 if (code == CHARSET_INVALID_CODE (charsetp))
1719 return Qnil;
1720 if (code > 0x7FFFFFF)
1721 return Fcons (make_number (code >> 16), make_number (code & 0xFFFF));
1722 return make_number (code);
6ae1f27e 1723}
9036eb45 1724
87b089ad 1725
b121a744
KH
1726DEFUN ("make-char", Fmake_char, Smake_char, 1, 5, 0,
1727 doc:
1728 /* Return a character of CHARSET whose position codes are CODEn.
1729
1730CODE1 through CODE4 are optional, but if you don't supply sufficient
1731position codes, it is assumed that the minimum code in each dimension
04c2f2c5 1732is specified. */)
b121a744
KH
1733 (charset, code1, code2, code3, code4)
1734 Lisp_Object charset, code1, code2, code3, code4;
87b089ad 1735{
3263d5a2
KH
1736 int id, dimension;
1737 struct charset *charsetp;
b121a744
KH
1738 unsigned code;
1739 int c;
87b089ad 1740
3263d5a2
KH
1741 CHECK_CHARSET_GET_ID (charset, id);
1742 charsetp = CHARSET_FROM_ID (id);
87b089ad 1743
b121a744
KH
1744 dimension = CHARSET_DIMENSION (charsetp);
1745 if (NILP (code1))
d47073ca
KH
1746 code = (CHARSET_ASCII_COMPATIBLE_P (charsetp)
1747 ? 0 : CHARSET_MIN_CODE (charsetp));
3263d5a2 1748 else
87b089ad 1749 {
b121a744
KH
1750 CHECK_NATNUM (code1);
1751 if (XFASTINT (code1) >= 0x100)
1752 args_out_of_range (make_number (0xFF), code1);
1753 code = XFASTINT (code1);
2e344af3 1754
b0a1e45e 1755 if (dimension > 1)
b121a744
KH
1756 {
1757 code <<= 8;
b0a1e45e
KH
1758 if (NILP (code2))
1759 code |= charsetp->code_space[(dimension - 2) * 4];
b121a744
KH
1760 else
1761 {
b0a1e45e
KH
1762 CHECK_NATNUM (code2);
1763 if (XFASTINT (code2) >= 0x100)
1764 args_out_of_range (make_number (0xFF), code2);
1765 code |= XFASTINT (code2);
b121a744
KH
1766 }
1767
b0a1e45e 1768 if (dimension > 2)
b121a744
KH
1769 {
1770 code <<= 8;
b0a1e45e
KH
1771 if (NILP (code3))
1772 code |= charsetp->code_space[(dimension - 3) * 4];
b121a744
KH
1773 else
1774 {
b0a1e45e
KH
1775 CHECK_NATNUM (code3);
1776 if (XFASTINT (code3) >= 0x100)
1777 args_out_of_range (make_number (0xFF), code3);
1778 code |= XFASTINT (code3);
1779 }
1780
1781 if (dimension > 3)
1782 {
1783 code <<= 8;
1784 if (NILP (code4))
1785 code |= charsetp->code_space[0];
1786 else
1787 {
1788 CHECK_NATNUM (code4);
1789 if (XFASTINT (code4) >= 0x100)
1790 args_out_of_range (make_number (0xFF), code4);
1791 code |= XFASTINT (code4);
1792 }
b121a744
KH
1793 }
1794 }
1795 }
1796 }
3263d5a2 1797
b121a744
KH
1798 if (CHARSET_ISO_FINAL (charsetp) >= 0)
1799 code &= 0x7F7F7F7F;
1800 c = DECODE_CHAR (charsetp, code);
1801 if (c < 0)
1802 error ("Invalid code(s)");
3263d5a2 1803 return make_number (c);
2e344af3
KH
1804}
1805
3263d5a2
KH
1806
1807/* Return the first charset in CHARSET_LIST that contains C.
1808 CHARSET_LIST is a list of charset IDs. If it is nil, use
1809 Vcharset_ordered_list. */
1810
1811struct charset *
1812char_charset (c, charset_list, code_return)
1813 int c;
1814 Lisp_Object charset_list;
1815 unsigned *code_return;
2e344af3 1816{
3263d5a2
KH
1817 if (NILP (charset_list))
1818 charset_list = Vcharset_ordered_list;
2e344af3 1819
3263d5a2 1820 while (CONSP (charset_list))
2e344af3 1821 {
3263d5a2
KH
1822 struct charset *charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
1823 unsigned code = ENCODE_CHAR (charset, c);
1824
1825 if (code != CHARSET_INVALID_CODE (charset))
1826 {
1827 if (code_return)
1828 *code_return = code;
1829 return charset;
1830 }
1831 charset_list = XCDR (charset_list);
2e344af3 1832 }
3263d5a2 1833 return NULL;
2e344af3
KH
1834}
1835
2e344af3 1836
56a46d1d 1837/* Fixme: `unknown' can't happen now? */
3263d5a2 1838DEFUN ("split-char", Fsplit_char, Ssplit_char, 1, 1, 0,
04c2f2c5 1839 doc: /*Return list of charset and one to three position-codes of CHAR.
16fed1fc 1840If CHAR is invalid as a character code, return a list `(unknown CHAR)'. */)
3263d5a2
KH
1841 (ch)
1842 Lisp_Object ch;
2e344af3 1843{
3263d5a2
KH
1844 struct charset *charset;
1845 int c, dimension;
1846 unsigned code;
1847 Lisp_Object val;
1848
1849 CHECK_CHARACTER (ch);
1850 c = XFASTINT (ch);
1851 charset = CHAR_CHARSET (c);
1852 if (! charset)
1853 return Fcons (intern ("unknown"), Fcons (ch, Qnil));
1854
1855 code = ENCODE_CHAR (charset, c);
1856 if (code == CHARSET_INVALID_CODE (charset))
1857 abort ();
1858 dimension = CHARSET_DIMENSION (charset);
1859 val = (dimension == 1 ? Fcons (make_number (code), Qnil)
1860 : dimension == 2 ? Fcons (make_number (code >> 8),
1861 Fcons (make_number (code & 0xFF), Qnil))
1862 : Fcons (make_number (code >> 16),
1863 Fcons (make_number ((code >> 8) & 0xFF),
1864 Fcons (make_number (code & 0xFF), Qnil))));
1865 return Fcons (CHARSET_NAME (charset), val);
2e344af3 1866}
87b089ad 1867
740f080d 1868
3263d5a2
KH
1869DEFUN ("char-charset", Fchar_charset, Schar_charset, 1, 1, 0,
1870 doc: /* Return the charset of highest priority that contains CHAR. */)
1871 (ch)
1872 Lisp_Object ch;
740f080d 1873{
3263d5a2 1874 struct charset *charset;
740f080d 1875
3263d5a2
KH
1876 CHECK_CHARACTER (ch);
1877 charset = CHAR_CHARSET (XINT (ch));
1878 return (CHARSET_NAME (charset));
740f080d
KH
1879}
1880
2e344af3 1881
3263d5a2
KH
1882DEFUN ("charset-after", Fcharset_after, Scharset_after, 0, 1, 0,
1883 doc: /*
1884Return charset of a character in the current buffer at position POS.
1885If POS is nil, it defauls to the current point.
1886If POS is out of range, the value is nil. */)
1887 (pos)
1888 Lisp_Object pos;
2e344af3 1889{
3263d5a2
KH
1890 Lisp_Object ch;
1891 struct charset *charset;
1892
1893 ch = Fchar_after (pos);
1894 if (! INTEGERP (ch))
1895 return ch;
1896 charset = CHAR_CHARSET (XINT (ch));
1897 return (CHARSET_NAME (charset));
87b089ad
RS
1898}
1899
2e344af3 1900
3263d5a2
KH
1901DEFUN ("iso-charset", Fiso_charset, Siso_charset, 3, 3, 0,
1902 doc: /*
1903Return charset of ISO's specification DIMENSION, CHARS, and FINAL-CHAR.
1904
1905ISO 2022's designation sequence (escape sequence) distinguishes charsets
1906by their DIMENSION, CHARS, and FINAL-CHAR,
1907where as Emacs distinguishes them by charset symbol.
1908See the documentation of the function `charset-info' for the meanings of
1909DIMENSION, CHARS, and FINAL-CHAR. */)
1910 (dimension, chars, final_char)
1911 Lisp_Object dimension, chars, final_char;
2e344af3 1912{
3263d5a2 1913 int id;
2e344af3 1914
3263d5a2
KH
1915 check_iso_charset_parameter (dimension, chars, final_char);
1916 id = ISO_CHARSET_TABLE (XFASTINT (dimension), XFASTINT (chars),
1917 XFASTINT (final_char));
1918 return (id >= 0 ? CHARSET_NAME (CHARSET_FROM_ID (id)) : Qnil);
2e344af3
KH
1919}
1920
3263d5a2
KH
1921
1922DEFUN ("clear-charset-maps", Fclear_charset_maps, Sclear_charset_maps,
1923 0, 0, 0,
1924 doc: /*
1925Clear encoder and decoder of charsets that are loaded from mapfiles. */)
1926 ()
4ed46869 1927{
53316e55 1928 int i;
3263d5a2
KH
1929 struct charset *charset;
1930 Lisp_Object attrs;
4ed46869 1931
3263d5a2 1932 for (i = 0; i < charset_table_used; i++)
4ed46869 1933 {
3263d5a2
KH
1934 charset = CHARSET_FROM_ID (i);
1935 attrs = CHARSET_ATTRIBUTES (charset);
1936
1937 if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP)
1938 {
1939 CHARSET_ATTR_DECODER (attrs) = Qnil;
1940 CHARSET_ATTR_ENCODER (attrs) = Qnil;
1941 CHARSET_METHOD (charset) = CHARSET_METHOD_MAP_DEFERRED;
1942 }
1943
1944 if (CHARSET_UNIFIED_P (charset))
1945 CHARSET_ATTR_DEUNIFIER (attrs) = Qnil;
5729c92f
KH
1946 }
1947
3263d5a2 1948 if (CHAR_TABLE_P (Vchar_unified_charset_table))
5729c92f 1949 {
3263d5a2
KH
1950 Foptimize_char_table (Vchar_unified_charset_table);
1951 Vchar_unify_table = Vchar_unified_charset_table;
1952 Vchar_unified_charset_table = Qnil;
4ed46869
KH
1953 }
1954
3263d5a2 1955 return Qnil;
4ed46869
KH
1956}
1957
8ddf5e57
DL
1958DEFUN ("charset-priority-list", Fcharset_priority_list,
1959 Scharset_priority_list, 0, 1, 0,
1960 doc: /* Return the list of charsets ordered by priority.
1961HIGHESTP non-nil means just return the highest priority one. */)
1962 (highestp)
1963 Lisp_Object highestp;
1964{
1965 Lisp_Object val = Qnil, list = Vcharset_ordered_list;
1966
1967 if (!NILP (highestp))
16fed1fc 1968 return CHARSET_NAME (CHARSET_FROM_ID (XINT (Fcar (list))));
8ddf5e57
DL
1969
1970 while (!NILP (list))
1971 {
16fed1fc 1972 val = Fcons (CHARSET_NAME (CHARSET_FROM_ID (XINT (XCAR (list)))), val);
8ddf5e57
DL
1973 list = XCDR (list);
1974 }
1975 return Fnreverse (val);
1976}
1977
1978DEFUN ("set-charset-priority", Fset_charset_priority, Sset_charset_priority,
1979 1, MANY, 0,
1980 doc: /* Assign higher priority to the charsets given as arguments.
1981usage: (set-charset-priority &rest charsets) */)
1982 (nargs, args)
1983 int nargs;
1984 Lisp_Object *args;
1985{
16fed1fc
DL
1986 Lisp_Object new_head = Qnil, old_list, arglist[2];
1987 int i, id;
8ddf5e57
DL
1988
1989 old_list = Fcopy_sequence (Vcharset_ordered_list);
1990 for (i = 0; i < nargs; i++)
1991 {
1992 CHECK_CHARSET_GET_ID (args[i], id);
16fed1fc
DL
1993 old_list = Fdelq (make_number (id), old_list);
1994 new_head = Fcons (make_number (id), new_head);
8ddf5e57
DL
1995 }
1996 arglist[0] = Fnreverse (new_head);
1997 arglist[1] = old_list;
1998 Vcharset_ordered_list = Fnconc (2, arglist);
dbbb237d 1999 charset_ordered_list_tick++;
8ddf5e57
DL
2000 return Qnil;
2001}
4ed46869 2002\f
3263d5a2
KH
2003void
2004init_charset ()
4ed46869 2005{
4ed46869 2006
4ed46869
KH
2007}
2008
4ed46869 2009
dfcf069d 2010void
4ed46869
KH
2011init_charset_once ()
2012{
2013 int i, j, k;
2014
3263d5a2
KH
2015 for (i = 0; i < ISO_MAX_DIMENSION; i++)
2016 for (j = 0; j < ISO_MAX_CHARS; j++)
2017 for (k = 0; k < ISO_MAX_FINAL; k++)
2018 iso_charset_table[i][j][k] = -1;
2019
2020 for (i = 0; i < 255; i++)
2021 emacs_mule_charset[i] = NULL;
4ed46869 2022
7c7dceee
KH
2023 charset_jisx0201_roman = -1;
2024 charset_jisx0208_1978 = -1;
2025 charset_jisx0208 = -1;
2026
3263d5a2
KH
2027#if 0
2028 Vchar_charset_set = Fmake_char_table (Qnil, Qnil);
2029 CHAR_TABLE_SET (Vchar_charset_set, make_number (97), Qnil);
2030
2031 DEFSYM (Qcharset_encode_table, "charset-encode-table");
4ed46869
KH
2032
2033 /* Intern this now in case it isn't already done.
2034 Setting this variable twice is harmless.
2035 But don't staticpro it here--that is done in alloc.c. */
2036 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2037
3263d5a2
KH
2038 /* Now we are ready to set up this property, so we can create syntax
2039 tables. */
2040 Fput (Qcharset_encode_table, Qchar_table_extra_slots, make_number (0));
2041#endif
4ed46869
KH
2042}
2043
2044#ifdef emacs
2045
dfcf069d 2046void
4ed46869
KH
2047syms_of_charset ()
2048{
3263d5a2
KH
2049 char *p;
2050
2051 DEFSYM (Qcharsetp, "charsetp");
2052
2053 DEFSYM (Qascii, "ascii");
2054 DEFSYM (Qunicode, "unicode");
2fe1edd1 2055 DEFSYM (Qeight_bit, "eight-bit");
3263d5a2
KH
2056 DEFSYM (Qiso_8859_1, "iso-8859-1");
2057
2058 DEFSYM (Qgl, "gl");
2059 DEFSYM (Qgr, "gr");
2060
2061 p = (char *) xmalloc (30000);
2062
2063 staticpro (&Vcharset_ordered_list);
2064 Vcharset_ordered_list = Qnil;
2065
2066 staticpro (&Viso_2022_charset_list);
2067 Viso_2022_charset_list = Qnil;
2068
2069 staticpro (&Vemacs_mule_charset_list);
2070 Vemacs_mule_charset_list = Qnil;
2071
2072 staticpro (&Vcharset_hash_table);
2073 Vcharset_hash_table = Fmakehash (Qeq);
2074
2075 charset_table_size = 128;
2076 charset_table = ((struct charset *)
2077 xmalloc (sizeof (struct charset) * charset_table_size));
2078 charset_table_used = 0;
2079
2080 staticpro (&Vchar_unified_charset_table);
2081 Vchar_unified_charset_table = Fmake_char_table (Qnil, make_number (-1));
2082
2083 defsubr (&Scharsetp);
2084 defsubr (&Smap_charset_chars);
2085 defsubr (&Sdefine_charset_internal);
2086 defsubr (&Sdefine_charset_alias);
2087 defsubr (&Sprimary_charset);
2088 defsubr (&Sset_primary_charset);
2089 defsubr (&Scharset_plist);
2090 defsubr (&Sset_charset_plist);
2091 defsubr (&Sunify_charset);
3fac5a51 2092 defsubr (&Sget_unused_iso_final_char);
4ed46869
KH
2093 defsubr (&Sdeclare_equiv_charset);
2094 defsubr (&Sfind_charset_region);
2095 defsubr (&Sfind_charset_string);
3263d5a2
KH
2096 defsubr (&Sdecode_char);
2097 defsubr (&Sencode_char);
4ed46869 2098 defsubr (&Ssplit_char);
3263d5a2 2099 defsubr (&Smake_char);
4ed46869 2100 defsubr (&Schar_charset);
90d7b74e 2101 defsubr (&Scharset_after);
4ed46869 2102 defsubr (&Siso_charset);
3263d5a2 2103 defsubr (&Sclear_charset_maps);
8ddf5e57
DL
2104 defsubr (&Scharset_priority_list);
2105 defsubr (&Sset_charset_priority);
3263d5a2
KH
2106
2107 DEFVAR_LISP ("charset-map-directory", &Vcharset_map_directory,
2108 doc: /* Directory of charset map files that come with GNU Emacs.
04c2f2c5 2109The default value is sub-directory "charsets" of `data-directory'. */);
3263d5a2
KH
2110 Vcharset_map_directory = Fexpand_file_name (build_string ("charsets"),
2111 Vdata_directory);
4ed46869
KH
2112
2113 DEFVAR_LISP ("charset-list", &Vcharset_list,
528623a0 2114 doc: /* List of all charsets ever defined. */);
3263d5a2
KH
2115 Vcharset_list = Qnil;
2116
2fe1edd1
KH
2117 charset_ascii
2118 = define_charset_internal (Qascii, 1, "\x00\x7F\x00\x00\x00\x00",
2119 0, 127, 'B', -1, 0, 1, 0, 0);
2120 charset_unicode
73fbf2d9 2121 = define_charset_internal (Qunicode, 3, "\x00\xFF\x00\xFF\x00\x10",
2fe1edd1
KH
2122 0, MAX_UNICODE_CHAR, -1, 0, -1, 1, 0, 0);
2123 charset_eight_bit
2124 = define_charset_internal (Qeight_bit, 1, "\x80\xFF\x00\x00\x00\x00",
2125 128, 255, -1, 0, -1, 0, 0,
2126 MAX_5_BYTE_CHAR + 1);
4ed46869
KH
2127}
2128
2129#endif /* emacs */