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