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