1 /* Basic character set support.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
9 Copyright (C) 2003, 2004
10 National Institute of Advanced Industrial Science and Technology (AIST)
11 Registration Number H13PRO009
13 This file is part of GNU Emacs.
15 GNU Emacs is free software: you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
20 GNU Emacs is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
33 #include <sys/types.h>
35 #include "character.h"
41 /*** GENERAL NOTES on CODED CHARACTER SETS (CHARSETS) ***
43 A coded character set ("charset" hereafter) is a meaningful
44 collection (i.e. language, culture, functionality, etc.) of
45 characters. Emacs handles multiple charsets at once. In Emacs Lisp
46 code, a charset is represented by a symbol. In C code, a charset is
47 represented by its ID number or by a pointer to a struct charset.
49 The actual information about each charset is stored in two places.
50 Lispy information is stored in the hash table Vcharset_hash_table as
51 a vector (charset attributes). The other information is stored in
52 charset_table as a struct charset.
56 /* List of all charsets. This variable is used only from Emacs
58 Lisp_Object Vcharset_list
;
60 /* Hash table that contains attributes of each charset. Keys are
61 charset symbols, and values are vectors of charset attributes. */
62 Lisp_Object Vcharset_hash_table
;
64 /* Table of struct charset. */
65 struct charset
*charset_table
;
67 static int charset_table_size
;
68 static int charset_table_used
;
70 Lisp_Object Qcharsetp
;
72 /* Special charset symbols. */
74 Lisp_Object Qeight_bit
;
75 Lisp_Object Qiso_8859_1
;
79 /* The corresponding charsets. */
81 int charset_eight_bit
;
82 int charset_iso_8859_1
;
86 /* The other special charsets. */
87 int charset_jisx0201_roman
;
88 int charset_jisx0208_1978
;
91 /* Value of charset attribute `charset-iso-plane'. */
94 /* Charset of unibyte characters. */
97 /* List of charsets ordered by the priority. */
98 Lisp_Object Vcharset_ordered_list
;
100 /* Sub-list of Vcharset_ordered_list that contains all non-preferred
102 Lisp_Object Vcharset_non_preferred_head
;
104 /* Incremented everytime we change Vcharset_ordered_list. This is
105 unsigned short so that it fits in Lisp_Int and never matches
107 unsigned short charset_ordered_list_tick
;
109 /* List of iso-2022 charsets. */
110 Lisp_Object Viso_2022_charset_list
;
112 /* List of emacs-mule charsets. */
113 Lisp_Object Vemacs_mule_charset_list
;
115 struct charset
*emacs_mule_charset
[256];
117 /* Mapping table from ISO2022's charset (specified by DIMENSION,
118 CHARS, and FINAL-CHAR) to Emacs' charset. */
119 int iso_charset_table
[ISO_MAX_DIMENSION
][ISO_MAX_CHARS
][ISO_MAX_FINAL
];
121 Lisp_Object Vcharset_map_path
;
123 Lisp_Object Vchar_unified_charset_table
;
125 Lisp_Object Vcurrent_iso639_language
;
127 /* Defined in chartab.c */
129 map_char_table_for_charset
P_ ((void (*c_function
) (Lisp_Object
, Lisp_Object
),
130 Lisp_Object function
, Lisp_Object table
,
131 Lisp_Object arg
, struct charset
*charset
,
132 unsigned from
, unsigned to
));
134 #define CODE_POINT_TO_INDEX(charset, code) \
135 ((charset)->code_linear_p \
136 ? (code) - (charset)->min_code \
137 : (((charset)->code_space_mask[(code) >> 24] & 0x8) \
138 && ((charset)->code_space_mask[((code) >> 16) & 0xFF] & 0x4) \
139 && ((charset)->code_space_mask[((code) >> 8) & 0xFF] & 0x2) \
140 && ((charset)->code_space_mask[(code) & 0xFF] & 0x1)) \
141 ? (((((code) >> 24) - (charset)->code_space[12]) \
142 * (charset)->code_space[11]) \
143 + (((((code) >> 16) & 0xFF) - (charset)->code_space[8]) \
144 * (charset)->code_space[7]) \
145 + (((((code) >> 8) & 0xFF) - (charset)->code_space[4]) \
146 * (charset)->code_space[3]) \
147 + (((code) & 0xFF) - (charset)->code_space[0]) \
148 - ((charset)->char_index_offset)) \
152 /* Convert the character index IDX to code-point CODE for CHARSET.
153 It is assumed that IDX is in a valid range. */
155 #define INDEX_TO_CODE_POINT(charset, idx) \
156 ((charset)->code_linear_p \
157 ? (idx) + (charset)->min_code \
158 : (idx += (charset)->char_index_offset, \
159 (((charset)->code_space[0] + (idx) % (charset)->code_space[2]) \
160 | (((charset)->code_space[4] \
161 + ((idx) / (charset)->code_space[3] % (charset)->code_space[6])) \
163 | (((charset)->code_space[8] \
164 + ((idx) / (charset)->code_space[7] % (charset)->code_space[10])) \
166 | (((charset)->code_space[12] + ((idx) / (charset)->code_space[11])) \
172 /* Set to 1 to warn that a charset map is loaded and thus a buffer
173 text and a string data may be relocated. */
174 int charset_map_loaded
;
176 struct charset_map_entries
182 struct charset_map_entries
*next
;
185 /* Load the mapping information for CHARSET from ENTRIES.
187 If CONTROL_FLAG is 0, setup CHARSET->min_char and CHARSET->max_char.
189 If CONTROL_FLAG is 1, setup CHARSET->min_char, CHARSET->max_char,
190 CHARSET->decoder, and CHARSET->encoder.
192 If CONTROL_FLAG is 2, setup CHARSET->deunifier and
193 Vchar_unify_table. If Vchar_unified_charset_table is non-nil,
197 load_charset_map (charset
, entries
, n_entries
, control_flag
)
198 struct charset
*charset
;
199 struct charset_map_entries
*entries
;
203 Lisp_Object vec
, table
;
204 unsigned max_code
= CHARSET_MAX_CODE (charset
);
205 int ascii_compatible_p
= charset
->ascii_compatible_p
;
206 int min_char
, max_char
, nonascii_min_char
;
208 unsigned char *fast_map
= charset
->fast_map
;
213 if (control_flag
> 0)
215 int n
= CODE_POINT_TO_INDEX (charset
, max_code
) + 1;
217 table
= Fmake_char_table (Qnil
, Qnil
);
218 if (control_flag
== 1)
219 vec
= Fmake_vector (make_number (n
), make_number (-1));
220 else if (! CHAR_TABLE_P (Vchar_unify_table
))
221 Vchar_unify_table
= Fmake_char_table (Qnil
, Qnil
);
223 charset_map_loaded
= 1;
226 min_char
= max_char
= entries
->entry
[0].c
;
227 nonascii_min_char
= MAX_CHAR
;
228 for (i
= 0; i
< n_entries
; i
++)
231 int from_index
, to_index
;
233 int idx
= i
% 0x10000;
235 if (i
> 0 && idx
== 0)
236 entries
= entries
->next
;
237 from
= entries
->entry
[idx
].from
;
238 to
= entries
->entry
[idx
].to
;
239 from_c
= entries
->entry
[idx
].c
;
240 from_index
= CODE_POINT_TO_INDEX (charset
, from
);
243 to_index
= from_index
;
248 to_index
= CODE_POINT_TO_INDEX (charset
, to
);
249 to_c
= from_c
+ (to_index
- from_index
);
251 if (from_index
< 0 || to_index
< 0)
254 if (control_flag
< 2)
260 else if (from_c
< min_char
)
262 if (ascii_compatible_p
)
264 if (! ASCII_BYTE_P (from_c
))
266 if (from_c
< nonascii_min_char
)
267 nonascii_min_char
= from_c
;
269 else if (! ASCII_BYTE_P (to_c
))
271 nonascii_min_char
= 0x80;
275 for (c
= from_c
; c
<= to_c
; c
++)
276 CHARSET_FAST_MAP_SET (c
, fast_map
);
278 if (control_flag
== 1)
280 unsigned code
= from
;
282 if (CHARSET_COMPACT_CODES_P (charset
))
285 ASET (vec
, from_index
, make_number (from_c
));
286 if (NILP (CHAR_TABLE_REF (table
, from_c
)))
287 CHAR_TABLE_SET (table
, from_c
, make_number (code
));
288 if (from_index
== to_index
)
290 from_index
++, from_c
++;
291 code
= INDEX_TO_CODE_POINT (charset
, from_index
);
294 for (; from_index
<= to_index
; from_index
++, from_c
++)
296 ASET (vec
, from_index
, make_number (from_c
));
297 if (NILP (CHAR_TABLE_REF (table
, from_c
)))
298 CHAR_TABLE_SET (table
, from_c
, make_number (from_index
));
304 unsigned code
= from
;
308 int c1
= DECODE_CHAR (charset
, code
);
312 CHAR_TABLE_SET (table
, from_c
, make_number (c1
));
313 CHAR_TABLE_SET (Vchar_unify_table
, c1
, make_number (from_c
));
314 if (CHAR_TABLE_P (Vchar_unified_charset_table
))
315 CHAR_TABLE_SET (Vchar_unified_charset_table
, c1
,
316 CHARSET_NAME (charset
));
318 if (from_index
== to_index
)
320 from_index
++, from_c
++;
321 code
= INDEX_TO_CODE_POINT (charset
, from_index
);
326 if (control_flag
< 2)
328 CHARSET_MIN_CHAR (charset
) = (ascii_compatible_p
329 ? nonascii_min_char
: min_char
);
330 CHARSET_MAX_CHAR (charset
) = max_char
;
331 if (control_flag
== 1)
333 CHARSET_DECODER (charset
) = vec
;
334 CHARSET_ENCODER (charset
) = table
;
338 CHARSET_DEUNIFIER (charset
) = table
;
342 /* Read a hexadecimal number (preceded by "0x") from the file FP while
343 paying attention to comment charcter '#'. */
345 static INLINE
unsigned
353 while ((c
= getc (fp
)) != EOF
)
357 while ((c
= getc (fp
)) != EOF
&& c
!= '\n');
361 if ((c
= getc (fp
)) == EOF
|| c
== 'x')
373 while ((c
= getc (fp
)) != EOF
&& isxdigit (c
))
375 | (c
<= '9' ? c
- '0' : c
<= 'F' ? c
- 'A' + 10 : c
- 'a' + 10));
377 while ((c
= getc (fp
)) != EOF
&& isdigit (c
))
378 n
= (n
* 10) + c
- '0';
385 /* Return a mapping vector for CHARSET loaded from MAPFILE.
386 Each line of MAPFILE has this form
388 where 0xAAAA is a code-point and 0xCCCC is the corresponding
389 character code, or this form
391 where 0xAAAA and 0xBBBB are code-points specifying a range, and
392 0xCCCC is the first character code of the range.
394 The returned vector has this form:
395 [ CODE1 CHAR1 CODE2 CHAR2 .... ]
396 where CODE1 is a code-point or a cons of code-points specifying a
399 extern void add_to_log
P_ ((char *, Lisp_Object
, Lisp_Object
));
402 load_charset_map_from_file (charset
, mapfile
, control_flag
)
403 struct charset
*charset
;
407 unsigned min_code
= CHARSET_MIN_CODE (charset
);
408 unsigned max_code
= CHARSET_MAX_CODE (charset
);
412 Lisp_Object suffixes
;
413 struct charset_map_entries
*head
, *entries
;
416 suffixes
= Fcons (build_string (".map"),
417 Fcons (build_string (".TXT"), Qnil
));
419 fd
= openp (Vcharset_map_path
, mapfile
, suffixes
, NULL
, Qnil
);
421 || ! (fp
= fdopen (fd
, "r")))
423 add_to_log ("Failure in loading charset map: %S", mapfile
, Qnil
);
427 head
= entries
= ((struct charset_map_entries
*)
428 alloca (sizeof (struct charset_map_entries
)));
437 from
= read_hex (fp
, &eof
);
440 if (getc (fp
) == '-')
441 to
= read_hex (fp
, &eof
);
444 c
= (int) read_hex (fp
, &eof
);
446 if (from
< min_code
|| to
> max_code
|| from
> to
|| c
> MAX_CHAR
)
449 if (n_entries
> 0 && (n_entries
% 0x10000) == 0)
451 entries
->next
= ((struct charset_map_entries
*)
452 alloca (sizeof (struct charset_map_entries
)));
453 entries
= entries
->next
;
455 idx
= n_entries
% 0x10000;
456 entries
->entry
[idx
].from
= from
;
457 entries
->entry
[idx
].to
= to
;
458 entries
->entry
[idx
].c
= c
;
464 load_charset_map (charset
, head
, n_entries
, control_flag
);
468 load_charset_map_from_vector (charset
, vec
, control_flag
)
469 struct charset
*charset
;
473 unsigned min_code
= CHARSET_MIN_CODE (charset
);
474 unsigned max_code
= CHARSET_MAX_CODE (charset
);
475 struct charset_map_entries
*head
, *entries
;
477 int len
= ASIZE (vec
);
482 add_to_log ("Failure in loading charset map: %V", vec
, Qnil
);
486 head
= entries
= ((struct charset_map_entries
*)
487 alloca (sizeof (struct charset_map_entries
)));
489 for (i
= 0; i
< len
; i
+= 2)
491 Lisp_Object val
, val2
;
503 from
= XFASTINT (val
);
504 to
= XFASTINT (val2
);
509 from
= to
= XFASTINT (val
);
511 val
= AREF (vec
, i
+ 1);
515 if (from
< min_code
|| to
> max_code
|| from
> to
|| c
> MAX_CHAR
)
518 if (n_entries
> 0 && (n_entries
% 0x10000) == 0)
520 entries
->next
= ((struct charset_map_entries
*)
521 alloca (sizeof (struct charset_map_entries
)));
522 entries
= entries
->next
;
524 idx
= n_entries
% 0x10000;
525 entries
->entry
[idx
].from
= from
;
526 entries
->entry
[idx
].to
= to
;
527 entries
->entry
[idx
].c
= c
;
531 load_charset_map (charset
, head
, n_entries
, control_flag
);
535 load_charset (charset
)
536 struct charset
*charset
;
538 if (CHARSET_METHOD (charset
) == CHARSET_METHOD_MAP_DEFERRED
)
542 map
= CHARSET_MAP (charset
);
544 load_charset_map_from_file (charset
, map
, 1);
546 load_charset_map_from_vector (charset
, map
, 1);
547 CHARSET_METHOD (charset
) = CHARSET_METHOD_MAP
;
552 DEFUN ("charsetp", Fcharsetp
, Scharsetp
, 1, 1, 0,
553 doc
: /* Return non-nil if and only if OBJECT is a charset.*/)
557 return (CHARSETP (object
) ? Qt
: Qnil
);
562 map_charset_chars (c_function
, function
, arg
,
564 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
));
565 Lisp_Object function
, arg
;
566 struct charset
*charset
;
572 if (CHARSET_METHOD (charset
) == CHARSET_METHOD_MAP_DEFERRED
)
573 load_charset (charset
);
575 partial
= (from
> CHARSET_MIN_CODE (charset
)
576 || to
< CHARSET_MAX_CODE (charset
));
578 if (CHARSET_UNIFIED_P (charset
)
579 && CHAR_TABLE_P (CHARSET_DEUNIFIER (charset
)))
581 map_char_table_for_charset (c_function
, function
,
582 CHARSET_DEUNIFIER (charset
), arg
,
583 partial
? charset
: NULL
, from
, to
);
586 if (CHARSET_METHOD (charset
) == CHARSET_METHOD_OFFSET
)
588 int from_idx
= CODE_POINT_TO_INDEX (charset
, from
);
589 int to_idx
= CODE_POINT_TO_INDEX (charset
, to
);
590 int from_c
= from_idx
+ CHARSET_CODE_OFFSET (charset
);
591 int to_c
= to_idx
+ CHARSET_CODE_OFFSET (charset
);
593 range
= Fcons (make_number (from_c
), make_number (to_c
));
595 (*c_function
) (arg
, range
);
597 call2 (function
, range
, arg
);
599 else if (CHARSET_METHOD (charset
) == CHARSET_METHOD_MAP
)
601 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset
)))
603 map_char_table_for_charset (c_function
, function
,
604 CHARSET_ENCODER (charset
), arg
,
605 partial
? charset
: NULL
, from
, to
);
607 else if (CHARSET_METHOD (charset
) == CHARSET_METHOD_SUBSET
)
609 Lisp_Object subset_info
;
612 subset_info
= CHARSET_SUBSET (charset
);
613 charset
= CHARSET_FROM_ID (XFASTINT (AREF (subset_info
, 0)));
614 offset
= XINT (AREF (subset_info
, 3));
616 if (from
< XFASTINT (AREF (subset_info
, 1)))
617 from
= XFASTINT (AREF (subset_info
, 1));
619 if (to
> XFASTINT (AREF (subset_info
, 2)))
620 to
= XFASTINT (AREF (subset_info
, 2));
621 map_charset_chars (c_function
, function
, arg
, charset
, from
, to
);
623 else /* i.e. CHARSET_METHOD_SUPERSET */
627 for (parents
= CHARSET_SUPERSET (charset
); CONSP (parents
);
628 parents
= XCDR (parents
))
631 unsigned this_from
, this_to
;
633 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (XCAR (parents
))));
634 offset
= XINT (XCDR (XCAR (parents
)));
635 this_from
= from
- offset
;
636 this_to
= to
- offset
;
637 if (this_from
< CHARSET_MIN_CODE (charset
))
638 this_from
= CHARSET_MIN_CODE (charset
);
639 if (this_to
> CHARSET_MAX_CODE (charset
))
640 this_to
= CHARSET_MAX_CODE (charset
);
641 map_charset_chars (c_function
, function
, arg
, charset
,
647 DEFUN ("map-charset-chars", Fmap_charset_chars
, Smap_charset_chars
, 2, 5, 0,
648 doc
: /* Call FUNCTION for all characters in CHARSET.
649 FUNCTION is called with an argument RANGE and the optional 3rd
652 RANGE is a cons (FROM . TO), where FROM and TO indicate a range of
653 characters contained in CHARSET.
655 The optional 4th and 5th arguments FROM-CODE and TO-CODE specify the
656 range of code points of target characters. */)
657 (function
, charset
, arg
, from_code
, to_code
)
658 Lisp_Object function
, charset
, arg
, from_code
, to_code
;
663 CHECK_CHARSET_GET_CHARSET (charset
, cs
);
664 if (NILP (from_code
))
665 from
= CHARSET_MIN_CODE (cs
);
668 CHECK_NATNUM (from_code
);
669 from
= XINT (from_code
);
670 if (from
< CHARSET_MIN_CODE (cs
))
671 from
= CHARSET_MIN_CODE (cs
);
674 to
= CHARSET_MAX_CODE (cs
);
677 CHECK_NATNUM (to_code
);
679 if (to
> CHARSET_MAX_CODE (cs
))
680 to
= CHARSET_MAX_CODE (cs
);
682 map_charset_chars (NULL
, function
, arg
, cs
, from
, to
);
687 /* Define a charset according to the arguments. The Nth argument is
688 the Nth attribute of the charset (the last attribute `charset-id'
689 is not included). See the docstring of `define-charset' for the
692 DEFUN ("define-charset-internal", Fdefine_charset_internal
,
693 Sdefine_charset_internal
, charset_arg_max
, MANY
, 0,
694 doc
: /* For internal use only.
695 usage: (define-charset-internal ...) */)
700 /* Charset attr vector. */
704 struct Lisp_Hash_Table
*hash_table
= XHASH_TABLE (Vcharset_hash_table
);
706 struct charset charset
;
709 int new_definition_p
;
712 if (nargs
!= charset_arg_max
)
713 return Fsignal (Qwrong_number_of_arguments
,
714 Fcons (intern ("define-charset-internal"),
715 make_number (nargs
)));
717 attrs
= Fmake_vector (make_number (charset_attr_max
), Qnil
);
719 CHECK_SYMBOL (args
[charset_arg_name
]);
720 ASET (attrs
, charset_name
, args
[charset_arg_name
]);
722 val
= args
[charset_arg_code_space
];
723 for (i
= 0, dimension
= 0, nchars
= 1; i
< 4; i
++)
725 int min_byte
, max_byte
;
727 min_byte
= XINT (Faref (val
, make_number (i
* 2)));
728 max_byte
= XINT (Faref (val
, make_number (i
* 2 + 1)));
729 if (min_byte
< 0 || min_byte
> max_byte
|| max_byte
>= 256)
730 error ("Invalid :code-space value");
731 charset
.code_space
[i
* 4] = min_byte
;
732 charset
.code_space
[i
* 4 + 1] = max_byte
;
733 charset
.code_space
[i
* 4 + 2] = max_byte
- min_byte
+ 1;
734 nchars
*= charset
.code_space
[i
* 4 + 2];
735 charset
.code_space
[i
* 4 + 3] = nchars
;
740 val
= args
[charset_arg_dimension
];
742 charset
.dimension
= dimension
;
746 charset
.dimension
= XINT (val
);
747 if (charset
.dimension
< 1 || charset
.dimension
> 4)
748 args_out_of_range_3 (val
, make_number (1), make_number (4));
751 charset
.code_linear_p
752 = (charset
.dimension
== 1
753 || (charset
.code_space
[2] == 256
754 && (charset
.dimension
== 2
755 || (charset
.code_space
[6] == 256
756 && (charset
.dimension
== 3
757 || charset
.code_space
[10] == 256)))));
759 if (! charset
.code_linear_p
)
761 charset
.code_space_mask
= (unsigned char *) xmalloc (256);
762 bzero (charset
.code_space_mask
, 256);
763 for (i
= 0; i
< 4; i
++)
764 for (j
= charset
.code_space
[i
* 4]; j
<= charset
.code_space
[i
* 4 + 1];
766 charset
.code_space_mask
[j
] |= (1 << i
);
769 charset
.iso_chars_96
= charset
.code_space
[2] == 96;
771 charset
.min_code
= (charset
.code_space
[0]
772 | (charset
.code_space
[4] << 8)
773 | (charset
.code_space
[8] << 16)
774 | (charset
.code_space
[12] << 24));
775 charset
.max_code
= (charset
.code_space
[1]
776 | (charset
.code_space
[5] << 8)
777 | (charset
.code_space
[9] << 16)
778 | (charset
.code_space
[13] << 24));
779 charset
.char_index_offset
= 0;
781 val
= args
[charset_arg_min_code
];
791 CHECK_NUMBER_CAR (val
);
792 CHECK_NUMBER_CDR (val
);
793 code
= (XINT (XCAR (val
)) << 16) | (XINT (XCDR (val
)));
795 if (code
< charset
.min_code
796 || code
> charset
.max_code
)
797 args_out_of_range_3 (make_number (charset
.min_code
),
798 make_number (charset
.max_code
), val
);
799 charset
.char_index_offset
= CODE_POINT_TO_INDEX (&charset
, code
);
800 charset
.min_code
= code
;
803 val
= args
[charset_arg_max_code
];
813 CHECK_NUMBER_CAR (val
);
814 CHECK_NUMBER_CDR (val
);
815 code
= (XINT (XCAR (val
)) << 16) | (XINT (XCDR (val
)));
817 if (code
< charset
.min_code
818 || code
> charset
.max_code
)
819 args_out_of_range_3 (make_number (charset
.min_code
),
820 make_number (charset
.max_code
), val
);
821 charset
.max_code
= code
;
824 charset
.compact_codes_p
= charset
.max_code
< 0x1000000;
826 val
= args
[charset_arg_invalid_code
];
829 if (charset
.min_code
> 0)
830 charset
.invalid_code
= 0;
833 XSETINT (val
, charset
.max_code
+ 1);
834 if (XINT (val
) == charset
.max_code
+ 1)
835 charset
.invalid_code
= charset
.max_code
+ 1;
837 error ("Attribute :invalid-code must be specified");
843 charset
.invalid_code
= XFASTINT (val
);
846 val
= args
[charset_arg_iso_final
];
848 charset
.iso_final
= -1;
852 if (XINT (val
) < '0' || XINT (val
) > 127)
853 error ("Invalid iso-final-char: %d", XINT (val
));
854 charset
.iso_final
= XINT (val
);
857 val
= args
[charset_arg_iso_revision
];
859 charset
.iso_revision
= -1;
864 args_out_of_range (make_number (63), val
);
865 charset
.iso_revision
= XINT (val
);
868 val
= args
[charset_arg_emacs_mule_id
];
870 charset
.emacs_mule_id
= -1;
874 if ((XINT (val
) > 0 && XINT (val
) <= 128) || XINT (val
) >= 256)
875 error ("Invalid emacs-mule-id: %d", XINT (val
));
876 charset
.emacs_mule_id
= XINT (val
);
879 charset
.ascii_compatible_p
= ! NILP (args
[charset_arg_ascii_compatible_p
]);
881 charset
.supplementary_p
= ! NILP (args
[charset_arg_supplementary_p
]);
883 charset
.unified_p
= 0;
885 bzero (charset
.fast_map
, sizeof (charset
.fast_map
));
887 if (! NILP (args
[charset_arg_code_offset
]))
889 val
= args
[charset_arg_code_offset
];
892 charset
.method
= CHARSET_METHOD_OFFSET
;
893 charset
.code_offset
= XINT (val
);
895 i
= CODE_POINT_TO_INDEX (&charset
, charset
.min_code
);
896 charset
.min_char
= i
+ charset
.code_offset
;
897 i
= CODE_POINT_TO_INDEX (&charset
, charset
.max_code
);
898 charset
.max_char
= i
+ charset
.code_offset
;
899 if (charset
.max_char
> MAX_CHAR
)
900 error ("Unsupported max char: %d", charset
.max_char
);
902 i
= (charset
.min_char
>> 7) << 7;
903 for (; i
< 0x10000 && i
<= charset
.max_char
; i
+= 128)
904 CHARSET_FAST_MAP_SET (i
, charset
.fast_map
);
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
);
914 load_charset_map_from_file (&charset
, val
, 0);
916 load_charset_map_from_vector (&charset
, val
, 0);
917 charset
.method
= CHARSET_METHOD_MAP_DEFERRED
;
919 else if (! NILP (args
[charset_arg_subset
]))
922 Lisp_Object parent_min_code
, parent_max_code
, parent_code_offset
;
923 struct charset
*parent_charset
;
925 val
= args
[charset_arg_subset
];
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
;
965 struct charset
*this_charset
;
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
);
978 CHECK_CHARSET_GET_ID (elt
, this_id
);
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
];
993 error ("None of :code-offset, :map, :parents are specified");
995 val
= args
[charset_arg_unify_map
];
996 if (! NILP (val
) && !STRINGP (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
],
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
;
1013 charset
.hash_index
= hash_put (hash_table
, args
[charset_arg_name
], attrs
,
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
));
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;
1054 emacs_mule_bytes
[charset
.emacs_mule_id
] = charset
.dimension
+ 2;
1055 if (new_definition_p
)
1056 Vemacs_mule_charset_list
= nconc2 (Vemacs_mule_charset_list
,
1057 Fcons (make_number (id
), Qnil
));
1060 if (new_definition_p
)
1062 Vcharset_list
= Fcons (args
[charset_arg_name
], Vcharset_list
);
1063 if (charset
.supplementary_p
)
1064 Vcharset_ordered_list
= nconc2 (Vcharset_ordered_list
,
1065 Fcons (make_number (id
), Qnil
));
1070 for (tail
= Vcharset_ordered_list
; CONSP (tail
); tail
= XCDR (tail
))
1072 struct charset
*cs
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
1074 if (cs
->supplementary_p
)
1077 if (EQ (tail
, Vcharset_ordered_list
))
1078 Vcharset_ordered_list
= Fcons (make_number (id
),
1079 Vcharset_ordered_list
);
1080 else if (NILP (tail
))
1081 Vcharset_ordered_list
= nconc2 (Vcharset_ordered_list
,
1082 Fcons (make_number (id
), Qnil
));
1085 val
= Fcons (XCAR (tail
), XCDR (tail
));
1086 XSETCDR (tail
, val
);
1087 XSETCAR (tail
, make_number (id
));
1090 charset_ordered_list_tick
++;
1097 /* Same as Fdefine_charset_internal but arguments are more convenient
1098 to call from C (typically in syms_of_charset). This can define a
1099 charset of `offset' method only. Return the ID of the new
1103 define_charset_internal (name
, dimension
, code_space
, min_code
, max_code
,
1104 iso_final
, iso_revision
, emacs_mule_id
,
1105 ascii_compatible
, supplementary
,
1109 unsigned char *code_space
;
1110 unsigned min_code
, max_code
;
1111 int iso_final
, iso_revision
, emacs_mule_id
;
1112 int ascii_compatible
, supplementary
;
1115 Lisp_Object args
[charset_arg_max
];
1116 Lisp_Object plist
[14];
1120 args
[charset_arg_name
] = name
;
1121 args
[charset_arg_dimension
] = make_number (dimension
);
1122 val
= Fmake_vector (make_number (8), make_number (0));
1123 for (i
= 0; i
< 8; i
++)
1124 ASET (val
, i
, make_number (code_space
[i
]));
1125 args
[charset_arg_code_space
] = val
;
1126 args
[charset_arg_min_code
] = make_number (min_code
);
1127 args
[charset_arg_max_code
] = make_number (max_code
);
1128 args
[charset_arg_iso_final
]
1129 = (iso_final
< 0 ? Qnil
: make_number (iso_final
));
1130 args
[charset_arg_iso_revision
] = make_number (iso_revision
);
1131 args
[charset_arg_emacs_mule_id
]
1132 = (emacs_mule_id
< 0 ? Qnil
: make_number (emacs_mule_id
));
1133 args
[charset_arg_ascii_compatible_p
] = ascii_compatible
? Qt
: Qnil
;
1134 args
[charset_arg_supplementary_p
] = supplementary
? Qt
: Qnil
;
1135 args
[charset_arg_invalid_code
] = Qnil
;
1136 args
[charset_arg_code_offset
] = make_number (code_offset
);
1137 args
[charset_arg_map
] = Qnil
;
1138 args
[charset_arg_subset
] = Qnil
;
1139 args
[charset_arg_superset
] = Qnil
;
1140 args
[charset_arg_unify_map
] = Qnil
;
1142 plist
[0] = intern (":name");
1143 plist
[1] = args
[charset_arg_name
];
1144 plist
[2] = intern (":dimension");
1145 plist
[3] = args
[charset_arg_dimension
];
1146 plist
[4] = intern (":code-space");
1147 plist
[5] = args
[charset_arg_code_space
];
1148 plist
[6] = intern (":iso-final-char");
1149 plist
[7] = args
[charset_arg_iso_final
];
1150 plist
[8] = intern (":emacs-mule-id");
1151 plist
[9] = args
[charset_arg_emacs_mule_id
];
1152 plist
[10] = intern (":ascii-compatible-p");
1153 plist
[11] = args
[charset_arg_ascii_compatible_p
];
1154 plist
[12] = intern (":code-offset");
1155 plist
[13] = args
[charset_arg_code_offset
];
1157 args
[charset_arg_plist
] = Flist (14, plist
);
1158 Fdefine_charset_internal (charset_arg_max
, args
);
1160 return XINT (CHARSET_SYMBOL_ID (name
));
1164 DEFUN ("define-charset-alias", Fdefine_charset_alias
,
1165 Sdefine_charset_alias
, 2, 2, 0,
1166 doc
: /* Define ALIAS as an alias for charset CHARSET. */)
1168 Lisp_Object alias
, charset
;
1172 CHECK_CHARSET_GET_ATTR (charset
, attr
);
1173 Fputhash (alias
, attr
, Vcharset_hash_table
);
1174 Vcharset_list
= Fcons (alias
, Vcharset_list
);
1179 DEFUN ("charset-plist", Fcharset_plist
, Scharset_plist
, 1, 1, 0,
1180 doc
: /* Return the property list of CHARSET. */)
1182 Lisp_Object charset
;
1186 CHECK_CHARSET_GET_ATTR (charset
, attrs
);
1187 return CHARSET_ATTR_PLIST (attrs
);
1191 DEFUN ("set-charset-plist", Fset_charset_plist
, Sset_charset_plist
, 2, 2, 0,
1192 doc
: /* Set CHARSET's property list to PLIST. */)
1194 Lisp_Object charset
, plist
;
1198 CHECK_CHARSET_GET_ATTR (charset
, attrs
);
1199 CHARSET_ATTR_PLIST (attrs
) = plist
;
1204 DEFUN ("unify-charset", Funify_charset
, Sunify_charset
, 1, 3, 0,
1205 doc
: /* Unify characters of CHARSET with Unicode.
1206 This means reading the relevant file and installing the table defined
1207 by CHARSET's `:unify-map' property.
1209 Optional second arg UNIFY-MAP is a file name string or a vector. It has
1210 the same meaning as the `:unify-map' attribute in the function
1211 `define-charset' (which see).
1213 Optional third argument DEUNIFY, if non-nil, means to de-unify CHARSET. */)
1214 (charset
, unify_map
, deunify
)
1215 Lisp_Object charset
, unify_map
, deunify
;
1220 CHECK_CHARSET_GET_ID (charset
, id
);
1221 cs
= CHARSET_FROM_ID (id
);
1222 if (CHARSET_METHOD (cs
) == CHARSET_METHOD_MAP_DEFERRED
)
1225 ? CHARSET_UNIFIED_P (cs
) && ! NILP (CHARSET_DEUNIFIER (cs
))
1226 : ! CHARSET_UNIFIED_P (cs
))
1229 CHARSET_UNIFIED_P (cs
) = 0;
1232 if (CHARSET_METHOD (cs
) != CHARSET_METHOD_OFFSET
)
1233 error ("Can't unify charset: %s", SDATA (SYMBOL_NAME (charset
)));
1234 if (NILP (unify_map
))
1235 unify_map
= CHARSET_UNIFY_MAP (cs
);
1236 if (STRINGP (unify_map
))
1237 load_charset_map_from_file (cs
, unify_map
, 2);
1238 else if (VECTORP (unify_map
))
1239 load_charset_map_from_vector (cs
, unify_map
, 2);
1240 else if (NILP (unify_map
))
1241 error ("No unify-map for charset");
1243 error ("Bad unify-map arg");
1244 CHARSET_UNIFIED_P (cs
) = 1;
1246 else if (CHAR_TABLE_P (Vchar_unify_table
))
1248 int min_code
= CHARSET_MIN_CODE (cs
);
1249 int max_code
= CHARSET_MAX_CODE (cs
);
1250 int min_char
= DECODE_CHAR (cs
, min_code
);
1251 int max_char
= DECODE_CHAR (cs
, max_code
);
1253 char_table_set_range (Vchar_unify_table
, min_char
, max_char
, Qnil
);
1259 DEFUN ("get-unused-iso-final-char", Fget_unused_iso_final_char
,
1260 Sget_unused_iso_final_char
, 2, 2, 0,
1262 Return an unused ISO final char for a charset of DIMENSION and CHARS.
1263 DIMENSION is the number of bytes to represent a character: 1 or 2.
1264 CHARS is the number of characters in a dimension: 94 or 96.
1266 This final char is for private use, thus the range is `0' (48) .. `?' (63).
1267 If there's no unused final char for the specified kind of charset,
1270 Lisp_Object dimension
, chars
;
1274 CHECK_NUMBER (dimension
);
1275 CHECK_NUMBER (chars
);
1276 if (XINT (dimension
) != 1 && XINT (dimension
) != 2 && XINT (dimension
) != 3)
1277 args_out_of_range_3 (dimension
, make_number (1), make_number (3));
1278 if (XINT (chars
) != 94 && XINT (chars
) != 96)
1279 args_out_of_range_3 (chars
, make_number (94), make_number (96));
1280 for (final_char
= '0'; final_char
<= '?'; final_char
++)
1281 if (ISO_CHARSET_TABLE (XINT (dimension
), XINT (chars
), final_char
) < 0)
1283 return (final_char
<= '?' ? make_number (final_char
) : Qnil
);
1287 check_iso_charset_parameter (dimension
, chars
, final_char
)
1288 Lisp_Object dimension
, chars
, final_char
;
1290 CHECK_NATNUM (dimension
);
1291 CHECK_NATNUM (chars
);
1292 CHECK_NATNUM (final_char
);
1294 if (XINT (dimension
) > 3)
1295 error ("Invalid DIMENSION %d, it should be 1, 2, or 3", XINT (dimension
));
1296 if (XINT (chars
) != 94 && XINT (chars
) != 96)
1297 error ("Invalid CHARS %d, it should be 94 or 96", XINT (chars
));
1298 if (XINT (final_char
) < '0' || XINT (final_char
) > '~')
1299 error ("Invalid FINAL-CHAR %c, it should be `0'..`~'", XINT (chars
));
1303 DEFUN ("declare-equiv-charset", Fdeclare_equiv_charset
, Sdeclare_equiv_charset
,
1305 doc
: /* Declare an equivalent charset for ISO-2022 decoding.
1307 On decoding by an ISO-2022 base coding system, when a charset
1308 specified by DIMENSION, CHARS, and FINAL-CHAR is designated, behave as
1309 if CHARSET is designated instead. */)
1310 (dimension
, chars
, final_char
, charset
)
1311 Lisp_Object dimension
, chars
, final_char
, charset
;
1316 CHECK_CHARSET_GET_ID (charset
, id
);
1317 check_iso_charset_parameter (dimension
, chars
, final_char
);
1318 chars_flag
= XINT (chars
) == 96;
1319 ISO_CHARSET_TABLE (XINT (dimension
), chars_flag
, XINT (final_char
)) = id
;
1324 /* Return information about charsets in the text at PTR of NBYTES
1325 bytes, which are NCHARS characters. The value is:
1327 0: Each character is represented by one byte. This is always
1328 true for a unibyte string. For a multibyte string, true if
1329 it contains only ASCII characters.
1331 1: No charsets other than ascii, control-1, and latin-1 are
1338 string_xstring_p (string
)
1341 const unsigned char *p
= SDATA (string
);
1342 const unsigned char *endp
= p
+ SBYTES (string
);
1344 if (SCHARS (string
) == SBYTES (string
))
1349 int c
= STRING_CHAR_ADVANCE (p
);
1358 /* Find charsets in the string at PTR of NCHARS and NBYTES.
1360 CHARSETS is a vector. If Nth element is non-nil, it means the
1361 charset whose id is N is already found.
1363 It may lookup a translation table TABLE if supplied. */
1366 find_charsets_in_text (ptr
, nchars
, nbytes
, charsets
, table
, multibyte
)
1367 const unsigned char *ptr
;
1368 EMACS_INT nchars
, nbytes
;
1369 Lisp_Object charsets
, table
;
1372 const unsigned char *pend
= ptr
+ nbytes
;
1374 if (nchars
== nbytes
)
1377 ASET (charsets
, charset_ascii
, Qt
);
1384 c
= translate_char (table
, c
);
1385 if (ASCII_BYTE_P (c
))
1386 ASET (charsets
, charset_ascii
, Qt
);
1388 ASET (charsets
, charset_eight_bit
, Qt
);
1395 int c
= STRING_CHAR_ADVANCE (ptr
);
1396 struct charset
*charset
;
1399 c
= translate_char (table
, c
);
1400 charset
= CHAR_CHARSET (c
);
1401 ASET (charsets
, CHARSET_ID (charset
), Qt
);
1406 DEFUN ("find-charset-region", Ffind_charset_region
, Sfind_charset_region
,
1408 doc
: /* Return a list of charsets in the region between BEG and END.
1409 BEG and END are buffer positions.
1410 Optional arg TABLE if non-nil is a translation table to look up.
1412 If the current buffer is unibyte, the returned list may contain
1413 only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
1415 Lisp_Object beg
, end
, table
;
1417 Lisp_Object charsets
;
1418 EMACS_INT from
, from_byte
, to
, stop
, stop_byte
;
1421 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1423 validate_region (&beg
, &end
);
1424 from
= XFASTINT (beg
);
1425 stop
= to
= XFASTINT (end
);
1427 if (from
< GPT
&& GPT
< to
)
1430 stop_byte
= GPT_BYTE
;
1433 stop_byte
= CHAR_TO_BYTE (stop
);
1435 from_byte
= CHAR_TO_BYTE (from
);
1437 charsets
= Fmake_vector (make_number (charset_table_used
), Qnil
);
1440 find_charsets_in_text (BYTE_POS_ADDR (from_byte
), stop
- from
,
1441 stop_byte
- from_byte
, charsets
, table
,
1445 from
= stop
, from_byte
= stop_byte
;
1446 stop
= to
, stop_byte
= CHAR_TO_BYTE (stop
);
1453 for (i
= charset_table_used
- 1; i
>= 0; i
--)
1454 if (!NILP (AREF (charsets
, i
)))
1455 val
= Fcons (CHARSET_NAME (charset_table
+ i
), val
);
1459 DEFUN ("find-charset-string", Ffind_charset_string
, Sfind_charset_string
,
1461 doc
: /* Return a list of charsets in STR.
1462 Optional arg TABLE if non-nil is a translation table to look up.
1464 If STR is unibyte, the returned list may contain
1465 only `ascii', `eight-bit-control', and `eight-bit-graphic'. */)
1467 Lisp_Object str
, table
;
1469 Lisp_Object charsets
;
1475 charsets
= Fmake_vector (make_number (charset_table_used
), Qnil
);
1476 find_charsets_in_text (SDATA (str
), SCHARS (str
), SBYTES (str
),
1478 STRING_MULTIBYTE (str
));
1480 for (i
= charset_table_used
- 1; i
>= 0; i
--)
1481 if (!NILP (AREF (charsets
, i
)))
1482 val
= Fcons (CHARSET_NAME (charset_table
+ i
), val
);
1488 /* Return a character correponding to the code-point CODE of
1492 decode_char (charset
, code
)
1493 struct charset
*charset
;
1497 enum charset_method method
= CHARSET_METHOD (charset
);
1499 if (code
< CHARSET_MIN_CODE (charset
) || code
> CHARSET_MAX_CODE (charset
))
1502 if (method
== CHARSET_METHOD_MAP_DEFERRED
)
1504 load_charset (charset
);
1505 method
= CHARSET_METHOD (charset
);
1508 if (method
== CHARSET_METHOD_SUBSET
)
1510 Lisp_Object subset_info
;
1512 subset_info
= CHARSET_SUBSET (charset
);
1513 charset
= CHARSET_FROM_ID (XFASTINT (AREF (subset_info
, 0)));
1514 code
-= XINT (AREF (subset_info
, 3));
1515 if (code
< XFASTINT (AREF (subset_info
, 1))
1516 || code
> XFASTINT (AREF (subset_info
, 2)))
1519 c
= DECODE_CHAR (charset
, code
);
1521 else if (method
== CHARSET_METHOD_SUPERSET
)
1523 Lisp_Object parents
;
1525 parents
= CHARSET_SUPERSET (charset
);
1527 for (; CONSP (parents
); parents
= XCDR (parents
))
1529 int id
= XINT (XCAR (XCAR (parents
)));
1530 int code_offset
= XINT (XCDR (XCAR (parents
)));
1531 unsigned this_code
= code
- code_offset
;
1533 charset
= CHARSET_FROM_ID (id
);
1534 if ((c
= DECODE_CHAR (charset
, this_code
)) >= 0)
1540 char_index
= CODE_POINT_TO_INDEX (charset
, code
);
1544 if (method
== CHARSET_METHOD_MAP
)
1546 Lisp_Object decoder
;
1548 decoder
= CHARSET_DECODER (charset
);
1549 if (! VECTORP (decoder
))
1551 c
= XINT (AREF (decoder
, char_index
));
1555 c
= char_index
+ CHARSET_CODE_OFFSET (charset
);
1559 if (CHARSET_UNIFIED_P (charset
)
1562 MAYBE_UNIFY_CHAR (c
);
1568 /* Variable used temporarily by the macro ENCODE_CHAR. */
1569 Lisp_Object charset_work
;
1571 /* Return a code-point of CHAR in CHARSET. If CHAR doesn't belong to
1572 CHARSET, return CHARSET_INVALID_CODE (CHARSET). If STRICT is true,
1573 use CHARSET's strict_max_char instead of max_char. */
1576 encode_char (charset
, c
)
1577 struct charset
*charset
;
1581 enum charset_method method
= CHARSET_METHOD (charset
);
1583 if (CHARSET_UNIFIED_P (charset
))
1585 Lisp_Object deunifier
, deunified
;
1587 deunifier
= CHARSET_DEUNIFIER (charset
);
1588 if (! CHAR_TABLE_P (deunifier
))
1590 Funify_charset (CHARSET_NAME (charset
), Qnil
, Qnil
);
1591 deunifier
= CHARSET_DEUNIFIER (charset
);
1593 deunified
= CHAR_TABLE_REF (deunifier
, c
);
1594 if (! NILP (deunified
))
1595 c
= XINT (deunified
);
1598 if (method
== CHARSET_METHOD_SUBSET
)
1600 Lisp_Object subset_info
;
1601 struct charset
*this_charset
;
1603 subset_info
= CHARSET_SUBSET (charset
);
1604 this_charset
= CHARSET_FROM_ID (XFASTINT (AREF (subset_info
, 0)));
1605 code
= ENCODE_CHAR (this_charset
, c
);
1606 if (code
== CHARSET_INVALID_CODE (this_charset
)
1607 || code
< XFASTINT (AREF (subset_info
, 1))
1608 || code
> XFASTINT (AREF (subset_info
, 2)))
1609 return CHARSET_INVALID_CODE (charset
);
1610 code
+= XINT (AREF (subset_info
, 3));
1614 if (method
== CHARSET_METHOD_SUPERSET
)
1616 Lisp_Object parents
;
1618 parents
= CHARSET_SUPERSET (charset
);
1619 for (; CONSP (parents
); parents
= XCDR (parents
))
1621 int id
= XINT (XCAR (XCAR (parents
)));
1622 int code_offset
= XINT (XCDR (XCAR (parents
)));
1623 struct charset
*this_charset
= CHARSET_FROM_ID (id
);
1625 code
= ENCODE_CHAR (this_charset
, c
);
1626 if (code
!= CHARSET_INVALID_CODE (this_charset
))
1627 return code
+ code_offset
;
1629 return CHARSET_INVALID_CODE (charset
);
1632 if (! CHARSET_FAST_MAP_REF ((c
), charset
->fast_map
)
1633 || c
< CHARSET_MIN_CHAR (charset
) || c
> CHARSET_MAX_CHAR (charset
))
1634 return CHARSET_INVALID_CODE (charset
);
1636 if (method
== CHARSET_METHOD_MAP_DEFERRED
)
1638 load_charset (charset
);
1639 method
= CHARSET_METHOD (charset
);
1642 if (method
== CHARSET_METHOD_MAP
)
1644 Lisp_Object encoder
;
1647 encoder
= CHARSET_ENCODER (charset
);
1648 if (! CHAR_TABLE_P (CHARSET_ENCODER (charset
)))
1649 return CHARSET_INVALID_CODE (charset
);
1650 val
= CHAR_TABLE_REF (encoder
, c
);
1652 return CHARSET_INVALID_CODE (charset
);
1654 if (! CHARSET_COMPACT_CODES_P (charset
))
1655 code
= INDEX_TO_CODE_POINT (charset
, code
);
1657 else /* method == CHARSET_METHOD_OFFSET */
1659 code
= c
- CHARSET_CODE_OFFSET (charset
);
1660 code
= INDEX_TO_CODE_POINT (charset
, code
);
1667 DEFUN ("decode-char", Fdecode_char
, Sdecode_char
, 2, 3, 0,
1668 doc
: /* Decode the pair of CHARSET and CODE-POINT into a character.
1669 Return nil if CODE-POINT is not valid in CHARSET.
1671 CODE-POINT may be a cons (HIGHER-16-BIT-VALUE . LOWER-16-BIT-VALUE).
1673 Optional argument RESTRICTION specifies a way to map the pair of CCS
1674 and CODE-POINT to a character. Currently not supported and just ignored. */)
1675 (charset
, code_point
, restriction
)
1676 Lisp_Object charset
, code_point
, restriction
;
1680 struct charset
*charsetp
;
1682 CHECK_CHARSET_GET_ID (charset
, id
);
1683 if (CONSP (code_point
))
1685 CHECK_NATNUM_CAR (code_point
);
1686 CHECK_NATNUM_CDR (code_point
);
1687 code
= (XINT (XCAR (code_point
)) << 16) | (XINT (XCDR (code_point
)));
1691 CHECK_NATNUM (code_point
);
1692 code
= XINT (code_point
);
1694 charsetp
= CHARSET_FROM_ID (id
);
1695 c
= DECODE_CHAR (charsetp
, code
);
1696 return (c
>= 0 ? make_number (c
) : Qnil
);
1700 DEFUN ("encode-char", Fencode_char
, Sencode_char
, 2, 3, 0,
1701 doc
: /* Encode the character CH into a code-point of CHARSET.
1702 Return nil if CHARSET doesn't include CH.
1704 Optional argument RESTRICTION specifies a way to map CH to a
1705 code-point in CCS. Currently not supported and just ignored. */)
1706 (ch
, charset
, restriction
)
1707 Lisp_Object ch
, charset
, restriction
;
1711 struct charset
*charsetp
;
1713 CHECK_CHARSET_GET_ID (charset
, id
);
1715 charsetp
= CHARSET_FROM_ID (id
);
1716 code
= ENCODE_CHAR (charsetp
, XINT (ch
));
1717 if (code
== CHARSET_INVALID_CODE (charsetp
))
1719 if (code
> 0x7FFFFFF)
1720 return Fcons (make_number (code
>> 16), make_number (code
& 0xFFFF));
1721 return make_number (code
);
1725 DEFUN ("make-char", Fmake_char
, Smake_char
, 1, 5, 0,
1727 /* Return a character of CHARSET whose position codes are CODEn.
1729 CODE1 through CODE4 are optional, but if you don't supply sufficient
1730 position codes, it is assumed that the minimum code in each dimension
1732 (charset
, code1
, code2
, code3
, code4
)
1733 Lisp_Object charset
, code1
, code2
, code3
, code4
;
1736 struct charset
*charsetp
;
1740 CHECK_CHARSET_GET_ID (charset
, id
);
1741 charsetp
= CHARSET_FROM_ID (id
);
1743 dimension
= CHARSET_DIMENSION (charsetp
);
1745 code
= (CHARSET_ASCII_COMPATIBLE_P (charsetp
)
1746 ? 0 : CHARSET_MIN_CODE (charsetp
));
1749 CHECK_NATNUM (code1
);
1750 if (XFASTINT (code1
) >= 0x100)
1751 args_out_of_range (make_number (0xFF), code1
);
1752 code
= XFASTINT (code1
);
1758 code
|= charsetp
->code_space
[(dimension
- 2) * 4];
1761 CHECK_NATNUM (code2
);
1762 if (XFASTINT (code2
) >= 0x100)
1763 args_out_of_range (make_number (0xFF), code2
);
1764 code
|= XFASTINT (code2
);
1771 code
|= charsetp
->code_space
[(dimension
- 3) * 4];
1774 CHECK_NATNUM (code3
);
1775 if (XFASTINT (code3
) >= 0x100)
1776 args_out_of_range (make_number (0xFF), code3
);
1777 code
|= XFASTINT (code3
);
1784 code
|= charsetp
->code_space
[0];
1787 CHECK_NATNUM (code4
);
1788 if (XFASTINT (code4
) >= 0x100)
1789 args_out_of_range (make_number (0xFF), code4
);
1790 code
|= XFASTINT (code4
);
1797 if (CHARSET_ISO_FINAL (charsetp
) >= 0)
1799 c
= DECODE_CHAR (charsetp
, code
);
1801 error ("Invalid code(s)");
1802 return make_number (c
);
1806 /* Return the first charset in CHARSET_LIST that contains C.
1807 CHARSET_LIST is a list of charset IDs. If it is nil, use
1808 Vcharset_ordered_list. */
1811 char_charset (c
, charset_list
, code_return
)
1813 Lisp_Object charset_list
;
1814 unsigned *code_return
;
1818 if (NILP (charset_list
))
1819 charset_list
= Vcharset_ordered_list
;
1823 while (CONSP (charset_list
))
1825 struct charset
*charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
1826 unsigned code
= ENCODE_CHAR (charset
, c
);
1828 if (code
!= CHARSET_INVALID_CODE (charset
))
1831 *code_return
= code
;
1834 charset_list
= XCDR (charset_list
);
1835 if (c
<= MAX_UNICODE_CHAR
1836 && EQ (charset_list
, Vcharset_non_preferred_head
))
1837 return CHARSET_FROM_ID (charset_unicode
);
1839 return (maybe_null
? NULL
1840 : c
<= MAX_5_BYTE_CHAR
? CHARSET_FROM_ID (charset_emacs
)
1841 : CHARSET_FROM_ID (charset_eight_bit
));
1845 DEFUN ("split-char", Fsplit_char
, Ssplit_char
, 1, 1, 0,
1847 /*Return list of charset and one to four position-codes of CH.
1848 The charset is decided by the current priority order of charsets.
1849 A position-code is a byte value of each dimension of the code-point of
1850 CH in the charset. */)
1854 struct charset
*charset
;
1859 CHECK_CHARACTER (ch
);
1861 charset
= CHAR_CHARSET (c
);
1864 code
= ENCODE_CHAR (charset
, c
);
1865 if (code
== CHARSET_INVALID_CODE (charset
))
1867 dimension
= CHARSET_DIMENSION (charset
);
1868 for (val
= Qnil
; dimension
> 0; dimension
--)
1870 val
= Fcons (make_number (code
& 0xFF), val
);
1873 return Fcons (CHARSET_NAME (charset
), val
);
1877 DEFUN ("char-charset", Fchar_charset
, Schar_charset
, 1, 1, 0,
1878 doc
: /* Return the charset of highest priority that contains CH. */)
1882 struct charset
*charset
;
1884 CHECK_CHARACTER (ch
);
1885 charset
= CHAR_CHARSET (XINT (ch
));
1886 return (CHARSET_NAME (charset
));
1890 DEFUN ("charset-after", Fcharset_after
, Scharset_after
, 0, 1, 0,
1892 Return charset of a character in the current buffer at position POS.
1893 If POS is nil, it defauls to the current point.
1894 If POS is out of range, the value is nil. */)
1899 struct charset
*charset
;
1901 ch
= Fchar_after (pos
);
1902 if (! INTEGERP (ch
))
1904 charset
= CHAR_CHARSET (XINT (ch
));
1905 return (CHARSET_NAME (charset
));
1909 DEFUN ("iso-charset", Fiso_charset
, Siso_charset
, 3, 3, 0,
1911 Return charset of ISO's specification DIMENSION, CHARS, and FINAL-CHAR.
1913 ISO 2022's designation sequence (escape sequence) distinguishes charsets
1914 by their DIMENSION, CHARS, and FINAL-CHAR,
1915 whereas Emacs distinguishes them by charset symbol.
1916 See the documentation of the function `charset-info' for the meanings of
1917 DIMENSION, CHARS, and FINAL-CHAR. */)
1918 (dimension
, chars
, final_char
)
1919 Lisp_Object dimension
, chars
, final_char
;
1924 check_iso_charset_parameter (dimension
, chars
, final_char
);
1925 chars_flag
= XFASTINT (chars
) == 96;
1926 id
= ISO_CHARSET_TABLE (XFASTINT (dimension
), chars_flag
,
1927 XFASTINT (final_char
));
1928 return (id
>= 0 ? CHARSET_NAME (CHARSET_FROM_ID (id
)) : Qnil
);
1932 DEFUN ("clear-charset-maps", Fclear_charset_maps
, Sclear_charset_maps
,
1935 Clear encoder and decoder of charsets that are loaded from mapfiles. */)
1939 struct charset
*charset
;
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
, Qnil
);
1961 Vchar_unify_table
= Vchar_unified_charset_table
;
1962 Vchar_unified_charset_table
= 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. */)
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
);
1985 return Fnreverse (val
);
1988 DEFUN ("set-charset-priority", Fset_charset_priority
, Sset_charset_priority
,
1990 doc
: /* Assign higher priority to the charsets given as arguments.
1991 usage: (set-charset-priority &rest charsets) */)
1996 Lisp_Object new_head
, old_list
, arglist
[2];
1997 Lisp_Object list_2022
, list_emacs_mule
;
2000 old_list
= Fcopy_sequence (Vcharset_ordered_list
);
2002 for (i
= 0; i
< nargs
; i
++)
2004 CHECK_CHARSET_GET_ID (args
[i
], id
);
2005 if (! NILP (Fmemq (make_number (id
), old_list
)))
2007 old_list
= Fdelq (make_number (id
), old_list
);
2008 new_head
= Fcons (make_number (id
), new_head
);
2011 arglist
[0] = Fnreverse (new_head
);
2012 arglist
[1] = Vcharset_non_preferred_head
= old_list
;
2013 Vcharset_ordered_list
= Fnconc (2, arglist
);
2014 charset_ordered_list_tick
++;
2016 for (old_list
= Vcharset_ordered_list
, list_2022
= list_emacs_mule
= Qnil
;
2017 CONSP (old_list
); old_list
= XCDR (old_list
))
2019 if (! NILP (Fmemq (XCAR (old_list
), Viso_2022_charset_list
)))
2020 list_2022
= Fcons (XCAR (old_list
), list_2022
);
2021 if (! NILP (Fmemq (XCAR (old_list
), Vemacs_mule_charset_list
)))
2022 list_emacs_mule
= Fcons (XCAR (old_list
), list_emacs_mule
);
2024 Viso_2022_charset_list
= Fnreverse (list_2022
);
2025 Vemacs_mule_charset_list
= Fnreverse (list_emacs_mule
);
2030 DEFUN ("charset-id-internal", Fcharset_id_internal
, Scharset_id_internal
,
2032 doc
: /* Internal use only.
2033 Return charset identification number of CHARSET. */)
2035 Lisp_Object charset
;
2039 CHECK_CHARSET_GET_ID (charset
, id
);
2040 return make_number (id
);
2047 Lisp_Object tempdir
;
2048 tempdir
= Fexpand_file_name (build_string ("charsets"), Vdata_directory
);
2049 if (access (SDATA (tempdir
), 0) < 0)
2051 dir_warning ("Error: charsets directory (%s) does not exist.\n\
2052 Emacs will not function correctly without the character map files.\n\
2053 Please check your installation!\n",
2055 /* TODO should this be a fatal error? (Bug#909) */
2058 Vcharset_map_path
= Fcons (tempdir
, Qnil
);
2063 init_charset_once ()
2067 for (i
= 0; i
< ISO_MAX_DIMENSION
; i
++)
2068 for (j
= 0; j
< ISO_MAX_CHARS
; j
++)
2069 for (k
= 0; k
< ISO_MAX_FINAL
; k
++)
2070 iso_charset_table
[i
][j
][k
] = -1;
2072 for (i
= 0; i
< 256; i
++)
2073 emacs_mule_charset
[i
] = NULL
;
2075 charset_jisx0201_roman
= -1;
2076 charset_jisx0208_1978
= -1;
2077 charset_jisx0208
= -1;
2079 for (i
= 0; i
< 128; i
++)
2080 unibyte_to_multibyte_table
[i
] = i
;
2081 for (; i
< 256; i
++)
2082 unibyte_to_multibyte_table
[i
] = BYTE8_TO_CHAR (i
);
2090 DEFSYM (Qcharsetp
, "charsetp");
2092 DEFSYM (Qascii
, "ascii");
2093 DEFSYM (Qunicode
, "unicode");
2094 DEFSYM (Qemacs
, "emacs");
2095 DEFSYM (Qeight_bit
, "eight-bit");
2096 DEFSYM (Qiso_8859_1
, "iso-8859-1");
2101 staticpro (&Vcharset_ordered_list
);
2102 Vcharset_ordered_list
= Qnil
;
2104 staticpro (&Viso_2022_charset_list
);
2105 Viso_2022_charset_list
= Qnil
;
2107 staticpro (&Vemacs_mule_charset_list
);
2108 Vemacs_mule_charset_list
= Qnil
;
2110 /* Don't staticpro them here. It's done in syms_of_fns. */
2111 QCtest
= intern (":test");
2112 Qeq
= intern ("eq");
2114 staticpro (&Vcharset_hash_table
);
2116 Lisp_Object args
[2];
2119 Vcharset_hash_table
= Fmake_hash_table (2, args
);
2122 charset_table_size
= 128;
2123 charset_table
= ((struct charset
*)
2124 xmalloc (sizeof (struct charset
) * charset_table_size
));
2125 charset_table_used
= 0;
2127 staticpro (&Vchar_unified_charset_table
);
2128 Vchar_unified_charset_table
= Fmake_char_table (Qnil
, make_number (-1));
2130 defsubr (&Scharsetp
);
2131 defsubr (&Smap_charset_chars
);
2132 defsubr (&Sdefine_charset_internal
);
2133 defsubr (&Sdefine_charset_alias
);
2134 defsubr (&Scharset_plist
);
2135 defsubr (&Sset_charset_plist
);
2136 defsubr (&Sunify_charset
);
2137 defsubr (&Sget_unused_iso_final_char
);
2138 defsubr (&Sdeclare_equiv_charset
);
2139 defsubr (&Sfind_charset_region
);
2140 defsubr (&Sfind_charset_string
);
2141 defsubr (&Sdecode_char
);
2142 defsubr (&Sencode_char
);
2143 defsubr (&Ssplit_char
);
2144 defsubr (&Smake_char
);
2145 defsubr (&Schar_charset
);
2146 defsubr (&Scharset_after
);
2147 defsubr (&Siso_charset
);
2148 defsubr (&Sclear_charset_maps
);
2149 defsubr (&Scharset_priority_list
);
2150 defsubr (&Sset_charset_priority
);
2151 defsubr (&Scharset_id_internal
);
2153 DEFVAR_LISP ("charset-map-path", &Vcharset_map_path
,
2154 doc
: /* *List of directories to search for charset map files. */);
2155 Vcharset_map_path
= Qnil
;
2157 DEFVAR_LISP ("charset-list", &Vcharset_list
,
2158 doc
: /* List of all charsets ever defined. */);
2159 Vcharset_list
= Qnil
;
2161 DEFVAR_LISP ("current-iso639-language", &Vcurrent_iso639_language
,
2162 doc
: /* ISO639 language mnemonic symbol for the current language environment.
2163 If the current language environment is for multiple languages (e.g. "Latin-1"),
2164 the value may be a list of mnemonics. */);
2165 Vcurrent_iso639_language
= Qnil
;
2168 = define_charset_internal (Qascii
, 1, "\x00\x7F\x00\x00\x00\x00",
2169 0, 127, 'B', -1, 0, 1, 0, 0);
2171 = define_charset_internal (Qiso_8859_1
, 1, "\x00\xFF\x00\x00\x00\x00",
2172 0, 255, -1, -1, -1, 1, 0, 0);
2174 = define_charset_internal (Qunicode
, 3, "\x00\xFF\x00\xFF\x00\x10",
2175 0, MAX_UNICODE_CHAR
, -1, 0, -1, 1, 0, 0);
2177 = define_charset_internal (Qemacs
, 3, "\x00\xFF\x00\xFF\x00\x3F",
2178 0, MAX_5_BYTE_CHAR
, -1, 0, -1, 1, 1, 0);
2180 = define_charset_internal (Qeight_bit
, 1, "\x80\xFF\x00\x00\x00\x00",
2181 128, 255, -1, 0, -1, 0, 1,
2182 MAX_5_BYTE_CHAR
+ 1);
2187 /* arch-tag: 66a89b8d-4c28-47d3-9ca1-56f78440d69f
2188 (do not change this comment) */