Add some non-word syntax cases.
[emacs.git] / src / charset.c
blob4512ad1e78d9345214734f7a3fde62d98b7184d9
1 /* Basic character set support.
2 Copyright (C) 1995, 97, 98, 2000, 2001 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001 Free Software Foundation, Inc.
5 Copyright (C) 2001, 2002
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H13PRO009
9 This file is part of GNU Emacs.
11 GNU Emacs is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
16 GNU Emacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU Emacs; see the file COPYING. If not, write to
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
26 #include <config.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include "lisp.h"
33 #include "character.h"
34 #include "charset.h"
35 #include "coding.h"
36 #include "disptab.h"
37 #include "buffer.h"
39 /*** GENERAL NOTES on CODED CHARACTER SETS (CHARSETS) ***
41 A coded character set ("charset" hereafter) is a meaningful
42 collection (i.e. language, culture, functionality, etc.) of
43 characters. Emacs handles multiple charsets at once. In Emacs Lisp
44 code, a charset is represented by a symbol. In C code, a charset is
45 represented by its ID number or by a pointer to a struct charset.
47 The actual information about each charset is stored in two places.
48 Lispy information is stored in the hash table Vcharset_hash_table as
49 a vector (charset attributes). The other information is stored in
50 charset_table as a struct charset.
54 /* List of all charsets. This variable is used only from Emacs
55 Lisp. */
56 Lisp_Object Vcharset_list;
58 /* Hash table that contains attributes of each charset. Keys are
59 charset symbols, and values are vectors of charset attributes. */
60 Lisp_Object Vcharset_hash_table;
62 /* Table of struct charset. */
63 struct charset *charset_table;
65 static int charset_table_size;
66 int charset_table_used;
68 Lisp_Object Qcharsetp;
70 /* Special charset symbols. */
71 Lisp_Object Qascii;
72 Lisp_Object Qeight_bit;
73 Lisp_Object Qiso_8859_1;
74 Lisp_Object Qunicode;
76 /* The corresponding charsets. */
77 int charset_ascii;
78 int charset_eight_bit;
79 int charset_iso_8859_1;
80 int charset_unicode;
82 /* The other special charsets. */
83 int charset_jisx0201_roman;
84 int charset_jisx0208_1978;
85 int charset_jisx0208;
87 /* Value of charset attribute `charset-iso-plane'. */
88 Lisp_Object Qgl, Qgr;
90 /* Charset of unibyte characters. */
91 int charset_unibyte;
93 /* List of charsets ordered by the priority. */
94 Lisp_Object Vcharset_ordered_list;
96 /* Incremented everytime we change Vcharset_ordered_list. This is
97 unsigned short so that it fits in Lisp_Int and never matches
98 -1. */
99 unsigned short charset_ordered_list_tick;
101 /* List of iso-2022 charsets. */
102 Lisp_Object Viso_2022_charset_list;
104 /* List of emacs-mule charsets. */
105 Lisp_Object Vemacs_mule_charset_list;
107 struct charset *emacs_mule_charset[256];
109 /* Mapping table from ISO2022's charset (specified by DIMENSION,
110 CHARS, and FINAL-CHAR) to Emacs' charset. */
111 int iso_charset_table[ISO_MAX_DIMENSION][ISO_MAX_CHARS][ISO_MAX_FINAL];
113 Lisp_Object Vcharset_map_directory;
115 Lisp_Object Vchar_unified_charset_table;
117 /* Defined in chartab.c */
118 extern void
119 map_char_table_for_charset P_ ((void (*c_function) (Lisp_Object, Lisp_Object),
120 Lisp_Object function, Lisp_Object table,
121 Lisp_Object arg, struct charset *charset,
122 unsigned from, unsigned to));
124 #define CODE_POINT_TO_INDEX(charset, code) \
125 ((charset)->code_linear_p \
126 ? (code) - (charset)->min_code \
127 : (((charset)->code_space_mask[(code) >> 24] & 0x8) \
128 && ((charset)->code_space_mask[((code) >> 16) & 0xFF] & 0x4) \
129 && ((charset)->code_space_mask[((code) >> 8) & 0xFF] & 0x2) \
130 && ((charset)->code_space_mask[(code) & 0xFF] & 0x1)) \
131 ? (((((code) >> 24) - (charset)->code_space[12]) \
132 * (charset)->code_space[11]) \
133 + (((((code) >> 16) & 0xFF) - (charset)->code_space[8]) \
134 * (charset)->code_space[7]) \
135 + (((((code) >> 8) & 0xFF) - (charset)->code_space[4]) \
136 * (charset)->code_space[3]) \
137 + (((code) & 0xFF) - (charset)->code_space[0]) \
138 - ((charset)->char_index_offset)) \
139 : -1)
142 /* Convert the character index IDX to code-point CODE for CHARSET.
143 It is assumed that IDX is in a valid range. */
145 #define INDEX_TO_CODE_POINT(charset, idx) \
146 ((charset)->code_linear_p \
147 ? (idx) + (charset)->min_code \
148 : (idx += (charset)->char_index_offset, \
149 (((charset)->code_space[0] + (idx) % (charset)->code_space[2]) \
150 | (((charset)->code_space[4] \
151 + ((idx) / (charset)->code_space[3] % (charset)->code_space[6])) \
152 << 8) \
153 | (((charset)->code_space[8] \
154 + ((idx) / (charset)->code_space[7] % (charset)->code_space[10])) \
155 << 16) \
156 | (((charset)->code_space[12] + ((idx) / (charset)->code_space[11])) \
157 << 24))))
162 /* Set to 1 to warn that a charset map is loaded and thus a buffer
163 text and a string data may be relocated. */
164 int charset_map_loaded;
166 struct charset_map_entries
168 struct {
169 unsigned from, to;
170 int c;
171 } entry[0x10000];
172 struct charset_map_entries *next;
175 /* Load the mapping information for CHARSET from ENTRIES.
177 If CONTROL_FLAG is 0, setup CHARSET->min_char and CHARSET->max_char.
179 If CONTROL_FLAG is 1, setup CHARSET->min_char, CHARSET->max_char,
180 CHARSET->decoder, and CHARSET->encoder.
182 If CONTROL_FLAG is 2, setup CHARSET->deunifier and
183 Vchar_unify_table. If Vchar_unified_charset_table is non-nil,
184 setup it too. */
186 static void
187 load_charset_map (charset, entries, n_entries, control_flag)
188 struct charset *charset;
189 struct charset_map_entries *entries;
190 int n_entries;
191 int control_flag;
193 Lisp_Object vec, table;
194 unsigned max_code = CHARSET_MAX_CODE (charset);
195 int ascii_compatible_p = charset->ascii_compatible_p;
196 int min_char, max_char, nonascii_min_char;
197 int i;
198 unsigned char *fast_map = charset->fast_map;
200 if (n_entries <= 0)
201 return;
203 if (control_flag > 0)
205 int n = CODE_POINT_TO_INDEX (charset, max_code) + 1;
207 table = Fmake_char_table (Qnil, Qnil);
208 if (control_flag == 1)
209 vec = Fmake_vector (make_number (n), make_number (-1));
210 else if (! CHAR_TABLE_P (Vchar_unify_table))
211 Vchar_unify_table = Fmake_char_table (Qnil, Qnil);
213 charset_map_loaded = 1;
216 min_char = max_char = entries->entry[0].c;
217 nonascii_min_char = MAX_CHAR;
218 for (i = 0; i < n_entries; i++)
220 unsigned from, to;
221 int from_index, to_index;
222 int from_c, to_c;
223 int idx = i % 0x10000;
225 if (i > 0 && idx == 0)
226 entries = entries->next;
227 from = entries->entry[idx].from;
228 to = entries->entry[idx].to;
229 from_c = entries->entry[idx].c;
230 from_index = CODE_POINT_TO_INDEX (charset, from);
231 if (from == to)
233 to_index = from_index;
234 to_c = from_c;
236 else
238 to_index = CODE_POINT_TO_INDEX (charset, to);
239 to_c = from_c + (to_index - from_index);
241 if (from_index < 0 || to_index < 0)
242 continue;
244 if (control_flag < 2)
246 int c;
248 if (to_c > max_char)
249 max_char = to_c;
250 else if (from_c < min_char)
251 min_char = from_c;
252 if (ascii_compatible_p)
254 if (! ASCII_BYTE_P (from_c))
256 if (from_c < nonascii_min_char)
257 nonascii_min_char = from_c;
259 else if (! ASCII_BYTE_P (to_c))
261 nonascii_min_char = 0x80;
265 for (c = from_c; c <= to_c; c++)
266 CHARSET_FAST_MAP_SET (c, fast_map);
268 if (control_flag == 1)
270 unsigned code = from;
272 if (CHARSET_COMPACT_CODES_P (charset))
273 while (1)
275 ASET (vec, from_index, make_number (from_c));
276 CHAR_TABLE_SET (table, from_c, make_number (code));
277 if (from_index == to_index)
278 break;
279 from_index++, from_c++;
280 code = INDEX_TO_CODE_POINT (charset, from_index);
282 else
283 for (; from_index <= to_index; from_index++, from_c++)
285 ASET (vec, from_index, make_number (from_c));
286 CHAR_TABLE_SET (table, from_c, make_number (from_index));
290 else
292 unsigned code = from;
294 while (1)
296 int c1 = DECODE_CHAR (charset, code);
298 if (c1 >= 0)
300 CHAR_TABLE_SET (table, from_c, make_number (c1));
301 CHAR_TABLE_SET (Vchar_unify_table, c1, make_number (from_c));
302 if (CHAR_TABLE_P (Vchar_unified_charset_table))
303 CHAR_TABLE_SET (Vchar_unified_charset_table, c1,
304 CHARSET_NAME (charset));
306 if (from_index == to_index)
307 break;
308 from_index++, from_c++;
309 code = INDEX_TO_CODE_POINT (charset, from_index);
314 if (control_flag < 2)
316 CHARSET_MIN_CHAR (charset) = (ascii_compatible_p
317 ? nonascii_min_char : min_char);
318 CHARSET_MAX_CHAR (charset) = max_char;
319 if (control_flag == 1)
321 CHARSET_DECODER (charset) = vec;
322 CHARSET_ENCODER (charset) = table;
325 else
326 CHARSET_DEUNIFIER (charset) = table;
330 /* Read a hexadecimal number (preceded by "0x") from the file FP while
331 paying attention to comment charcter '#'. */
333 static INLINE unsigned
334 read_hex (fp, eof)
335 FILE *fp;
336 int *eof;
338 int c;
339 unsigned n;
341 while ((c = getc (fp)) != EOF)
343 if (c == '#')
345 while ((c = getc (fp)) != EOF && c != '\n');
347 else if (c == '0')
349 if ((c = getc (fp)) == EOF || c == 'x')
350 break;
353 if (c == EOF)
355 *eof = 1;
356 return 0;
358 *eof = 0;
359 n = 0;
360 if (c == 'x')
361 while ((c = getc (fp)) != EOF && isxdigit (c))
362 n = ((n << 4)
363 | (c <= '9' ? c - '0' : c <= 'F' ? c - 'A' + 10 : c - 'a' + 10));
364 else
365 while ((c = getc (fp)) != EOF && isdigit (c))
366 n = (n * 10) + c - '0';
367 if (c != EOF)
368 ungetc (c, fp);
369 return n;
373 /* Return a mapping vector for CHARSET loaded from MAPFILE.
374 Each line of MAPFILE has this form
375 0xAAAA 0xCCCC
376 where 0xAAAA is a code-point and 0xCCCC is the corresponding
377 character code, or this form
378 0xAAAA-0xBBBB 0xCCCC
379 where 0xAAAA and 0xBBBB are code-points specifying a range, and
380 0xCCCC is the first character code of the range.
382 The returned vector has this form:
383 [ CODE1 CHAR1 CODE2 CHAR2 .... ]
384 where CODE1 is a code-point or a cons of code-points specifying a
385 range. */
387 extern void add_to_log P_ ((char *, Lisp_Object, Lisp_Object));
389 static void
390 load_charset_map_from_file (charset, mapfile, control_flag)
391 struct charset *charset;
392 Lisp_Object mapfile;
393 int control_flag;
395 unsigned min_code = CHARSET_MIN_CODE (charset);
396 unsigned max_code = CHARSET_MAX_CODE (charset);
397 int fd;
398 FILE *fp;
399 int eof;
400 Lisp_Object suffixes;
401 struct charset_map_entries *head, *entries;
402 int n_entries;
404 suffixes = Fcons (build_string (".map"),
405 Fcons (build_string (".TXT"), Qnil));
407 fd = openp (Fcons (Vcharset_map_directory, Qnil), mapfile, suffixes,
408 NULL, 0);
409 if (fd < 0
410 || ! (fp = fdopen (fd, "r")))
412 add_to_log ("Failure in loading charset map: %S", mapfile, Qnil);
413 return;
416 head = entries = ((struct charset_map_entries *)
417 alloca (sizeof (struct charset_map_entries)));
418 n_entries = 0;
419 eof = 0;
420 while (1)
422 unsigned from, to;
423 int c;
424 int idx;
426 from = read_hex (fp, &eof);
427 if (eof)
428 break;
429 if (getc (fp) == '-')
430 to = read_hex (fp, &eof);
431 else
432 to = from;
433 c = (int) read_hex (fp, &eof);
435 if (from < min_code || to > max_code || from > to || c > MAX_CHAR)
436 continue;
438 if (n_entries > 0 && (n_entries % 0x10000) == 0)
440 entries->next = ((struct charset_map_entries *)
441 alloca (sizeof (struct charset_map_entries)));
442 entries = entries->next;
444 idx = n_entries % 0x10000;
445 entries->entry[idx].from = from;
446 entries->entry[idx].to = to;
447 entries->entry[idx].c = c;
448 n_entries++;
450 fclose (fp);
451 close (fd);
453 load_charset_map (charset, head, n_entries, control_flag);
456 static void
457 load_charset_map_from_vector (charset, vec, control_flag)
458 struct charset *charset;
459 Lisp_Object vec;
460 int control_flag;
462 unsigned min_code = CHARSET_MIN_CODE (charset);
463 unsigned max_code = CHARSET_MAX_CODE (charset);
464 struct charset_map_entries *head, *entries;
465 int n_entries;
466 int len = ASIZE (vec);
467 int i;
469 if (len % 2 == 1)
471 add_to_log ("Failure in loading charset map: %V", vec, Qnil);
472 return;
475 head = entries = ((struct charset_map_entries *)
476 alloca (sizeof (struct charset_map_entries)));
477 n_entries = 0;
478 for (i = 0; i < len; i += 2)
480 Lisp_Object val, val2;
481 unsigned from, to;
482 int c;
483 int idx;
485 val = AREF (vec, i);
486 if (CONSP (val))
488 val2 = XCDR (val);
489 val = XCAR (val);
490 CHECK_NATNUM (val);
491 CHECK_NATNUM (val2);
492 from = XFASTINT (val);
493 to = XFASTINT (val2);
495 else
497 CHECK_NATNUM (val);
498 from = to = XFASTINT (val);
500 val = AREF (vec, i + 1);
501 CHECK_NATNUM (val);
502 c = XFASTINT (val);
504 if (from < min_code || to > max_code || from > to || c > MAX_CHAR)
505 continue;
507 if ((n_entries % 0x10000) == 0)
509 entries->next = ((struct charset_map_entries *)
510 alloca (sizeof (struct charset_map_entries)));
511 entries = entries->next;
513 idx = n_entries % 0x10000;
514 entries->entry[idx].from = from;
515 entries->entry[idx].to = to;
516 entries->entry[idx].c = c;
517 n_entries++;
520 load_charset_map (charset, head, n_entries, control_flag);
523 static void
524 load_charset (charset)
525 struct charset *charset;
527 if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP_DEFERRED)
529 Lisp_Object map;
531 map = CHARSET_MAP (charset);
532 if (STRINGP (map))
533 load_charset_map_from_file (charset, map, 1);
534 else
535 load_charset_map_from_vector (charset, map, 1);
536 CHARSET_METHOD (charset) = CHARSET_METHOD_MAP;
541 DEFUN ("charsetp", Fcharsetp, Scharsetp, 1, 1, 0,
542 doc: /* Return non-nil if and only if OBJECT is a charset.*/)
543 (object)
544 Lisp_Object object;
546 return (CHARSETP (object) ? Qt : Qnil);
550 void
551 map_charset_chars (c_function, function, arg,
552 charset, from, to)
553 void (*c_function) P_ ((Lisp_Object, Lisp_Object));
554 Lisp_Object function, arg;
555 struct charset *charset;
556 unsigned from, to;
559 Lisp_Object range;
560 int partial;
562 if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP_DEFERRED)
563 load_charset (charset);
565 partial = (from > CHARSET_MIN_CODE (charset)
566 || to < CHARSET_MAX_CODE (charset));
568 if (CHARSET_UNIFIED_P (charset)
569 && CHAR_TABLE_P (CHARSET_DEUNIFIER (charset)))
571 map_char_table_for_charset (c_function, function,
572 CHARSET_DEUNIFIER (charset), arg,
573 partial ? charset : NULL, from, to);
576 if (CHARSET_METHOD (charset) == CHARSET_METHOD_OFFSET)
578 int from_idx = CODE_POINT_TO_INDEX (charset, from);
579 int to_idx = CODE_POINT_TO_INDEX (charset, to);
580 int from_c = from_idx + CHARSET_CODE_OFFSET (charset);
581 int to_c = to_idx + CHARSET_CODE_OFFSET (charset);
583 range = Fcons (make_number (from_c), make_number (to_c));
584 if (NILP (function))
585 (*c_function) (range, arg);
586 else
587 call2 (function, range, arg);
589 else if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP)
591 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset)))
592 return;
593 if (CHARSET_ASCII_COMPATIBLE_P (charset) && from <= 127)
595 range = Fcons (make_number (from), make_number (to));
596 if (to >= 128)
597 XSETCAR (range, make_number (127));
599 if (NILP (function))
600 (*c_function) (range, arg);
601 else
602 call2 (function, range, arg);
604 map_char_table_for_charset (c_function, function,
605 CHARSET_ENCODER (charset), arg,
606 partial ? charset : NULL, from, to);
608 else if (CHARSET_METHOD (charset) == CHARSET_METHOD_SUBSET)
610 Lisp_Object subset_info;
611 int offset;
613 subset_info = CHARSET_SUBSET (charset);
614 charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
615 offset = XINT (AREF (subset_info, 3));
616 from -= offset;
617 if (from < XFASTINT (AREF (subset_info, 1)))
618 from = XFASTINT (AREF (subset_info, 1));
619 to -= offset;
620 if (to > XFASTINT (AREF (subset_info, 2)))
621 to = XFASTINT (AREF (subset_info, 2));
622 map_charset_chars (c_function, function, arg, charset, from, to);
624 else /* i.e. CHARSET_METHOD_SUPERSET */
626 Lisp_Object parents;
628 for (parents = CHARSET_SUPERSET (charset); CONSP (parents);
629 parents = XCDR (parents))
631 int offset;
632 unsigned this_from, this_to;
634 charset = CHARSET_FROM_ID (XFASTINT (XCAR (XCAR (parents))));
635 offset = XINT (XCDR (XCAR (parents)));
636 this_from = from - offset;
637 this_to = to - offset;
638 if (this_from < CHARSET_MIN_CODE (charset))
639 this_from = CHARSET_MIN_CODE (charset);
640 if (this_to > CHARSET_MAX_CODE (charset))
641 this_to = CHARSET_MAX_CODE (charset);
642 map_charset_chars (c_function, function, arg, charset, from, to);
648 DEFUN ("map-charset-chars", Fmap_charset_chars, Smap_charset_chars, 2, 5, 0,
649 doc: /* Call FUNCTION for all characters in CHARSET.
650 FUNCTION is called with an argument RANGE and the optional 3rd
651 argument ARG.
653 RANGE is a cons (FROM . TO), where FROM and TO indicate a range of
654 characters contained in CHARSET.
656 The optional 4th and 5th arguments FROM-CODE and TO-CODE specify the
657 range of code points of targer characters. */)
658 (function, charset, arg, from_code, to_code)
659 Lisp_Object function, charset, arg, from_code, to_code;
661 struct charset *cs;
662 unsigned from, to;
664 CHECK_CHARSET_GET_CHARSET (charset, cs);
665 if (NILP (from_code))
666 from = CHARSET_MIN_CODE (cs);
667 else
669 CHECK_NATNUM (from_code);
670 from = XINT (from_code);
671 if (from < CHARSET_MIN_CODE (cs))
672 from = CHARSET_MIN_CODE (cs);
674 if (NILP (to_code))
675 to = CHARSET_MAX_CODE (cs);
676 else
678 CHECK_NATNUM (to_code);
679 to = XINT (to_code);
680 if (to > CHARSET_MAX_CODE (cs))
681 to = CHARSET_MAX_CODE (cs);
683 map_charset_chars (NULL, function, arg, cs, from, to);
684 return Qnil;
688 /* Define a charset according to the arguments. The Nth argument is
689 the Nth attribute of the charset (the last attribute `charset-id'
690 is not included). See the docstring of `define-charset' for the
691 detail. */
693 DEFUN ("define-charset-internal", Fdefine_charset_internal,
694 Sdefine_charset_internal, charset_arg_max, MANY, 0,
695 doc: /* For internal use only.
696 usage: (define-charset-internal ...) */)
697 (nargs, args)
698 int nargs;
699 Lisp_Object *args;
701 /* Charset attr vector. */
702 Lisp_Object attrs;
703 Lisp_Object val;
704 unsigned hash_code;
705 struct Lisp_Hash_Table *hash_table = XHASH_TABLE (Vcharset_hash_table);
706 int i, j;
707 struct charset charset;
708 int id;
709 int dimension;
710 int new_definition_p;
711 int nchars;
713 if (nargs != charset_arg_max)
714 return Fsignal (Qwrong_number_of_arguments,
715 Fcons (intern ("define-charset-internal"),
716 make_number (nargs)));
718 attrs = Fmake_vector (make_number (charset_attr_max), Qnil);
720 CHECK_SYMBOL (args[charset_arg_name]);
721 ASET (attrs, charset_name, args[charset_arg_name]);
723 val = args[charset_arg_code_space];
724 for (i = 0, dimension = 0, nchars = 1; i < 4; i++)
726 int min_byte, max_byte;
728 min_byte = XINT (Faref (val, make_number (i * 2)));
729 max_byte = XINT (Faref (val, make_number (i * 2 + 1)));
730 if (min_byte < 0 || min_byte > max_byte || max_byte >= 256)
731 error ("Invalid :code-space value");
732 charset.code_space[i * 4] = min_byte;
733 charset.code_space[i * 4 + 1] = max_byte;
734 charset.code_space[i * 4 + 2] = max_byte - min_byte + 1;
735 nchars *= charset.code_space[i * 4 + 2];
736 charset.code_space[i * 4 + 3] = nchars;
737 if (max_byte > 0)
738 dimension = i + 1;
741 val = args[charset_arg_dimension];
742 if (NILP (val))
743 charset.dimension = dimension;
744 else
746 CHECK_NATNUM (val);
747 charset.dimension = XINT (val);
748 if (charset.dimension < 1 || charset.dimension > 4)
749 args_out_of_range_3 (val, make_number (1), make_number (4));
752 charset.code_linear_p
753 = (charset.dimension == 1
754 || (charset.code_space[2] == 256
755 && (charset.dimension == 2
756 || (charset.code_space[6] == 256
757 && (charset.dimension == 3
758 || charset.code_space[10] == 256)))));
760 if (! charset.code_linear_p)
762 charset.code_space_mask = (unsigned char *) xmalloc (256);
763 bzero (charset.code_space_mask, 256);
764 for (i = 0; i < 4; i++)
765 for (j = charset.code_space[i * 4]; j <= charset.code_space[i * 4 + 1];
766 j++)
767 charset.code_space_mask[j] |= (1 << i);
770 charset.iso_chars_96 = charset.code_space[2] == 96;
772 charset.min_code = (charset.code_space[0]
773 | (charset.code_space[4] << 8)
774 | (charset.code_space[8] << 16)
775 | (charset.code_space[12] << 24));
776 charset.max_code = (charset.code_space[1]
777 | (charset.code_space[5] << 8)
778 | (charset.code_space[9] << 16)
779 | (charset.code_space[13] << 24));
780 charset.char_index_offset = 0;
782 val = args[charset_arg_min_code];
783 if (! NILP (val))
785 unsigned code;
787 if (INTEGERP (val))
788 code = XINT (val);
789 else
791 CHECK_CONS (val);
792 CHECK_NUMBER (XCAR (val));
793 CHECK_NUMBER (XCDR (val));
794 code = (XINT (XCAR (val)) << 16) | (XINT (XCDR (val)));
796 if (code < charset.min_code
797 || code > charset.max_code)
798 args_out_of_range_3 (make_number (charset.min_code),
799 make_number (charset.max_code), val);
800 charset.char_index_offset = CODE_POINT_TO_INDEX (&charset, code);
801 charset.min_code = code;
804 val = args[charset_arg_max_code];
805 if (! NILP (val))
807 unsigned code;
809 if (INTEGERP (val))
810 code = XINT (val);
811 else
813 CHECK_CONS (val);
814 CHECK_NUMBER (XCAR (val));
815 CHECK_NUMBER (XCDR (val));
816 code = (XINT (XCAR (val)) << 16) | (XINT (XCDR (val)));
818 if (code < charset.min_code
819 || code > charset.max_code)
820 args_out_of_range_3 (make_number (charset.min_code),
821 make_number (charset.max_code), val);
822 charset.max_code = code;
825 charset.compact_codes_p = charset.max_code < 0x1000000;
827 val = args[charset_arg_invalid_code];
828 if (NILP (val))
830 if (charset.min_code > 0)
831 charset.invalid_code = 0;
832 else
834 XSETINT (val, charset.max_code + 1);
835 if (XINT (val) == charset.max_code + 1)
836 charset.invalid_code = charset.max_code + 1;
837 else
838 error ("Attribute :invalid-code must be specified");
841 else
843 CHECK_NATNUM (val);
844 charset.invalid_code = XFASTINT (val);
847 val = args[charset_arg_iso_final];
848 if (NILP (val))
849 charset.iso_final = -1;
850 else
852 CHECK_NUMBER (val);
853 if (XINT (val) < '0' || XINT (val) > 127)
854 error ("Invalid iso-final-char: %d", XINT (val));
855 charset.iso_final = XINT (val);
858 val = args[charset_arg_iso_revision];
859 if (NILP (val))
860 charset.iso_revision = -1;
861 else
863 CHECK_NUMBER (val);
864 if (XINT (val) > 63)
865 args_out_of_range (make_number (63), val);
866 charset.iso_revision = XINT (val);
869 val = args[charset_arg_emacs_mule_id];
870 if (NILP (val))
871 charset.emacs_mule_id = -1;
872 else
874 CHECK_NATNUM (val);
875 if ((XINT (val) > 0 && XINT (val) <= 128) || XINT (val) >= 256)
876 error ("Invalid emacs-mule-id: %d", XINT (val));
877 charset.emacs_mule_id = XINT (val);
880 charset.ascii_compatible_p = ! NILP (args[charset_arg_ascii_compatible_p]);
882 charset.supplementary_p = ! NILP (args[charset_arg_supplementary_p]);
884 charset.unified_p = 0;
886 bzero (charset.fast_map, sizeof (charset.fast_map));
888 if (! NILP (args[charset_arg_code_offset]))
890 val = args[charset_arg_code_offset];
891 CHECK_NUMBER (val);
893 charset.method = CHARSET_METHOD_OFFSET;
894 charset.code_offset = XINT (val);
896 i = CODE_POINT_TO_INDEX (&charset, charset.min_code);
897 charset.min_char = i + charset.code_offset;
898 i = CODE_POINT_TO_INDEX (&charset, charset.max_code);
899 charset.max_char = i + charset.code_offset;
900 if (charset.max_char > MAX_CHAR)
901 error ("Unsupported max char: %d", charset.max_char);
903 for (i = charset.min_char; i < 0x10000 && i <= charset.max_char;
904 i += 128)
905 CHARSET_FAST_MAP_SET (i, charset.fast_map);
906 for (; i <= charset.max_char; i += 0x1000)
907 CHARSET_FAST_MAP_SET (i, charset.fast_map);
909 else if (! NILP (args[charset_arg_map]))
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;
919 else if (! NILP (args[charset_arg_subset]))
921 Lisp_Object parent;
922 Lisp_Object parent_min_code, parent_max_code, parent_code_offset;
923 struct charset *parent_charset;
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);
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];
948 /* We also copy these for parents. */
949 charset.min_char = parent_charset->min_char;
950 charset.max_char = parent_charset->max_char;
952 else if (! NILP (args[charset_arg_superset]))
954 val = args[charset_arg_superset];
955 charset.method = CHARSET_METHOD_SUPERSET;
956 val = Fcopy_sequence (val);
957 ASET (attrs, charset_superset, val);
959 charset.min_char = MAX_CHAR;
960 charset.max_char = 0;
961 for (; ! NILP (val); val = Fcdr (val))
963 Lisp_Object elt, car_part, cdr_part;
964 int this_id, offset;
965 struct charset *this_charset;
967 elt = Fcar (val);
968 if (CONSP (elt))
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);
976 else
978 CHECK_CHARSET_GET_ID (elt, this_id);
979 offset = 0;
981 XSETCAR (val, Fcons (make_number (this_id), make_number (offset)));
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];
992 else
993 error ("None of :code-offset, :map, :parents are specified");
995 val = args[charset_arg_unify_map];
996 if (! NILP (val) && !STRINGP (val))
997 CHECK_VECTOR (val);
998 ASET (attrs, charset_unify_map, val);
1000 CHECK_LIST (args[charset_arg_plist]);
1001 ASET (attrs, charset_plist, args[charset_arg_plist]);
1003 charset.hash_index = hash_lookup (hash_table, args[charset_arg_name],
1004 &hash_code);
1005 if (charset.hash_index >= 0)
1007 new_definition_p = 0;
1008 id = XFASTINT (CHARSET_SYMBOL_ID (args[charset_arg_name]));
1009 HASH_VALUE (hash_table, charset.hash_index) = attrs;
1011 else
1013 charset.hash_index = hash_put (hash_table, args[charset_arg_name], attrs,
1014 hash_code);
1015 if (charset_table_used == charset_table_size)
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;
1025 id = charset_table_used++;
1026 new_definition_p = 1;
1029 ASET (attrs, charset_id, make_number (id));
1030 charset.id = id;
1031 charset_table[id] = charset;
1033 if (charset.iso_final >= 0)
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;
1048 if (charset.emacs_mule_id >= 0)
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 if (new_definition_p)
1054 Vemacs_mule_charset_list = nconc2 (Vemacs_mule_charset_list,
1055 Fcons (make_number (id), Qnil));
1058 if (new_definition_p)
1060 Vcharset_list = Fcons (args[charset_arg_name], Vcharset_list);
1061 Vcharset_ordered_list = nconc2 (Vcharset_ordered_list,
1062 Fcons (make_number (id), Qnil));
1063 charset_ordered_list_tick++;
1066 return Qnil;
1070 /* Same as Fdefine_charset_internal but arguments are more convenient
1071 to call from C (typically in syms_of_charset). This can define a
1072 charset of `offset' method only. Return the ID of the new
1073 charset. */
1075 static int
1076 define_charset_internal (name, dimension, code_space, min_code, max_code,
1077 iso_final, iso_revision, emacs_mule_id,
1078 ascii_compatible, supprementary,
1079 code_offset)
1080 Lisp_Object name;
1081 int dimension;
1082 unsigned char *code_space;
1083 unsigned min_code, max_code;
1084 int iso_final, iso_revision, emacs_mule_id;
1085 int ascii_compatible, supprementary;
1086 int code_offset;
1088 Lisp_Object args[charset_arg_max];
1089 Lisp_Object plist[14];
1090 Lisp_Object val;
1091 int i;
1093 args[charset_arg_name] = name;
1094 args[charset_arg_dimension] = make_number (dimension);
1095 val = Fmake_vector (make_number (8), make_number (0));
1096 for (i = 0; i < 8; i++)
1097 ASET (val, i, make_number (code_space[i]));
1098 args[charset_arg_code_space] = val;
1099 args[charset_arg_min_code] = make_number (min_code);
1100 args[charset_arg_max_code] = make_number (max_code);
1101 args[charset_arg_iso_final]
1102 = (iso_final < 0 ? Qnil : make_number (iso_final));
1103 args[charset_arg_iso_revision] = make_number (iso_revision);
1104 args[charset_arg_emacs_mule_id]
1105 = (emacs_mule_id < 0 ? Qnil : make_number (emacs_mule_id));
1106 args[charset_arg_ascii_compatible_p] = ascii_compatible ? Qt : Qnil;
1107 args[charset_arg_supplementary_p] = supprementary ? Qt : Qnil;
1108 args[charset_arg_invalid_code] = Qnil;
1109 args[charset_arg_code_offset] = make_number (code_offset);
1110 args[charset_arg_map] = Qnil;
1111 args[charset_arg_subset] = Qnil;
1112 args[charset_arg_superset] = Qnil;
1113 args[charset_arg_unify_map] = Qnil;
1115 plist[0] = intern (":name");
1116 plist[1] = args[charset_arg_name];
1117 plist[2] = intern (":dimension");
1118 plist[3] = args[charset_arg_dimension];
1119 plist[4] = intern (":code-space");
1120 plist[5] = args[charset_arg_code_space];
1121 plist[6] = intern (":iso-final-char");
1122 plist[7] = args[charset_arg_iso_final];
1123 plist[8] = intern (":emacs-mule-id");
1124 plist[9] = args[charset_arg_emacs_mule_id];
1125 plist[10] = intern (":ascii-compatible-p");
1126 plist[11] = args[charset_arg_ascii_compatible_p];
1127 plist[12] = intern (":code-offset");
1128 plist[13] = args[charset_arg_code_offset];
1130 args[charset_arg_plist] = Flist (14, plist);
1131 Fdefine_charset_internal (charset_arg_max, args);
1133 return XINT (CHARSET_SYMBOL_ID (name));
1137 DEFUN ("define-charset-alias", Fdefine_charset_alias,
1138 Sdefine_charset_alias, 2, 2, 0,
1139 doc: /* Define ALIAS as an alias for charset CHARSET. */)
1140 (alias, charset)
1141 Lisp_Object alias, charset;
1143 Lisp_Object attr;
1145 CHECK_CHARSET_GET_ATTR (charset, attr);
1146 Fputhash (alias, attr, Vcharset_hash_table);
1147 Vcharset_list = Fcons (alias, Vcharset_list);
1148 return Qnil;
1152 DEFUN ("unibyte-charset", Funibyte_charset, Sunibyte_charset, 0, 0, 0,
1153 doc: /* Return the unibyte charset (set by `set-unibyte-charset'). */)
1156 return CHARSET_NAME (CHARSET_FROM_ID (charset_unibyte));
1160 DEFUN ("set-unibyte-charset", Fset_unibyte_charset, Sset_unibyte_charset,
1161 1, 1, 0,
1162 doc: /* Set the unibyte charset to CHARSET.
1163 This determines how unibyte/multibyte conversion is done. See also
1164 function `unibyte-charset'. */)
1165 (charset)
1166 Lisp_Object charset;
1168 struct charset *cs;
1169 int i, c;
1171 CHECK_CHARSET_GET_CHARSET (charset, cs);
1172 if (! cs->ascii_compatible_p
1173 || cs->dimension != 1)
1174 error ("Inappropriate unibyte charset: %s", XSYMBOL (charset)->name->data);
1175 charset_unibyte = cs->id;
1176 for (i = 128; i < 256; i++)
1178 c = DECODE_CHAR (cs, i);
1179 unibyte_to_multibyte_table[i] = (c < 0 ? i : c);
1182 return Qnil;
1186 DEFUN ("charset-plist", Fcharset_plist, Scharset_plist, 1, 1, 0,
1187 doc: /* Return the property list of CHARSET. */)
1188 (charset)
1189 Lisp_Object charset;
1191 Lisp_Object attrs;
1193 CHECK_CHARSET_GET_ATTR (charset, attrs);
1194 return CHARSET_ATTR_PLIST (attrs);
1198 DEFUN ("set-charset-plist", Fset_charset_plist, Sset_charset_plist, 2, 2, 0,
1199 doc: /* Set CHARSET's property list to PLIST. */)
1200 (charset, plist)
1201 Lisp_Object charset, plist;
1203 Lisp_Object attrs;
1205 CHECK_CHARSET_GET_ATTR (charset, attrs);
1206 CHARSET_ATTR_PLIST (attrs) = plist;
1207 return plist;
1211 DEFUN ("unify-charset", Funify_charset, Sunify_charset, 1, 3, 0,
1212 doc: /* Unify characters of CHARSET with Unicode.
1213 This means reading the relevant file and installing the table defined
1214 by CHARSET's `:unify-map' property.
1216 Optional second arg UNIFY-MAP is a file name string or a vector. It has
1217 the same meaning as the `:unify-map' attribute in the function
1218 `define-charset' (which see).
1220 Optional third argument DEUNIFY, if non-nil, means to de-unify CHARSET. */)
1221 (charset, unify_map, deunify)
1222 Lisp_Object charset, unify_map, deunify;
1224 int id;
1225 struct charset *cs;
1227 CHECK_CHARSET_GET_ID (charset, id);
1228 cs = CHARSET_FROM_ID (id);
1229 if (CHARSET_METHOD (cs) == CHARSET_METHOD_MAP_DEFERRED)
1230 load_charset (cs);
1231 if (NILP (deunify)
1232 ? CHARSET_UNIFIED_P (cs) && ! NILP (CHARSET_DEUNIFIER (cs))
1233 : ! CHARSET_UNIFIED_P (cs))
1234 return Qnil;
1236 CHARSET_UNIFIED_P (cs) = 0;
1237 if (NILP (deunify))
1239 if (CHARSET_METHOD (cs) != CHARSET_METHOD_OFFSET)
1240 error ("Can't unify charset: %s", XSYMBOL (charset)->name->data);
1241 if (NILP (unify_map))
1242 unify_map = CHARSET_UNIFY_MAP (cs);
1243 if (STRINGP (unify_map))
1244 load_charset_map_from_file (cs, unify_map, 2);
1245 else if (VECTORP (unify_map))
1246 load_charset_map_from_vector (cs, unify_map, 2);
1247 else if (NILP (unify_map))
1248 error ("No unify-map for charset");
1249 else
1250 error ("Bad unify-map arg");
1251 CHARSET_UNIFIED_P (cs) = 1;
1253 else if (CHAR_TABLE_P (Vchar_unify_table))
1255 int min_code = CHARSET_MIN_CODE (cs);
1256 int max_code = CHARSET_MAX_CODE (cs);
1257 int min_char = DECODE_CHAR (cs, min_code);
1258 int max_char = DECODE_CHAR (cs, max_code);
1260 char_table_set_range (Vchar_unify_table, min_char, max_char, Qnil);
1263 return Qnil;
1266 DEFUN ("get-unused-iso-final-char", Fget_unused_iso_final_char,
1267 Sget_unused_iso_final_char, 2, 2, 0,
1268 doc: /*
1269 Return an unsed ISO final char for a charset of DIMENISION and CHARS.
1270 DIMENSION is the number of bytes to represent a character: 1 or 2.
1271 CHARS is the number of characters in a dimension: 94 or 96.
1273 This final char is for private use, thus the range is `0' (48) .. `?' (63).
1274 If there's no unused final char for the specified kind of charset,
1275 return nil. */)
1276 (dimension, chars)
1277 Lisp_Object dimension, chars;
1279 int final_char;
1281 CHECK_NUMBER (dimension);
1282 CHECK_NUMBER (chars);
1283 if (XINT (dimension) != 1 && XINT (dimension) != 2 && XINT (dimension) != 3)
1284 args_out_of_range_3 (dimension, make_number (1), make_number (3));
1285 if (XINT (chars) != 94 && XINT (chars) != 96)
1286 args_out_of_range_3 (chars, make_number (94), make_number (96));
1287 for (final_char = '0'; final_char <= '?'; final_char++)
1288 if (ISO_CHARSET_TABLE (XINT (dimension), XINT (chars), final_char) < 0)
1289 break;
1290 return (final_char <= '?' ? make_number (final_char) : Qnil);
1293 static void
1294 check_iso_charset_parameter (dimension, chars, final_char)
1295 Lisp_Object dimension, chars, final_char;
1297 CHECK_NATNUM (dimension);
1298 CHECK_NATNUM (chars);
1299 CHECK_NATNUM (final_char);
1301 if (XINT (dimension) > 3)
1302 error ("Invalid DIMENSION %d, it should be 1, 2, or 3", XINT (dimension));
1303 if (XINT (chars) != 94 && XINT (chars) != 96)
1304 error ("Invalid CHARS %d, it should be 94 or 96", XINT (chars));
1305 if (XINT (final_char) < '0' || XINT (final_char) > '~')
1306 error ("Invalid FINAL-CHAR %c, it should be `0'..`~'", XINT (chars));
1310 DEFUN ("declare-equiv-charset", Fdeclare_equiv_charset, Sdeclare_equiv_charset,
1311 4, 4, 0,
1312 doc: /*
1313 Declare a charset of DIMENSION, CHARS, FINAL-CHAR is the same as CHARSET.
1314 CHARSET should be defined by `define-charset' in advance. */)
1315 (dimension, chars, final_char, charset)
1316 Lisp_Object dimension, chars, final_char, charset;
1318 int id;
1320 CHECK_CHARSET_GET_ID (charset, id);
1321 check_iso_charset_parameter (dimension, chars, final_char);
1323 ISO_CHARSET_TABLE (XINT (dimension), XINT (chars), XINT (final_char)) = id;
1324 return Qnil;
1328 /* Return information about charsets in the text at PTR of NBYTES
1329 bytes, which are NCHARS characters. The value is:
1331 0: Each character is represented by one byte. This is always
1332 true for a unibyte string. For a multibyte string, true if
1333 it contains only ASCII characters.
1335 1: No charsets other than ascii, control-1, and latin-1 are
1336 found.
1338 2: Otherwise.
1342 string_xstring_p (string)
1343 Lisp_Object string;
1345 const unsigned char *p = XSTRING (string)->data;
1346 const unsigned char *endp = p + STRING_BYTES (XSTRING (string));
1347 struct charset *charset;
1349 if (XSTRING (string)->size == STRING_BYTES (XSTRING (string)))
1350 return 0;
1352 charset = CHARSET_FROM_ID (charset_iso_8859_1);
1353 while (p < endp)
1355 int c = STRING_CHAR_ADVANCE (p);
1357 /* Fixme: comparison of unsigned expression < 0 is always false */
1358 if (ENCODE_CHAR (charset, c) < 0)
1359 return 2;
1361 return 1;
1365 /* Find charsets in the string at PTR of NCHARS and NBYTES.
1367 CHARSETS is a vector. Each element is a cons of CHARSET and
1368 FOUND-FLAG. CHARSET is a charset id, and FOUND-FLAG is nil or t.
1369 FOUND-FLAG t (or nil) means that the corresponding charset is
1370 already found (or not yet found).
1372 It may lookup a translation table TABLE if supplied. */
1374 static void
1375 find_charsets_in_text (ptr, nchars, nbytes, charsets, table)
1376 const unsigned char *ptr;
1377 int nchars, nbytes;
1378 Lisp_Object charsets, table;
1380 const unsigned char *pend = ptr + nbytes;
1381 int ncharsets = ASIZE (charsets);
1383 if (nchars == nbytes)
1384 return;
1386 while (ptr < pend)
1388 int c = STRING_CHAR_ADVANCE (ptr);
1389 int i;
1390 int all_found = 1;
1391 Lisp_Object elt;
1393 if (!NILP (table))
1394 c = translate_char (table, c);
1395 for (i = 0; i < ncharsets; i++)
1397 elt = AREF (charsets, i);
1398 if (NILP (XCDR (elt)))
1400 struct charset *charset = CHARSET_FROM_ID (XINT (XCAR (elt)));
1402 if (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset))
1403 XCDR (elt) = Qt;
1404 else
1405 all_found = 0;
1408 if (all_found)
1409 break;
1413 /* Fixme: returns nil for unibyte. */
1414 DEFUN ("find-charset-region", Ffind_charset_region, Sfind_charset_region,
1415 2, 3, 0,
1416 doc: /* Return a list of charsets in the region between BEG and END.
1417 BEG and END are buffer positions.
1418 Optional arg TABLE if non-nil is a translation table to look up.
1420 If the current buffer is unibyte, the returned list may contain
1421 only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
1422 (beg, end, table)
1423 Lisp_Object beg, end, table;
1425 Lisp_Object charsets;
1426 int from, from_byte, to, stop, stop_byte, i;
1427 Lisp_Object val;
1429 validate_region (&beg, &end);
1430 from = XFASTINT (beg);
1431 stop = to = XFASTINT (end);
1433 if (from < GPT && GPT < to)
1435 stop = GPT;
1436 stop_byte = GPT_BYTE;
1438 else
1439 stop_byte = CHAR_TO_BYTE (stop);
1441 from_byte = CHAR_TO_BYTE (from);
1443 charsets = Fmake_vector (make_number (charset_table_used), Qnil);
1444 for (i = 0; i < charset_table_used; i++)
1445 ASET (charsets, i, Fcons (make_number (i), Qnil));
1447 while (1)
1449 find_charsets_in_text (BYTE_POS_ADDR (from_byte), stop - from,
1450 stop_byte - from_byte, charsets, table);
1451 if (stop < to)
1453 from = stop, from_byte = stop_byte;
1454 stop = to, stop_byte = CHAR_TO_BYTE (stop);
1456 else
1457 break;
1460 val = Qnil;
1461 for (i = charset_table_used - 1; i >= 0; i--)
1462 if (!NILP (XCDR (AREF (charsets, i))))
1463 val = Fcons (CHARSET_NAME (charset_table + i), val);
1464 return val;
1467 /* Fixme: returns nil for unibyte. */
1468 DEFUN ("find-charset-string", Ffind_charset_string, Sfind_charset_string,
1469 1, 2, 0,
1470 doc: /* Return a list of charsets in STR.
1471 Optional arg TABLE if non-nil is a translation table to look up.
1473 If STR is unibyte, the returned list may contain
1474 only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
1475 (str, table)
1476 Lisp_Object str, table;
1478 Lisp_Object charsets;
1479 int i;
1480 Lisp_Object val;
1482 CHECK_STRING (str);
1484 charsets = Fmake_vector (make_number (charset_table_used), Qnil);
1485 for (i = 0; i < charset_table_used; i++)
1486 ASET (charsets, i, Fcons (make_number (i), Qnil));
1487 find_charsets_in_text (XSTRING (str)->data, XSTRING (str)->size,
1488 STRING_BYTES (XSTRING (str)), charsets, table);
1490 val = Qnil;
1491 for (i = charset_table_used - 1; i >= 0; i--)
1492 if (!NILP (XCDR (AREF (charsets, i))))
1493 val = Fcons (CHARSET_NAME (charset_table + i), val);
1494 return val;
1499 /* Return a character correponding to the code-point CODE of
1500 CHARSET. */
1503 decode_char (charset, code)
1504 struct charset *charset;
1505 unsigned code;
1507 int c, char_index;
1508 enum charset_method method = CHARSET_METHOD (charset);
1510 if (code < CHARSET_MIN_CODE (charset) || code > CHARSET_MAX_CODE (charset))
1511 return -1;
1513 if (method == CHARSET_METHOD_MAP_DEFERRED)
1515 load_charset (charset);
1516 method = CHARSET_METHOD (charset);
1519 if (method == CHARSET_METHOD_SUBSET)
1521 Lisp_Object subset_info;
1523 subset_info = CHARSET_SUBSET (charset);
1524 charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
1525 code -= XINT (AREF (subset_info, 3));
1526 if (code < XFASTINT (AREF (subset_info, 1))
1527 || code > XFASTINT (AREF (subset_info, 2)))
1528 c = -1;
1529 else
1530 c = DECODE_CHAR (charset, code);
1532 else if (method == CHARSET_METHOD_SUPERSET)
1534 Lisp_Object parents;
1536 parents = CHARSET_SUPERSET (charset);
1537 c = -1;
1538 for (; CONSP (parents); parents = XCDR (parents))
1540 int id = XINT (XCAR (XCAR (parents)));
1541 int code_offset = XINT (XCDR (XCAR (parents)));
1542 unsigned this_code = code - code_offset;
1544 charset = CHARSET_FROM_ID (id);
1545 if ((c = DECODE_CHAR (charset, this_code)) >= 0)
1546 break;
1549 else
1551 char_index = CODE_POINT_TO_INDEX (charset, code);
1552 if (char_index < 0)
1553 return -1;
1555 if (method == CHARSET_METHOD_MAP)
1557 Lisp_Object decoder;
1559 decoder = CHARSET_DECODER (charset);
1560 if (! VECTORP (decoder))
1561 return -1;
1562 c = XINT (AREF (decoder, char_index));
1564 else
1566 c = char_index + CHARSET_CODE_OFFSET (charset);
1570 if (CHARSET_UNIFIED_P (charset)
1571 && c >= 0)
1573 MAYBE_UNIFY_CHAR (c);
1576 return c;
1579 /* Variable used temporarily by the macro ENCODE_CHAR. */
1580 Lisp_Object charset_work;
1582 /* Return a code-point of CHAR in CHARSET. If CHAR doesn't belong to
1583 CHARSET, return CHARSET_INVALID_CODE (CHARSET). If STRICT is true,
1584 use CHARSET's strict_max_char instead of max_char. */
1586 unsigned
1587 encode_char (charset, c)
1588 struct charset *charset;
1589 int c;
1591 unsigned code;
1592 enum charset_method method = CHARSET_METHOD (charset);
1594 if (CHARSET_UNIFIED_P (charset))
1596 Lisp_Object deunifier, deunified;
1598 deunifier = CHARSET_DEUNIFIER (charset);
1599 if (! CHAR_TABLE_P (deunifier))
1601 Funify_charset (CHARSET_NAME (charset), Qnil, Qnil);
1602 deunifier = CHARSET_DEUNIFIER (charset);
1604 deunified = CHAR_TABLE_REF (deunifier, c);
1605 if (! NILP (deunified))
1606 c = XINT (deunified);
1609 if (! CHARSET_FAST_MAP_REF ((c), charset->fast_map)
1610 || c < CHARSET_MIN_CHAR (charset) || c > CHARSET_MAX_CHAR (charset))
1611 return CHARSET_INVALID_CODE (charset);
1613 if (method == CHARSET_METHOD_SUBSET)
1615 Lisp_Object subset_info;
1616 struct charset *this_charset;
1618 subset_info = CHARSET_SUBSET (charset);
1619 this_charset = CHARSET_FROM_ID (XFASTINT (AREF (subset_info, 0)));
1620 code = ENCODE_CHAR (this_charset, c);
1621 if (code == CHARSET_INVALID_CODE (this_charset)
1622 || code < XFASTINT (AREF (subset_info, 1))
1623 || code > XFASTINT (AREF (subset_info, 2)))
1624 return CHARSET_INVALID_CODE (charset);
1625 code += XINT (AREF (subset_info, 3));
1626 return code;
1629 if (method == CHARSET_METHOD_SUPERSET)
1631 Lisp_Object parents;
1633 parents = CHARSET_SUPERSET (charset);
1634 for (; CONSP (parents); parents = XCDR (parents))
1636 int id = XINT (XCAR (XCAR (parents)));
1637 int code_offset = XINT (XCDR (XCAR (parents)));
1638 struct charset *this_charset = CHARSET_FROM_ID (id);
1640 code = ENCODE_CHAR (this_charset, c);
1641 if (code != CHARSET_INVALID_CODE (this_charset))
1642 return code + code_offset;
1644 return CHARSET_INVALID_CODE (charset);
1647 if (method == CHARSET_METHOD_MAP_DEFERRED)
1649 load_charset (charset);
1650 method = CHARSET_METHOD (charset);
1653 if (method == CHARSET_METHOD_MAP)
1655 Lisp_Object encoder;
1656 Lisp_Object val;
1658 encoder = CHARSET_ENCODER (charset);
1659 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset)))
1660 return CHARSET_INVALID_CODE (charset);
1661 val = CHAR_TABLE_REF (encoder, c);
1662 if (NILP (val))
1663 return CHARSET_INVALID_CODE (charset);
1664 code = XINT (val);
1665 if (! CHARSET_COMPACT_CODES_P (charset))
1666 code = INDEX_TO_CODE_POINT (charset, code);
1668 else /* method == CHARSET_METHOD_OFFSET */
1670 code = c - CHARSET_CODE_OFFSET (charset);
1671 code = INDEX_TO_CODE_POINT (charset, code);
1674 return code;
1678 DEFUN ("decode-char", Fdecode_char, Sdecode_char, 2, 3, 0,
1679 doc: /* Decode the pair of CHARSET and CODE-POINT into a character.
1680 Return nil if CODE-POINT is not valid in CHARSET.
1682 CODE-POINT may be a cons (HIGHER-16-BIT-VALUE . LOWER-16-BIT-VALUE).
1684 Optional argument RESTRICTION specifies a way to map the pair of CCS
1685 and CODE-POINT to a chracter. Currently not supported and just ignored. */)
1686 (charset, code_point, restriction)
1687 Lisp_Object charset, code_point, restriction;
1689 int c, id;
1690 unsigned code;
1691 struct charset *charsetp;
1693 CHECK_CHARSET_GET_ID (charset, id);
1694 if (CONSP (code_point))
1696 CHECK_NATNUM (XCAR (code_point));
1697 CHECK_NATNUM (XCDR (code_point));
1698 code = (XINT (XCAR (code_point)) << 16) | (XINT (XCDR (code_point)));
1700 else
1702 CHECK_NATNUM (code_point);
1703 code = XINT (code_point);
1705 charsetp = CHARSET_FROM_ID (id);
1706 c = DECODE_CHAR (charsetp, code);
1707 return (c >= 0 ? make_number (c) : Qnil);
1711 DEFUN ("encode-char", Fencode_char, Sencode_char, 2, 3, 0,
1712 doc: /* Encode the character CH into a code-point of CHARSET.
1713 Return nil if CHARSET doesn't include CH.
1715 Optional argument RESTRICTION specifies a way to map CHAR to a
1716 code-point in CCS. Currently not supported and just ignored. */)
1717 (ch, charset, restriction)
1718 Lisp_Object ch, charset, restriction;
1720 int id;
1721 unsigned code;
1722 struct charset *charsetp;
1724 CHECK_CHARSET_GET_ID (charset, id);
1725 CHECK_NATNUM (ch);
1726 charsetp = CHARSET_FROM_ID (id);
1727 code = ENCODE_CHAR (charsetp, XINT (ch));
1728 if (code == CHARSET_INVALID_CODE (charsetp))
1729 return Qnil;
1730 if (code > 0x7FFFFFF)
1731 return Fcons (make_number (code >> 16), make_number (code & 0xFFFF));
1732 return make_number (code);
1736 DEFUN ("make-char", Fmake_char, Smake_char, 1, 5, 0,
1737 doc:
1738 /* Return a character of CHARSET whose position codes are CODEn.
1740 CODE1 through CODE4 are optional, but if you don't supply sufficient
1741 position codes, it is assumed that the minimum code in each dimension
1742 is specified. */)
1743 (charset, code1, code2, code3, code4)
1744 Lisp_Object charset, code1, code2, code3, code4;
1746 int id, dimension;
1747 struct charset *charsetp;
1748 unsigned code;
1749 int c;
1751 CHECK_CHARSET_GET_ID (charset, id);
1752 charsetp = CHARSET_FROM_ID (id);
1754 dimension = CHARSET_DIMENSION (charsetp);
1755 if (NILP (code1))
1756 code = (CHARSET_ASCII_COMPATIBLE_P (charsetp)
1757 ? 0 : CHARSET_MIN_CODE (charsetp));
1758 else
1760 CHECK_NATNUM (code1);
1761 if (XFASTINT (code1) >= 0x100)
1762 args_out_of_range (make_number (0xFF), code1);
1763 code = XFASTINT (code1);
1765 if (dimension > 1)
1767 code <<= 8;
1768 if (NILP (code2))
1769 code |= charsetp->code_space[(dimension - 2) * 4];
1770 else
1772 CHECK_NATNUM (code2);
1773 if (XFASTINT (code2) >= 0x100)
1774 args_out_of_range (make_number (0xFF), code2);
1775 code |= XFASTINT (code2);
1778 if (dimension > 2)
1780 code <<= 8;
1781 if (NILP (code3))
1782 code |= charsetp->code_space[(dimension - 3) * 4];
1783 else
1785 CHECK_NATNUM (code3);
1786 if (XFASTINT (code3) >= 0x100)
1787 args_out_of_range (make_number (0xFF), code3);
1788 code |= XFASTINT (code3);
1791 if (dimension > 3)
1793 code <<= 8;
1794 if (NILP (code4))
1795 code |= charsetp->code_space[0];
1796 else
1798 CHECK_NATNUM (code4);
1799 if (XFASTINT (code4) >= 0x100)
1800 args_out_of_range (make_number (0xFF), code4);
1801 code |= XFASTINT (code4);
1808 if (CHARSET_ISO_FINAL (charsetp) >= 0)
1809 code &= 0x7F7F7F7F;
1810 c = DECODE_CHAR (charsetp, code);
1811 if (c < 0)
1812 error ("Invalid code(s)");
1813 return make_number (c);
1817 /* Return the first charset in CHARSET_LIST that contains C.
1818 CHARSET_LIST is a list of charset IDs. If it is nil, use
1819 Vcharset_ordered_list. */
1821 struct charset *
1822 char_charset (c, charset_list, code_return)
1823 int c;
1824 Lisp_Object charset_list;
1825 unsigned *code_return;
1827 if (NILP (charset_list))
1828 charset_list = Vcharset_ordered_list;
1830 while (CONSP (charset_list))
1832 struct charset *charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
1833 unsigned code = ENCODE_CHAR (charset, c);
1835 if (code != CHARSET_INVALID_CODE (charset))
1837 if (code_return)
1838 *code_return = code;
1839 return charset;
1841 charset_list = XCDR (charset_list);
1843 return NULL;
1847 /* Fixme: `unknown' can't happen now? */
1848 DEFUN ("split-char", Fsplit_char, Ssplit_char, 1, 1, 0,
1849 doc: /*Return list of charset and one to three position-codes of CHAR.
1850 If CHAR is invalid as a character code, return a list `(unknown CHAR)'. */)
1851 (ch)
1852 Lisp_Object ch;
1854 struct charset *charset;
1855 int c, dimension;
1856 unsigned code;
1857 Lisp_Object val;
1859 CHECK_CHARACTER (ch);
1860 c = XFASTINT (ch);
1861 charset = CHAR_CHARSET (c);
1862 if (! charset)
1863 return Fcons (intern ("unknown"), Fcons (ch, Qnil));
1865 code = ENCODE_CHAR (charset, c);
1866 if (code == CHARSET_INVALID_CODE (charset))
1867 abort ();
1868 dimension = CHARSET_DIMENSION (charset);
1869 val = (dimension == 1 ? Fcons (make_number (code), Qnil)
1870 : dimension == 2 ? Fcons (make_number (code >> 8),
1871 Fcons (make_number (code & 0xFF), Qnil))
1872 : Fcons (make_number (code >> 16),
1873 Fcons (make_number ((code >> 8) & 0xFF),
1874 Fcons (make_number (code & 0xFF), Qnil))));
1875 return Fcons (CHARSET_NAME (charset), val);
1879 DEFUN ("char-charset", Fchar_charset, Schar_charset, 1, 1, 0,
1880 doc: /* Return the charset of highest priority that contains CHAR. */)
1881 (ch)
1882 Lisp_Object ch;
1884 struct charset *charset;
1886 CHECK_CHARACTER (ch);
1887 charset = CHAR_CHARSET (XINT (ch));
1888 return (CHARSET_NAME (charset));
1892 DEFUN ("charset-after", Fcharset_after, Scharset_after, 0, 1, 0,
1893 doc: /*
1894 Return charset of a character in the current buffer at position POS.
1895 If POS is nil, it defauls to the current point.
1896 If POS is out of range, the value is nil. */)
1897 (pos)
1898 Lisp_Object pos;
1900 Lisp_Object ch;
1901 struct charset *charset;
1903 ch = Fchar_after (pos);
1904 if (! INTEGERP (ch))
1905 return ch;
1906 charset = CHAR_CHARSET (XINT (ch));
1907 return (CHARSET_NAME (charset));
1911 DEFUN ("iso-charset", Fiso_charset, Siso_charset, 3, 3, 0,
1912 doc: /*
1913 Return charset of ISO's specification DIMENSION, CHARS, and FINAL-CHAR.
1915 ISO 2022's designation sequence (escape sequence) distinguishes charsets
1916 by their DIMENSION, CHARS, and FINAL-CHAR,
1917 where as Emacs distinguishes them by charset symbol.
1918 See the documentation of the function `charset-info' for the meanings of
1919 DIMENSION, CHARS, and FINAL-CHAR. */)
1920 (dimension, chars, final_char)
1921 Lisp_Object dimension, chars, final_char;
1923 int id;
1925 check_iso_charset_parameter (dimension, chars, final_char);
1926 id = ISO_CHARSET_TABLE (XFASTINT (dimension), XFASTINT (chars),
1927 XFASTINT (final_char));
1928 return (id >= 0 ? CHARSET_NAME (CHARSET_FROM_ID (id)) : Qnil);
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. */)
1938 int i;
1939 struct charset *charset;
1940 Lisp_Object attrs;
1942 for (i = 0; i < charset_table_used; i++)
1944 charset = CHARSET_FROM_ID (i);
1945 attrs = CHARSET_ATTRIBUTES (charset);
1947 if (CHARSET_METHOD (charset) == CHARSET_METHOD_MAP)
1949 CHARSET_ATTR_DECODER (attrs) = Qnil;
1950 CHARSET_ATTR_ENCODER (attrs) = Qnil;
1951 CHARSET_METHOD (charset) = CHARSET_METHOD_MAP_DEFERRED;
1954 if (CHARSET_UNIFIED_P (charset))
1955 CHARSET_ATTR_DEUNIFIER (attrs) = Qnil;
1958 if (CHAR_TABLE_P (Vchar_unified_charset_table))
1960 Foptimize_char_table (Vchar_unified_charset_table);
1961 Vchar_unify_table = Vchar_unified_charset_table;
1962 Vchar_unified_charset_table = Qnil;
1965 return Qnil;
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;
1975 Lisp_Object val = Qnil, list = Vcharset_ordered_list;
1977 if (!NILP (highestp))
1978 return CHARSET_NAME (CHARSET_FROM_ID (XINT (Fcar (list))));
1980 while (!NILP (list))
1982 val = Fcons (CHARSET_NAME (CHARSET_FROM_ID (XINT (XCAR (list)))), val);
1983 list = XCDR (list);
1985 return Fnreverse (val);
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;
1996 Lisp_Object new_head = Qnil, old_list, arglist[2];
1997 int i, id;
1999 old_list = Fcopy_sequence (Vcharset_ordered_list);
2000 for (i = 0; i < nargs; i++)
2002 CHECK_CHARSET_GET_ID (args[i], id);
2003 old_list = Fdelq (make_number (id), old_list);
2004 new_head = Fcons (make_number (id), new_head);
2006 arglist[0] = Fnreverse (new_head);
2007 arglist[1] = old_list;
2008 Vcharset_ordered_list = Fnconc (2, arglist);
2009 charset_ordered_list_tick++;
2010 return Qnil;
2013 void
2014 init_charset ()
2020 void
2021 init_charset_once ()
2023 int i, j, k;
2025 for (i = 0; i < ISO_MAX_DIMENSION; i++)
2026 for (j = 0; j < ISO_MAX_CHARS; j++)
2027 for (k = 0; k < ISO_MAX_FINAL; k++)
2028 iso_charset_table[i][j][k] = -1;
2030 for (i = 0; i < 256; i++)
2031 emacs_mule_charset[i] = NULL;
2033 charset_jisx0201_roman = -1;
2034 charset_jisx0208_1978 = -1;
2035 charset_jisx0208 = -1;
2037 for (i = 0; i < 256; i++)
2038 unibyte_to_multibyte_table[i] = i;
2041 #ifdef emacs
2043 void
2044 syms_of_charset ()
2046 char *p;
2048 DEFSYM (Qcharsetp, "charsetp");
2050 DEFSYM (Qascii, "ascii");
2051 DEFSYM (Qunicode, "unicode");
2052 DEFSYM (Qeight_bit, "eight-bit");
2053 DEFSYM (Qiso_8859_1, "iso-8859-1");
2055 DEFSYM (Qgl, "gl");
2056 DEFSYM (Qgr, "gr");
2058 p = (char *) xmalloc (30000);
2060 staticpro (&Vcharset_ordered_list);
2061 Vcharset_ordered_list = Qnil;
2063 staticpro (&Viso_2022_charset_list);
2064 Viso_2022_charset_list = Qnil;
2066 staticpro (&Vemacs_mule_charset_list);
2067 Vemacs_mule_charset_list = Qnil;
2069 staticpro (&Vcharset_hash_table);
2070 Vcharset_hash_table = Fmakehash (Qeq);
2072 charset_table_size = 128;
2073 charset_table = ((struct charset *)
2074 xmalloc (sizeof (struct charset) * charset_table_size));
2075 charset_table_used = 0;
2077 staticpro (&Vchar_unified_charset_table);
2078 Vchar_unified_charset_table = Fmake_char_table (Qnil, make_number (-1));
2080 defsubr (&Scharsetp);
2081 defsubr (&Smap_charset_chars);
2082 defsubr (&Sdefine_charset_internal);
2083 defsubr (&Sdefine_charset_alias);
2084 defsubr (&Sunibyte_charset);
2085 defsubr (&Sset_unibyte_charset);
2086 defsubr (&Scharset_plist);
2087 defsubr (&Sset_charset_plist);
2088 defsubr (&Sunify_charset);
2089 defsubr (&Sget_unused_iso_final_char);
2090 defsubr (&Sdeclare_equiv_charset);
2091 defsubr (&Sfind_charset_region);
2092 defsubr (&Sfind_charset_string);
2093 defsubr (&Sdecode_char);
2094 defsubr (&Sencode_char);
2095 defsubr (&Ssplit_char);
2096 defsubr (&Smake_char);
2097 defsubr (&Schar_charset);
2098 defsubr (&Scharset_after);
2099 defsubr (&Siso_charset);
2100 defsubr (&Sclear_charset_maps);
2101 defsubr (&Scharset_priority_list);
2102 defsubr (&Sset_charset_priority);
2104 DEFVAR_LISP ("charset-map-directory", &Vcharset_map_directory,
2105 doc: /* Directory of charset map files that come with GNU Emacs.
2106 The default value is sub-directory "charsets" of `data-directory'. */);
2107 Vcharset_map_directory = Fexpand_file_name (build_string ("charsets"),
2108 Vdata_directory);
2110 DEFVAR_LISP ("charset-list", &Vcharset_list,
2111 doc: /* List of all charsets ever defined. */);
2112 Vcharset_list = Qnil;
2114 charset_ascii
2115 = define_charset_internal (Qascii, 1, "\x00\x7F\x00\x00\x00\x00",
2116 0, 127, 'B', -1, 0, 1, 0, 0);
2117 charset_iso_8859_1
2118 = define_charset_internal (Qiso_8859_1, 1, "\x00\xFF\x00\x00\x00\x00",
2119 0, 255, -1, -1, -1, 1, 0, 0);
2120 charset_unicode
2121 = define_charset_internal (Qunicode, 3, "\x00\xFF\x00\xFF\x00\x10",
2122 0, MAX_UNICODE_CHAR, -1, 0, -1, 1, 0, 0);
2123 charset_eight_bit
2124 = define_charset_internal (Qeight_bit, 1, "\x80\xFF\x00\x00\x00\x00",
2125 128, 255, -1, 0, -1, 0, 0,
2126 MAX_5_BYTE_CHAR + 1);
2129 #endif /* emacs */