1 /* Basic multilingual character support.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* At first, see the document in `charset.h' to understand the code in
29 #include <sys/types.h>
43 Lisp_Object Qcharset
, Qascii
, Qcomposition
;
45 /* Declaration of special leading-codes. */
46 int leading_code_composition
; /* for composite characters */
47 int leading_code_private_11
; /* for private DIMENSION1 of 1-column */
48 int leading_code_private_12
; /* for private DIMENSION1 of 2-column */
49 int leading_code_private_21
; /* for private DIMENSION2 of 1-column */
50 int leading_code_private_22
; /* for private DIMENSION2 of 2-column */
52 /* Declaration of special charsets. */
53 int charset_ascii
; /* ASCII */
54 int charset_composition
; /* for a composite character */
55 int charset_latin_iso8859_1
; /* ISO8859-1 (Latin-1) */
56 int charset_jisx0208_1978
; /* JISX0208.1978 (Japanese Kanji old set) */
57 int charset_jisx0208
; /* JISX0208.1983 (Japanese Kanji) */
58 int charset_katakana_jisx0201
; /* JISX0201.Kana (Japanese Katakana) */
59 int charset_latin_jisx0201
; /* JISX0201.Roman (Japanese Roman) */
60 int charset_big5_1
; /* Big5 Level 1 (Chinese Traditional) */
61 int charset_big5_2
; /* Big5 Level 2 (Chinese Traditional) */
63 Lisp_Object Qcharset_table
;
65 /* A char-table containing information of each character set. */
66 Lisp_Object Vcharset_table
;
68 /* A vector of charset symbol indexed by charset-id. This is used
69 only for returning charset symbol from C functions. */
70 Lisp_Object Vcharset_symbol_table
;
72 /* A list of charset symbols ever defined. */
73 Lisp_Object Vcharset_list
;
75 /* Tables used by macros BYTES_BY_CHAR_HEAD and WIDTH_BY_CHAR_HEAD. */
76 int bytes_by_char_head
[256];
77 int width_by_char_head
[256];
79 /* Mapping table from ISO2022's charset (specified by DIMENSION,
80 CHARS, and FINAL-CHAR) to Emacs' charset. */
81 int iso_charset_table
[2][2][128];
83 /* Table of pointers to the structure `cmpchar_info' indexed by
85 struct cmpchar_info
**cmpchar_table
;
86 /* The current size of `cmpchar_table'. */
87 static int cmpchar_table_size
;
88 /* Number of the current composite characters. */
91 /* Variables used locally in the macro FETCH_MULTIBYTE_CHAR. */
92 unsigned char *_fetch_multibyte_char_p
;
93 int _fetch_multibyte_char_len
;
95 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
96 #define max(X, Y) ((X) > (Y) ? (X) : (Y))
98 /* Set STR a pointer to the multi-byte form of the character C. If C
99 is not a composite character, the multi-byte form is set in WORKBUF
100 and STR points WORKBUF. The caller should allocate at least 4-byte
101 area at WORKBUF in advance. Returns the length of the multi-byte
102 form. If C is an invalid character to have a multi-byte form,
105 Use macro `CHAR_STRING (C, WORKBUF, STR)' instead of calling this
106 function directly if C can be an ASCII character. */
109 non_ascii_char_to_string (c
, workbuf
, str
)
111 unsigned char *workbuf
, **str
;
115 if (COMPOSITE_CHAR_P (c
))
117 int cmpchar_id
= COMPOSITE_CHAR_ID (c
);
119 if (cmpchar_id
< n_cmpchars
)
121 *str
= cmpchar_table
[cmpchar_id
]->data
;
122 return cmpchar_table
[cmpchar_id
]->len
;
126 error ("Invalid character: %d", c
);
130 SPLIT_NON_ASCII_CHAR (c
, charset
, c1
, c2
);
132 || ! CHARSET_DEFINED_P (charset
)
133 || c1
>= 0 && c1
< 32
134 || c2
>= 0 && c2
< 32)
135 error ("Invalid character: %d", c
);
138 *workbuf
++ = CHARSET_LEADING_CODE_BASE (charset
);
139 if (*workbuf
= CHARSET_LEADING_CODE_EXT (charset
))
141 *workbuf
++ = c1
| 0x80;
143 *workbuf
++ = c2
| 0x80;
145 return (workbuf
- *str
);
148 /* Return a non-ASCII character of which multi-byte form is at STR of
149 length LEN. If ACTUAL_LEN is not NULL, the actual length of the
150 character is set to the address ACTUAL_LEN.
152 Use macro `STRING_CHAR (STR, LEN)' instead of calling this function
153 directly if STR can hold an ASCII character. */
155 string_to_non_ascii_char (str
, len
, actual_len
)
157 int len
, *actual_len
;
160 unsigned char c1
, c2
;
163 if (SPLIT_STRING (str
, len
, charset
, c1
, c2
) == CHARSET_ASCII
)
170 c
= MAKE_NON_ASCII_CHAR (charset
, c1
, c2
);
173 *actual_len
= (charset
== CHARSET_COMPOSITION
174 ? cmpchar_table
[COMPOSITE_CHAR_ID (c
)]->len
175 : BYTES_BY_CHAR_HEAD (*str
));
179 /* Return the length of the multi-byte form at string STR of length LEN. */
181 multibyte_form_length (str
, len
)
186 unsigned char c1
, c2
;
189 if (SPLIT_STRING (str
, len
, charset
, c1
, c2
) == CHARSET_ASCII
)
192 return (charset
== CHARSET_COMPOSITION
193 ? cmpchar_table
[(c1
<< 7) | c2
]->len
194 : BYTES_BY_CHAR_HEAD (*str
));
197 /* Check if string STR of length LEN contains valid multi-byte form of
198 a character. If valid, charset and position codes of the character
199 is set at *CHARSET, *C1, and *C2, and return 0. If not valid,
200 return -1. This should be used only in the macro SPLIT_STRING
201 which checks range of STR in advance. */
203 split_non_ascii_string (str
, len
, charset
, c1
, c2
)
204 register unsigned char *str
, *c1
, *c2
;
205 register int len
, *charset
;
207 register unsigned int cs
= *str
++;
209 if (cs
== LEADING_CODE_COMPOSITION
)
211 int cmpchar_id
= str_cmpchar_id (str
- 1, len
);
215 *charset
= cs
, *c1
= cmpchar_id
>> 7, *c2
= cmpchar_id
& 0x7F;
217 else if ((cs
< LEADING_CODE_PRIVATE_11
|| (cs
= *str
++) >= 0xA0)
218 && CHARSET_DEFINED_P (cs
))
223 *c1
= (*str
++) & 0x7F;
224 if (CHARSET_DIMENSION (cs
) == 2)
228 *c2
= (*str
++) & 0x7F;
236 /* Return a character unified with C (or a character made of CHARSET,
237 C1, and C2) in unification table TABLE. If no unification is found
238 in TABLE, return C. */
239 unify_char (table
, c
, charset
, c1
, c2
)
241 int c
, charset
, c1
, c2
;
244 int alt_charset
, alt_c1
, alt_c2
, dimension
;
246 if (c
< 0) c
= MAKE_CHAR (charset
, c1
, c2
);
247 if (!CHAR_TABLE_P (table
)
248 || (ch
= Faref (table
, make_number (c
)), !INTEGERP (ch
))
252 SPLIT_CHAR (XFASTINT (ch
), alt_charset
, alt_c1
, alt_c2
);
253 dimension
= CHARSET_DIMENSION (alt_charset
);
254 if (dimension
== 1 && alt_c1
> 0 || dimension
== 2 && alt_c2
> 0)
255 /* CH is not a generic character, just return it. */
256 return XFASTINT (ch
);
258 /* Since CH is a generic character, we must return a specific
259 charater which has the same position codes as C from CH. */
261 SPLIT_CHAR (c
, charset
, c1
, c2
);
262 if (dimension
!= CHARSET_DIMENSION (charset
))
263 /* We can't make such a character because of dimension mismatch. */
265 if (!alt_c1
) alt_c1
= c1
;
266 if (!alt_c2
) alt_c2
= c2
;
267 return MAKE_CHAR (alt_charset
, c1
, c2
);
270 /* Update the table Vcharset_table with the given arguments (see the
271 document of `define-charset' for the meaning of each argument).
272 Several other table contents are also updated. The caller should
273 check the validity of CHARSET-ID and the remaining arguments in
277 update_charset_table (charset_id
, dimension
, chars
, width
, direction
,
278 iso_final_char
, iso_graphic_plane
,
279 short_name
, long_name
, description
)
280 Lisp_Object charset_id
, dimension
, chars
, width
, direction
;
281 Lisp_Object iso_final_char
, iso_graphic_plane
;
282 Lisp_Object short_name
, long_name
, description
;
284 int charset
= XINT (charset_id
);
286 unsigned char leading_code_base
, leading_code_ext
;
288 if (NILP (CHARSET_TABLE_ENTRY (charset
)))
289 CHARSET_TABLE_ENTRY (charset
)
290 = Fmake_vector (make_number (CHARSET_MAX_IDX
), Qnil
);
292 /* Get byte length of multibyte form, base leading-code, and
293 extended leading-code of the charset. See the comment under the
294 title "GENERAL NOTE on CHARACTER SET (CHARSET)" in charset.h. */
295 bytes
= XINT (dimension
);
296 if (charset
< MIN_CHARSET_PRIVATE_DIMENSION1
)
298 /* Official charset, it doesn't have an extended leading-code. */
299 if (charset
!= CHARSET_ASCII
)
300 bytes
+= 1; /* For a base leading-code. */
301 leading_code_base
= charset
;
302 leading_code_ext
= 0;
306 /* Private charset. */
307 bytes
+= 2; /* For base and extended leading-codes. */
309 = (charset
< LEADING_CODE_EXT_12
310 ? LEADING_CODE_PRIVATE_11
311 : (charset
< LEADING_CODE_EXT_21
312 ? LEADING_CODE_PRIVATE_12
313 : (charset
< LEADING_CODE_EXT_22
314 ? LEADING_CODE_PRIVATE_21
315 : LEADING_CODE_PRIVATE_22
)));
316 leading_code_ext
= charset
;
319 CHARSET_TABLE_INFO (charset
, CHARSET_ID_IDX
) = charset_id
;
320 CHARSET_TABLE_INFO (charset
, CHARSET_BYTES_IDX
) = make_number (bytes
);
321 CHARSET_TABLE_INFO (charset
, CHARSET_DIMENSION_IDX
) = dimension
;
322 CHARSET_TABLE_INFO (charset
, CHARSET_CHARS_IDX
) = chars
;
323 CHARSET_TABLE_INFO (charset
, CHARSET_WIDTH_IDX
) = width
;
324 CHARSET_TABLE_INFO (charset
, CHARSET_DIRECTION_IDX
) = direction
;
325 CHARSET_TABLE_INFO (charset
, CHARSET_LEADING_CODE_BASE_IDX
)
326 = make_number (leading_code_base
);
327 CHARSET_TABLE_INFO (charset
, CHARSET_LEADING_CODE_EXT_IDX
)
328 = make_number (leading_code_ext
);
329 CHARSET_TABLE_INFO (charset
, CHARSET_ISO_FINAL_CHAR_IDX
) = iso_final_char
;
330 CHARSET_TABLE_INFO (charset
, CHARSET_ISO_GRAPHIC_PLANE_IDX
)
332 CHARSET_TABLE_INFO (charset
, CHARSET_SHORT_NAME_IDX
) = short_name
;
333 CHARSET_TABLE_INFO (charset
, CHARSET_LONG_NAME_IDX
) = long_name
;
334 CHARSET_TABLE_INFO (charset
, CHARSET_DESCRIPTION_IDX
) = description
;
335 CHARSET_TABLE_INFO (charset
, CHARSET_PLIST_IDX
) = Qnil
;
338 /* If we have already defined a charset which has the same
339 DIMENSION, CHARS and ISO-FINAL-CHAR but the different
340 DIRECTION, we must update the entry REVERSE-CHARSET of both
341 charsets. If there's no such charset, the value of the entry
345 for (i
= 0; i
<= MAX_CHARSET
; i
++)
346 if (!NILP (CHARSET_TABLE_ENTRY (i
)))
348 if (CHARSET_DIMENSION (i
) == XINT (dimension
)
349 && CHARSET_CHARS (i
) == XINT (chars
)
350 && CHARSET_ISO_FINAL_CHAR (i
) == XINT (iso_final_char
)
351 && CHARSET_DIRECTION (i
) != XINT (direction
))
353 CHARSET_TABLE_INFO (charset
, CHARSET_REVERSE_CHARSET_IDX
)
355 CHARSET_TABLE_INFO (i
, CHARSET_REVERSE_CHARSET_IDX
) = charset_id
;
360 /* No such a charset. */
361 CHARSET_TABLE_INFO (charset
, CHARSET_REVERSE_CHARSET_IDX
)
365 if (charset
!= CHARSET_ASCII
366 && charset
< MIN_CHARSET_PRIVATE_DIMENSION1
)
368 /* Update tables bytes_by_char_head and width_by_char_head. */
369 bytes_by_char_head
[leading_code_base
] = bytes
;
370 width_by_char_head
[leading_code_base
] = XINT (width
);
372 /* Update table emacs_code_class. */
373 emacs_code_class
[charset
] = (bytes
== 2
374 ? EMACS_leading_code_2
376 ? EMACS_leading_code_3
377 : EMACS_leading_code_4
));
380 /* Update table iso_charset_table. */
381 if (ISO_CHARSET_TABLE (dimension
, chars
, iso_final_char
) < 0)
382 ISO_CHARSET_TABLE (dimension
, chars
, iso_final_char
) = charset
;
387 /* Return charset id of CHARSET_SYMBOL, or return -1 if CHARSET_SYMBOL
390 get_charset_id (charset_symbol
)
391 Lisp_Object charset_symbol
;
396 return ((SYMBOLP (charset_symbol
)
397 && (val
= Fget (charset_symbol
, Qcharset
), VECTORP (val
))
398 && (charset
= XINT (XVECTOR (val
)->contents
[CHARSET_ID_IDX
]),
399 CHARSET_VALID_P (charset
)))
403 /* Return an identification number for a new private charset of
404 DIMENSION and WIDTH. If there's no more room for the new charset,
407 get_new_private_charset_id (dimension
, width
)
408 int dimension
, width
;
410 int charset
, from
, to
;
415 from
= LEADING_CODE_EXT_11
, to
= LEADING_CODE_EXT_12
;
417 from
= LEADING_CODE_EXT_12
, to
= LEADING_CODE_EXT_21
;
422 from
= LEADING_CODE_EXT_21
, to
= LEADING_CODE_EXT_22
;
424 from
= LEADING_CODE_EXT_22
, to
= LEADING_CODE_EXT_MAX
- 1;
427 for (charset
= from
; charset
< to
; charset
++)
428 if (!CHARSET_DEFINED_P (charset
)) break;
430 return make_number (charset
< to
? charset
: 0);
433 DEFUN ("define-charset", Fdefine_charset
, Sdefine_charset
, 3, 3, 0,
434 "Define CHARSET-ID as the identification number of CHARSET with INFO-VECTOR.\n\
435 If CHARSET-ID is nil, it is decided automatically, which means CHARSET is\n\
436 treated as a private charset.\n\
437 INFO-VECTOR is a vector of the format:\n\
438 [DIMENSION CHARS WIDTH DIRECTION ISO-FINAL-CHAR ISO-GRAPHIC-PLANE\n\
439 SHORT-NAME LONG-NAME DESCRIPTION]\n\
440 The meanings of each elements is as follows:\n\
441 DIMENSION (integer) is the number of bytes to represent a character: 1 or 2.\n\
442 CHARS (integer) is the number of characters in a dimension: 94 or 96.\n\
443 WIDTH (integer) is the number of columns a character in the charset\n\
444 occupies on the screen: one of 0, 1, and 2.\n\
446 DIRECTION (integer) is the rendering direction of characters in the\n\
447 charset when rendering. If 0, render from right to left, else\n\
448 render from left to right.\n\
450 ISO-FINAL-CHAR (character) is the final character of the\n\
451 corresponding ISO 2022 charset.\n\
453 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked\n\
454 while encoding to variants of ISO 2022 coding system, one of the\n\
455 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).\n\
457 SHORT-NAME (string) is the short name to refer to the charset.\n\
459 LONG-NAME (string) is the long name to refer to the charset.\n\
461 DESCRIPTION (string) is the description string of the charset.")
462 (charset_id
, charset_symbol
, info_vector
)
463 Lisp_Object charset_id
, charset_symbol
, info_vector
;
467 if (!NILP (charset_id
))
468 CHECK_NUMBER (charset_id
, 0);
469 CHECK_SYMBOL (charset_symbol
, 1);
470 CHECK_VECTOR (info_vector
, 2);
472 if (! NILP (charset_id
))
474 if (! CHARSET_VALID_P (XINT (charset_id
)))
475 error ("Invalid CHARSET: %d", XINT (charset_id
));
476 else if (CHARSET_DEFINED_P (XINT (charset_id
)))
477 error ("Already defined charset: %d", XINT (charset_id
));
480 vec
= XVECTOR (info_vector
)->contents
;
481 if (XVECTOR (info_vector
)->size
!= 9
482 || !INTEGERP (vec
[0]) || !(XINT (vec
[0]) == 1 || XINT (vec
[0]) == 2)
483 || !INTEGERP (vec
[1]) || !(XINT (vec
[1]) == 94 || XINT (vec
[1]) == 96)
484 || !INTEGERP (vec
[2]) || !(XINT (vec
[2]) == 1 || XINT (vec
[2]) == 2)
485 || !INTEGERP (vec
[3]) || !(XINT (vec
[3]) == 0 || XINT (vec
[3]) == 1)
486 || !INTEGERP (vec
[4]) || !(XINT (vec
[4]) >= '0' && XINT (vec
[4]) <= '~')
487 || !INTEGERP (vec
[5]) || !(XINT (vec
[5]) == 0 || XINT (vec
[5]) == 1)
490 || !STRINGP (vec
[8]))
491 error ("Invalid info-vector argument for defining charset %s",
492 XSYMBOL (charset_symbol
)->name
->data
);
494 if (NILP (charset_id
))
496 charset_id
= get_new_private_charset_id (XINT (vec
[0]), XINT (vec
[2]));
497 if (XINT (charset_id
) == 0)
498 error ("There's no room for a new private charset %s",
499 XSYMBOL (charset_symbol
)->name
->data
);
502 update_charset_table (charset_id
, vec
[0], vec
[1], vec
[2], vec
[3],
503 vec
[4], vec
[5], vec
[6], vec
[7], vec
[8]);
504 Fput (charset_symbol
, Qcharset
, CHARSET_TABLE_ENTRY (XINT (charset_id
)));
505 CHARSET_SYMBOL (XINT (charset_id
)) = charset_symbol
;
506 Vcharset_list
= Fcons (charset_symbol
, Vcharset_list
);
510 DEFUN ("declare-equiv-charset", Fdeclare_equiv_charset
, Sdeclare_equiv_charset
,
512 "Declare a charset of DIMENSION, CHARS, FINAL-CHAR is the same as CHARSET.\n\
513 CHARSET should be defined by `defined-charset' in advance.")
514 (dimension
, chars
, final_char
, charset_symbol
)
515 Lisp_Object dimension
, chars
, final_char
, charset_symbol
;
519 CHECK_NUMBER (dimension
, 0);
520 CHECK_NUMBER (chars
, 1);
521 CHECK_NUMBER (final_char
, 2);
522 CHECK_SYMBOL (charset_symbol
, 3);
524 if (XINT (dimension
) != 1 && XINT (dimension
) != 2)
525 error ("Invalid DIMENSION %d, it should be 1 or 2", XINT (dimension
));
526 if (XINT (chars
) != 94 && XINT (chars
) != 96)
527 error ("Invalid CHARS %d, it should be 94 or 96", XINT (chars
));
528 if (XINT (final_char
) < '0' || XFASTINT (final_char
) > '~')
529 error ("Invalid FINAL-CHAR %c, it should be `0'..`~'", XINT (chars
));
530 if ((charset
= get_charset_id (charset_symbol
)) < 0)
531 error ("Invalid charset %s", XSYMBOL (charset_symbol
)->name
->data
);
533 ISO_CHARSET_TABLE (dimension
, chars
, final_char
) = charset
;
537 /* Return number of different charsets in STR of length LEN. In
538 addition, for each found charset N, CHARSETS[N] is set 1. The
539 caller should allocate CHARSETS (MAX_CHARSET + 1 elements) in advance.
540 It may lookup a unification table TABLE if supplied. */
543 find_charset_in_str (str
, len
, charsets
, table
)
550 unsigned char str_work
[4], *str_tmp
;
552 if (! CHAR_TABLE_P (table
))
559 if (*str
== LEADING_CODE_COMPOSITION
)
565 else if (CHAR_HEAD_P (str
))
574 str_work
[0] = *str
& 0x7F;
578 bcopy (str
, str_work
, min (4, len
));
586 bytes
= BYTES_BY_CHAR_HEAD (*str_tmp
);
589 charset
= CHARSET_AT (str_tmp
);
593 unsigned char c1
, c2
;
595 SPLIT_STRING(str_tmp
, bytes
, charset
, c1
, c2
);
596 if ((c
= unify_char (table
, -1, charset
, c1
, c2
)) >= 0)
597 charset
= CHAR_CHARSET (c
);
600 if (!charsets
[charset
])
602 charsets
[charset
] = 1;
611 DEFUN ("find-charset-region", Ffind_charset_region
, Sfind_charset_region
,
613 "Return a list of charsets in the region between BEG and END.\n\
614 BEG and END are buffer positions.\n\
615 Optional arg TABLE if non-nil is a unification table to look up.")
617 Lisp_Object beg
, end
, table
;
619 int charsets
[MAX_CHARSET
+ 1];
620 int from
, to
, stop
, i
;
623 validate_region (&beg
, &end
);
624 from
= XFASTINT (beg
);
625 stop
= to
= XFASTINT (end
);
626 if (from
< GPT
&& GPT
< to
)
628 bzero (charsets
, (MAX_CHARSET
+ 1) * sizeof (int));
631 find_charset_in_str (POS_ADDR (from
), stop
- from
, charsets
, table
);
633 from
= stop
, stop
= to
;
638 for (i
= MAX_CHARSET
; i
>= 0; i
--)
640 val
= Fcons (CHARSET_SYMBOL (i
), val
);
644 DEFUN ("find-charset-string", Ffind_charset_string
, Sfind_charset_string
,
646 "Return a list of charsets in STR.\n\
647 Optional arg TABLE if non-nil is a unification table to look up.")
649 Lisp_Object str
, table
;
651 int charsets
[MAX_CHARSET
+ 1];
655 CHECK_STRING (str
, 0);
656 bzero (charsets
, (MAX_CHARSET
+ 1) * sizeof (int));
657 find_charset_in_str (XSTRING (str
)->data
, XSTRING (str
)->size
,
660 for (i
= MAX_CHARSET
; i
>= 0; i
--)
662 val
= Fcons (CHARSET_SYMBOL (i
), val
);
666 DEFUN ("make-char-internal", Fmake_char_internal
, Smake_char_internal
, 1, 3, 0,
668 (charset
, code1
, code2
)
669 Lisp_Object charset
, code1
, code2
;
671 CHECK_NUMBER (charset
, 0);
674 XSETFASTINT (code1
, 0);
676 CHECK_NUMBER (code1
, 1);
678 XSETFASTINT (code2
, 0);
680 CHECK_NUMBER (code2
, 2);
682 if (!CHARSET_DEFINED_P (XINT (charset
)))
683 error ("Invalid charset: %d", XINT (charset
));
685 return make_number (MAKE_CHAR (XINT (charset
), XINT (code1
), XINT (code2
)));
688 DEFUN ("split-char", Fsplit_char
, Ssplit_char
, 1, 1, 0,
689 "Return list of charset and one or two position-codes of CHAR.")
696 CHECK_NUMBER (ch
, 0);
697 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
699 ? Fcons (CHARSET_SYMBOL (charset
),
700 Fcons (make_number (c1
), Fcons (make_number (c2
), Qnil
)))
701 : Fcons (CHARSET_SYMBOL (charset
), Fcons (make_number (c1
), Qnil
)));
704 DEFUN ("char-charset", Fchar_charset
, Schar_charset
, 1, 1, 0,
705 "Return charset of CHAR.")
709 CHECK_NUMBER (ch
, 0);
711 return CHARSET_SYMBOL (CHAR_CHARSET (XINT (ch
)));
714 DEFUN ("iso-charset", Fiso_charset
, Siso_charset
, 3, 3, 0,
715 "Return charset of ISO's specification DIMENSION, CHARS, and FINAL-CHAR.")
716 (dimension
, chars
, final_char
)
717 Lisp_Object dimension
, chars
, final_char
;
721 CHECK_NUMBER (dimension
, 0);
722 CHECK_NUMBER (chars
, 1);
723 CHECK_NUMBER (final_char
, 2);
725 if ((charset
= ISO_CHARSET_TABLE (dimension
, chars
, final_char
)) < 0)
727 return CHARSET_SYMBOL (charset
);
730 /* If GENERICP is nonzero, return nonzero iff C is a valid normal or
731 generic character. If GENERICP is zero, return nonzero iff C is a
732 valid normal character. Do not call this function directly,
733 instead use macro CHAR_VALID_P. */
735 char_valid_p (c
, genericp
)
742 if (SINGLE_BYTE_CHAR_P (c
))
744 SPLIT_NON_ASCII_CHAR (c
, charset
, c1
, c2
);
745 if (!CHARSET_VALID_P (charset
))
747 return (c
< MIN_CHAR_COMPOSITION
748 ? ((c
& CHAR_FIELD1_MASK
) /* i.e. dimension of C is two. */
749 ? (genericp
&& c1
== 0 && c2
== 0
750 || c1
>= 32 && c2
>= 32)
751 : (genericp
&& c1
== 0
753 : c
< MIN_CHAR_COMPOSITION
+ n_cmpchars
);
756 DEFUN ("char-valid-p", Fchar_valid_p
, Schar_valid_p
, 1, 2, 0,
757 "Return t if OBJECT is a valid normal character.
758 If optional arg GENERICP is non-nil, also return t if OBJECT is
759 a valid generic character.")
761 Lisp_Object object
, genericp
;
763 if (! NATNUMP (object
))
765 return (CHAR_VALID_P (XFASTINT (object
), !NILP (genericp
)) ? Qt
: Qnil
);
768 DEFUN ("char-bytes", Fchar_bytes
, Schar_bytes
, 1, 1, 0,
769 "Return byte length of multi-byte form of CHAR.")
776 CHECK_NUMBER (ch
, 0);
777 if (COMPOSITE_CHAR_P (XFASTINT (ch
)))
779 unsigned int id
= COMPOSITE_CHAR_ID (XFASTINT (ch
));
781 bytes
= (id
< n_cmpchars
? cmpchar_table
[id
]->len
: 1);
785 int charset
= CHAR_CHARSET (XFASTINT (ch
));
787 bytes
= CHARSET_DEFINED_P (charset
) ? CHARSET_BYTES (charset
) : 1;
790 XSETFASTINT (val
, bytes
);
794 /* Return the width of character of which multi-byte form starts with
795 C. The width is measured by how many columns occupied on the
796 screen when displayed in the current buffer. */
798 #define ONE_BYTE_CHAR_WIDTH(c) \
801 ? XFASTINT (current_buffer->tab_width) \
802 : (c == '\n' ? 0 : (NILP (current_buffer->ctl_arrow) ? 4 : 2))) \
806 ? (NILP (current_buffer->ctl_arrow) ? 4 : 2) \
807 : ((! NILP (current_buffer->enable_multibyte_characters) \
808 && BASE_LEADING_CODE_P (c)) \
809 ? WIDTH_BY_CHAR_HEAD (c) \
813 DEFUN ("char-width", Fchar_width, Schar_width, 1, 1, 0,
814 "Return width of CHAR when displayed in the current buffer.\n\
815 The width is measured by how many columns it occupies on the screen.")
819 Lisp_Object val
, disp
;
821 struct Lisp_Char_Table
*dp
= buffer_display_table ();
823 CHECK_NUMBER (ch
, 0);
827 /* Get the way the display table would display it. */
828 disp
= dp
? DISP_CHAR_VECTOR (dp
, c
) : Qnil
;
831 XSETINT (val
, XVECTOR (disp
)->size
);
832 else if (SINGLE_BYTE_CHAR_P (c
))
833 XSETINT (val
, ONE_BYTE_CHAR_WIDTH (c
));
834 else if (COMPOSITE_CHAR_P (c
))
836 int id
= COMPOSITE_CHAR_ID (XFASTINT (ch
));
837 XSETFASTINT (val
, (id
< n_cmpchars
? cmpchar_table
[id
]->width
: 0));
841 int charset
= CHAR_CHARSET (c
);
843 XSETFASTINT (val
, CHARSET_WIDTH (charset
));
848 /* Return width of string STR of length LEN when displayed in the
849 current buffer. The width is measured by how many columns it
850 occupies on the screen. */
857 unsigned char *endp
= str
+ len
;
859 struct Lisp_Char_Table
*dp
= buffer_display_table (current_buffer
);
863 if (*str
== LEADING_CODE_COMPOSITION
)
865 int id
= str_cmpchar_id (str
, endp
- str
);
874 width
+= cmpchar_table
[id
]->width
;
875 str
+= cmpchar_table
[id
]->len
;
882 int c
= STRING_CHAR (str
, endp
- str
);
884 /* Get the way the display table would display it. */
886 disp
= DISP_CHAR_VECTOR (dp
, c
);
891 thiswidth
= XVECTOR (disp
)->size
;
893 thiswidth
= ONE_BYTE_CHAR_WIDTH (*str
);
896 str
+= BYTES_BY_CHAR_HEAD (*str
);
902 DEFUN ("string-width", Fstring_width
, Sstring_width
, 1, 1, 0,
903 "Return width of STRING when displayed in the current buffer.\n\
904 Width is measured by how many columns it occupies on the screen.\n\
905 When calculating width of a multibyte character in STRING,\n\
906 only the base leading-code is considered; the validity of\n\
907 the following bytes is not checked.")
913 CHECK_STRING (str
, 0);
914 XSETFASTINT (val
, strwidth (XSTRING (str
)->data
, XSTRING (str
)->size
));
918 DEFUN ("char-direction", Fchar_direction
, Schar_direction
, 1, 1, 0,
919 "Return the direction of CHAR.\n\
920 The returned value is 0 for left-to-right and 1 for right-to-left.")
926 CHECK_NUMBER (ch
, 0);
927 charset
= CHAR_CHARSET (XFASTINT (ch
));
928 if (!CHARSET_DEFINED_P (charset
))
929 error ("Invalid character: %d", XINT (ch
));
930 return CHARSET_TABLE_INFO (charset
, CHARSET_DIRECTION_IDX
);
933 DEFUN ("chars-in-string", Fchars_in_string
, Schars_in_string
, 1, 1, 0,
934 "Return number of characters in STRING.\n\
935 When using multibyte characters, this is not the necessarily same as\n\
936 the length of STRING; the length counts a multibyte characters as\n\
937 several bytes, but this function counts a multibyte character as one\n\
943 unsigned char *p
, *endp
;
946 CHECK_STRING (str
, 0);
948 if (NILP (current_buffer
->enable_multibyte_characters
))
949 return make_number (XSTRING (str
)->size
);
951 p
= XSTRING (str
)->data
; endp
= p
+ XSTRING (str
)->size
;
955 if (*p
== LEADING_CODE_COMPOSITION
)
958 while (p
< endp
&& ! CHAR_HEAD_P (p
)) p
++;
961 p
+= BYTES_BY_CHAR_HEAD (*p
);
965 XSETFASTINT (val
, chars
);
969 DEFUN ("chars-in-region", Fchars_in_region
, Schars_in_region
, 2, 2, 0,
970 "Return number of characters between BEG and END.\n\
971 When using multibyte characters, this is not the necessarily same\n\
972 as (- END BEG); that subtraction gives you the number of bytes, which\n\
973 may be more than the number of characters.")
975 Lisp_Object beg
, end
;
980 unsigned char *p
, *endp
;
982 validate_region (&beg
, &end
);
984 from
= min (XFASTINT (beg
), XFASTINT (end
));
985 to
= max (XFASTINT (beg
), XFASTINT (end
));
988 if (NILP (current_buffer
->enable_multibyte_characters
))
989 return make_number (to
- from
);
991 if (from
< GPT
&& GPT
<= to
)
999 endp
= POS_ADDR (stop
);
1009 p
= POS_ADDR (stop
);
1011 endp
= POS_ADDR (stop
);
1014 if (*p
== LEADING_CODE_COMPOSITION
)
1017 while (p
< endp
&& ! CHAR_HEAD_P (p
)) p
++;
1020 p
+= BYTES_BY_CHAR_HEAD (*p
);
1025 return make_number (chars
);
1028 DEFUN ("char-boundary-p", Fchar_boundary_p
, Schar_boundary_p
, 1, 1, 0,
1029 "Return non-nil value if POS is at character boundary of multibyte form.\n\
1030 When the value is non-nil, it contains some additional information:\n\
1031 0 if POS is at an ASCII character or at the end of range,\n\
1032 1 if POS is before a 2-byte length multi-byte form,\n\
1033 2 if POS is before a 3-byte length multi-byte form,\n\
1034 3 if POS is before a 4-byte length multi-byte form,\n\
1035 4 if POS is before a composite character.\n\
1036 If POS is out of range or not at character boundary, return NIL.")
1043 CHECK_NUMBER_COERCE_MARKER (pos
, 0);
1046 if (n
< BEGV
|| n
> ZV
)
1049 if (n
== ZV
|| NILP (current_buffer
->enable_multibyte_characters
))
1050 XSETFASTINT (val
, 0);
1053 unsigned char *p
= POS_ADDR (n
);
1055 if (SINGLE_BYTE_CHAR_P (*p
))
1056 XSETFASTINT (val
, 0);
1057 else if (*p
== LEADING_CODE_COMPOSITION
)
1058 XSETFASTINT (val
, 4);
1059 else if (BYTES_BY_CHAR_HEAD (*p
) > 1)
1060 XSETFASTINT (val
, BYTES_BY_CHAR_HEAD (*p
) - 1);
1067 DEFUN ("concat-chars", Fconcat_chars
, Sconcat_chars
, 1, MANY
, 0,
1068 "Concatenate all the argument characters and make the result a string.")
1075 = (unsigned char *) alloca (MAX_LENGTH_OF_MULTI_BYTE_FORM
* n
);
1076 unsigned char *p
= buf
;
1079 for (i
= 0; i
< n
; i
++)
1084 if (!INTEGERP (args
[i
]))
1087 CHECK_NUMBER (args
[i
], 0);
1090 len
= CHAR_STRING (c
, p
, str
);
1092 /* C is a composite character. */
1093 bcopy (str
, p
, len
);
1097 val
= make_string (buf
, p
- buf
);
1103 /*** Composite characters staffs ***/
1105 /* Each composite character is identified by CMPCHAR-ID which is
1106 assigned when Emacs needs the character code of the composite
1107 character (e.g. when displaying it on the screen). See the
1108 document "GENERAL NOTE on COMPOSITE CHARACTER" in `charset.h' how a
1109 composite character is represented in Emacs. */
1111 /* If `static' is defined, it means that it is defined to null string. */
1113 /* The following function is copied from lread.c. */
1115 hash_string (ptr
, len
)
1119 register unsigned char *p
= ptr
;
1120 register unsigned char *end
= p
+ len
;
1121 register unsigned char c
;
1122 register int hash
= 0;
1127 if (c
>= 0140) c
-= 40;
1128 hash
= ((hash
<<3) + (hash
>>28) + c
);
1130 return hash
& 07777777777;
1134 #define CMPCHAR_HASH_TABLE_SIZE 0xFFF
1136 static int *cmpchar_hash_table
[CMPCHAR_HASH_TABLE_SIZE
];
1138 /* Each element of `cmpchar_hash_table' is a pointer to an array of
1139 integer, where the 1st element is the size of the array, the 2nd
1140 element is how many elements are actually used in the array, and
1141 the remaining elements are CMPCHAR-IDs of composite characters of
1142 the same hash value. */
1143 #define CMPCHAR_HASH_SIZE(table) table[0]
1144 #define CMPCHAR_HASH_USED(table) table[1]
1145 #define CMPCHAR_HASH_CMPCHAR_ID(table, i) table[i]
1147 /* Return CMPCHAR-ID of the composite character in STR of the length
1148 LEN. If the composite character has not yet been registered,
1149 register it in `cmpchar_table' and assign new CMPCHAR-ID. This
1150 is the sole function for assigning CMPCHAR-ID. */
1152 str_cmpchar_id (str
, len
)
1156 int hash_idx
, *hashp
;
1158 int embedded_rule
; /* 1 if composition rule is embedded. */
1159 int chars
; /* number of components. */
1161 struct cmpchar_info
*cmpcharp
;
1164 /* Any composite char have at least 3-byte length. */
1167 /* The second byte 0xFF means compostion rule is embedded. */
1168 embedded_rule
= (str
[1] == 0xFF);
1170 /* At first, get the actual length of the composite character. */
1172 unsigned char *p
, *endp
= str
+ 1, *lastp
= str
+ len
;
1175 while (endp
< lastp
&& ! CHAR_HEAD_P (endp
)) endp
++;
1177 p
= str
+ 1 + embedded_rule
;
1180 /* No need of checking if *P is 0xA0 because
1181 BYTES_BY_CHAR_HEAD (0x80) surely returns 2. */
1182 p
+= (bytes
= BYTES_BY_CHAR_HEAD (*p
- 0x20) + embedded_rule
);
1185 len
= (p
-= embedded_rule
) - str
;
1187 len
-= - bytes
, chars
--;
1189 if (chars
< 2 || chars
> MAX_COMPONENT_COUNT
)
1190 /* Invalid number of components. */
1193 hash_idx
= hash_string (str
, len
) % CMPCHAR_HASH_TABLE_SIZE
;
1194 hashp
= cmpchar_hash_table
[hash_idx
];
1196 /* Then, look into the hash table. */
1198 /* Find the correct one among composite characters of the same
1200 for (i
= 2; i
< CMPCHAR_HASH_USED (hashp
); i
++)
1202 cmpcharp
= cmpchar_table
[CMPCHAR_HASH_CMPCHAR_ID (hashp
, i
)];
1203 if (len
== cmpcharp
->len
1204 && ! bcmp (str
, cmpcharp
->data
, len
))
1205 return CMPCHAR_HASH_CMPCHAR_ID (hashp
, i
);
1208 /* We have to register the composite character in cmpchar_table. */
1209 if (n_cmpchars
> (CHAR_FIELD2_MASK
| CHAR_FIELD3_MASK
))
1210 /* No, we have no more room for a new composite character. */
1213 /* Make the entry in hash table. */
1216 /* Make a table for 8 composite characters initially. */
1217 hashp
= (cmpchar_hash_table
[hash_idx
]
1218 = (int *) xmalloc (sizeof (int) * (2 + 8)));
1219 CMPCHAR_HASH_SIZE (hashp
) = 10;
1220 CMPCHAR_HASH_USED (hashp
) = 2;
1222 else if (CMPCHAR_HASH_USED (hashp
) >= CMPCHAR_HASH_SIZE (hashp
))
1224 CMPCHAR_HASH_SIZE (hashp
) += 8;
1225 hashp
= (cmpchar_hash_table
[hash_idx
]
1226 = (int *) xrealloc (hashp
,
1227 sizeof (int) * CMPCHAR_HASH_SIZE (hashp
)));
1229 CMPCHAR_HASH_CMPCHAR_ID (hashp
, CMPCHAR_HASH_USED (hashp
)) = n_cmpchars
;
1230 CMPCHAR_HASH_USED (hashp
)++;
1232 /* Set information of the composite character in cmpchar_table. */
1233 if (cmpchar_table_size
== 0)
1235 /* This is the first composite character to be registered. */
1236 cmpchar_table_size
= 256;
1238 = (struct cmpchar_info
**) xmalloc (sizeof (cmpchar_table
[0])
1239 * cmpchar_table_size
);
1241 else if (cmpchar_table_size
<= n_cmpchars
)
1243 cmpchar_table_size
+= 256;
1245 = (struct cmpchar_info
**) xrealloc (cmpchar_table
,
1246 sizeof (cmpchar_table
[0])
1247 * cmpchar_table_size
);
1250 cmpcharp
= (struct cmpchar_info
*) xmalloc (sizeof (struct cmpchar_info
));
1252 cmpcharp
->len
= len
;
1253 cmpcharp
->data
= (unsigned char *) xmalloc (len
+ 1);
1254 bcopy (str
, cmpcharp
->data
, len
);
1255 cmpcharp
->data
[len
] = 0;
1256 cmpcharp
->glyph_len
= chars
;
1257 cmpcharp
->glyph
= (GLYPH
*) xmalloc (sizeof (GLYPH
) * chars
);
1260 cmpcharp
->cmp_rule
= (unsigned char *) xmalloc (chars
);
1261 cmpcharp
->col_offset
= (float *) xmalloc (sizeof (float) * chars
);
1265 cmpcharp
->cmp_rule
= NULL
;
1266 cmpcharp
->col_offset
= NULL
;
1269 /* Setup GLYPH data and composition rules (if any) so as not to make
1270 them every time on displaying. */
1272 unsigned char *bufp
;
1274 float leftmost
= 0.0, rightmost
= 1.0;
1277 /* At first, col_offset[N] is set to relative to col_offset[0]. */
1278 cmpcharp
->col_offset
[0] = 0;
1280 for (i
= 0, bufp
= cmpcharp
->data
+ 1; i
< chars
; i
++)
1283 cmpcharp
->cmp_rule
[i
] = *bufp
++;
1285 if (*bufp
== 0xA0) /* This is an ASCII character. */
1287 cmpcharp
->glyph
[i
] = FAST_MAKE_GLYPH ((*++bufp
& 0x7F), 0);
1291 else /* Multibyte character. */
1293 /* Make `bufp' point normal multi-byte form temporally. */
1296 = FAST_MAKE_GLYPH (string_to_non_ascii_char (bufp
, 4, 0), 0);
1297 width
= WIDTH_BY_CHAR_HEAD (*bufp
);
1299 bufp
+= BYTES_BY_CHAR_HEAD (*bufp
- 0x20);
1302 if (embedded_rule
&& i
> 0)
1304 /* Reference points (global_ref and new_ref) are
1315 Now, we calculate the column offset of the new glyph
1316 from the left edge of the first glyph. This can avoid
1317 the same calculation everytime displaying this
1318 composite character. */
1320 /* Reference points of global glyph and new glyph. */
1321 int global_ref
= (cmpcharp
->cmp_rule
[i
] - 0xA0) / 9;
1322 int new_ref
= (cmpcharp
->cmp_rule
[i
] - 0xA0) % 9;
1323 /* Column offset relative to the first glyph. */
1324 float left
= (leftmost
1325 + (global_ref
% 3) * (rightmost
- leftmost
) / 2.0
1326 - (new_ref
% 3) * width
/ 2.0);
1328 cmpcharp
->col_offset
[i
] = left
;
1329 if (left
< leftmost
)
1331 if (left
+ width
> rightmost
)
1332 rightmost
= left
+ width
;
1336 if (width
> rightmost
)
1342 /* Now col_offset[N] are relative to the left edge of the
1343 first component. Make them relative to the left edge of
1345 for (i
= 0; i
< chars
; i
++)
1346 cmpcharp
->col_offset
[i
] -= leftmost
;
1347 /* Make rightmost holds width of overall glyph. */
1348 rightmost
-= leftmost
;
1351 cmpcharp
->width
= rightmost
;
1352 if (cmpcharp
->width
< rightmost
)
1353 /* To get a ceiling integer value. */
1357 cmpchar_table
[n_cmpchars
] = cmpcharp
;
1359 return n_cmpchars
++;
1362 /* Return the Nth element of the composite character C. */
1364 cmpchar_component (c
, n
)
1367 int id
= COMPOSITE_CHAR_ID (c
);
1369 if (id
>= n_cmpchars
/* C is not a valid composite character. */
1370 || n
>= cmpchar_table
[id
]->glyph_len
) /* No such component. */
1372 /* No face data is stored in glyph code. */
1373 return ((int) (cmpchar_table
[id
]->glyph
[n
]));
1376 DEFUN ("cmpcharp", Fcmpcharp
, Scmpcharp
, 1, 1, 0,
1377 "T if CHAR is a composite character.")
1381 CHECK_NUMBER (ch
, 0);
1382 return (COMPOSITE_CHAR_P (XINT (ch
)) ? Qt
: Qnil
);
1385 DEFUN ("composite-char-component", Fcmpchar_component
, Scmpchar_component
,
1387 "Return the IDXth component character of composite character CHARACTER.")
1389 Lisp_Object character
, idx
;
1393 CHECK_NUMBER (character
, 0);
1394 CHECK_NUMBER (idx
, 1);
1396 if ((c
= cmpchar_component (XINT (character
), XINT (idx
))) < 0)
1397 args_out_of_range (character
, idx
);
1399 return make_number (c
);
1402 DEFUN ("composite-char-composition-rule", Fcmpchar_cmp_rule
, Scmpchar_cmp_rule
,
1404 "Return the Nth composition rule embedded in composite character CHARACTER.\n\
1405 The returned rule is for composing the Nth component\n\
1406 on the (N-1)th component. If N is 0, the returned value is always 255.")
1408 Lisp_Object character
, n
;
1412 CHECK_NUMBER (character
, 0);
1413 CHECK_NUMBER (n
, 1);
1415 id
= COMPOSITE_CHAR_ID (XINT (character
));
1416 if (id
< 0 || id
>= n_cmpchars
)
1417 error ("Invalid composite character: %d", XINT (character
));
1419 if (i
> cmpchar_table
[id
]->glyph_len
)
1420 args_out_of_range (character
, n
);
1422 return make_number (cmpchar_table
[id
]->cmp_rule
[i
]);
1425 DEFUN ("composite-char-composition-rule-p", Fcmpchar_cmp_rule_p
,
1426 Scmpchar_cmp_rule_p
, 1, 1, 0,
1427 "Return non-nil if composite character CHARACTER contains a embedded rule.")
1429 Lisp_Object character
;
1433 CHECK_NUMBER (character
, 0);
1434 id
= COMPOSITE_CHAR_ID (XINT (character
));
1435 if (id
< 0 || id
>= n_cmpchars
)
1436 error ("Invalid composite character: %d", XINT (character
));
1438 return (cmpchar_table
[id
]->cmp_rule
? Qt
: Qnil
);
1441 DEFUN ("composite-char-component-count", Fcmpchar_cmp_count
,
1442 Scmpchar_cmp_count
, 1, 1, 0,
1443 "Return number of compoents of composite character CHARACTER.")
1445 Lisp_Object character
;
1449 CHECK_NUMBER (character
, 0);
1450 id
= COMPOSITE_CHAR_ID (XINT (character
));
1451 if (id
< 0 || id
>= n_cmpchars
)
1452 error ("Invalid composite character: %d", XINT (character
));
1454 return (make_number (cmpchar_table
[id
]->glyph_len
));
1457 DEFUN ("compose-string", Fcompose_string
, Scompose_string
,
1459 "Return one char string composed from all characters in STRING.")
1463 unsigned char buf
[MAX_LENGTH_OF_MULTI_BYTE_FORM
], *p
, *pend
, *ptemp
;
1466 CHECK_STRING (str
, 0);
1468 buf
[0] = LEADING_CODE_COMPOSITION
;
1469 p
= XSTRING (str
)->data
;
1470 pend
= p
+ XSTRING (str
)->size
;
1474 if (*p
< 0x20 || *p
== 127) /* control code */
1475 error ("Invalid component character: %d", *p
);
1476 else if (*p
< 0x80) /* ASCII */
1478 if (i
+ 2 >= MAX_LENGTH_OF_MULTI_BYTE_FORM
)
1479 error ("Too long string to be composed: %s", XSTRING (str
)->data
);
1480 /* Prepend an ASCII charset indicator 0xA0, set MSB of the
1483 buf
[i
++] = *p
++ + 0x80;
1485 else if (*p
== LEADING_CODE_COMPOSITION
) /* composite char */
1487 /* Already composed. Eliminate the heading
1488 LEADING_CODE_COMPOSITION, keep the remaining bytes
1492 while (! CHAR_HEAD_P (p
)) p
++;
1493 if (i
+ (p
- ptemp
) >= MAX_LENGTH_OF_MULTI_BYTE_FORM
)
1494 error ("Too long string to be composed: %s", XSTRING (str
)->data
);
1495 bcopy (ptemp
, buf
+ i
, p
- ptemp
);
1498 else /* multibyte char */
1500 /* Add 0x20 to the base leading-code, keep the remaining
1502 len
= BYTES_BY_CHAR_HEAD (*p
);
1503 if (i
+ len
>= MAX_LENGTH_OF_MULTI_BYTE_FORM
)
1504 error ("Too long string to be composed: %s", XSTRING (str
)->data
);
1505 bcopy (p
, buf
+ i
, len
);
1512 /* STR contains only one character, which can't be composed. */
1513 error ("Too short string to be composed: %s", XSTRING (str
)->data
);
1515 return make_string (buf
, i
);
1519 charset_id_internal (charset_name
)
1522 Lisp_Object val
= Fget (intern (charset_name
), Qcharset
);
1525 error ("Charset %s is not defined", charset_name
);
1527 return (XINT (XVECTOR (val
)->contents
[0]));
1530 DEFUN ("setup-special-charsets", Fsetup_special_charsets
,
1531 Ssetup_special_charsets
, 0, 0, 0, "Internal use only.")
1534 charset_latin_iso8859_1
= charset_id_internal ("latin-iso8859-1");
1535 charset_jisx0208_1978
= charset_id_internal ("japanese-jisx0208-1978");
1536 charset_jisx0208
= charset_id_internal ("japanese-jisx0208");
1537 charset_katakana_jisx0201
= charset_id_internal ("katakana-jisx0201");
1538 charset_latin_jisx0201
= charset_id_internal ("latin-jisx0201");
1539 charset_big5_1
= charset_id_internal ("chinese-big5-1");
1540 charset_big5_2
= charset_id_internal ("chinese-big5-2");
1544 init_charset_once ()
1548 staticpro (&Vcharset_table
);
1549 staticpro (&Vcharset_symbol_table
);
1551 /* This has to be done here, before we call Fmake_char_table. */
1552 Qcharset_table
= intern ("charset-table");
1553 staticpro (&Qcharset_table
);
1555 /* Intern this now in case it isn't already done.
1556 Setting this variable twice is harmless.
1557 But don't staticpro it here--that is done in alloc.c. */
1558 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
1560 /* Now we are ready to set up this property, so we can
1561 create the charset table. */
1562 Fput (Qcharset_table
, Qchar_table_extra_slots
, make_number (0));
1563 Vcharset_table
= Fmake_char_table (Qcharset_table
, Qnil
);
1565 Vcharset_symbol_table
= Fmake_vector (make_number (MAX_CHARSET
+ 1), Qnil
);
1568 for (i
= 0; i
< 2; i
++)
1569 for (j
= 0; j
< 2; j
++)
1570 for (k
= 0; k
< 128; k
++)
1571 iso_charset_table
[i
][j
][k
] = -1;
1573 bzero (cmpchar_hash_table
, sizeof cmpchar_hash_table
);
1574 cmpchar_table_size
= n_cmpchars
= 0;
1576 for (i
= 0; i
< 256; i
++)
1577 BYTES_BY_CHAR_HEAD (i
) = 1;
1578 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_11
) = 3;
1579 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_12
) = 3;
1580 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_21
) = 4;
1581 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_22
) = 4;
1582 /* The following doesn't reflect the actual bytes, but just to tell
1583 that it is a start of a multibyte character. */
1584 BYTES_BY_CHAR_HEAD (LEADING_CODE_COMPOSITION
) = 2;
1586 for (i
= 0; i
< 128; i
++)
1587 WIDTH_BY_CHAR_HEAD (i
) = 1;
1588 for (; i
< 256; i
++)
1589 WIDTH_BY_CHAR_HEAD (i
) = 4;
1590 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_11
) = 1;
1591 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_12
) = 2;
1592 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_21
) = 1;
1593 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_22
) = 2;
1600 Qascii
= intern ("ascii");
1601 staticpro (&Qascii
);
1603 Qcharset
= intern ("charset");
1604 staticpro (&Qcharset
);
1606 /* Define ASCII charset now. */
1607 update_charset_table (make_number (CHARSET_ASCII
),
1608 make_number (1), make_number (94),
1613 build_string ("ASCII"),
1614 build_string ("ASCII"),
1615 build_string ("ASCII (ISO646 IRV)"));
1616 CHARSET_SYMBOL (CHARSET_ASCII
) = Qascii
;
1617 Fput (Qascii
, Qcharset
, CHARSET_TABLE_ENTRY (CHARSET_ASCII
));
1619 Qcomposition
= intern ("composition");
1620 staticpro (&Qcomposition
);
1621 CHARSET_SYMBOL (CHARSET_COMPOSITION
) = Qcomposition
;
1623 defsubr (&Sdefine_charset
);
1624 defsubr (&Sdeclare_equiv_charset
);
1625 defsubr (&Sfind_charset_region
);
1626 defsubr (&Sfind_charset_string
);
1627 defsubr (&Smake_char_internal
);
1628 defsubr (&Ssplit_char
);
1629 defsubr (&Schar_charset
);
1630 defsubr (&Siso_charset
);
1631 defsubr (&Schar_valid_p
);
1632 defsubr (&Schar_bytes
);
1633 defsubr (&Schar_width
);
1634 defsubr (&Sstring_width
);
1635 defsubr (&Schar_direction
);
1636 defsubr (&Schars_in_string
);
1637 defsubr (&Schars_in_region
);
1638 defsubr (&Schar_boundary_p
);
1639 defsubr (&Sconcat_chars
);
1640 defsubr (&Scmpcharp
);
1641 defsubr (&Scmpchar_component
);
1642 defsubr (&Scmpchar_cmp_rule
);
1643 defsubr (&Scmpchar_cmp_rule_p
);
1644 defsubr (&Scmpchar_cmp_count
);
1645 defsubr (&Scompose_string
);
1646 defsubr (&Ssetup_special_charsets
);
1648 DEFVAR_LISP ("charset-list", &Vcharset_list
,
1649 "List of charsets ever defined.");
1650 Vcharset_list
= Fcons (Qascii
, Qnil
);
1652 DEFVAR_INT ("leading-code-composition", &leading_code_composition
,
1653 "Leading-code of composite characters.");
1654 leading_code_composition
= LEADING_CODE_COMPOSITION
;
1656 DEFVAR_INT ("leading-code-private-11", &leading_code_private_11
,
1657 "Leading-code of private TYPE9N charset of column-width 1.");
1658 leading_code_private_11
= LEADING_CODE_PRIVATE_11
;
1660 DEFVAR_INT ("leading-code-private-12", &leading_code_private_12
,
1661 "Leading-code of private TYPE9N charset of column-width 2.");
1662 leading_code_private_12
= LEADING_CODE_PRIVATE_12
;
1664 DEFVAR_INT ("leading-code-private-21", &leading_code_private_21
,
1665 "Leading-code of private TYPE9Nx9N charset of column-width 1.");
1666 leading_code_private_21
= LEADING_CODE_PRIVATE_21
;
1668 DEFVAR_INT ("leading-code-private-22", &leading_code_private_22
,
1669 "Leading-code of private TYPE9Nx9N charset of column-width 2.");
1670 leading_code_private_22
= LEADING_CODE_PRIVATE_22
;