Slightly better basic type detection.
[sxemacs] / info / lispref / mule.texi
1 @c -*-texinfo-*-
2 @c This is part of the SXEmacs Lisp Reference Manual.
3 @c Copyright (C) 1996 Ben Wing.
4 @c Copyright (C) 2005 Sebastian Freundt <hroptatyr@sxemacs.org>
5 @c See the file lispref.texi for copying conditions.
6 @setfilename ../../info/internationalization.info
7
8 @node MULE, Tips, Foreign Functions, top
9 @chapter MULE
10
11   @dfn{MULE} is the name originally given to the version of GNU Emacs
12 extended for multi-lingual (and in particular Asian-language) support.
13 ``MULE'' is short for ``MUlti-Lingual Emacs''.  It is an extension and
14 complete rewrite of Nemacs (``Nihon Emacs'' where ``Nihon'' is the
15 Japanese word for ``Japan''), which only provided support for Japanese.
16 SXEmacs refers to its multi-lingual support as @dfn{MULE support} since
17 it is based on @dfn{MULE}.
18
19 @menu
20 * Internationalization Terminology::
21                         Definition of various internationalization terms.
22 * Charsets::            Sets of related characters.
23 * MULE Characters::     Working with characters in SXEmacs/MULE.
24 * Composite Characters:: Making new characters by overstriking other ones.
25 * Coding Systems::      Ways of representing a string of chars using integers.
26 * CCL::                 A special language for writing fast converters.
27 * Category Tables::     Subdividing charsets into groups.
28 @end menu
29
30
31 @node Internationalization Terminology, Charsets, , MULE
32 @section Internationalization Terminology
33
34   In internationalization terminology, a string of text is divided up
35 into @dfn{characters}, which are the printable units that make up the
36 text.  A single character is (for example) a capital @samp{A}, the
37 number @samp{2}, a Katakana character, a Hangul character, a Kanji
38 ideograph (an @dfn{ideograph} is a ``picture'' character, such as is
39 used in Japanese Kanji, Chinese Hanzi, and Korean Hanja; typically there
40 are thousands of such ideographs in each language), etc.  The basic
41 property of a character is that it is the smallest unit of text with
42 semantic significance in text processing.
43
44   Human beings normally process text visually, so to a first approximation
45 a character may be identified with its shape.  Note that the same
46 character may be drawn by two different people (or in two different
47 fonts) in slightly different ways, although the "basic shape" will be the
48 same.  But consider the works of Scott Kim; human beings can recognize
49 hugely variant shapes as the "same" character.  Sometimes, especially
50 where characters are extremely complicated to write, completely
51 different shapes may be defined as the "same" character in national
52 standards.  The Taiwanese variant of Hanzi is generally the most
53 complicated; over the centuries, the Japanese, Koreans, and the People's
54 Republic of China have adopted simplifications of the shape, but the
55 line of descent from the original shape is recorded, and the meanings
56 and pronunciation of different forms of the same character are
57 considered to be identical within each language.  (Of course, it may
58 take a specialist to recognize the related form; the point is that the
59 relations are standardized, despite the differing shapes.)
60
61   In some cases, the differences will be significant enough that it is
62 actually possible to identify two or more distinct shapes that both
63 represent the same character.  For example, the lowercase letters
64 @samp{a} and @samp{g} each have two distinct possible shapes---the
65 @samp{a} can optionally have a curved tail projecting off the top, and
66 the @samp{g} can be formed either of two loops, or of one loop and a
67 tail hanging off the bottom.  Such distinct possible shapes of a
68 character are called @dfn{glyphs}.  The important characteristic of two
69 glyphs making up the same character is that the choice between one or
70 the other is purely stylistic and has no linguistic effect on a word
71 (this is the reason why a capital @samp{A} and lowercase @samp{a}
72 are different characters rather than different glyphs---e.g.
73 @samp{Aspen} is a city while @samp{aspen} is a kind of tree).
74
75   Note that @dfn{character} and @dfn{glyph} are used differently
76 here than elsewhere in SXEmacs.
77
78   A @dfn{character set} is essentially a set of related characters.  ASCII,
79 for example, is a set of 94 characters (or 128, if you count
80 non-printing characters).  Other character sets are ISO8859-1 (ASCII
81 plus various accented characters and other international symbols),
82 JIS X 0201 (ASCII, more or less, plus half-width Katakana), JIS X 0208
83 (Japanese Kanji), JIS X 0212 (a second set of less-used Japanese Kanji),
84 GB2312 (Mainland Chinese Hanzi), etc.
85
86   The definition of a character set will implicitly or explicitly give
87 it an @dfn{ordering}, a way of assigning a number to each character in
88 the set.  For many character sets, there is a natural ordering, for
89 example the ``ABC'' ordering of the Roman letters.  But it is not clear
90 whether digits should come before or after the letters, and in fact
91 different European languages treat the ordering of accented characters
92 differently.  It is useful to use the natural order where available, of
93 course.  The number assigned to any particular character is called the
94 character's @dfn{code point}.  (Within a given character set, each
95 character has a unique code point.  Thus the word "set" is ill-chosen;
96 different orderings of the same characters are different character sets.
97 Identifying characters is simple enough for alphabetic character sets,
98 but the difference in ordering can cause great headaches when the same
99 thousands of characters are used by different cultures as in the Hanzi.)
100
101   A code point may be broken into a number of @dfn{position codes}.  The
102 number of position codes required to index a particular character in a
103 character set is called the @dfn{dimension} of the character set.  For
104 practical purposes, a position code may be thought of as a byte-sized
105 index.  The printing characters of ASCII, being a relatively small
106 character set, is of dimension one, and each character in the set is
107 indexed using a single position code, in the range 1 through 94.  Use of
108 this unusual range, rather than the familiar 33 through 126, is an
109 intentional abstraction; to understand the programming issues you must
110 break the equation between character sets and encodings.
111
112   JIS X 0208, i.e. Japanese Kanji, has thousands of characters, and is
113 of dimension two -- every character is indexed by two position codes,
114 each in the range 1 through 94.  (This number ``94'' is not a
115 coincidence; we shall see that the JIS position codes were chosen so
116 that JIS kanji could be encoded without using codes that in ASCII are
117 associated with device control functions.)  Note that the choice of the
118 range here is somewhat arbitrary.  You could just as easily index the
119 printing characters in ASCII using numbers in the range 0 through 93, 2
120 through 95, 3 through 96, etc.  In fact, the standardized
121 @emph{encoding} for the ASCII @emph{character set} uses the range 33
122 through 126.
123
124   An @dfn{encoding} is a way of numerically representing characters from
125 one or more character sets into a stream of like-sized numerical values
126 called @dfn{words}; typically these are 8-bit, 16-bit, or 32-bit
127 quantities.  If an encoding encompasses only one character set, then the
128 position codes for the characters in that character set could be used
129 directly.  (This is the case with the trivial cipher used by children,
130 assigning 1 to `A', 2 to `B', and so on.)  However, even with ASCII,
131 other considerations intrude.  For example, why are the upper- and
132 lowercase alphabets separated by 8 characters?  Why do the digits start
133 with `0' being assigned the code 48?  In both cases because semantically
134 interesting operations (case conversion and numerical value extraction)
135 become convenient masking operations.  Other artificial aspects (the
136 control characters being assigned to codes 0--31 and 127) are historical
137 accidents.  (The use of 127 for @samp{DEL} is an artifact of the "punch
138 once" nature of paper tape, for example.)
139
140   Naive use of the position code is not possible, however, if more than
141 one character set is to be used in the encoding.  For example, printed
142 Japanese text typically requires characters from multiple character sets
143 -- ASCII, JIS X 0208, and JIS X 0212, to be specific.  Each of these is
144 indexed using one or more position codes in the range 1 through 94, so
145 the position codes could not be used directly or there would be no way
146 to tell which character was meant.  Different Japanese encodings handle
147 this differently -- JIS uses special escape characters to denote
148 different character sets; EUC sets the high bit of the position codes
149 for JIS X 0208 and JIS X 0212, and puts a special extra byte before each
150 JIS X 0212 character; etc.  (JIS, EUC, and most of the other encodings
151 you will encounter in files are 7-bit or 8-bit encodings.  There is one
152 common 16-bit encoding, which is Unicode; this strives to represent all
153 the world's characters in a single large character set.  32-bit
154 encodings are often used internally in programs, such as SXEmacs with
155 MULE support, to simplify the code that manipulates them; however, they
156 are not used externally because they are not very space-efficient.)
157
158   A general method of handling text using multiple character sets
159 (whether for multilingual text, or simply text in an extremely
160 complicated single language like Japanese) is defined in the
161 international standard ISO 2022.  ISO 2022 will be discussed in more
162 detail later (@pxref{ISO 2022}), but for now suffice it to say that text
163 needs control functions (at least spacing), and if escape sequences are
164 to be used, an escape sequence introducer.  It was decided to make all
165 text streams compatible with ASCII in the sense that the codes 0--31
166 (and 128-159) would always be control codes, never graphic characters,
167 and where defined by the character set the @samp{SPC} character would be
168 assigned code 32, and @samp{DEL} would be assigned 127.  Thus there are
169 94 code points remaining if 7 bits are used.  This is the reason that
170 most character sets are defined using position codes in the range 1
171 through 94.  Then ISO 2022 compatible encodings are produced by shifting
172 the position codes 1 to 94 into character codes 33 to 126, or (if 8 bit
173 codes are available) into character codes 161 to 254.
174
175   Encodings are classified as either @dfn{modal} or @dfn{non-modal}.  In
176 a @dfn{modal encoding}, there are multiple states that the encoding can
177 be in, and the interpretation of the values in the stream depends on the
178 current global state of the encoding.  Special values in the encoding,
179 called @dfn{escape sequences}, are used to change the global state.
180 JIS, for example, is a modal encoding.  The bytes @samp{ESC $ B}
181 indicate that, from then on, bytes are to be interpreted as position
182 codes for JIS X 0208, rather than as ASCII.  This effect is cancelled
183 using the bytes @samp{ESC ( B}, which mean ``switch from whatever the
184 current state is to ASCII''.  To switch to JIS X 0212, the escape
185 sequence @samp{ESC $ ( D}. (Note that here, as is common, the escape
186 sequences do in fact begin with @samp{ESC}.  This is not necessarily the
187 case, however.  Some encodings use control characters called "locking
188 shifts" (effect persists until cancelled) to switch character sets.)
189
190   A @dfn{non-modal encoding} has no global state that extends past the
191 character currently being interpreted.  EUC, for example, is a
192 non-modal encoding.  Characters in JIS X 0208 are encoded by setting
193 the high bit of the position codes, and characters in JIS X 0212 are
194 encoded by doing the same but also prefixing the character with the
195 byte 0x8F.
196
197   The advantage of a modal encoding is that it is generally more
198 space-efficient, and is easily extendible because there are essentially
199 an arbitrary number of escape sequences that can be created.  The
200 disadvantage, however, is that it is much more difficult to work with
201 if it is not being processed in a sequential manner.  In the non-modal
202 EUC encoding, for example, the byte 0x41 always refers to the letter
203 @samp{A}; whereas in JIS, it could either be the letter @samp{A}, or
204 one of the two position codes in a JIS X 0208 character, or one of the
205 two position codes in a JIS X 0212 character.  Determining exactly which
206 one is meant could be difficult and time-consuming if the previous
207 bytes in the string have not already been processed, or impossible if
208 they are drawn from an external stream that cannot be rewound.
209
210   Non-modal encodings are further divided into @dfn{fixed-width} and
211 @dfn{variable-width} formats.  A fixed-width encoding always uses
212 the same number of words per character, whereas a variable-width
213 encoding does not.  EUC is a good example of a variable-width
214 encoding: one to three bytes are used per character, depending on
215 the character set.  16-bit and 32-bit encodings are nearly always
216 fixed-width, and this is in fact one of the main reasons for using
217 an encoding with a larger word size.  The advantages of fixed-width
218 encodings should be obvious.  The advantages of variable-width
219 encodings are that they are generally more space-efficient and allow
220 for compatibility with existing 8-bit encodings such as ASCII.  (For
221 example, in Unicode ASCII characters are simply promoted to a 16-bit
222 representation.  That means that every ASCII character contains a
223 @samp{NUL} byte; evidently all of the standard string manipulation
224 functions will lose badly in a fixed-width Unicode environment.)
225
226   The bytes in an 8-bit encoding are often referred to as @dfn{octets}
227 rather than simply as bytes.  This terminology dates back to the days
228 before 8-bit bytes were universal, when some computers had 9-bit bytes,
229 others had 10-bit bytes, etc.
230
231
232 @node Charsets, MULE Characters, Internationalization Terminology, MULE
233 @section Charsets
234
235   A @dfn{charset} in MULE is an object that encapsulates a
236 particular character set as well as an ordering of those characters.
237 Charsets are permanent objects and are named using symbols, like
238 faces.
239
240 @defun charsetp object
241 This function returns non-@code{nil} if @var{object} is a charset.
242 @end defun
243
244 @menu
245 * Charset Properties::          Properties of a charset.
246 * Basic Charset Functions::     Functions for working with charsets.
247 * Charset Property Functions::  Functions for accessing charset properties.
248 * Predefined Charsets::         Predefined charset objects.
249 @end menu
250
251
252 @node Charset Properties, Basic Charset Functions, , Charsets
253 @subsection Charset Properties
254
255   Charsets have the following properties:
256
257 @table @code
258 @item name
259 A symbol naming the charset.  Every charset must have a different name;
260 this allows a charset to be referred to using its name rather than
261 the actual charset object.
262 @item doc-string
263 A documentation string describing the charset.
264 @item registry
265 A regular expression matching the font registry field for this character
266 set.  For example, both the @code{ascii} and @code{latin-iso8859-1}
267 charsets use the registry @code{"ISO8859-1"}.  This field is used to
268 choose an appropriate font when the user gives a general font
269 specification such as @samp{-*-courier-medium-r-*-140-*}, i.e. a
270 14-point upright medium-weight Courier font.
271 @item dimension
272 Number of position codes used to index a character in the character set.
273 SXEmacs/MULE can only handle character sets of dimension 1 or 2.
274 This property defaults to 1.
275 @item chars
276 Number of characters in each dimension.  In SXEmacs/MULE, the only
277 allowed values are 94 or 96. (There are a couple of pre-defined
278 character sets, such as ASCII, that do not follow this, but you cannot
279 define new ones like this.) Defaults to 94.  Note that if the dimension
280 is 2, the character set thus described is 94x94 or 96x96.
281 @item columns
282 Number of columns used to display a character in this charset.
283 Only used in TTY mode. (Under X, the actual width of a character
284 can be derived from the font used to display the characters.)
285 If unspecified, defaults to the dimension. (This is almost
286 always the correct value, because character sets with dimension 2
287 are usually ideograph character sets, which need two columns to
288 display the intricate ideographs.)
289 @item direction
290 A symbol, either @code{l2r} (left-to-right) or @code{r2l}
291 (right-to-left).  Defaults to @code{l2r}.  This specifies the
292 direction that the text should be displayed in, and will be
293 left-to-right for most charsets but right-to-left for Hebrew
294 and Arabic. (Right-to-left display is not currently implemented.)
295 @item final
296 Final byte of the standard ISO 2022 escape sequence designating this
297 charset.  Must be supplied.  Each combination of (@var{dimension},
298 @var{chars}) defines a separate namespace for final bytes, and each
299 charset within a particular namespace must have a different final byte.
300 Note that ISO 2022 restricts the final byte to the range 0x30 - 0x7E if
301 dimension == 1, and 0x30 - 0x5F if dimension == 2.  Note also that final
302 bytes in the range 0x30 - 0x3F are reserved for user-defined (not
303 official) character sets.  For more information on ISO 2022, see @ref{Coding
304 Systems}.
305 @item graphic
306 0 (use left half of font on output) or 1 (use right half of font on
307 output).  Defaults to 0.  This specifies how to convert the position
308 codes that index a character in a character set into an index into the
309 font used to display the character set.  With @code{graphic} set to 0,
310 position codes 33 through 126 map to font indices 33 through 126; with
311 it set to 1, position codes 33 through 126 map to font indices 161
312 through 254 (i.e. the same number but with the high bit set).  For
313 example, for a font whose registry is ISO8859-1, the left half of the
314 font (octets 0x20 - 0x7F) is the @code{ascii} charset, while the right
315 half (octets 0xA0 - 0xFF) is the @code{latin-iso8859-1} charset.
316 @item ccl-program
317 A compiled CCL program used to convert a character in this charset into
318 an index into the font.  This is in addition to the @code{graphic}
319 property.  If a CCL program is defined, the position codes of a
320 character will first be processed according to @code{graphic} and
321 then passed through the CCL program, with the resulting values used
322 to index the font.
323
324   This is used, for example, in the Big5 character set (used in Taiwan).
325 This character set is not ISO-2022-compliant, and its size (94x157) does
326 not fit within the maximum 96x96 size of ISO-2022-compliant character
327 sets.  As a result, SXEmacs/MULE splits it (in a rather complex fashion,
328 so as to group the most commonly used characters together) into two
329 charset objects (@code{big5-1} and @code{big5-2}), each of size 94x94,
330 and each charset object uses a CCL program to convert the modified
331 position codes back into standard Big5 indices to retrieve a character
332 from a Big5 font.
333 @end table
334
335   Most of the above properties can only be set when the charset is
336 initialized, and cannot be changed later.
337 @xref{Charset Property Functions}.
338
339
340 @node Basic Charset Functions, Charset Property Functions, Charset Properties, Charsets
341 @subsection Basic Charset Functions
342
343 @defun find-charset charset-or-name
344 This function retrieves the charset of the given name.  If
345 @var{charset-or-name} is a charset object, it is simply returned.
346 Otherwise, @var{charset-or-name} should be a symbol.  If there is no
347 such charset, @code{nil} is returned.  Otherwise the associated charset
348 object is returned.
349 @end defun
350
351 @defun get-charset name
352 This function retrieves the charset of the given name.  Same as
353 @code{find-charset} except an error is signalled if there is no such
354 charset instead of returning @code{nil}.
355 @end defun
356
357 @defun charset-list
358 This function returns a list of the names of all defined charsets.
359 @end defun
360
361 @defun make-charset name doc-string props
362 This function defines a new character set.  This function is for use
363 with MULE support.  @var{name} is a symbol, the name by which the
364 character set is normally referred.  @var{doc-string} is a string
365 describing the character set.  @var{props} is a property list,
366 describing the specific nature of the character set.  The recognized
367 properties are @code{registry}, @code{dimension}, @code{columns},
368 @code{chars}, @code{final}, @code{graphic}, @code{direction}, and
369 @code{ccl-program}, as previously described.
370 @end defun
371
372 @defun make-reverse-direction-charset charset new-name
373 This function makes a charset equivalent to @var{charset} but which goes
374 in the opposite direction.  @var{new-name} is the name of the new
375 charset.  The new charset is returned.
376 @end defun
377
378 @defun charset-from-attributes dimension chars final &optional direction
379 This function returns a charset with the given @var{dimension},
380 @var{chars}, @var{final}, and @var{direction}.  If @var{direction} is
381 omitted, both directions will be checked (left-to-right will be returned
382 if character sets exist for both directions).
383 @end defun
384
385 @defun charset-reverse-direction-charset charset
386 This function returns the charset (if any) with the same dimension,
387 number of characters, and final byte as @var{charset}, but which is
388 displayed in the opposite direction.
389 @end defun
390
391
392 @node Charset Property Functions, Predefined Charsets, Basic Charset Functions, Charsets
393 @subsection Charset Property Functions
394
395   All of these functions accept either a charset name or charset object.
396
397 @defun charset-property charset prop
398 This function returns property @var{prop} of @var{charset}.
399 @xref{Charset Properties}.
400 @end defun
401
402   Convenience functions are also provided for retrieving individual
403 properties of a charset.
404
405 @defun charset-name charset
406 This function returns the name of @var{charset}.  This will be a symbol.
407 @end defun
408
409 @defun charset-description charset
410 This function returns the documentation string of @var{charset}.
411 @end defun
412
413 @defun charset-registry charset
414 This function returns the registry of @var{charset}.
415 @end defun
416
417 @defun charset-dimension charset
418 This function returns the dimension of @var{charset}.
419 @end defun
420
421 @defun charset-chars charset
422 This function returns the number of characters per dimension of
423 @var{charset}.
424 @end defun
425
426 @defun charset-width charset
427 This function returns the number of display columns per character (in
428 TTY mode) of @var{charset}.
429 @end defun
430
431 @defun charset-direction charset
432 This function returns the display direction of @var{charset}---either
433 @code{l2r} or @code{r2l}.
434 @end defun
435
436 @defun charset-iso-final-char charset
437 This function returns the final byte of the ISO 2022 escape sequence
438 designating @var{charset}.
439 @end defun
440
441 @defun charset-iso-graphic-plane charset
442 This function returns either 0 or 1, depending on whether the position
443 codes of characters in @var{charset} map to the left or right half
444 of their font, respectively.
445 @end defun
446
447 @defun charset-ccl-program charset
448 This function returns the CCL program, if any, for converting
449 position codes of characters in @var{charset} into font indices.
450 @end defun
451
452   The two properties of a charset that can currently be set after the
453 charset has been created are the CCL program and the font registry.
454
455 @defun set-charset-ccl-program charset ccl-program
456 This function sets the @code{ccl-program} property of @var{charset} to
457 @var{ccl-program}.
458 @end defun
459
460 @defun set-charset-registry charset registry
461 This function sets the @code{registry} property of @var{charset} to
462 @var{registry}.
463 @end defun
464
465
466 @node Predefined Charsets, , Charset Property Functions, Charsets
467 @subsection Predefined Charsets
468
469   The following charsets are predefined in the C code.
470
471 @example
472 Name                    Type  Fi Gr Dir Registry
473 --------------------------------------------------------------
474 ascii                    94    B  0  l2r ISO8859-1
475 control-1                94       0  l2r ---
476 latin-iso8859-1          94    A  1  l2r ISO8859-1
477 latin-iso8859-2          96    B  1  l2r ISO8859-2
478 latin-iso8859-3          96    C  1  l2r ISO8859-3
479 latin-iso8859-4          96    D  1  l2r ISO8859-4
480 cyrillic-iso8859-5       96    L  1  l2r ISO8859-5
481 arabic-iso8859-6         96    G  1  r2l ISO8859-6
482 greek-iso8859-7          96    F  1  l2r ISO8859-7
483 hebrew-iso8859-8         96    H  1  r2l ISO8859-8
484 latin-iso8859-9          96    M  1  l2r ISO8859-9
485 thai-tis620              96    T  1  l2r TIS620
486 katakana-jisx0201        94    I  1  l2r JISX0201.1976
487 latin-jisx0201           94    J  0  l2r JISX0201.1976
488 japanese-jisx0208-1978   94x94 @@  0  l2r JISX0208.1978
489 japanese-jisx0208        94x94 B  0  l2r JISX0208.19(83|90)
490 japanese-jisx0212        94x94 D  0  l2r JISX0212
491 chinese-gb2312           94x94 A  0  l2r GB2312
492 chinese-cns11643-1       94x94 G  0  l2r CNS11643.1
493 chinese-cns11643-2       94x94 H  0  l2r CNS11643.2
494 chinese-big5-1           94x94 0  0  l2r Big5
495 chinese-big5-2           94x94 1  0  l2r Big5
496 korean-ksc5601           94x94 C  0  l2r KSC5601
497 composite                96x96    0  l2r ---
498 @end example
499
500   The following charsets are predefined in the Lisp code.
501
502 @example
503 Name                     Type  Fi Gr Dir Registry
504 --------------------------------------------------------------
505 arabic-digit             94    2  0  l2r MuleArabic-0
506 arabic-1-column          94    3  0  r2l MuleArabic-1
507 arabic-2-column          94    4  0  r2l MuleArabic-2
508 sisheng                  94    0  0  l2r sisheng_cwnn\|OMRON_UDC_ZH
509 chinese-cns11643-3       94x94 I  0  l2r CNS11643.1
510 chinese-cns11643-4       94x94 J  0  l2r CNS11643.1
511 chinese-cns11643-5       94x94 K  0  l2r CNS11643.1
512 chinese-cns11643-6       94x94 L  0  l2r CNS11643.1
513 chinese-cns11643-7       94x94 M  0  l2r CNS11643.1
514 ethiopic                 94x94 2  0  l2r Ethio
515 ascii-r2l                94    B  0  r2l ISO8859-1
516 ipa                      96    0  1  l2r MuleIPA
517 vietnamese-viscii-lower  96    1  1  l2r VISCII1.1
518 vietnamese-viscii-upper  96    2  1  l2r VISCII1.1
519 @end example
520
521 For all of the above charsets, the dimension and number of columns are
522 the same.
523
524   Note that ASCII, Control-1, and Composite are handled specially.
525 This is why some of the fields are blank; and some of the filled-in
526 fields (e.g. the type) are not really accurate.
527
528
529 @node MULE Characters, Composite Characters, Charsets, MULE
530 @section MULE Characters
531
532 @defun make-char charset arg1 &optional arg2
533 This function makes a multi-byte character from @var{charset} and octets
534 @var{arg1} and @var{arg2}.
535 @end defun
536
537 @defun char-charset character
538 This function returns the character set of char @var{character}.
539 @end defun
540
541 @defun char-octet character &optional n
542 This function returns the octet (i.e. position code) numbered @var{n}
543 (should be 0 or 1) of char @var{character}.  @var{n} defaults to 0 if omitted.
544 @end defun
545
546 @defun find-charset-region start end &optional buffer
547 This function returns a list of the charsets in the region between
548 @var{start} and @var{end}.  @var{buffer} defaults to the current buffer
549 if omitted.
550 @end defun
551
552 @defun find-charset-string string
553 This function returns a list of the charsets in @var{string}.
554 @end defun
555
556
557 @node Composite Characters, Coding Systems, MULE Characters, MULE
558 @section Composite Characters
559
560   Composite characters are not yet completely implemented.
561
562 @defun make-composite-char string
563 This function converts a string into a single composite character.  The
564 character is the result of overstriking all the characters in the
565 string.
566 @end defun
567
568 @defun composite-char-string character
569 This function returns a string of the characters comprising a composite
570 character.
571 @end defun
572
573 @defun compose-region start end &optional buffer
574 This function composes the characters in the region from @var{start} to
575 @var{end} in @var{buffer} into one composite character.  The composite
576 character replaces the composed characters.  @var{buffer} defaults to
577 the current buffer if omitted.
578 @end defun
579
580 @defun decompose-region start end &optional buffer
581 This function decomposes any composite characters in the region from
582 @var{start} to @var{end} in @var{buffer}.  This converts each composite
583 character into one or more characters, the individual characters out of
584 which the composite character was formed.  Non-composite characters are
585 left as-is.  @var{buffer} defaults to the current buffer if omitted.
586 @end defun
587
588
589 @node Coding Systems, CCL, Composite Characters, MULE
590 @section Coding Systems
591
592   A coding system is an object that defines how text containing multiple
593 character sets is encoded into a stream of (typically 8-bit) bytes.  The
594 coding system is used to decode the stream into a series of characters
595 (which may be from multiple charsets) when the text is read from a file
596 or process, and is used to encode the text back into the same format
597 when it is written out to a file or process.
598
599   For example, many ISO-2022-compliant coding systems (such as Compound
600 Text, which is used for inter-client data under the X Window System) use
601 escape sequences to switch between different charsets -- Japanese Kanji,
602 for example, is invoked with @samp{ESC $ ( B}; ASCII is invoked with
603 @samp{ESC ( B}; and Cyrillic is invoked with @samp{ESC - L}.  See
604 @code{make-coding-system} for more information.
605
606   Coding systems are normally identified using a symbol, and the symbol is
607 accepted in place of the actual coding system object whenever a coding
608 system is called for. (This is similar to how faces and charsets work.)
609
610 @defun coding-system-p object
611 This function returns non-@code{nil} if @var{object} is a coding system.
612 @end defun
613
614 @menu
615 * Coding System Types::               Classifying coding systems.
616 * ISO 2022::                          An international standard for
617                                         charsets and encodings.
618 * EOL Conversion::                    Dealing with different ways of denoting
619                                         the end of a line.
620 * Coding System Properties::          Properties of a coding system.
621 * Basic Coding System Functions::     Working with coding systems.
622 * Coding System Property Functions::  Retrieving a coding system's properties.
623 * Encoding and Decoding Text::        Encoding and decoding text.
624 * Detection of Textual Encoding::     Determining how text is encoded.
625 * Big5 and Shift-JIS Functions::      Special functions for these non-standard
626                                         encodings.
627 * Predefined Coding Systems::         Coding systems implemented by MULE.
628 @end menu
629
630
631 @node Coding System Types, ISO 2022, , Coding Systems
632 @subsection Coding System Types
633
634   The coding system type determines the basic algorithm SXEmacs will use to
635 decode or encode a data stream.  Character encodings will be converted
636 to the MULE encoding, escape sequences processed, and newline sequences
637 converted to SXEmacs's internal representation.  There are three basic
638 classes of coding system type: no-conversion, ISO-2022, and special.
639
640   No conversion allows you to look at the file's internal representation.
641 Since SXEmacs is basically a text editor, "no conversion" does convert
642 newline conventions by default.  (Use the 'binary coding-system if this
643 is not desired.)
644
645   ISO 2022 (@pxref{ISO 2022}) is the basic international standard regulating
646 use of "coded character sets for the exchange of data", ie, text
647 streams.  ISO 2022 contains functions that make it possible to encode
648 text streams to comply with restrictions of the Internet mail system and
649 de facto restrictions of most file systems (eg, use of the separator
650 character in file names).  Coding systems which are not ISO 2022
651 conformant can be difficult to handle.  Perhaps more important, they are
652 not adaptable to multilingual information interchange, with the obvious
653 exception of ISO 10646 (Unicode).  (Unicode is partially supported by
654 SXEmacs with the addition of the Lisp package ucs-conv.)
655
656   The special class of coding systems includes automatic detection, CCL (a
657 "little language" embedded as an interpreter, useful for translating
658 between variants of a single character set), non-ISO-2022-conformant
659 encodings like Unicode, Shift JIS, and Big5, and MULE internal coding.
660 (NB: this list is based on XEmacs 21.2.  Terminology may vary slightly
661 for other versions of SXEmacs, XEmacs and for GNU Emacs 20.)
662
663 @table @code
664 @item no-conversion
665 No conversion, for binary files, and a few special cases of non-ISO-2022
666 coding systems where conversion is done by hook functions (usually
667 implemented in CCL).  On output, graphic characters that are not in
668 ASCII or Latin-1 will be replaced by a @samp{?}. (For a
669 no-conversion-encoded buffer, these characters will only be present if
670 you explicitly insert them.)
671 @item iso2022
672 Any ISO-2022-compliant encoding.  Among others, this includes JIS (the
673 Japanese encoding commonly used for e-mail), national variants of EUC
674 (the standard Unix encoding for Japanese and other languages), and
675 Compound Text (an encoding used in X11).  You can specify more specific
676 information about the conversion with the @var{flags} argument.
677 @item ucs-4
678 ISO 10646 UCS-4 encoding.  A 31-bit fixed-width superset of Unicode.
679 @item utf-8
680 ISO 10646 UTF-8 encoding.  A ``file system safe'' transformation format
681 that can be used with both UCS-4 and Unicode.
682 @item undecided
683 Automatic conversion.  SXEmacs attempts to detect the coding system used
684 in the file.
685 @item shift-jis
686 Shift-JIS (a Japanese encoding commonly used in PC operating systems).
687 @item big5
688 Big5 (the encoding commonly used for Taiwanese).
689 @item ccl
690 The conversion is performed using a user-written pseudo-code program.
691 CCL (Code Conversion Language) is the name of this pseudo-code.  For
692 example, CCL is used to map KOI8-R characters (an encoding for Russian
693 Cyrillic) to ISO8859-5 (the form used internally by MULE).
694 @item internal
695 Write out or read in the raw contents of the memory representing the
696 buffer's text.  This is primarily useful for debugging purposes, and is
697 only enabled when SXEmacs has been compiled with @code{DEBUG_XEMACS} set
698 (the @samp{--debug} configure option).  @strong{Warning}: Reading in a
699 file using @code{internal} conversion can result in an internal
700 inconsistency in the memory representing a buffer's text, which will
701 produce unpredictable results and may cause SXEmacs to crash.  Under
702 normal circumstances you should never use @code{internal} conversion.
703 @end table
704
705
706 @node ISO 2022, EOL Conversion, Coding System Types, Coding Systems
707 @section ISO 2022
708
709   This section briefly describes the ISO 2022 encoding standard.  A more
710 thorough treatment is available in the original document of ISO
711 2022 as well as various national standards (such as JIS X 0202).
712
713   Character sets (@dfn{charsets}) are classified into the following four
714 categories, according to the number of characters in the charset:
715 94-charset, 96-charset, 94x94-charset, and 96x96-charset.  This means
716 that although an ISO 2022 coding system may have variable width
717 characters, each charset used is fixed-width (in contrast to the MULE
718 character set and UTF-8, for example).
719
720   ISO 2022 provides for switching between character sets via escape
721 sequences.  This switching is somewhat complicated, because ISO 2022
722 provides for both legacy applications like Internet mail that accept
723 only 7 significant bits in some contexts (RFC 822 headers, for example),
724 and more modern "8-bit clean" applications.  It also provides for
725 compact and transparent representation of languages like Japanese which
726 mix ASCII and a national script (even outside of computer programs).
727
728   First, ISO 2022 codified prevailing practice by dividing the code space
729 into "control" and "graphic" regions.  The code points 0x00-0x1F and
730 0x80-0x9F are reserved for "control characters", while "graphic
731 characters" must be assigned to code points in the regions 0x20-0x7F and
732 0xA0-0xFF.  The positions 0x20 and 0x7F are special, and under some
733 circumstances must be assigned the graphic character "ASCII SPACE" and
734 the control character "ASCII DEL" respectively.
735
736   The various regions are given the name C0 (0x00-0x1F), GL (0x20-0x7F),
737 C1 (0x80-0x9F), and GR (0xA0-0xFF).  GL and GR stand for "graphic left"
738 and "graphic right", respectively, because of the standard method of
739 displaying graphic character sets in tables with the high byte indexing
740 columns and the low byte indexing rows.  I don't find it very intuitive,
741 but these are called "registers".
742
743   An ISO 2022-conformant encoding for a graphic character set must use a
744 fixed number of bytes per character, and the values must fit into a
745 single register; that is, each byte must range over either 0x20-0x7F, or
746 0xA0-0xFF.  It is not allowed to extend the range of the repertoire of a
747 character set by using both ranges at the same.  This is why a standard
748 character set such as ISO 8859-1 is actually considered by ISO 2022 to
749 be an aggregation of two character sets, ASCII and LATIN-1, and why it
750 is technically incorrect to refer to ISO 8859-1 as "Latin 1".  Also, a
751 single character's bytes must all be drawn from the same register; this
752 is why Shift JIS (for Japanese) and Big 5 (for Chinese) are not ISO
753 2022-compatible encodings.
754
755   The reason for this restriction becomes clear when you attempt to define
756 an efficient, robust encoding for a language like Japanese.  Like ISO
757 8859, Japanese encodings are aggregations of several character sets.  In
758 practice, the vast majority of characters are drawn from the "JIS Roman"
759 character set (a derivative of ASCII; it won't hurt to think of it as
760 ASCII) and the JIS X 0208 standard "basic Japanese" character set
761 including not only ideographic characters ("kanji") but syllabic
762 Japanese characters ("kana"), a wide variety of symbols, and many
763 alphabetic characters (Roman, Greek, and Cyrillic) as well.  Although
764 JIS X 0208 includes the whole Roman alphabet, as a 2-byte code it is not
765 suited to programming; thus the inclusion of ASCII in the standard
766 Japanese encodings.
767
768   For normal Japanese text such as in newspapers, a broad repertoire of
769 approximately 3000 characters is used.  Evidently this won't fit into
770 one byte; two must be used.  But much of the text processed by Japanese
771 computers is computer source code, nearly all of which is ASCII.  A not
772 insignificant portion of ordinary text is English (as such or as
773 borrowed Japanese vocabulary) or other languages which can represented
774 at least approximately in ASCII, as well.  It seems reasonable then to
775 represent ASCII in one byte, and JIS X 0208 in two.  And this is exactly
776 what the Extended Unix Code for Japanese (EUC-JP) does.  ASCII is
777 invoked to the GL register, and JIS X 0208 is invoked to the GR
778 register.  Thus, each byte can be tested for its character set by
779 looking at the high bit; if set, it is Japanese, if clear, it is ASCII.
780 Furthermore, since control characters like newline can never be part of
781 a graphic character, even in the case of corruption in transmission the
782 stream will be resynchronized at every line break, on the order of 60-80
783 bytes.  This coding system requires no escape sequences or special
784 control codes to represent 99.9% of all Japanese text.
785
786   Note carefully the distinction between the character sets (ASCII and JIS
787 X 0208), the encoding (EUC-JP), and the coding system (ISO 2022).  The
788 JIS X 0208 character set is used in three different encodings for
789 Japanese, but in ISO-2022-JP it is invoked into GL (so the high bit is
790 always clear), in EUC-JP it is invoked into GR (setting the high bit in
791 the process), and in Shift JIS the high bit may be set or reset, and the
792 significant bits are shifted within the 16-bit character so that the two
793 main character sets can coexist with a third (the "halfwidth katakana"
794 of JIS X 0201).  As the name implies, the ISO-2022-JP encoding is also a
795 version of the ISO-2022 coding system.
796
797   In order to systematically treat subsidiary character sets (like the
798 "halfwidth katakana" already mentioned, and the "supplementary kanji" of
799 JIS X 0212), four further registers are defined: G0, G1, G2, and G3.
800 Unlike GL and GR, they are not logically distinguished by internal
801 format.  Instead, the process of "invocation" mentioned earlier is
802 broken into two steps: first, a character set is @dfn{designated} to one
803 of the registers G0-G3 by use of an @dfn{escape sequence} of the form:
804
805 @example
806         ESC [@var{I}] @var{I} @var{F}
807 @end example
808
809 where @var{I} is an intermediate character or characters in the range
810 0x20 - 0x3F, and @var{F}, from the range 0x30-0x7Fm is the final
811 character identifying this charset.  (Final characters in the range
812 0x30-0x3F are reserved for private use and will never have a publicly
813 registered meaning.)
814
815   Then that register is @dfn{invoked} to either GL or GR, either
816 automatically (designations to G0 normally involve invocation to GL as
817 well), or by use of shifting (affecting only the following character in
818 the data stream) or locking (effective until the next designation or
819 locking) control sequences.  An encoding conformant to ISO 2022 is
820 typically defined by designating the initial contents of the G0-G3
821 registers, specifying a 7 or 8 bit environment, and specifying whether
822 further designations will be recognized.
823
824   Some examples of character sets and the registered final characters
825 @var{F} used to designate them:
826
827 @need 1000
828 @table @asis
829 @item 94-charset
830  ASCII (B), left (J) and right (I) half of JIS X 0201, ...
831 @item 96-charset
832  Latin-1 (A), Latin-2 (B), Latin-3 (C), ...
833 @item 94x94-charset
834  GB2312 (A), JIS X 0208 (B), KSC5601 (C), ...
835 @item 96x96-charset
836  none for the moment
837 @end table
838
839   The meanings of the various characters in these sequences, where not
840 specified by the ISO 2022 standard (such as the ESC character), are
841 assigned by @dfn{ECMA}, the European Computer Manufacturers Association.
842
843   The meaning of intermediate characters are:
844
845 @example
846 @group
847         $ [0x24]: indicate charset of dimension 2 (94x94 or 96x96).
848         ( [0x28]: designate to G0 a 94-charset whose final byte is @var{F}.
849         ) [0x29]: designate to G1 a 94-charset whose final byte is @var{F}.
850         * [0x2A]: designate to G2 a 94-charset whose final byte is @var{F}.
851         + [0x2B]: designate to G3 a 94-charset whose final byte is @var{F}.
852         , [0x2C]: designate to G0 a 96-charset whose final byte is @var{F}.
853         - [0x2D]: designate to G1 a 96-charset whose final byte is @var{F}.
854         . [0x2E]: designate to G2 a 96-charset whose final byte is @var{F}.
855         / [0x2F]: designate to G3 a 96-charset whose final byte is @var{F}.
856 @end group
857 @end example
858
859   The comma may be used in files read and written only by MULE, as a MULE
860 extension, but this is illegal in ISO 2022.  (The reason is that in ISO
861 2022 G0 must be a 94-member character set, with 0x20 assigned the value
862 SPACE, and 0x7F assigned the value DEL.)
863
864   Here are examples of designations:
865
866 @example
867 @group
868         ESC ( B :              designate to G0 ASCII
869         ESC - A :              designate to G1 Latin-1
870         ESC $ ( A or ESC $ A : designate to G0 GB2312
871         ESC $ ( B or ESC $ B : designate to G0 JISX0208
872         ESC $ ) C :            designate to G1 KSC5601
873 @end group
874 @end example
875
876 (The short forms used to designate GB2312 and JIS X 0208 are for
877 backwards compatibility; the long forms are preferred.)
878
879   To use a charset designated to G2 or G3, and to use a charset designated
880 to G1 in a 7-bit environment, you must explicitly invoke G1, G2, or G3
881 into GL.  There are two types of invocation, Locking Shift (forever) and
882 Single Shift (one character only).
883
884   Locking Shift is done as follows:
885
886 @example
887         LS0 or SI (0x0F): invoke G0 into GL
888         LS1 or SO (0x0E): invoke G1 into GL
889         LS2:  invoke G2 into GL
890         LS3:  invoke G3 into GL
891         LS1R: invoke G1 into GR
892         LS2R: invoke G2 into GR
893         LS3R: invoke G3 into GR
894 @end example
895
896   Single Shift is done as follows:
897
898 @example
899 @group
900         SS2 or ESC N: invoke G2 into GL
901         SS3 or ESC O: invoke G3 into GL
902 @end group
903 @end example
904
905   The shift functions (such as LS1R and SS3) are represented by control
906 characters (from C1) in 8 bit environments and by escape sequences in 7
907 bit environments.
908
909 (#### Ben says: I think the above is slightly incorrect.  It appears that
910 SS2 invokes G2 into GR and SS3 invokes G3 into GR, whereas ESC N and
911 ESC O behave as indicated.  The above definitions will not parse
912 EUC-encoded text correctly, and it looks like the code in mule-coding.c
913 has similar problems.)
914
915   Evidently there are a lot of ISO-2022-compliant ways of encoding
916 multilingual text.  Now, in the world, there exist many coding systems
917 such as X11's Compound Text, Japanese JUNET code, and so-called EUC
918 (Extended UNIX Code); all of these are variants of ISO 2022.
919
920   In MULE, we characterize a version of ISO 2022 by the following
921 attributes:
922
923 @enumerate
924 @item
925 The character sets initially designated to G0 thru G3.
926 @item
927 Whether short form designations are allowed for Japanese and Chinese.
928 @item
929 Whether ASCII should be designated to G0 before control characters.
930 @item
931 Whether ASCII should be designated to G0 at the end of line.
932 @item
933 7-bit environment or 8-bit environment.
934 @item
935 Whether Locking Shifts are used or not.
936 @item
937 Whether to use ASCII or the variant JIS X 0201-1976-Roman.
938 @item
939 Whether to use JIS X 0208-1983 or the older version JIS X 0208-1976.
940 @end enumerate
941
942 (The last two are only for Japanese.)
943
944   By specifying these attributes, you can create any variant
945 of ISO 2022.
946
947   Here are several examples:
948
949 @example
950 @group
951 ISO-2022-JP -- Coding system used in Japanese email (RFC 1463 #### check).
952         1. G0 <- ASCII, G1..3 <- never used
953         2. Yes.
954         3. Yes.
955         4. Yes.
956         5. 7-bit environment
957         6. No.
958         7. Use ASCII
959         8. Use JIS X 0208-1983
960 @end group
961
962 @group
963 ctext -- X11 Compound Text
964         1. G0 <- ASCII, G1 <- Latin-1, G2,3 <- never used.
965         2. No.
966         3. No.
967         4. Yes.
968         5. 8-bit environment.
969         6. No.
970         7. Use ASCII.
971         8. Use JIS X 0208-1983.
972 @end group
973
974 @group
975 euc-china -- Chinese EUC.  Often called the "GB encoding", but that is
976 technically incorrect.
977         1. G0 <- ASCII, G1 <- GB 2312, G2,3 <- never used.
978         2. No.
979         3. Yes.
980         4. Yes.
981         5. 8-bit environment.
982         6. No.
983         7. Use ASCII.
984         8. Use JIS X 0208-1983.
985 @end group
986
987 @group
988 ISO-2022-KR -- Coding system used in Korean email.
989         1. G0 <- ASCII, G1 <- KSC 5601, G2,3 <- never used.
990         2. No.
991         3. Yes.
992         4. Yes.
993         5. 7-bit environment.
994         6. Yes.
995         7. Use ASCII.
996         8. Use JIS X 0208-1983.
997 @end group
998 @end example
999
1000 MULE creates all of these coding systems by default.
1001
1002
1003 @node EOL Conversion, Coding System Properties, ISO 2022, Coding Systems
1004 @subsection EOL Conversion
1005
1006 @table @code
1007 @item nil
1008 Automatically detect the end-of-line type (LF, CRLF, or CR).  Also
1009 generate subsidiary coding systems named @code{@var{name}-unix},
1010 @code{@var{name}-dos}, and @code{@var{name}-mac}, that are identical to
1011 this coding system but have an EOL-TYPE value of @code{lf}, @code{crlf},
1012 and @code{cr}, respectively.
1013 @item lf
1014 The end of a line is marked externally using ASCII LF.  Since this is
1015 also the way that SXEmacs represents an end-of-line internally,
1016 specifying this option results in no end-of-line conversion.  This is
1017 the standard format for Unix text files.
1018 @item crlf
1019 The end of a line is marked externally using ASCII CRLF.  This is the
1020 standard format for MS-DOS text files.
1021 @item cr
1022 The end of a line is marked externally using ASCII CR.  This is the
1023 standard format for Macintosh text files.
1024 @item t
1025 Automatically detect the end-of-line type but do not generate subsidiary
1026 coding systems.  (This value is converted to @code{nil} when stored
1027 internally, and @code{coding-system-property} will return @code{nil}.)
1028 @end table
1029
1030
1031 @node Coding System Properties, Basic Coding System Functions, EOL Conversion, Coding Systems
1032 @subsection Coding System Properties
1033
1034 @table @code
1035 @item mnemonic
1036 String to be displayed in the modeline when this coding system is
1037 active.
1038
1039 @item eol-type
1040 End-of-line conversion to be used.  It should be one of the types
1041 listed in @ref{EOL Conversion}.
1042
1043 @item eol-lf
1044 The coding system which is the same as this one, except that it uses the
1045 Unix line-breaking convention.
1046
1047 @item eol-crlf
1048 The coding system which is the same as this one, except that it uses the
1049 DOS line-breaking convention.
1050
1051 @item eol-cr
1052 The coding system which is the same as this one, except that it uses the
1053 Macintosh line-breaking convention.
1054
1055 @item post-read-conversion
1056 Function called after a file has been read in, to perform the decoding.
1057 Called with two arguments, @var{start} and @var{end}, denoting a region of
1058 the current buffer to be decoded.
1059
1060 @item pre-write-conversion
1061 Function called before a file is written out, to perform the encoding.
1062 Called with two arguments, @var{start} and @var{end}, denoting a region of
1063 the current buffer to be encoded.
1064 @end table
1065
1066   The following additional properties are recognized if @var{type} is
1067 @code{iso2022}:
1068
1069 @table @code
1070 @item charset-g0
1071 @itemx charset-g1
1072 @itemx charset-g2
1073 @itemx charset-g3
1074 The character set initially designated to the G0 - G3 registers.
1075 The value should be one of
1076
1077 @itemize @bullet
1078 @item
1079 A charset object (designate that character set)
1080 @item
1081 @code{nil} (do not ever use this register)
1082 @item
1083 @code{t} (no character set is initially designated to the register, but
1084 may be later on; this automatically sets the corresponding
1085 @code{force-g*-on-output} property)
1086 @end itemize
1087
1088 @item force-g0-on-output
1089 @itemx force-g1-on-output
1090 @itemx force-g2-on-output
1091 @itemx force-g3-on-output
1092 If non-@code{nil}, send an explicit designation sequence on output
1093 before using the specified register.
1094
1095 @item short
1096 If non-@code{nil}, use the short forms @samp{ESC $ @@}, @samp{ESC $ A},
1097 and @samp{ESC $ B} on output in place of the full designation sequences
1098 @samp{ESC $ ( @@}, @samp{ESC $ ( A}, and @samp{ESC $ ( B}.
1099
1100 @item no-ascii-eol
1101 If non-@code{nil}, don't designate ASCII to G0 at each end of line on
1102 output.  Setting this to non-@code{nil} also suppresses other
1103 state-resetting that normally happens at the end of a line.
1104
1105 @item no-ascii-cntl
1106 If non-@code{nil}, don't designate ASCII to G0 before control chars on
1107 output.
1108
1109 @item seven
1110 If non-@code{nil}, use 7-bit environment on output.  Otherwise, use 8-bit
1111 environment.
1112
1113 @item lock-shift
1114 If non-@code{nil}, use locking-shift (SO/SI) instead of single-shift or
1115 designation by escape sequence.
1116
1117 @item no-iso6429
1118 If non-@code{nil}, don't use ISO6429's direction specification.
1119
1120 @item escape-quoted
1121 If non-@code{nil}, literal control characters that are the same as the
1122 beginning of a recognized ISO 2022 or ISO 6429 escape sequence (in
1123 particular, ESC (0x1B), SO (0x0E), SI (0x0F), SS2 (0x8E), SS3 (0x8F),
1124 and CSI (0x9B)) are ``quoted'' with an escape character so that they can
1125 be properly distinguished from an escape sequence.  (Note that doing
1126 this results in a non-portable encoding.) This encoding flag is used for
1127 byte-compiled files.  Note that ESC is a good choice for a quoting
1128 character because there are no escape sequences whose second byte is a
1129 character from the Control-0 or Control-1 character sets; this is
1130 explicitly disallowed by the ISO 2022 standard.
1131
1132 @item input-charset-conversion
1133 A list of conversion specifications, specifying conversion of characters
1134 in one charset to another when decoding is performed.  Each
1135 specification is a list of two elements: the source charset, and the
1136 destination charset.
1137
1138 @item output-charset-conversion
1139 A list of conversion specifications, specifying conversion of characters
1140 in one charset to another when encoding is performed.  The form of each
1141 specification is the same as for @code{input-charset-conversion}.
1142 @end table
1143
1144   The following additional properties are recognized (and required) if
1145 @var{type} is @code{ccl}:
1146
1147 @table @code
1148 @item decode
1149 CCL program used for decoding (converting to internal format).
1150
1151 @item encode
1152 CCL program used for encoding (converting to external format).
1153 @end table
1154
1155   The following properties are used internally:  @var{eol-cr},
1156 @var{eol-crlf}, @var{eol-lf}, and @var{base}.
1157
1158
1159 @node Basic Coding System Functions, Coding System Property Functions, Coding System Properties, Coding Systems
1160 @subsection Basic Coding System Functions
1161
1162 @defun find-coding-system coding-system-or-name
1163 This function retrieves the coding system of the given name.
1164
1165   If @var{coding-system-or-name} is a coding-system object, it is simply
1166 returned.  Otherwise, @var{coding-system-or-name} should be a symbol.
1167 If there is no such coding system, @code{nil} is returned.  Otherwise
1168 the associated coding system object is returned.
1169 @end defun
1170
1171 @defun get-coding-system name
1172 This function retrieves the coding system of the given name.  Same as
1173 @code{find-coding-system} except an error is signalled if there is no
1174 such coding system instead of returning @code{nil}.
1175 @end defun
1176
1177 @defun coding-system-list
1178 This function returns a list of the names of all defined coding systems.
1179 @end defun
1180
1181 @defun coding-system-name coding-system
1182 This function returns the name of the given coding system.
1183 @end defun
1184
1185 @defun coding-system-base coding-system
1186 Returns the base coding system (undecided EOL convention)
1187 coding system.
1188 @end defun
1189
1190 @defun make-coding-system name type &optional doc-string props
1191 This function registers symbol @var{name} as a coding system.
1192
1193 @var{type} describes the conversion method used and should be one of
1194 the types listed in @ref{Coding System Types}.
1195
1196 @var{doc-string} is a string describing the coding system.
1197
1198 @var{props} is a property list, describing the specific nature of the
1199 character set.  Recognized properties are as in @ref{Coding System
1200 Properties}.
1201 @end defun
1202
1203 @defun copy-coding-system old-coding-system new-name
1204 This function copies @var{old-coding-system} to @var{new-name}.  If
1205 @var{new-name} does not name an existing coding system, a new one will
1206 be created.
1207 @end defun
1208
1209 @defun subsidiary-coding-system coding-system eol-type
1210 This function returns the subsidiary coding system of
1211 @var{coding-system} with eol type @var{eol-type}.
1212 @end defun
1213
1214
1215 @node Coding System Property Functions, Encoding and Decoding Text, Basic Coding System Functions, Coding Systems
1216 @subsection Coding System Property Functions
1217
1218 @defun coding-system-doc-string coding-system
1219 This function returns the doc string for @var{coding-system}.
1220 @end defun
1221
1222 @defun coding-system-type coding-system
1223 This function returns the type of @var{coding-system}.
1224 @end defun
1225
1226 @defun coding-system-property coding-system prop
1227 This function returns the @var{prop} property of @var{coding-system}.
1228 @end defun
1229
1230
1231 @node Encoding and Decoding Text, Detection of Textual Encoding, Coding System Property Functions, Coding Systems
1232 @subsection Encoding and Decoding Text
1233
1234 @defun decode-coding-region start end coding-system &optional buffer
1235 This function decodes the text between @var{start} and @var{end} which
1236 is encoded in @var{coding-system}.  This is useful if you've read in
1237 encoded text from a file without decoding it (e.g. you read in a
1238 JIS-formatted file but used the @code{binary} or @code{no-conversion} coding
1239 system, so that it shows up as @samp{^[$B!<!+^[(B}).  The length of the
1240 encoded text is returned.  @var{buffer} defaults to the current buffer
1241 if unspecified.
1242 @end defun
1243
1244 @defun encode-coding-region start end coding-system &optional buffer
1245 This function encodes the text between @var{start} and @var{end} using
1246 @var{coding-system}.  This will, for example, convert Japanese
1247 characters into stuff such as @samp{^[$B!<!+^[(B} if you use the JIS
1248 encoding.  The length of the encoded text is returned.  @var{buffer}
1249 defaults to the current buffer if unspecified.
1250 @end defun
1251
1252
1253 @node Detection of Textual Encoding, Big5 and Shift-JIS Functions, Encoding and Decoding Text, Coding Systems
1254 @subsection Detection of Textual Encoding
1255
1256 @defun coding-category-list
1257 This function returns a list of all recognized coding categories.
1258 @end defun
1259
1260 @defun set-coding-priority-list list
1261 This function changes the priority order of the coding categories.
1262 @var{list} should be a list of coding categories, in descending order of
1263 priority.  Unspecified coding categories will be lower in priority than
1264 all specified ones, in the same relative order they were in previously.
1265 @end defun
1266
1267 @defun coding-priority-list
1268 This function returns a list of coding categories in descending order of
1269 priority.
1270 @end defun
1271
1272 @defun set-coding-category-system coding-category coding-system
1273 This function changes the coding system associated with a coding category.
1274 @end defun
1275
1276 @defun coding-category-system coding-category
1277 This function returns the coding system associated with a coding category.
1278 @end defun
1279
1280 @defun detect-coding-region start end &optional buffer
1281 This function detects coding system of the text in the region between
1282 @var{start} and @var{end}.  Returned value is a list of possible coding
1283 systems ordered by priority.  If only ASCII characters are found, it
1284 returns @code{autodetect} or one of its subsidiary coding systems
1285 according to a detected end-of-line type.  Optional arg @var{buffer}
1286 defaults to the current buffer.
1287 @end defun
1288
1289
1290 @node Big5 and Shift-JIS Functions, Predefined Coding Systems, Detection of Textual Encoding, Coding Systems
1291 @subsection Big5 and Shift-JIS Functions
1292
1293   These are special functions for working with the non-standard
1294 Shift-JIS and Big5 encodings.
1295
1296 @defun decode-shift-jis-char code
1297 This function decodes a JIS X 0208 character of Shift-JIS coding-system.
1298 @var{code} is the character code in Shift-JIS as a cons of type bytes.
1299 The corresponding character is returned.
1300 @end defun
1301
1302 @defun encode-shift-jis-char character
1303 This function encodes a JIS X 0208 character @var{character} to
1304 SHIFT-JIS coding-system.  The corresponding character code in SHIFT-JIS
1305 is returned as a cons of two bytes.
1306 @end defun
1307
1308 @defun decode-big5-char code
1309 This function decodes a Big5 character @var{code} of BIG5 coding-system.
1310 @var{code} is the character code in BIG5.  The corresponding character
1311 is returned.
1312 @end defun
1313
1314 @defun encode-big5-char character
1315 This function encodes the Big5 character @var{character} to BIG5
1316 coding-system.  The corresponding character code in Big5 is returned.
1317 @end defun
1318
1319
1320 @node Predefined Coding Systems, , Big5 and Shift-JIS Functions, Coding Systems
1321 @subsection Coding Systems Implemented
1322
1323   MULE initializes most of the commonly used coding systems at SXEmacs's
1324 startup.  A few others are initialized only when the relevant language
1325 environment is selected and support libraries are loaded.  (NB: The
1326 following list is based on XEmacs 21.2.19, the development branch at the
1327 time of writing.  The list may be somewhat different for other
1328 versions.  Recent versions of GNU Emacs 20 implement a few more rare
1329 coding systems; work is being done to port these to SXEmacs.)
1330
1331   Unfortunately, there is not a consistent naming convention for character
1332 sets, and for practical purposes coding systems often take their name
1333 from their principal character sets (ASCII, KOI8-R, Shift JIS).  Others
1334 take their names from the coding system (ISO-2022-JP, EUC-KR), and a few
1335 from their non-text usages (internal, binary).  To provide for this, and
1336 for the fact that many coding systems have several common names, an
1337 aliasing system is provided.  Finally, some effort has been made to use
1338 names that are registered as MIME charsets (this is why the name
1339 'shift_jis contains that un-Lisp-y underscore).
1340
1341   There is a systematic naming convention regarding end-of-line (EOL)
1342 conventions for different systems.  A coding system whose name ends in
1343 "-unix" forces the assumptions that lines are broken by newlines (0x0A).
1344 A coding system whose name ends in "-mac" forces the assumptions that
1345 lines are broken by ASCII CRs (0x0D).  A coding system whose name ends
1346 in "-dos" forces the assumptions that lines are broken by CRLF sequences
1347 (0x0D 0x0A).  These subsidiary coding systems are automatically derived
1348 from a base coding system.  Use of the base coding system implies
1349 autodetection of the text file convention.  (The fact that the -unix,
1350 -mac, and -dos are derived from a base system results in them showing up
1351 as "aliases" in `list-coding-systems'.)  These subsidiaries have a
1352 consistent modeline indicator as well.  "-dos" coding systems have ":T"
1353 appended to their modeline indicator, while "-mac" coding systems have
1354 ":t" appended (eg, "ISO8:t" for iso-2022-8-mac).
1355
1356   In the following table, each coding system is given with its mode line
1357 indicator in parentheses.  Non-textual coding systems are listed first,
1358 followed by textual coding systems and their aliases. (The coding system
1359 subsidiary modeline indicators ":T" and ":t" will be omitted from the
1360 table of coding systems.)
1361
1362   ### SJT 1999-08-23 Maybe should order these by language?  Definitely
1363 need language usage for the ISO-8859 family.
1364
1365   Note that although true coding system aliases have been implemented for
1366 XEmacs 21.2, the coding system initialization has not yet been converted
1367 as of 21.2.19.  So coding systems described as aliases have the same
1368 properties as the aliased coding system, but will not be equal as Lisp
1369 objects.
1370
1371 @table @code
1372
1373 @item automatic-conversion
1374 @itemx undecided
1375 @itemx undecided-dos
1376 @itemx undecided-mac
1377 @itemx undecided-unix
1378
1379 Modeline indicator: @code{Auto}.  A type @code{undecided} coding system.
1380 Attempts to determine an appropriate coding system from file contents or
1381 the environment.
1382
1383 @item raw-text
1384 @itemx no-conversion
1385 @itemx raw-text-dos
1386 @itemx raw-text-mac
1387 @itemx raw-text-unix
1388 @itemx no-conversion-dos
1389 @itemx no-conversion-mac
1390 @itemx no-conversion-unix
1391
1392 Modeline indicator: @code{Raw}.  A type @code{no-conversion} coding system,
1393 which converts only line-break-codes.  An implementation quirk means
1394 that this coding system is also used for ISO8859-1.
1395
1396 @item binary
1397 Modeline indicator: @code{Binary}.  A type @code{no-conversion} coding
1398 system which does no character coding or EOL conversions.  An alias for
1399 @code{raw-text-unix}.
1400
1401 @item alternativnyj
1402 @itemx alternativnyj-dos
1403 @itemx alternativnyj-mac
1404 @itemx alternativnyj-unix
1405
1406 Modeline indicator: @code{Cy.Alt}.  A type @code{ccl} coding system used for
1407 Alternativnyj, an encoding of the Cyrillic alphabet.
1408
1409 @item big5
1410 @itemx big5-dos
1411 @itemx big5-mac
1412 @itemx big5-unix
1413
1414 Modeline indicator: @code{Zh/Big5}.  A type @code{big5} coding system used for
1415 BIG5, the most common encoding of traditional Chinese as used in Taiwan.
1416
1417 @item cn-gb-2312
1418 @itemx cn-gb-2312-dos
1419 @itemx cn-gb-2312-mac
1420 @itemx cn-gb-2312-unix
1421
1422 Modeline indicator: @code{Zh-GB/EUC}.  A type @code{iso2022} coding system used
1423 for simplified Chinese (as used in the People's Republic of China), with
1424 the @code{ascii} (G0), @code{chinese-gb2312} (G1), and @code{sisheng}
1425 (G2) character sets initially designated.  Chinese EUC (Extended Unix
1426 Code).
1427
1428 @item ctext-hebrew
1429 @itemx ctext-hebrew-dos
1430 @itemx ctext-hebrew-mac
1431 @itemx ctext-hebrew-unix
1432
1433 Modeline indicator: @code{CText/Hbrw}.  A type @code{iso2022} coding system
1434 with the @code{ascii} (G0) and @code{hebrew-iso8859-8} (G1) character
1435 sets initially designated for Hebrew.
1436
1437 @item ctext
1438 @itemx ctext-dos
1439 @itemx ctext-mac
1440 @itemx ctext-unix
1441
1442 Modeline indicator: @code{CText}.  A type @code{iso2022} 8-bit coding system
1443 with the @code{ascii} (G0) and @code{latin-iso8859-1} (G1) character
1444 sets initially designated.  X11 Compound Text Encoding.  Often
1445 mistakenly recognized instead of EUC encodings; usual cause is
1446 inappropriate setting of @code{coding-priority-list}.
1447
1448 @item escape-quoted
1449
1450 Modeline indicator: @code{ESC/Quot}.  A type @code{iso2022} 8-bit coding
1451 system with the @code{ascii} (G0) and @code{latin-iso8859-1} (G1)
1452 character sets initially designated and escape quoting.  Unix EOL
1453 conversion (ie, no conversion).  It is used for .ELC files.
1454
1455 @item euc-jp
1456 @itemx euc-jp-dos
1457 @itemx euc-jp-mac
1458 @itemx euc-jp-unix
1459
1460 Modeline indicator: @code{Ja/EUC}.  A type @code{iso2022} 8-bit coding system
1461 with @code{ascii} (G0), @code{japanese-jisx0208} (G1),
1462 @code{katakana-jisx0201} (G2), and @code{japanese-jisx0212} (G3)
1463 initially designated.  Japanese EUC (Extended Unix Code).
1464
1465 @item euc-kr
1466 @itemx euc-kr-dos
1467 @itemx euc-kr-mac
1468 @itemx euc-kr-unix
1469
1470 Modeline indicator: @code{ko/EUC}.  A type @code{iso2022} 8-bit coding system
1471 with @code{ascii} (G0) and @code{korean-ksc5601} (G1) initially
1472 designated.  Korean EUC (Extended Unix Code).
1473
1474 @item hz-gb-2312
1475 Modeline indicator: @code{Zh-GB/Hz}.  A type @code{no-conversion} coding
1476 system with Unix EOL convention (ie, no conversion) using
1477 post-read-decode and pre-write-encode functions to translate the Hz/ZW
1478 coding system used for Chinese.
1479
1480 @item iso-2022-7bit
1481 @itemx iso-2022-7bit-unix
1482 @itemx iso-2022-7bit-dos
1483 @itemx iso-2022-7bit-mac
1484 @itemx iso-2022-7
1485
1486 Modeline indicator: @code{ISO7}.  A type @code{iso2022} 7-bit coding system
1487 with @code{ascii} (G0) initially designated.  Other character sets must
1488 be explicitly designated to be used.
1489
1490 @item iso-2022-7bit-ss2
1491 @itemx iso-2022-7bit-ss2-dos
1492 @itemx iso-2022-7bit-ss2-mac
1493 @itemx iso-2022-7bit-ss2-unix
1494
1495 Modeline indicator: @code{ISO7/SS}.  A type @code{iso2022} 7-bit coding system
1496 with @code{ascii} (G0) initially designated.  Other character sets must
1497 be explicitly designated to be used.  SS2 is used to invoke a
1498 96-charset, one character at a time.
1499
1500 @item iso-2022-8
1501 @itemx iso-2022-8-dos
1502 @itemx iso-2022-8-mac
1503 @itemx iso-2022-8-unix
1504
1505 Modeline indicator: @code{ISO8}.  A type @code{iso2022} 8-bit coding system
1506 with @code{ascii} (G0) and @code{latin-iso8859-1} (G1) initially
1507 designated.  Other character sets must be explicitly designated to be
1508 used.  No single-shift or locking-shift.
1509
1510 @item iso-2022-8bit-ss2
1511 @itemx iso-2022-8bit-ss2-dos
1512 @itemx iso-2022-8bit-ss2-mac
1513 @itemx iso-2022-8bit-ss2-unix
1514
1515 Modeline indicator: @code{ISO8/SS}.  A type @code{iso2022} 8-bit coding system
1516 with @code{ascii} (G0) and @code{latin-iso8859-1} (G1) initially
1517 designated.  Other character sets must be explicitly designated to be
1518 used.  SS2 is used to invoke a 96-charset, one character at a time.
1519
1520 @item iso-2022-int-1
1521 @itemx iso-2022-int-1-dos
1522 @itemx iso-2022-int-1-mac
1523 @itemx iso-2022-int-1-unix
1524
1525 Modeline indicator: @code{INT-1}.  A type @code{iso2022} 7-bit coding system
1526 with @code{ascii} (G0) and @code{korean-ksc5601} (G1) initially
1527 designated.  ISO-2022-INT-1.
1528
1529 @item iso-2022-jp-1978-irv
1530 @itemx iso-2022-jp-1978-irv-dos
1531 @itemx iso-2022-jp-1978-irv-mac
1532 @itemx iso-2022-jp-1978-irv-unix
1533
1534 Modeline indicator: @code{Ja-78/7bit}.  A type @code{iso2022} 7-bit coding
1535 system.  For compatibility with old Japanese terminals; if you need to
1536 know, look at the source.
1537
1538 @item iso-2022-jp
1539 @itemx iso-2022-jp-2 (ISO7/SS)
1540 @itemx iso-2022-jp-dos
1541 @itemx iso-2022-jp-mac
1542 @itemx iso-2022-jp-unix
1543 @itemx iso-2022-jp-2-dos
1544 @itemx iso-2022-jp-2-mac
1545 @itemx iso-2022-jp-2-unix
1546
1547 Modeline indicator: @code{MULE/7bit}.  A type @code{iso2022} 7-bit coding
1548 system with @code{ascii} (G0) initially designated, and complex
1549 specifications to insure backward compatibility with old Japanese
1550 systems.  Used for communication with mail and news in Japan.  The "-2"
1551 versions also use SS2 to invoke a 96-charset one character at a time.
1552
1553 @item iso-2022-kr
1554 Modeline indicator: @code{Ko/7bit}  A type @code{iso2022} 7-bit coding
1555 system with @code{ascii} (G0) and @code{korean-ksc5601} (G1) initially
1556 designated.  Used for e-mail in Korea.
1557
1558 @item iso-2022-lock
1559 @itemx iso-2022-lock-dos
1560 @itemx iso-2022-lock-mac
1561 @itemx iso-2022-lock-unix
1562
1563 Modeline indicator: @code{ISO7/Lock}.  A type @code{iso2022} 7-bit coding
1564 system with @code{ascii} (G0) initially designated, using Locking-Shift
1565 to invoke a 96-charset.
1566
1567 @item iso-8859-1
1568 @itemx iso-8859-1-dos
1569 @itemx iso-8859-1-mac
1570 @itemx iso-8859-1-unix
1571
1572 Due to implementation, this is not a type @code{iso2022} coding system,
1573 but rather an alias for the @code{raw-text} coding system.
1574
1575 @item iso-8859-2
1576 @itemx iso-8859-2-dos
1577 @itemx iso-8859-2-mac
1578 @itemx iso-8859-2-unix
1579
1580 Modeline indicator: @code{MIME/Ltn-2}.  A type @code{iso2022} coding
1581 system with @code{ascii} (G0) and @code{latin-iso8859-2} (G1) initially
1582 invoked.
1583
1584 @item iso-8859-3
1585 @itemx iso-8859-3-dos
1586 @itemx iso-8859-3-mac
1587 @itemx iso-8859-3-unix
1588
1589 Modeline indicator: @code{MIME/Ltn-3}.  A type @code{iso2022} coding system
1590 with @code{ascii} (G0) and @code{latin-iso8859-3} (G1) initially
1591 invoked.
1592
1593 @item iso-8859-4
1594 @itemx iso-8859-4-dos
1595 @itemx iso-8859-4-mac
1596 @itemx iso-8859-4-unix
1597
1598 Modeline indicator: @code{MIME/Ltn-4}.  A type @code{iso2022} coding system
1599 with @code{ascii} (G0) and @code{latin-iso8859-4} (G1) initially
1600 invoked.
1601
1602 @item iso-8859-5
1603 @itemx iso-8859-5-dos
1604 @itemx iso-8859-5-mac
1605 @itemx iso-8859-5-unix
1606
1607 Modeline indicator: @code{ISO8/Cyr}.  A type @code{iso2022} coding system with
1608 @code{ascii} (G0) and @code{cyrillic-iso8859-5} (G1) initially invoked.
1609
1610 @item iso-8859-7
1611 @itemx iso-8859-7-dos
1612 @itemx iso-8859-7-mac
1613 @itemx iso-8859-7-unix
1614
1615 Modeline indicator: @code{Grk}.  A type @code{iso2022} coding system with
1616 @code{ascii} (G0) and @code{greek-iso8859-7} (G1) initially invoked.
1617
1618 @item iso-8859-8
1619 @itemx iso-8859-8-dos
1620 @itemx iso-8859-8-mac
1621 @itemx iso-8859-8-unix
1622
1623 Modeline indicator: @code{MIME/Hbrw}.  A type @code{iso2022} coding system with
1624 @code{ascii} (G0) and @code{hebrew-iso8859-8} (G1) initially invoked.
1625
1626 @item iso-8859-9
1627 @itemx iso-8859-9-dos
1628 @itemx iso-8859-9-mac
1629 @itemx iso-8859-9-unix
1630
1631 Modeline indicator: @code{MIME/Ltn-5}.  A type @code{iso2022} coding system
1632 with @code{ascii} (G0) and @code{latin-iso8859-9} (G1) initially
1633 invoked.
1634
1635 @item koi8-r
1636 @itemx koi8-r-dos
1637 @itemx koi8-r-mac
1638 @itemx koi8-r-unix
1639
1640 Modeline indicator: @code{KOI8}.  A type @code{ccl} coding-system used for
1641 KOI8-R, an encoding of the Cyrillic alphabet.
1642
1643 @item shift_jis
1644 @itemx shift_jis-dos
1645 @itemx shift_jis-mac
1646 @itemx shift_jis-unix
1647
1648 Modeline indicator: @code{Ja/SJIS}.  A type @code{shift-jis} coding-system
1649 implementing the Shift-JIS encoding for Japanese.  The underscore is to
1650 conform to the MIME charset implementing this encoding.
1651
1652 @item tis-620
1653 @itemx tis-620-dos
1654 @itemx tis-620-mac
1655 @itemx tis-620-unix
1656
1657 Modeline indicator: @code{TIS620}.  A type @code{ccl} encoding for Thai.  The
1658 external encoding is defined by TIS620, the internal encoding is
1659 peculiar to MULE, and called @code{thai-xtis}.
1660
1661 @item viqr
1662
1663 Modeline indicator: @code{VIQR}.  A type @code{no-conversion} coding
1664 system with Unix EOL convention (ie, no conversion) using
1665 post-read-decode and pre-write-encode functions to translate the VIQR
1666 coding system for Vietnamese.
1667
1668 @item viscii
1669 @itemx viscii-dos
1670 @itemx viscii-mac
1671 @itemx viscii-unix
1672
1673 Modeline indicator: @code{VISCII}.  A type @code{ccl} coding-system used
1674 for VISCII 1.1 for Vietnamese.  Differs slightly from VSCII; VISCII is
1675 given priority by SXEmacs.
1676
1677 @item vscii
1678 @itemx vscii-dos
1679 @itemx vscii-mac
1680 @itemx vscii-unix
1681
1682 Modeline indicator: @code{VSCII}.  A type @code{ccl} coding-system used
1683 for VSCII 1.1 for Vietnamese.  Differs slightly from VISCII, which is
1684 given priority by SXEmacs.  Use
1685 @code{(prefer-coding-system 'vietnamese-vscii)} to give priority to VSCII.
1686
1687 @end table
1688
1689
1690 @node CCL, Category Tables, Coding Systems, MULE
1691 @section CCL
1692
1693   CCL (Code Conversion Language) is a simple structured programming
1694 language designed for character coding conversions.  A CCL program is
1695 compiled to CCL code (represented by a vector of integers) and executed
1696 by the CCL interpreter embedded in SXEmacs.  The CCL interpreter
1697 implements a virtual machine with 8 registers called @code{r0}, ...,
1698 @code{r7}, a number of control structures, and some I/O operators.  Take
1699 care when using registers @code{r0} (used in implicit @dfn{set}
1700 statements) and especially @code{r7} (used internally by several
1701 statements and operations, especially for multiple return values and I/O
1702 operations).
1703
1704   CCL is used for code conversion during process I/O and file I/O for
1705 non-ISO2022 coding systems.  (It is the only way for a user to specify a
1706 code conversion function.)  It is also used for calculating the code
1707 point of an X11 font from a character code.  However, since CCL is
1708 designed as a powerful programming language, it can be used for more
1709 generic calculation where efficiency is demanded.  A combination of
1710 three or more arithmetic operations can be calculated faster by CCL than
1711 by SXEmacs Lisp.
1712
1713   @strong{Warning:}  The code in @file{src/mule-ccl.c} and
1714 @file{$packages/lisp/mule-base/mule-ccl.el} is the definitive
1715 description of CCL's semantics.  The previous version of this section
1716 contained several typos and obsolete names left from earlier versions of
1717 MULE, and many may remain.  (I am not an experienced CCL programmer; the
1718 few who know CCL well find writing English painful.)
1719
1720   A CCL program transforms an input data stream into an output data
1721 stream.  The input stream, held in a buffer of constant bytes, is left
1722 unchanged.  The buffer may be filled by an external input operation,
1723 taken from a SXEmacs buffer, or taken from a Lisp string.  The output
1724 buffer is a dynamic array of bytes, which can be written by an external
1725 output operation, inserted into a SXEmacs buffer, or returned as a Lisp
1726 string.
1727
1728   A CCL program is a (Lisp) list containing two or three members.  The
1729 first member is the @dfn{buffer magnification}, which indicates the
1730 required minimum size of the output buffer as a multiple of the input
1731 buffer.  It is followed by the @dfn{main block} which executes while
1732 there is input remaining, and an optional @dfn{EOF block} which is
1733 executed when the input is exhausted.  Both the main block and the EOF
1734 block are CCL blocks.
1735
1736   A @dfn{CCL block} is either a CCL statement or list of CCL statements.
1737 A @dfn{CCL statement} is either a @dfn{set statement} (either an integer
1738 or an @dfn{assignment}, which is a list of a register to receive the
1739 assignment, an assignment operator, and an expression) or a @dfn{control
1740 statement} (a list starting with a keyword, whose allowable syntax
1741 depends on the keyword).
1742
1743 @menu
1744 * CCL Syntax::          CCL program syntax in BNF notation.
1745 * CCL Statements::      Semantics of CCL statements.
1746 * CCL Expressions::     Operators and expressions in CCL.
1747 * Calling CCL::         Running CCL programs.
1748 * CCL Examples::        The encoding functions for Big5 and KOI-8.
1749 @end menu
1750
1751
1752 @node    CCL Syntax, CCL Statements, , CCL
1753 @comment Node,       Next,           Previous,  Up
1754 @subsection CCL Syntax
1755
1756   The full syntax of a CCL program in BNF notation:
1757
1758 @format
1759 CCL_PROGRAM :=
1760         (BUFFER_MAGNIFICATION
1761          CCL_MAIN_BLOCK
1762          [ CCL_EOF_BLOCK ])
1763
1764 BUFFER_MAGNIFICATION := integer
1765 CCL_MAIN_BLOCK := CCL_BLOCK
1766 CCL_EOF_BLOCK := CCL_BLOCK
1767
1768 CCL_BLOCK :=
1769         STATEMENT | (STATEMENT [STATEMENT ...])
1770 STATEMENT :=
1771         SET | IF | BRANCH | LOOP | REPEAT | BREAK | READ | WRITE
1772         | CALL | END
1773
1774 SET :=
1775         (REG = EXPRESSION)
1776         | (REG ASSIGNMENT_OPERATOR EXPRESSION)
1777         | integer
1778
1779 EXPRESSION := ARG | (EXPRESSION OPERATOR ARG)
1780
1781 IF := (if EXPRESSION CCL_BLOCK [CCL_BLOCK])
1782 BRANCH := (branch EXPRESSION CCL_BLOCK [CCL_BLOCK ...])
1783 LOOP := (loop STATEMENT [STATEMENT ...])
1784 BREAK := (break)
1785 REPEAT :=
1786         (repeat)
1787         | (write-repeat [REG | integer | string])
1788         | (write-read-repeat REG [integer | ARRAY])
1789 READ :=
1790         (read REG ...)
1791         | (read-if (REG OPERATOR ARG) CCL_BLOCK CCL_BLOCK)
1792         | (read-branch REG CCL_BLOCK [CCL_BLOCK ...])
1793 WRITE :=
1794         (write REG ...)
1795         | (write EXPRESSION)
1796         | (write integer) | (write string) | (write REG ARRAY)
1797         | string
1798 CALL := (call ccl-program-name)
1799 END := (end)
1800
1801 REG := r0 | r1 | r2 | r3 | r4 | r5 | r6 | r7
1802 ARG := REG | integer
1803 OPERATOR :=
1804         + | - | * | / | % | & | '|' | ^ | << | >> | <8 | >8 | //
1805         | < | > | == | <= | >= | != | de-sjis | en-sjis
1806 ASSIGNMENT_OPERATOR :=
1807         += | -= | *= | /= | %= | &= | '|=' | ^= | <<= | >>=
1808 ARRAY := '[' integer ... ']'
1809 @end format
1810
1811
1812 @node    CCL Statements, CCL Expressions, CCL Syntax, CCL
1813 @comment Node,           Next,            Previous,   Up
1814 @subsection CCL Statements
1815
1816   The SXEmacs Code Conversion Language provides the following statement
1817 types: @dfn{set}, @dfn{if}, @dfn{branch}, @dfn{loop}, @dfn{repeat},
1818 @dfn{break}, @dfn{read}, @dfn{write}, @dfn{call}, and @dfn{end}.
1819
1820 @heading Set statement:
1821
1822   The @dfn{set} statement has three variants with the syntaxes
1823 @samp{(@var{reg} = @var{expression})},
1824 @samp{(@var{reg} @var{assignment_operator} @var{expression})}, and
1825 @samp{@var{integer}}.  The assignment operator variation of the
1826 @dfn{set} statement works the same way as the corresponding C expression
1827 statement does.  The assignment operators are @code{+=}, @code{-=},
1828 @code{*=}, @code{/=}, @code{%=}, @code{&=}, @code{|=}, @code{^=},
1829 @code{<<=}, and @code{>>=}, and they have the same meanings as in C.  A
1830 "naked integer" @var{integer} is equivalent to a @var{set} statement of
1831 the form @code{(r0 = @var{integer})}.
1832
1833 @heading I/O statements:
1834
1835   The @dfn{read} statement takes one or more registers as arguments.  It
1836 reads one byte (a C char) from the input into each register in turn.
1837
1838   The @dfn{write} takes several forms.  In the form @samp{(write @var{reg}
1839 ...)} it takes one or more registers as arguments and writes each in
1840 turn to the output.  The integer in a register (interpreted as an
1841 Emchar) is encoded to multibyte form (ie, Bufbytes) and written to the
1842 current output buffer.  If it is less than 256, it is written as is.
1843 The forms @samp{(write @var{expression})} and @samp{(write
1844 @var{integer})} are treated analogously.  The form @samp{(write
1845 @var{string})} writes the constant string to the output.  A
1846 "naked string" @samp{@var{string}} is equivalent to the statement @samp{(write
1847 @var{string})}.  The form @samp{(write @var{reg} @var{array})} writes
1848 the @var{reg}th element of the @var{array} to the output.
1849
1850 @heading Conditional statements:
1851
1852   The @dfn{if} statement takes an @var{expression}, a @var{CCL block}, and
1853 an optional @var{second CCL block} as arguments.  If the
1854 @var{expression} evaluates to non-zero, the first @var{CCL block} is
1855 executed.  Otherwise, if there is a @var{second CCL block}, it is
1856 executed.
1857
1858   The @dfn{read-if} variant of the @dfn{if} statement takes an
1859 @var{expression}, a @var{CCL block}, and an optional @var{second CCL
1860 block} as arguments.  The @var{expression} must have the form
1861 @code{(@var{reg} @var{operator} @var{operand})} (where @var{operand} is
1862 a register or an integer).  The @code{read-if} statement first reads
1863 from the input into the first register operand in the @var{expression},
1864 then conditionally executes a CCL block just as the @code{if} statement
1865 does.
1866
1867   The @dfn{branch} statement takes an @var{expression} and one or more CCL
1868 blocks as arguments.  The CCL blocks are treated as a zero-indexed
1869 array, and the @code{branch} statement uses the @var{expression} as the
1870 index of the CCL block to execute.  Null CCL blocks may be used as
1871 no-ops, continuing execution with the statement following the
1872 @code{branch} statement in the containing CCL block.  Out-of-range
1873 values for the @var{expression} are also treated as no-ops.
1874
1875   The @dfn{read-branch} variant of the @dfn{branch} statement takes an
1876 @var{register}, a @var{CCL block}, and an optional @var{second CCL
1877 block} as arguments.  The @code{read-branch} statement first reads from
1878 the input into the @var{register}, then conditionally executes a CCL
1879 block just as the @code{branch} statement does.
1880
1881 @heading Loop control statements:
1882
1883   The @dfn{loop} statement creates a block with an implied jump from the
1884 end of the block back to its head.  The loop is exited on a @code{break}
1885 statement, and continued without executing the tail by a @code{repeat}
1886 statement.
1887
1888   The @dfn{break} statement, written @samp{(break)}, terminates the
1889 current loop and continues with the next statement in the current
1890 block.
1891
1892   The @dfn{repeat} statement has three variants, @code{repeat},
1893 @code{write-repeat}, and @code{write-read-repeat}.  Each continues the
1894 current loop from its head, possibly after performing I/O.
1895 @code{repeat} takes no arguments and does no I/O before jumping.
1896 @code{write-repeat} takes a single argument (a register, an
1897 integer, or a string), writes it to the output, then jumps.
1898 @code{write-read-repeat} takes one or two arguments.  The first must
1899 be a register.  The second may be an integer or an array; if absent, it
1900 is implicitly set to the first (register) argument.
1901 @code{write-read-repeat} writes its second argument to the output, then
1902 reads from the input into the register, and finally jumps.  See the
1903 @code{write} and @code{read} statements for the semantics of the I/O
1904 operations for each type of argument.
1905
1906 @heading Other control statements:
1907
1908   The @dfn{call} statement, written @samp{(call @var{ccl-program-name})},
1909 executes a CCL program as a subroutine.  It does not return a value to
1910 the caller, but can modify the register status.
1911
1912   The @dfn{end} statement, written @samp{(end)}, terminates the CCL
1913 program successfully, and returns to caller (which may be a CCL
1914 program).  It does not alter the status of the registers.
1915
1916
1917 @node    CCL Expressions, Calling CCL, CCL Statements, CCL
1918 @comment Node,            Next,        Previous,       Up
1919 @subsection CCL Expressions
1920
1921   CCL, unlike Lisp, uses infix expressions.  The simplest CCL expressions
1922 consist of a single @var{operand}, either a register (one of @code{r0},
1923 ..., @code{r0}) or an integer.  Complex expressions are lists of the
1924 form @code{( @var{expression} @var{operator} @var{operand} )}.  Unlike
1925 C, assignments are not expressions.
1926
1927   In the following table, @var{X} is the target resister for a @dfn{set}.
1928 In subexpressions, this is implicitly @code{r7}.  This means that
1929 @code{>8}, @code{//}, @code{de-sjis}, and @code{en-sjis} cannot be used
1930 freely in subexpressions, since they return parts of their values in
1931 @code{r7}.  @var{Y} may be an expression, register, or integer, while
1932 @var{Z} must be a register or an integer.
1933
1934 @multitable @columnfractions .22 .14 .09 .55
1935 @item Name @tab Operator @tab Code @tab C-like Description
1936 @item CCL_PLUS @tab @code{+} @tab 0x00 @tab X = Y + Z
1937 @item CCL_MINUS @tab @code{-} @tab 0x01 @tab X = Y - Z
1938 @item CCL_MUL @tab @code{*} @tab 0x02 @tab X = Y * Z
1939 @item CCL_DIV @tab @code{/} @tab 0x03 @tab X = Y / Z
1940 @item CCL_MOD @tab @code{%} @tab 0x04 @tab X = Y % Z
1941 @item CCL_AND @tab @code{&} @tab 0x05 @tab X = Y & Z
1942 @item CCL_OR @tab @code{|} @tab 0x06 @tab X = Y | Z
1943 @item CCL_XOR @tab @code{^} @tab 0x07 @tab X = Y ^ Z
1944 @item CCL_LSH @tab @code{<<} @tab 0x08 @tab X = Y << Z
1945 @item CCL_RSH @tab @code{>>} @tab 0x09 @tab X = Y >> Z
1946 @item CCL_LSH8 @tab @code{<8} @tab 0x0A @tab X = (Y << 8) | Z
1947 @item CCL_RSH8 @tab @code{>8} @tab 0x0B @tab X = Y >> 8, r[7] = Y & 0xFF
1948 @item CCL_DIVMOD @tab @code{//} @tab 0x0C @tab X = Y / Z, r[7] = Y % Z
1949 @item CCL_LS @tab @code{<} @tab 0x10 @tab X = (X < Y)
1950 @item CCL_GT @tab @code{>} @tab 0x11 @tab X = (X > Y)
1951 @item CCL_EQ @tab @code{==} @tab 0x12 @tab X = (X == Y)
1952 @item CCL_LE @tab @code{<=} @tab 0x13 @tab X = (X <= Y)
1953 @item CCL_GE @tab @code{>=} @tab 0x14 @tab X = (X >= Y)
1954 @item CCL_NE @tab @code{!=} @tab 0x15 @tab X = (X != Y)
1955 @item CCL_ENCODE_SJIS @tab @code{en-sjis} @tab 0x16 @tab X = HIGHER_BYTE (SJIS (Y, Z))
1956 @item @tab @tab @tab r[7] = LOWER_BYTE (SJIS (Y, Z)
1957 @item CCL_DECODE_SJIS @tab @code{de-sjis} @tab 0x17 @tab X = HIGHER_BYTE (DE-SJIS (Y, Z))
1958 @item @tab @tab @tab r[7] = LOWER_BYTE (DE-SJIS (Y, Z))
1959 @end multitable
1960
1961   The CCL operators are as in C, with the addition of CCL_LSH8, CCL_RSH8,
1962 CCL_DIVMOD, CCL_ENCODE_SJIS, and CCL_DECODE_SJIS.  The CCL_ENCODE_SJIS
1963 and CCL_DECODE_SJIS treat their first and second bytes as the high and
1964 low bytes of a two-byte character code.  (SJIS stands for Shift JIS, an
1965 encoding of Japanese characters used by Microsoft.  CCL_ENCODE_SJIS is a
1966 complicated transformation of the Japanese standard JIS encoding to
1967 Shift JIS.  CCL_DECODE_SJIS is its inverse.)  It is somewhat odd to
1968 represent the SJIS operations in infix form.
1969
1970
1971 @node    Calling CCL, CCL Examples, CCL Expressions, CCL
1972 @comment Node,        Next,          Previous,        Up
1973 @subsection Calling CCL
1974
1975   CCL programs are called automatically during SXEmacs buffer I/O when the
1976 external representation has a coding system type of @code{shift-jis},
1977 @code{big5}, or @code{ccl}.  The program is specified by the coding
1978 system (@pxref{Coding Systems}).  You can also call CCL programs from
1979 other CCL programs, and from Lisp using these functions:
1980
1981 @defun ccl-execute ccl-program status
1982 Execute @var{ccl-program} with registers initialized by
1983 @var{status}.  @var{ccl-program} is a vector of compiled CCL code
1984 created by @code{ccl-compile}.  It is an error for the program to try to
1985 execute a CCL I/O command.  @var{status} must be a vector of nine
1986 values, specifying the initial value for the R0, R1 .. R7 registers and
1987 for the instruction counter IC.  A @code{nil} value for a register
1988 initializer causes the register to be set to 0.  A @code{nil} value for
1989 the IC initializer causes execution to start at the beginning of the
1990 program.  When the program is done, @var{status} is modified (by
1991 side-effect) to contain the ending values for the corresponding
1992 registers and IC.
1993 @end defun
1994
1995 @defun ccl-execute-on-string ccl-program status string &optional continue
1996 Execute @var{ccl-program} with initial @var{status} on
1997 @var{string}.  @var{ccl-program} is a vector of compiled CCL code
1998 created by @code{ccl-compile}.  @var{status} must be a vector of nine
1999 values, specifying the initial value for the R0, R1 .. R7 registers and
2000 for the instruction counter IC.  A @code{nil} value for a register
2001 initializer causes the register to be set to 0.  A @code{nil} value for
2002 the IC initializer causes execution to start at the beginning of the
2003 program.  An optional fourth argument @var{continue}, if non-@code{nil}, causes
2004 the IC to
2005 remain on the unsatisfied read operation if the program terminates due
2006 to exhaustion of the input buffer.  Otherwise the IC is set to the end
2007 of the program.  When the program is done, @var{status} is modified (by
2008 side-effect) to contain the ending values for the corresponding
2009 registers and IC.  Returns the resulting string.
2010 @end defun
2011
2012   To call a CCL program from another CCL program, it must first be
2013 registered:
2014
2015 @defun register-ccl-program name ccl-program
2016 Register @var{name} for CCL program @var{ccl-program} in
2017 @code{ccl-program-table}.  @var{ccl-program} should be the compiled form of
2018 a CCL program, or @code{nil}.  Return index number of the registered CCL
2019 program.
2020 @end defun
2021
2022   Information about the processor time used by the CCL interpreter can be
2023 obtained using these functions:
2024
2025 @defun ccl-elapsed-time
2026 Returns the elapsed processor time of the CCL interpreter as cons of
2027 user and system time, as
2028 floating point numbers measured in seconds.  If only one
2029 overall value can be determined, the return value will be a cons of that
2030 value and 0.
2031 @end defun
2032
2033 @defun ccl-reset-elapsed-time
2034 Resets the CCL interpreter's internal elapsed time registers.
2035 @end defun
2036
2037
2038 @node    CCL Examples, ,  Calling CCL, CCL
2039 @comment Node,         Next, Previous,    Up
2040 @subsection CCL Examples
2041
2042   This section is not yet written.
2043
2044
2045 @node Category Tables, , CCL, MULE
2046 @section Category Tables
2047
2048   A category table is a type of char table used for keeping track of
2049 categories.  Categories are used for classifying characters for use in
2050 regexps---you can refer to a category rather than having to use a
2051 complicated [] expression (and category lookups are significantly
2052 faster).
2053
2054   There are 95 different categories available, one for each printable
2055 character (including space) in the ASCII charset.  Each category is
2056 designated by one such character, called a @dfn{category designator}.
2057 They are specified in a regexp using the syntax @samp{\cX}, where X is a
2058 category designator. (This is not yet implemented.)
2059
2060   A category table specifies, for each character, the categories that
2061 the character is in.  Note that a character can be in more than one
2062 category.  More specifically, a category table maps from a character to
2063 either the value @code{nil} (meaning the character is in no categories)
2064 or a 95-element bit vector, specifying for each of the 95 categories
2065 whether the character is in that category.
2066
2067   Special Lisp functions are provided that abstract this, so you do not
2068 have to directly manipulate bit vectors.
2069
2070 @defun category-table-p object
2071 This function returns @code{t} if @var{object} is a category table.
2072 @end defun
2073
2074 @defun category-table &optional buffer
2075 This function returns the current category table.  This is the one
2076 specified by the current buffer, or by @var{buffer} if it is
2077 non-@code{nil}.
2078 @end defun
2079
2080 @defun standard-category-table
2081 This function returns the standard category table.  This is the one used
2082 for new buffers.
2083 @end defun
2084
2085 @defun copy-category-table &optional category-table
2086 This function returns a new category table which is a copy of
2087 @var{category-table}, which defaults to the standard category table.
2088 @end defun
2089
2090 @defun set-category-table category-table &optional buffer
2091 This function selects @var{category-table} as the new category table for
2092 @var{buffer}.  @var{buffer} defaults to the current buffer if omitted.
2093 @end defun
2094
2095 @defun category-designator-p object
2096 This function returns @code{t} if @var{object} is a category designator (a
2097 char in the range @samp{' '} to @samp{'~'}).
2098 @end defun
2099
2100 @defun category-table-value-p object
2101 This function returns @code{t} if @var{object} is a category table value.
2102 Valid values are @code{nil} or a bit vector of size 95.
2103 @end defun
2104