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, 2004 Free Software Foundation, Inc.
5 Copyright (C) 2003, 2004
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)
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. */
31 #include <sys/types.h>
33 #include "character.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
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 static int charset_table_used
;
68 Lisp_Object Qcharsetp
;
70 /* Special charset symbols. */
72 Lisp_Object Qeight_bit
;
73 Lisp_Object Qiso_8859_1
;
76 /* The corresponding charsets. */
78 int charset_eight_bit
;
79 int charset_iso_8859_1
;
82 /* The other special charsets. */
83 int charset_jisx0201_roman
;
84 int charset_jisx0208_1978
;
87 /* Value of charset attribute `charset-iso-plane'. */
90 /* Charset of unibyte characters. */
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
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_path
;
115 Lisp_Object Vchar_unified_charset_table
;
117 /* Defined in chartab.c */
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)) \
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])) \
153 | (((charset)->code_space[8] \
154 + ((idx) / (charset)->code_space[7] % (charset)->code_space[10])) \
156 | (((charset)->code_space[12] + ((idx) / (charset)->code_space[11])) \
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
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,
187 load_charset_map (charset
, entries
, n_entries
, control_flag
)
188 struct charset
*charset
;
189 struct charset_map_entries
*entries
;
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
;
198 unsigned char *fast_map
= charset
->fast_map
;
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
++)
221 int from_index
, to_index
;
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
);
233 to_index
= from_index
;
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)
244 if (control_flag
< 2)
250 else if (from_c
< min_char
)
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
))
275 ASET (vec
, from_index
, make_number (from_c
));
276 if (NILP (CHAR_TABLE_REF (table
, from_c
)))
277 CHAR_TABLE_SET (table
, from_c
, make_number (code
));
278 if (from_index
== to_index
)
280 from_index
++, from_c
++;
281 code
= INDEX_TO_CODE_POINT (charset
, from_index
);
284 for (; from_index
<= to_index
; from_index
++, from_c
++)
286 ASET (vec
, from_index
, make_number (from_c
));
287 if (NILP (CHAR_TABLE_REF (table
, from_c
)))
288 CHAR_TABLE_SET (table
, from_c
, make_number (from_index
));
294 unsigned code
= from
;
298 int c1
= DECODE_CHAR (charset
, code
);
302 CHAR_TABLE_SET (table
, from_c
, make_number (c1
));
303 CHAR_TABLE_SET (Vchar_unify_table
, c1
, make_number (from_c
));
304 if (CHAR_TABLE_P (Vchar_unified_charset_table
))
305 CHAR_TABLE_SET (Vchar_unified_charset_table
, c1
,
306 CHARSET_NAME (charset
));
308 if (from_index
== to_index
)
310 from_index
++, from_c
++;
311 code
= INDEX_TO_CODE_POINT (charset
, from_index
);
316 if (control_flag
< 2)
318 CHARSET_MIN_CHAR (charset
) = (ascii_compatible_p
319 ? nonascii_min_char
: min_char
);
320 CHARSET_MAX_CHAR (charset
) = max_char
;
321 if (control_flag
== 1)
323 CHARSET_DECODER (charset
) = vec
;
324 CHARSET_ENCODER (charset
) = table
;
328 CHARSET_DEUNIFIER (charset
) = table
;
332 /* Read a hexadecimal number (preceded by "0x") from the file FP while
333 paying attention to comment charcter '#'. */
335 static INLINE
unsigned
343 while ((c
= getc (fp
)) != EOF
)
347 while ((c
= getc (fp
)) != EOF
&& c
!= '\n');
351 if ((c
= getc (fp
)) == EOF
|| c
== 'x')
363 while ((c
= getc (fp
)) != EOF
&& isxdigit (c
))
365 | (c
<= '9' ? c
- '0' : c
<= 'F' ? c
- 'A' + 10 : c
- 'a' + 10));
367 while ((c
= getc (fp
)) != EOF
&& isdigit (c
))
368 n
= (n
* 10) + c
- '0';
375 /* Return a mapping vector for CHARSET loaded from MAPFILE.
376 Each line of MAPFILE has this form
378 where 0xAAAA is a code-point and 0xCCCC is the corresponding
379 character code, or this form
381 where 0xAAAA and 0xBBBB are code-points specifying a range, and
382 0xCCCC is the first character code of the range.
384 The returned vector has this form:
385 [ CODE1 CHAR1 CODE2 CHAR2 .... ]
386 where CODE1 is a code-point or a cons of code-points specifying a
389 extern void add_to_log
P_ ((char *, Lisp_Object
, Lisp_Object
));
392 load_charset_map_from_file (charset
, mapfile
, control_flag
)
393 struct charset
*charset
;
397 unsigned min_code
= CHARSET_MIN_CODE (charset
);
398 unsigned max_code
= CHARSET_MAX_CODE (charset
);
402 Lisp_Object suffixes
;
403 struct charset_map_entries
*head
, *entries
;
406 suffixes
= Fcons (build_string (".map"),
407 Fcons (build_string (".TXT"), Qnil
));
409 fd
= openp (Vcharset_map_path
, mapfile
, suffixes
, NULL
, Qnil
);
411 || ! (fp
= fdopen (fd
, "r")))
413 add_to_log ("Failure in loading charset map: %S", mapfile
, Qnil
);
417 head
= entries
= ((struct charset_map_entries
*)
418 alloca (sizeof (struct charset_map_entries
)));
427 from
= read_hex (fp
, &eof
);
430 if (getc (fp
) == '-')
431 to
= read_hex (fp
, &eof
);
434 c
= (int) read_hex (fp
, &eof
);
436 if (from
< min_code
|| to
> max_code
|| from
> to
|| c
> MAX_CHAR
)
439 if (n_entries
> 0 && (n_entries
% 0x10000) == 0)
441 entries
->next
= ((struct charset_map_entries
*)
442 alloca (sizeof (struct charset_map_entries
)));
443 entries
= entries
->next
;
445 idx
= n_entries
% 0x10000;
446 entries
->entry
[idx
].from
= from
;
447 entries
->entry
[idx
].to
= to
;
448 entries
->entry
[idx
].c
= c
;
454 load_charset_map (charset
, head
, n_entries
, control_flag
);
458 load_charset_map_from_vector (charset
, vec
, control_flag
)
459 struct charset
*charset
;
463 unsigned min_code
= CHARSET_MIN_CODE (charset
);
464 unsigned max_code
= CHARSET_MAX_CODE (charset
);
465 struct charset_map_entries
*head
, *entries
;
467 int len
= ASIZE (vec
);
472 add_to_log ("Failure in loading charset map: %V", vec
, Qnil
);
476 head
= entries
= ((struct charset_map_entries
*)
477 alloca (sizeof (struct charset_map_entries
)));
479 for (i
= 0; i
< len
; i
+= 2)
481 Lisp_Object val
, val2
;
493 from
= XFASTINT (val
);
494 to
= XFASTINT (val2
);
499 from
= to
= XFASTINT (val
);
501 val
= AREF (vec
, i
+ 1);
505 if (from
< min_code
|| to
> max_code
|| from
> to
|| c
> MAX_CHAR
)
508 if ((n_entries
% 0x10000) == 0)
510 entries
->next
= ((struct charset_map_entries
*)
511 alloca (sizeof (struct charset_map_entries
)));
512 entries
= entries
->next
;
514 idx
= n_entries
% 0x10000;
515 entries
->entry
[idx
].from
= from
;
516 entries
->entry
[idx
].to
= to
;
517 entries
->entry
[idx
].c
= c
;
521 load_charset_map (charset
, head
, n_entries
, control_flag
);
525 load_charset (charset
)
526 struct charset
*charset
;
528 if (CHARSET_METHOD (charset
) == CHARSET_METHOD_MAP_DEFERRED
)
532 map
= CHARSET_MAP (charset
);
534 load_charset_map_from_file (charset
, map
, 1);
536 load_charset_map_from_vector (charset
, map
, 1);
537 CHARSET_METHOD (charset
) = CHARSET_METHOD_MAP
;
542 DEFUN ("charsetp", Fcharsetp
, Scharsetp
, 1, 1, 0,
543 doc
: /* Return non-nil if and only if OBJECT is a charset.*/)
547 return (CHARSETP (object
) ? Qt
: Qnil
);
552 map_charset_chars (c_function
, function
, arg
,
554 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
));
555 Lisp_Object function
, arg
;
556 struct charset
*charset
;
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
));
585 (*c_function
) (arg
, range
);
587 call2 (function
, range
, arg
);
589 else if (CHARSET_METHOD (charset
) == CHARSET_METHOD_MAP
)
591 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset
)))
593 if (CHARSET_ASCII_COMPATIBLE_P (charset
) && from
<= 127)
595 range
= Fcons (make_number (from
), make_number (to
));
597 XSETCAR (range
, make_number (127));
600 (*c_function
) (arg
, range
);
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
;
613 subset_info
= CHARSET_SUBSET (charset
);
614 charset
= CHARSET_FROM_ID (XFASTINT (AREF (subset_info
, 0)));
615 offset
= XINT (AREF (subset_info
, 3));
617 if (from
< XFASTINT (AREF (subset_info
, 1)))
618 from
= XFASTINT (AREF (subset_info
, 1));
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 */
628 for (parents
= CHARSET_SUPERSET (charset
); CONSP (parents
);
629 parents
= XCDR (parents
))
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
,
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
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 target characters. */)
658 (function
, charset
, arg
, from_code
, to_code
)
659 Lisp_Object function
, charset
, arg
, from_code
, to_code
;
664 CHECK_CHARSET_GET_CHARSET (charset
, cs
);
665 if (NILP (from_code
))
666 from
= CHARSET_MIN_CODE (cs
);
669 CHECK_NATNUM (from_code
);
670 from
= XINT (from_code
);
671 if (from
< CHARSET_MIN_CODE (cs
))
672 from
= CHARSET_MIN_CODE (cs
);
675 to
= CHARSET_MAX_CODE (cs
);
678 CHECK_NATNUM (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
);
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
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 ...) */)
701 /* Charset attr vector. */
705 struct Lisp_Hash_Table
*hash_table
= XHASH_TABLE (Vcharset_hash_table
);
707 struct charset charset
;
710 int new_definition_p
;
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
;
741 val
= args
[charset_arg_dimension
];
743 charset
.dimension
= dimension
;
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];
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
];
792 CHECK_NUMBER_CAR (val
);
793 CHECK_NUMBER_CDR (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
];
814 CHECK_NUMBER_CAR (val
);
815 CHECK_NUMBER_CDR (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
];
830 if (charset
.min_code
> 0)
831 charset
.invalid_code
= 0;
834 XSETINT (val
, charset
.max_code
+ 1);
835 if (XINT (val
) == charset
.max_code
+ 1)
836 charset
.invalid_code
= charset
.max_code
+ 1;
838 error ("Attribute :invalid-code must be specified");
844 charset
.invalid_code
= XFASTINT (val
);
847 val
= args
[charset_arg_iso_final
];
849 charset
.iso_final
= -1;
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
];
860 charset
.iso_revision
= -1;
865 args_out_of_range (make_number (63), val
);
866 charset
.iso_revision
= XINT (val
);
869 val
= args
[charset_arg_emacs_mule_id
];
871 charset
.emacs_mule_id
= -1;
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
];
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 i
= (charset
.min_char
>> 7) << 7;
904 for (; i
< 0x10000 && i
<= charset
.max_char
; i
+= 128)
905 CHARSET_FAST_MAP_SET (i
, charset
.fast_map
);
907 for (; i
<= charset
.max_char
; i
+= 0x1000)
908 CHARSET_FAST_MAP_SET (i
, charset
.fast_map
);
910 else if (! NILP (args
[charset_arg_map
]))
912 val
= args
[charset_arg_map
];
913 ASET (attrs
, charset_map
, val
);
915 load_charset_map_from_file (&charset
, val
, 0);
917 load_charset_map_from_vector (&charset
, val
, 0);
918 charset
.method
= CHARSET_METHOD_MAP_DEFERRED
;
920 else if (! NILP (args
[charset_arg_subset
]))
923 Lisp_Object parent_min_code
, parent_max_code
, parent_code_offset
;
924 struct charset
*parent_charset
;
926 val
= args
[charset_arg_subset
];
928 CHECK_CHARSET_GET_CHARSET (parent
, parent_charset
);
929 parent_min_code
= Fnth (make_number (1), val
);
930 CHECK_NATNUM (parent_min_code
);
931 parent_max_code
= Fnth (make_number (2), val
);
932 CHECK_NATNUM (parent_max_code
);
933 parent_code_offset
= Fnth (make_number (3), val
);
934 CHECK_NUMBER (parent_code_offset
);
935 val
= Fmake_vector (make_number (4), Qnil
);
936 ASET (val
, 0, make_number (parent_charset
->id
));
937 ASET (val
, 1, parent_min_code
);
938 ASET (val
, 2, parent_max_code
);
939 ASET (val
, 3, parent_code_offset
);
940 ASET (attrs
, charset_subset
, val
);
942 charset
.method
= CHARSET_METHOD_SUBSET
;
943 /* Here, we just copy the parent's fast_map. It's not accurate,
944 but at least it works for quickly detecting which character
945 DOESN'T belong to this charset. */
946 for (i
= 0; i
< 190; i
++)
947 charset
.fast_map
[i
] = parent_charset
->fast_map
[i
];
949 /* We also copy these for parents. */
950 charset
.min_char
= parent_charset
->min_char
;
951 charset
.max_char
= parent_charset
->max_char
;
953 else if (! NILP (args
[charset_arg_superset
]))
955 val
= args
[charset_arg_superset
];
956 charset
.method
= CHARSET_METHOD_SUPERSET
;
957 val
= Fcopy_sequence (val
);
958 ASET (attrs
, charset_superset
, val
);
960 charset
.min_char
= MAX_CHAR
;
961 charset
.max_char
= 0;
962 for (; ! NILP (val
); val
= Fcdr (val
))
964 Lisp_Object elt
, car_part
, cdr_part
;
966 struct charset
*this_charset
;
971 car_part
= XCAR (elt
);
972 cdr_part
= XCDR (elt
);
973 CHECK_CHARSET_GET_ID (car_part
, this_id
);
974 CHECK_NUMBER (cdr_part
);
975 offset
= XINT (cdr_part
);
979 CHECK_CHARSET_GET_ID (elt
, this_id
);
982 XSETCAR (val
, Fcons (make_number (this_id
), make_number (offset
)));
984 this_charset
= CHARSET_FROM_ID (this_id
);
985 if (charset
.min_char
> this_charset
->min_char
)
986 charset
.min_char
= this_charset
->min_char
;
987 if (charset
.max_char
< this_charset
->max_char
)
988 charset
.max_char
= this_charset
->max_char
;
989 for (i
= 0; i
< 190; i
++)
990 charset
.fast_map
[i
] |= this_charset
->fast_map
[i
];
994 error ("None of :code-offset, :map, :parents are specified");
996 val
= args
[charset_arg_unify_map
];
997 if (! NILP (val
) && !STRINGP (val
))
999 ASET (attrs
, charset_unify_map
, val
);
1001 CHECK_LIST (args
[charset_arg_plist
]);
1002 ASET (attrs
, charset_plist
, args
[charset_arg_plist
]);
1004 charset
.hash_index
= hash_lookup (hash_table
, args
[charset_arg_name
],
1006 if (charset
.hash_index
>= 0)
1008 new_definition_p
= 0;
1009 id
= XFASTINT (CHARSET_SYMBOL_ID (args
[charset_arg_name
]));
1010 HASH_VALUE (hash_table
, charset
.hash_index
) = attrs
;
1014 charset
.hash_index
= hash_put (hash_table
, args
[charset_arg_name
], attrs
,
1016 if (charset_table_used
== charset_table_size
)
1018 struct charset
*new_table
1019 = (struct charset
*) xmalloc (sizeof (struct charset
)
1020 * (charset_table_size
+ 16));
1021 bcopy (charset_table
, new_table
,
1022 sizeof (struct charset
) * charset_table_size
);
1023 charset_table_size
+= 16;
1024 charset_table
= new_table
;
1026 id
= charset_table_used
++;
1027 new_definition_p
= 1;
1030 ASET (attrs
, charset_id
, make_number (id
));
1032 charset_table
[id
] = charset
;
1034 if (charset
.iso_final
>= 0)
1036 ISO_CHARSET_TABLE (charset
.dimension
, charset
.iso_chars_96
,
1037 charset
.iso_final
) = id
;
1038 if (new_definition_p
)
1039 Viso_2022_charset_list
= nconc2 (Viso_2022_charset_list
,
1040 Fcons (make_number (id
), Qnil
));
1041 if (ISO_CHARSET_TABLE (1, 0, 'J') == id
)
1042 charset_jisx0201_roman
= id
;
1043 else if (ISO_CHARSET_TABLE (2, 0, '@') == id
)
1044 charset_jisx0208_1978
= id
;
1045 else if (ISO_CHARSET_TABLE (2, 0, 'B') == id
)
1046 charset_jisx0208
= id
;
1049 if (charset
.emacs_mule_id
>= 0)
1051 emacs_mule_charset
[charset
.emacs_mule_id
] = CHARSET_FROM_ID (id
);
1052 if (charset
.emacs_mule_id
< 0xA0)
1053 emacs_mule_bytes
[charset
.emacs_mule_id
] = charset
.dimension
+ 1;
1054 if (new_definition_p
)
1055 Vemacs_mule_charset_list
= nconc2 (Vemacs_mule_charset_list
,
1056 Fcons (make_number (id
), Qnil
));
1059 if (new_definition_p
)
1061 Vcharset_list
= Fcons (args
[charset_arg_name
], Vcharset_list
);
1062 Vcharset_ordered_list
= nconc2 (Vcharset_ordered_list
,
1063 Fcons (make_number (id
), Qnil
));
1064 charset_ordered_list_tick
++;
1071 /* Same as Fdefine_charset_internal but arguments are more convenient
1072 to call from C (typically in syms_of_charset). This can define a
1073 charset of `offset' method only. Return the ID of the new
1077 define_charset_internal (name
, dimension
, code_space
, min_code
, max_code
,
1078 iso_final
, iso_revision
, emacs_mule_id
,
1079 ascii_compatible
, supplementary
,
1083 unsigned char *code_space
;
1084 unsigned min_code
, max_code
;
1085 int iso_final
, iso_revision
, emacs_mule_id
;
1086 int ascii_compatible
, supplementary
;
1089 Lisp_Object args
[charset_arg_max
];
1090 Lisp_Object plist
[14];
1094 args
[charset_arg_name
] = name
;
1095 args
[charset_arg_dimension
] = make_number (dimension
);
1096 val
= Fmake_vector (make_number (8), make_number (0));
1097 for (i
= 0; i
< 8; i
++)
1098 ASET (val
, i
, make_number (code_space
[i
]));
1099 args
[charset_arg_code_space
] = val
;
1100 args
[charset_arg_min_code
] = make_number (min_code
);
1101 args
[charset_arg_max_code
] = make_number (max_code
);
1102 args
[charset_arg_iso_final
]
1103 = (iso_final
< 0 ? Qnil
: make_number (iso_final
));
1104 args
[charset_arg_iso_revision
] = make_number (iso_revision
);
1105 args
[charset_arg_emacs_mule_id
]
1106 = (emacs_mule_id
< 0 ? Qnil
: make_number (emacs_mule_id
));
1107 args
[charset_arg_ascii_compatible_p
] = ascii_compatible
? Qt
: Qnil
;
1108 args
[charset_arg_supplementary_p
] = supplementary
? Qt
: Qnil
;
1109 args
[charset_arg_invalid_code
] = Qnil
;
1110 args
[charset_arg_code_offset
] = make_number (code_offset
);
1111 args
[charset_arg_map
] = Qnil
;
1112 args
[charset_arg_subset
] = Qnil
;
1113 args
[charset_arg_superset
] = Qnil
;
1114 args
[charset_arg_unify_map
] = Qnil
;
1116 plist
[0] = intern (":name");
1117 plist
[1] = args
[charset_arg_name
];
1118 plist
[2] = intern (":dimension");
1119 plist
[3] = args
[charset_arg_dimension
];
1120 plist
[4] = intern (":code-space");
1121 plist
[5] = args
[charset_arg_code_space
];
1122 plist
[6] = intern (":iso-final-char");
1123 plist
[7] = args
[charset_arg_iso_final
];
1124 plist
[8] = intern (":emacs-mule-id");
1125 plist
[9] = args
[charset_arg_emacs_mule_id
];
1126 plist
[10] = intern (":ascii-compatible-p");
1127 plist
[11] = args
[charset_arg_ascii_compatible_p
];
1128 plist
[12] = intern (":code-offset");
1129 plist
[13] = args
[charset_arg_code_offset
];
1131 args
[charset_arg_plist
] = Flist (14, plist
);
1132 Fdefine_charset_internal (charset_arg_max
, args
);
1134 return XINT (CHARSET_SYMBOL_ID (name
));
1138 DEFUN ("define-charset-alias", Fdefine_charset_alias
,
1139 Sdefine_charset_alias
, 2, 2, 0,
1140 doc
: /* Define ALIAS as an alias for charset CHARSET. */)
1142 Lisp_Object alias
, charset
;
1146 CHECK_CHARSET_GET_ATTR (charset
, attr
);
1147 Fputhash (alias
, attr
, Vcharset_hash_table
);
1148 Vcharset_list
= Fcons (alias
, Vcharset_list
);
1153 DEFUN ("unibyte-charset", Funibyte_charset
, Sunibyte_charset
, 0, 0, 0,
1154 doc
: /* Return the unibyte charset (set by `set-unibyte-charset'). */)
1157 return CHARSET_NAME (CHARSET_FROM_ID (charset_unibyte
));
1161 DEFUN ("set-unibyte-charset", Fset_unibyte_charset
, Sset_unibyte_charset
,
1163 doc
: /* Set the unibyte charset to CHARSET.
1164 This determines how unibyte/multibyte conversion is done. See also
1165 function `unibyte-charset'. */)
1167 Lisp_Object charset
;
1172 CHECK_CHARSET_GET_CHARSET (charset
, cs
);
1173 if (! cs
->ascii_compatible_p
1174 || cs
->dimension
!= 1)
1175 error ("Inappropriate unibyte charset: %s", SDATA (SYMBOL_NAME (charset
)));
1176 charset_unibyte
= cs
->id
;
1177 memset (unibyte_has_multibyte_table
, 1, 128);
1178 for (i
= 128; i
< 256; i
++)
1180 c
= DECODE_CHAR (cs
, i
);
1181 unibyte_to_multibyte_table
[i
] = (c
< 0 ? BYTE8_TO_CHAR (i
) : c
);
1182 unibyte_has_multibyte_table
[i
] = c
>= 0;
1189 DEFUN ("charset-plist", Fcharset_plist
, Scharset_plist
, 1, 1, 0,
1190 doc
: /* Return the property list of CHARSET. */)
1192 Lisp_Object charset
;
1196 CHECK_CHARSET_GET_ATTR (charset
, attrs
);
1197 return CHARSET_ATTR_PLIST (attrs
);
1201 DEFUN ("set-charset-plist", Fset_charset_plist
, Sset_charset_plist
, 2, 2, 0,
1202 doc
: /* Set CHARSET's property list to PLIST. */)
1204 Lisp_Object charset
, plist
;
1208 CHECK_CHARSET_GET_ATTR (charset
, attrs
);
1209 CHARSET_ATTR_PLIST (attrs
) = plist
;
1214 DEFUN ("unify-charset", Funify_charset
, Sunify_charset
, 1, 3, 0,
1215 doc
: /* Unify characters of CHARSET with Unicode.
1216 This means reading the relevant file and installing the table defined
1217 by CHARSET's `:unify-map' property.
1219 Optional second arg UNIFY-MAP is a file name string or a vector. It has
1220 the same meaning as the `:unify-map' attribute in the function
1221 `define-charset' (which see).
1223 Optional third argument DEUNIFY, if non-nil, means to de-unify CHARSET. */)
1224 (charset
, unify_map
, deunify
)
1225 Lisp_Object charset
, unify_map
, deunify
;
1230 CHECK_CHARSET_GET_ID (charset
, id
);
1231 cs
= CHARSET_FROM_ID (id
);
1232 if (CHARSET_METHOD (cs
) == CHARSET_METHOD_MAP_DEFERRED
)
1235 ? CHARSET_UNIFIED_P (cs
) && ! NILP (CHARSET_DEUNIFIER (cs
))
1236 : ! CHARSET_UNIFIED_P (cs
))
1239 CHARSET_UNIFIED_P (cs
) = 0;
1242 if (CHARSET_METHOD (cs
) != CHARSET_METHOD_OFFSET
)
1243 error ("Can't unify charset: %s", SDATA (SYMBOL_NAME (charset
)));
1244 if (NILP (unify_map
))
1245 unify_map
= CHARSET_UNIFY_MAP (cs
);
1246 if (STRINGP (unify_map
))
1247 load_charset_map_from_file (cs
, unify_map
, 2);
1248 else if (VECTORP (unify_map
))
1249 load_charset_map_from_vector (cs
, unify_map
, 2);
1250 else if (NILP (unify_map
))
1251 error ("No unify-map for charset");
1253 error ("Bad unify-map arg");
1254 CHARSET_UNIFIED_P (cs
) = 1;
1256 else if (CHAR_TABLE_P (Vchar_unify_table
))
1258 int min_code
= CHARSET_MIN_CODE (cs
);
1259 int max_code
= CHARSET_MAX_CODE (cs
);
1260 int min_char
= DECODE_CHAR (cs
, min_code
);
1261 int max_char
= DECODE_CHAR (cs
, max_code
);
1263 char_table_set_range (Vchar_unify_table
, min_char
, max_char
, Qnil
);
1269 DEFUN ("get-unused-iso-final-char", Fget_unused_iso_final_char
,
1270 Sget_unused_iso_final_char
, 2, 2, 0,
1272 Return an unused ISO final char for a charset of DIMENISION and CHARS.
1273 DIMENSION is the number of bytes to represent a character: 1 or 2.
1274 CHARS is the number of characters in a dimension: 94 or 96.
1276 This final char is for private use, thus the range is `0' (48) .. `?' (63).
1277 If there's no unused final char for the specified kind of charset,
1280 Lisp_Object dimension
, chars
;
1284 CHECK_NUMBER (dimension
);
1285 CHECK_NUMBER (chars
);
1286 if (XINT (dimension
) != 1 && XINT (dimension
) != 2 && XINT (dimension
) != 3)
1287 args_out_of_range_3 (dimension
, make_number (1), make_number (3));
1288 if (XINT (chars
) != 94 && XINT (chars
) != 96)
1289 args_out_of_range_3 (chars
, make_number (94), make_number (96));
1290 for (final_char
= '0'; final_char
<= '?'; final_char
++)
1291 if (ISO_CHARSET_TABLE (XINT (dimension
), XINT (chars
), final_char
) < 0)
1293 return (final_char
<= '?' ? make_number (final_char
) : Qnil
);
1297 check_iso_charset_parameter (dimension
, chars
, final_char
)
1298 Lisp_Object dimension
, chars
, final_char
;
1300 CHECK_NATNUM (dimension
);
1301 CHECK_NATNUM (chars
);
1302 CHECK_NATNUM (final_char
);
1304 if (XINT (dimension
) > 3)
1305 error ("Invalid DIMENSION %d, it should be 1, 2, or 3", XINT (dimension
));
1306 if (XINT (chars
) != 94 && XINT (chars
) != 96)
1307 error ("Invalid CHARS %d, it should be 94 or 96", XINT (chars
));
1308 if (XINT (final_char
) < '0' || XINT (final_char
) > '~')
1309 error ("Invalid FINAL-CHAR %c, it should be `0'..`~'", XINT (chars
));
1313 DEFUN ("declare-equiv-charset", Fdeclare_equiv_charset
, Sdeclare_equiv_charset
,
1315 doc
: /* Declare an equivalent charset for ISO-2022 decoding.
1317 On decoding by an ISO-2022 base coding system, when a charset
1318 specified by DIMENSION, CHARS, and FINAL-CHAR is designated, behave as
1319 if CHARSET is designated instead. */)
1320 (dimension
, chars
, final_char
, charset
)
1321 Lisp_Object dimension
, chars
, final_char
, charset
;
1326 CHECK_CHARSET_GET_ID (charset
, id
);
1327 check_iso_charset_parameter (dimension
, chars
, final_char
);
1328 chars_flag
= XINT (chars
) == 96;
1329 ISO_CHARSET_TABLE (XINT (dimension
), chars_flag
, XINT (final_char
)) = id
;
1334 /* Return information about charsets in the text at PTR of NBYTES
1335 bytes, which are NCHARS characters. The value is:
1337 0: Each character is represented by one byte. This is always
1338 true for a unibyte string. For a multibyte string, true if
1339 it contains only ASCII characters.
1341 1: No charsets other than ascii, control-1, and latin-1 are
1348 string_xstring_p (string
)
1351 const unsigned char *p
= SDATA (string
);
1352 const unsigned char *endp
= p
+ SBYTES (string
);
1354 if (SCHARS (string
) == SBYTES (string
))
1359 int c
= STRING_CHAR_ADVANCE (p
);
1368 /* Find charsets in the string at PTR of NCHARS and NBYTES.
1370 CHARSETS is a vector. If Nth element is non-nil, it means the
1371 charset whose id is N is already found.
1373 It may lookup a translation table TABLE if supplied. */
1376 find_charsets_in_text (ptr
, nchars
, nbytes
, charsets
, table
, multibyte
)
1377 const unsigned char *ptr
;
1378 EMACS_INT nchars
, nbytes
;
1379 Lisp_Object charsets
, table
;
1382 const unsigned char *pend
= ptr
+ nbytes
;
1384 if (nchars
== nbytes
)
1387 ASET (charsets
, charset_ascii
, Qt
);
1394 c
= translate_char (table
, c
);
1395 if (ASCII_BYTE_P (c
))
1396 ASET (charsets
, charset_ascii
, Qt
);
1398 ASET (charsets
, charset_eight_bit
, Qt
);
1405 int c
= STRING_CHAR_ADVANCE (ptr
);
1406 struct charset
*charset
;
1409 c
= translate_char (table
, c
);
1410 charset
= CHAR_CHARSET (c
);
1411 ASET (charsets
, CHARSET_ID (charset
), Qt
);
1416 DEFUN ("find-charset-region", Ffind_charset_region
, Sfind_charset_region
,
1418 doc
: /* Return a list of charsets in the region between BEG and END.
1419 BEG and END are buffer positions.
1420 Optional arg TABLE if non-nil is a translation table to look up.
1422 If the current buffer is unibyte, the returned list may contain
1423 only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
1425 Lisp_Object beg
, end
, table
;
1427 Lisp_Object charsets
;
1428 EMACS_INT from
, from_byte
, to
, stop
, stop_byte
;
1431 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1433 validate_region (&beg
, &end
);
1434 from
= XFASTINT (beg
);
1435 stop
= to
= XFASTINT (end
);
1437 if (from
< GPT
&& GPT
< to
)
1440 stop_byte
= GPT_BYTE
;
1443 stop_byte
= CHAR_TO_BYTE (stop
);
1445 from_byte
= CHAR_TO_BYTE (from
);
1447 charsets
= Fmake_vector (make_number (charset_table_used
), Qnil
);
1450 find_charsets_in_text (BYTE_POS_ADDR (from_byte
), stop
- from
,
1451 stop_byte
- from_byte
, charsets
, table
,
1455 from
= stop
, from_byte
= stop_byte
;
1456 stop
= to
, stop_byte
= CHAR_TO_BYTE (stop
);
1463 for (i
= charset_table_used
- 1; i
>= 0; i
--)
1464 if (!NILP (AREF (charsets
, i
)))
1465 val
= Fcons (CHARSET_NAME (charset_table
+ i
), val
);
1469 DEFUN ("find-charset-string", Ffind_charset_string
, Sfind_charset_string
,
1471 doc
: /* Return a list of charsets in STR.
1472 Optional arg TABLE if non-nil is a translation table to look up.
1474 If STR is unibyte, the returned list may contain
1475 only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
1477 Lisp_Object str
, table
;
1479 Lisp_Object charsets
;
1485 charsets
= Fmake_vector (make_number (charset_table_used
), Qnil
);
1486 find_charsets_in_text (SDATA (str
), SCHARS (str
), SBYTES (str
),
1488 STRING_MULTIBYTE (str
));
1490 for (i
= charset_table_used
- 1; i
>= 0; i
--)
1491 if (!NILP (AREF (charsets
, i
)))
1492 val
= Fcons (CHARSET_NAME (charset_table
+ i
), val
);
1498 /* Return a character correponding to the code-point CODE of
1502 decode_char (charset
, code
)
1503 struct charset
*charset
;
1507 enum charset_method method
= CHARSET_METHOD (charset
);
1509 if (code
< CHARSET_MIN_CODE (charset
) || code
> CHARSET_MAX_CODE (charset
))
1512 if (method
== CHARSET_METHOD_MAP_DEFERRED
)
1514 load_charset (charset
);
1515 method
= CHARSET_METHOD (charset
);
1518 if (method
== CHARSET_METHOD_SUBSET
)
1520 Lisp_Object subset_info
;
1522 subset_info
= CHARSET_SUBSET (charset
);
1523 charset
= CHARSET_FROM_ID (XFASTINT (AREF (subset_info
, 0)));
1524 code
-= XINT (AREF (subset_info
, 3));
1525 if (code
< XFASTINT (AREF (subset_info
, 1))
1526 || code
> XFASTINT (AREF (subset_info
, 2)))
1529 c
= DECODE_CHAR (charset
, code
);
1531 else if (method
== CHARSET_METHOD_SUPERSET
)
1533 Lisp_Object parents
;
1535 parents
= CHARSET_SUPERSET (charset
);
1537 for (; CONSP (parents
); parents
= XCDR (parents
))
1539 int id
= XINT (XCAR (XCAR (parents
)));
1540 int code_offset
= XINT (XCDR (XCAR (parents
)));
1541 unsigned this_code
= code
- code_offset
;
1543 charset
= CHARSET_FROM_ID (id
);
1544 if ((c
= DECODE_CHAR (charset
, this_code
)) >= 0)
1550 char_index
= CODE_POINT_TO_INDEX (charset
, code
);
1554 if (method
== CHARSET_METHOD_MAP
)
1556 Lisp_Object decoder
;
1558 decoder
= CHARSET_DECODER (charset
);
1559 if (! VECTORP (decoder
))
1561 c
= XINT (AREF (decoder
, char_index
));
1565 c
= char_index
+ CHARSET_CODE_OFFSET (charset
);
1569 if (CHARSET_UNIFIED_P (charset
)
1572 MAYBE_UNIFY_CHAR (c
);
1578 /* Variable used temporarily by the macro ENCODE_CHAR. */
1579 Lisp_Object charset_work
;
1581 /* Return a code-point of CHAR in CHARSET. If CHAR doesn't belong to
1582 CHARSET, return CHARSET_INVALID_CODE (CHARSET). If STRICT is true,
1583 use CHARSET's strict_max_char instead of max_char. */
1586 encode_char (charset
, c
)
1587 struct charset
*charset
;
1591 enum charset_method method
= CHARSET_METHOD (charset
);
1593 if (CHARSET_UNIFIED_P (charset
))
1595 Lisp_Object deunifier
, deunified
;
1597 deunifier
= CHARSET_DEUNIFIER (charset
);
1598 if (! CHAR_TABLE_P (deunifier
))
1600 Funify_charset (CHARSET_NAME (charset
), Qnil
, Qnil
);
1601 deunifier
= CHARSET_DEUNIFIER (charset
);
1603 deunified
= CHAR_TABLE_REF (deunifier
, c
);
1604 if (! NILP (deunified
))
1605 c
= XINT (deunified
);
1608 if (method
== CHARSET_METHOD_SUBSET
)
1610 Lisp_Object subset_info
;
1611 struct charset
*this_charset
;
1613 subset_info
= CHARSET_SUBSET (charset
);
1614 this_charset
= CHARSET_FROM_ID (XFASTINT (AREF (subset_info
, 0)));
1615 code
= ENCODE_CHAR (this_charset
, c
);
1616 if (code
== CHARSET_INVALID_CODE (this_charset
)
1617 || code
< XFASTINT (AREF (subset_info
, 1))
1618 || code
> XFASTINT (AREF (subset_info
, 2)))
1619 return CHARSET_INVALID_CODE (charset
);
1620 code
+= XINT (AREF (subset_info
, 3));
1624 if (method
== CHARSET_METHOD_SUPERSET
)
1626 Lisp_Object parents
;
1628 parents
= CHARSET_SUPERSET (charset
);
1629 for (; CONSP (parents
); parents
= XCDR (parents
))
1631 int id
= XINT (XCAR (XCAR (parents
)));
1632 int code_offset
= XINT (XCDR (XCAR (parents
)));
1633 struct charset
*this_charset
= CHARSET_FROM_ID (id
);
1635 code
= ENCODE_CHAR (this_charset
, c
);
1636 if (code
!= CHARSET_INVALID_CODE (this_charset
))
1637 return code
+ code_offset
;
1639 return CHARSET_INVALID_CODE (charset
);
1642 if (! CHARSET_FAST_MAP_REF ((c
), charset
->fast_map
)
1643 || c
< CHARSET_MIN_CHAR (charset
) || c
> CHARSET_MAX_CHAR (charset
))
1644 return CHARSET_INVALID_CODE (charset
);
1646 if (method
== CHARSET_METHOD_MAP_DEFERRED
)
1648 load_charset (charset
);
1649 method
= CHARSET_METHOD (charset
);
1652 if (method
== CHARSET_METHOD_MAP
)
1654 Lisp_Object encoder
;
1657 encoder
= CHARSET_ENCODER (charset
);
1658 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset
)))
1659 return CHARSET_INVALID_CODE (charset
);
1660 val
= CHAR_TABLE_REF (encoder
, c
);
1662 return CHARSET_INVALID_CODE (charset
);
1664 if (! CHARSET_COMPACT_CODES_P (charset
))
1665 code
= INDEX_TO_CODE_POINT (charset
, code
);
1667 else /* method == CHARSET_METHOD_OFFSET */
1669 code
= c
- CHARSET_CODE_OFFSET (charset
);
1670 code
= INDEX_TO_CODE_POINT (charset
, code
);
1677 DEFUN ("decode-char", Fdecode_char
, Sdecode_char
, 2, 3, 0,
1678 doc
: /* Decode the pair of CHARSET and CODE-POINT into a character.
1679 Return nil if CODE-POINT is not valid in CHARSET.
1681 CODE-POINT may be a cons (HIGHER-16-BIT-VALUE . LOWER-16-BIT-VALUE).
1683 Optional argument RESTRICTION specifies a way to map the pair of CCS
1684 and CODE-POINT to a chracter. Currently not supported and just ignored. */)
1685 (charset
, code_point
, restriction
)
1686 Lisp_Object charset
, code_point
, restriction
;
1690 struct charset
*charsetp
;
1692 CHECK_CHARSET_GET_ID (charset
, id
);
1693 if (CONSP (code_point
))
1695 CHECK_NATNUM_CAR (code_point
);
1696 CHECK_NATNUM_CDR (code_point
);
1697 code
= (XINT (XCAR (code_point
)) << 16) | (XINT (XCDR (code_point
)));
1701 CHECK_NATNUM (code_point
);
1702 code
= XINT (code_point
);
1704 charsetp
= CHARSET_FROM_ID (id
);
1705 c
= DECODE_CHAR (charsetp
, code
);
1706 return (c
>= 0 ? make_number (c
) : Qnil
);
1710 DEFUN ("encode-char", Fencode_char
, Sencode_char
, 2, 3, 0,
1711 doc
: /* Encode the character CH into a code-point of CHARSET.
1712 Return nil if CHARSET doesn't include CH.
1714 Optional argument RESTRICTION specifies a way to map CHAR to a
1715 code-point in CCS. Currently not supported and just ignored. */)
1716 (ch
, charset
, restriction
)
1717 Lisp_Object ch
, charset
, restriction
;
1721 struct charset
*charsetp
;
1723 CHECK_CHARSET_GET_ID (charset
, id
);
1725 charsetp
= CHARSET_FROM_ID (id
);
1726 code
= ENCODE_CHAR (charsetp
, XINT (ch
));
1727 if (code
== CHARSET_INVALID_CODE (charsetp
))
1729 if (code
> 0x7FFFFFF)
1730 return Fcons (make_number (code
>> 16), make_number (code
& 0xFFFF));
1731 return make_number (code
);
1735 DEFUN ("make-char", Fmake_char
, Smake_char
, 1, 5, 0,
1737 /* Return a character of CHARSET whose position codes are CODEn.
1739 CODE1 through CODE4 are optional, but if you don't supply sufficient
1740 position codes, it is assumed that the minimum code in each dimension
1742 (charset
, code1
, code2
, code3
, code4
)
1743 Lisp_Object charset
, code1
, code2
, code3
, code4
;
1746 struct charset
*charsetp
;
1750 CHECK_CHARSET_GET_ID (charset
, id
);
1751 charsetp
= CHARSET_FROM_ID (id
);
1753 dimension
= CHARSET_DIMENSION (charsetp
);
1755 code
= (CHARSET_ASCII_COMPATIBLE_P (charsetp
)
1756 ? 0 : CHARSET_MIN_CODE (charsetp
));
1759 CHECK_NATNUM (code1
);
1760 if (XFASTINT (code1
) >= 0x100)
1761 args_out_of_range (make_number (0xFF), code1
);
1762 code
= XFASTINT (code1
);
1768 code
|= charsetp
->code_space
[(dimension
- 2) * 4];
1771 CHECK_NATNUM (code2
);
1772 if (XFASTINT (code2
) >= 0x100)
1773 args_out_of_range (make_number (0xFF), code2
);
1774 code
|= XFASTINT (code2
);
1781 code
|= charsetp
->code_space
[(dimension
- 3) * 4];
1784 CHECK_NATNUM (code3
);
1785 if (XFASTINT (code3
) >= 0x100)
1786 args_out_of_range (make_number (0xFF), code3
);
1787 code
|= XFASTINT (code3
);
1794 code
|= charsetp
->code_space
[0];
1797 CHECK_NATNUM (code4
);
1798 if (XFASTINT (code4
) >= 0x100)
1799 args_out_of_range (make_number (0xFF), code4
);
1800 code
|= XFASTINT (code4
);
1807 if (CHARSET_ISO_FINAL (charsetp
) >= 0)
1809 c
= DECODE_CHAR (charsetp
, code
);
1811 error ("Invalid code(s)");
1812 return make_number (c
);
1816 /* Return the first charset in CHARSET_LIST that contains C.
1817 CHARSET_LIST is a list of charset IDs. If it is nil, use
1818 Vcharset_ordered_list. */
1821 char_charset (c
, charset_list
, code_return
)
1823 Lisp_Object charset_list
;
1824 unsigned *code_return
;
1826 if (NILP (charset_list
))
1827 charset_list
= Vcharset_ordered_list
;
1829 while (CONSP (charset_list
))
1831 struct charset
*charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
1832 unsigned code
= ENCODE_CHAR (charset
, c
);
1834 if (code
!= CHARSET_INVALID_CODE (charset
))
1837 *code_return
= code
;
1840 charset_list
= XCDR (charset_list
);
1846 DEFUN ("split-char", Fsplit_char
, Ssplit_char
, 1, 1, 0,
1848 /*Return list of charset and one to four position-codes of CHAR.
1849 The charset is decided by the current priority order of charsets.
1850 A position-code is a byte value of each dimension of the code-point of
1851 CHAR in the charset. */)
1855 struct charset
*charset
;
1860 CHECK_CHARACTER (ch
);
1862 charset
= CHAR_CHARSET (c
);
1865 code
= ENCODE_CHAR (charset
, c
);
1866 if (code
== CHARSET_INVALID_CODE (charset
))
1868 dimension
= CHARSET_DIMENSION (charset
);
1869 for (val
= Qnil
; dimension
> 0; dimension
--)
1871 val
= Fcons (make_number (code
& 0xFF), val
);
1874 return Fcons (CHARSET_NAME (charset
), val
);
1878 DEFUN ("char-charset", Fchar_charset
, Schar_charset
, 1, 1, 0,
1879 doc
: /* Return the charset of highest priority that contains CH. */)
1883 struct charset
*charset
;
1885 CHECK_CHARACTER (ch
);
1886 charset
= CHAR_CHARSET (XINT (ch
));
1887 return (CHARSET_NAME (charset
));
1891 DEFUN ("charset-after", Fcharset_after
, Scharset_after
, 0, 1, 0,
1893 Return charset of a character in the current buffer at position POS.
1894 If POS is nil, it defauls to the current point.
1895 If POS is out of range, the value is nil. */)
1900 struct charset
*charset
;
1902 ch
= Fchar_after (pos
);
1903 if (! INTEGERP (ch
))
1905 charset
= CHAR_CHARSET (XINT (ch
));
1906 return (CHARSET_NAME (charset
));
1910 DEFUN ("iso-charset", Fiso_charset
, Siso_charset
, 3, 3, 0,
1912 Return charset of ISO's specification DIMENSION, CHARS, and FINAL-CHAR.
1914 ISO 2022's designation sequence (escape sequence) distinguishes charsets
1915 by their DIMENSION, CHARS, and FINAL-CHAR,
1916 where as Emacs distinguishes them by charset symbol.
1917 See the documentation of the function `charset-info' for the meanings of
1918 DIMENSION, CHARS, and FINAL-CHAR. */)
1919 (dimension
, chars
, final_char
)
1920 Lisp_Object dimension
, chars
, final_char
;
1925 check_iso_charset_parameter (dimension
, chars
, final_char
);
1926 chars_flag
= XFASTINT (chars
) == 96;
1927 id
= ISO_CHARSET_TABLE (XFASTINT (dimension
), chars_flag
,
1928 XFASTINT (final_char
));
1929 return (id
>= 0 ? CHARSET_NAME (CHARSET_FROM_ID (id
)) : Qnil
);
1933 DEFUN ("clear-charset-maps", Fclear_charset_maps
, Sclear_charset_maps
,
1936 Clear encoder and decoder of charsets that are loaded from mapfiles. */)
1940 struct charset
*charset
;
1943 for (i
= 0; i
< charset_table_used
; i
++)
1945 charset
= CHARSET_FROM_ID (i
);
1946 attrs
= CHARSET_ATTRIBUTES (charset
);
1948 if (CHARSET_METHOD (charset
) == CHARSET_METHOD_MAP
)
1950 CHARSET_ATTR_DECODER (attrs
) = Qnil
;
1951 CHARSET_ATTR_ENCODER (attrs
) = Qnil
;
1952 CHARSET_METHOD (charset
) = CHARSET_METHOD_MAP_DEFERRED
;
1955 if (CHARSET_UNIFIED_P (charset
))
1956 CHARSET_ATTR_DEUNIFIER (attrs
) = Qnil
;
1959 if (CHAR_TABLE_P (Vchar_unified_charset_table
))
1961 Foptimize_char_table (Vchar_unified_charset_table
);
1962 Vchar_unify_table
= Vchar_unified_charset_table
;
1963 Vchar_unified_charset_table
= Qnil
;
1969 DEFUN ("charset-priority-list", Fcharset_priority_list
,
1970 Scharset_priority_list
, 0, 1, 0,
1971 doc
: /* Return the list of charsets ordered by priority.
1972 HIGHESTP non-nil means just return the highest priority one. */)
1974 Lisp_Object highestp
;
1976 Lisp_Object val
= Qnil
, list
= Vcharset_ordered_list
;
1978 if (!NILP (highestp
))
1979 return CHARSET_NAME (CHARSET_FROM_ID (XINT (Fcar (list
))));
1981 while (!NILP (list
))
1983 val
= Fcons (CHARSET_NAME (CHARSET_FROM_ID (XINT (XCAR (list
)))), val
);
1986 return Fnreverse (val
);
1989 DEFUN ("set-charset-priority", Fset_charset_priority
, Sset_charset_priority
,
1991 doc
: /* Assign higher priority to the charsets given as arguments.
1992 usage: (set-charset-priority &rest charsets) */)
1997 Lisp_Object new_head
, old_list
, arglist
[2];
1998 Lisp_Object list_2022
, list_emacs_mule
;
2001 old_list
= Fcopy_sequence (Vcharset_ordered_list
);
2003 for (i
= 0; i
< nargs
; i
++)
2005 CHECK_CHARSET_GET_ID (args
[i
], id
);
2006 if (! NILP (Fmemq (make_number (id
), old_list
)))
2008 old_list
= Fdelq (make_number (id
), old_list
);
2009 new_head
= Fcons (make_number (id
), new_head
);
2012 arglist
[0] = Fnreverse (new_head
);
2013 arglist
[1] = old_list
;
2014 Vcharset_ordered_list
= Fnconc (2, arglist
);
2015 charset_ordered_list_tick
++;
2017 for (old_list
= Vcharset_ordered_list
, list_2022
= list_emacs_mule
= Qnil
;
2018 CONSP (old_list
); old_list
= XCDR (old_list
))
2020 if (! NILP (Fmemq (XCAR (old_list
), Viso_2022_charset_list
)))
2021 list_2022
= Fcons (XCAR (old_list
), list_2022
);
2022 if (! NILP (Fmemq (XCAR (old_list
), Vemacs_mule_charset_list
)))
2023 list_emacs_mule
= Fcons (XCAR (old_list
), list_emacs_mule
);
2025 Viso_2022_charset_list
= Fnreverse (list_2022
);
2026 Vemacs_mule_charset_list
= Fnreverse (list_emacs_mule
);
2031 DEFUN ("charset-id-internal", Fcharset_id_internal
, Scharset_id_internal
,
2033 doc
: /* Internal use only.
2034 Return charset identification number of CHARSET. */)
2036 Lisp_Object charset
;
2040 CHECK_CHARSET_GET_ID (charset
, id
);
2041 return make_number (id
);
2049 = Fcons (Fexpand_file_name (build_string ("charsets"), Vdata_directory
),
2055 init_charset_once ()
2059 for (i
= 0; i
< ISO_MAX_DIMENSION
; i
++)
2060 for (j
= 0; j
< ISO_MAX_CHARS
; j
++)
2061 for (k
= 0; k
< ISO_MAX_FINAL
; k
++)
2062 iso_charset_table
[i
][j
][k
] = -1;
2064 for (i
= 0; i
< 256; i
++)
2065 emacs_mule_charset
[i
] = NULL
;
2067 charset_jisx0201_roman
= -1;
2068 charset_jisx0208_1978
= -1;
2069 charset_jisx0208
= -1;
2071 for (i
= 0; i
< 128; i
++)
2072 unibyte_to_multibyte_table
[i
] = i
;
2073 for (; i
< 256; i
++)
2074 unibyte_to_multibyte_table
[i
] = BYTE8_TO_CHAR (i
);
2082 DEFSYM (Qcharsetp
, "charsetp");
2084 DEFSYM (Qascii
, "ascii");
2085 DEFSYM (Qunicode
, "unicode");
2086 DEFSYM (Qeight_bit
, "eight-bit");
2087 DEFSYM (Qiso_8859_1
, "iso-8859-1");
2092 staticpro (&Vcharset_ordered_list
);
2093 Vcharset_ordered_list
= Qnil
;
2095 staticpro (&Viso_2022_charset_list
);
2096 Viso_2022_charset_list
= Qnil
;
2098 staticpro (&Vemacs_mule_charset_list
);
2099 Vemacs_mule_charset_list
= Qnil
;
2101 staticpro (&Vcharset_hash_table
);
2103 Lisp_Object args
[2];
2106 Vcharset_hash_table
= Fmake_hash_table (2, args
);
2109 charset_table_size
= 128;
2110 charset_table
= ((struct charset
*)
2111 xmalloc (sizeof (struct charset
) * charset_table_size
));
2112 charset_table_used
= 0;
2114 staticpro (&Vchar_unified_charset_table
);
2115 Vchar_unified_charset_table
= Fmake_char_table (Qnil
, make_number (-1));
2117 defsubr (&Scharsetp
);
2118 defsubr (&Smap_charset_chars
);
2119 defsubr (&Sdefine_charset_internal
);
2120 defsubr (&Sdefine_charset_alias
);
2121 defsubr (&Sunibyte_charset
);
2122 defsubr (&Sset_unibyte_charset
);
2123 defsubr (&Scharset_plist
);
2124 defsubr (&Sset_charset_plist
);
2125 defsubr (&Sunify_charset
);
2126 defsubr (&Sget_unused_iso_final_char
);
2127 defsubr (&Sdeclare_equiv_charset
);
2128 defsubr (&Sfind_charset_region
);
2129 defsubr (&Sfind_charset_string
);
2130 defsubr (&Sdecode_char
);
2131 defsubr (&Sencode_char
);
2132 defsubr (&Ssplit_char
);
2133 defsubr (&Smake_char
);
2134 defsubr (&Schar_charset
);
2135 defsubr (&Scharset_after
);
2136 defsubr (&Siso_charset
);
2137 defsubr (&Sclear_charset_maps
);
2138 defsubr (&Scharset_priority_list
);
2139 defsubr (&Sset_charset_priority
);
2140 defsubr (&Scharset_id_internal
);
2142 DEFVAR_LISP ("charset-map-path", &Vcharset_map_path
,
2143 doc
: /* *Lisp of directories to search for charset map files. */);
2144 Vcharset_map_path
= Qnil
;
2146 DEFVAR_LISP ("charset-list", &Vcharset_list
,
2147 doc
: /* List of all charsets ever defined. */);
2148 Vcharset_list
= Qnil
;
2151 = define_charset_internal (Qascii
, 1, "\x00\x7F\x00\x00\x00\x00",
2152 0, 127, 'B', -1, 0, 1, 0, 0);
2154 = define_charset_internal (Qiso_8859_1
, 1, "\x00\xFF\x00\x00\x00\x00",
2155 0, 255, -1, -1, -1, 1, 0, 0);
2157 = define_charset_internal (Qunicode
, 3, "\x00\xFF\x00\xFF\x00\x10",
2158 0, MAX_UNICODE_CHAR
, -1, 0, -1, 1, 0, 0);
2160 = define_charset_internal (Qeight_bit
, 1, "\x80\xFF\x00\x00\x00\x00",
2161 128, 255, -1, 0, -1, 0, 0,
2162 MAX_5_BYTE_CHAR
+ 1);
2167 /* arch-tag: 66a89b8d-4c28-47d3-9ca1-56f78440d69f
2168 (do not change this comment) */