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