*** empty log message ***
[emacs.git] / lispref / nonascii.texi
blob3661079c56e197279a6e821f8a9b78926ab6309e
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004,
4 @c   2005, 2006, 2007  Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../info/characters
7 @node Non-ASCII Characters, Searching and Matching, Text, Top
8 @chapter Non-@acronym{ASCII} Characters
9 @cindex multibyte characters
10 @cindex non-@acronym{ASCII} characters
12   This chapter covers the special issues relating to non-@acronym{ASCII}
13 characters and how they are stored in strings and buffers.
15 @menu
16 * Text Representations::    Unibyte and multibyte representations
17 * Converting Representations::  Converting unibyte to multibyte and vice versa.
18 * Selecting a Representation::  Treating a byte sequence as unibyte or multi.
19 * Character Codes::         How unibyte and multibyte relate to
20                                 codes of individual characters.
21 * Character Sets::          The space of possible character codes
22                                 is divided into various character sets.
23 * Chars and Bytes::         More information about multibyte encodings.
24 * Splitting Characters::    Converting a character to its byte sequence.
25 * Scanning Charsets::       Which character sets are used in a buffer?
26 * Translation of Characters::   Translation tables are used for conversion.
27 * Coding Systems::          Coding systems are conversions for saving files.
28 * Input Methods::           Input methods allow users to enter various
29                                 non-ASCII characters without special keyboards.
30 * Locales::                 Interacting with the POSIX locale.
31 @end menu
33 @node Text Representations
34 @section Text Representations
35 @cindex text representations
37   Emacs has two @dfn{text representations}---two ways to represent text
38 in a string or buffer.  These are called @dfn{unibyte} and
39 @dfn{multibyte}.  Each string, and each buffer, uses one of these two
40 representations.  For most purposes, you can ignore the issue of
41 representations, because Emacs converts text between them as
42 appropriate.  Occasionally in Lisp programming you will need to pay
43 attention to the difference.
45 @cindex unibyte text
46   In unibyte representation, each character occupies one byte and
47 therefore the possible character codes range from 0 to 255.  Codes 0
48 through 127 are @acronym{ASCII} characters; the codes from 128 through 255
49 are used for one non-@acronym{ASCII} character set (you can choose which
50 character set by setting the variable @code{nonascii-insert-offset}).
52 @cindex leading code
53 @cindex multibyte text
54 @cindex trailing codes
55   In multibyte representation, a character may occupy more than one
56 byte, and as a result, the full range of Emacs character codes can be
57 stored.  The first byte of a multibyte character is always in the range
58 128 through 159 (octal 0200 through 0237).  These values are called
59 @dfn{leading codes}.  The second and subsequent bytes of a multibyte
60 character are always in the range 160 through 255 (octal 0240 through
61 0377); these values are @dfn{trailing codes}.
63   Some sequences of bytes are not valid in multibyte text: for example,
64 a single isolated byte in the range 128 through 159 is not allowed.  But
65 character codes 128 through 159 can appear in multibyte text,
66 represented as two-byte sequences.  All the character codes 128 through
67 255 are possible (though slightly abnormal) in multibyte text; they
68 appear in multibyte buffers and strings when you do explicit encoding
69 and decoding (@pxref{Explicit Encoding}).
71   In a buffer, the buffer-local value of the variable
72 @code{enable-multibyte-characters} specifies the representation used.
73 The representation for a string is determined and recorded in the string
74 when the string is constructed.
76 @defvar enable-multibyte-characters
77 This variable specifies the current buffer's text representation.
78 If it is non-@code{nil}, the buffer contains multibyte text; otherwise,
79 it contains unibyte text.
81 You cannot set this variable directly; instead, use the function
82 @code{set-buffer-multibyte} to change a buffer's representation.
83 @end defvar
85 @defvar default-enable-multibyte-characters
86 This variable's value is entirely equivalent to @code{(default-value
87 'enable-multibyte-characters)}, and setting this variable changes that
88 default value.  Setting the local binding of
89 @code{enable-multibyte-characters} in a specific buffer is not allowed,
90 but changing the default value is supported, and it is a reasonable
91 thing to do, because it has no effect on existing buffers.
93 The @samp{--unibyte} command line option does its job by setting the
94 default value to @code{nil} early in startup.
95 @end defvar
97 @defun position-bytes position
98 Return the byte-position corresponding to buffer position
99 @var{position} in the current buffer.  This is 1 at the start of the
100 buffer, and counts upward in bytes.  If @var{position} is out of
101 range, the value is @code{nil}.
102 @end defun
104 @defun byte-to-position byte-position
105 Return the buffer position corresponding to byte-position
106 @var{byte-position} in the current buffer.  If @var{byte-position} is
107 out of range, the value is @code{nil}.
108 @end defun
110 @defun multibyte-string-p string
111 Return @code{t} if @var{string} is a multibyte string.
112 @end defun
114 @defun string-bytes string
115 @cindex string, number of bytes
116 This function returns the number of bytes in @var{string}.
117 If @var{string} is a multibyte string, this can be greater than
118 @code{(length @var{string})}.
119 @end defun
121 @node Converting Representations
122 @section Converting Text Representations
124   Emacs can convert unibyte text to multibyte; it can also convert
125 multibyte text to unibyte, though this conversion loses information.  In
126 general these conversions happen when inserting text into a buffer, or
127 when putting text from several strings together in one string.  You can
128 also explicitly convert a string's contents to either representation.
130   Emacs chooses the representation for a string based on the text that
131 it is constructed from.  The general rule is to convert unibyte text to
132 multibyte text when combining it with other multibyte text, because the
133 multibyte representation is more general and can hold whatever
134 characters the unibyte text has.
136   When inserting text into a buffer, Emacs converts the text to the
137 buffer's representation, as specified by
138 @code{enable-multibyte-characters} in that buffer.  In particular, when
139 you insert multibyte text into a unibyte buffer, Emacs converts the text
140 to unibyte, even though this conversion cannot in general preserve all
141 the characters that might be in the multibyte text.  The other natural
142 alternative, to convert the buffer contents to multibyte, is not
143 acceptable because the buffer's representation is a choice made by the
144 user that cannot be overridden automatically.
146   Converting unibyte text to multibyte text leaves @acronym{ASCII} characters
147 unchanged, and likewise character codes 128 through 159.  It converts
148 the non-@acronym{ASCII} codes 160 through 255 by adding the value
149 @code{nonascii-insert-offset} to each character code.  By setting this
150 variable, you specify which character set the unibyte characters
151 correspond to (@pxref{Character Sets}).  For example, if
152 @code{nonascii-insert-offset} is 2048, which is @code{(- (make-char
153 'latin-iso8859-1) 128)}, then the unibyte non-@acronym{ASCII} characters
154 correspond to Latin 1.  If it is 2688, which is @code{(- (make-char
155 'greek-iso8859-7) 128)}, then they correspond to Greek letters.
157   Converting multibyte text to unibyte is simpler: it discards all but
158 the low 8 bits of each character code.  If @code{nonascii-insert-offset}
159 has a reasonable value, corresponding to the beginning of some character
160 set, this conversion is the inverse of the other: converting unibyte
161 text to multibyte and back to unibyte reproduces the original unibyte
162 text.
164 @defvar nonascii-insert-offset
165 This variable specifies the amount to add to a non-@acronym{ASCII} character
166 when converting unibyte text to multibyte.  It also applies when
167 @code{self-insert-command} inserts a character in the unibyte
168 non-@acronym{ASCII} range, 128 through 255.  However, the functions
169 @code{insert} and @code{insert-char} do not perform this conversion.
171 The right value to use to select character set @var{cs} is @code{(-
172 (make-char @var{cs}) 128)}.  If the value of
173 @code{nonascii-insert-offset} is zero, then conversion actually uses the
174 value for the Latin 1 character set, rather than zero.
175 @end defvar
177 @defvar nonascii-translation-table
178 This variable provides a more general alternative to
179 @code{nonascii-insert-offset}.  You can use it to specify independently
180 how to translate each code in the range of 128 through 255 into a
181 multibyte character.  The value should be a char-table, or @code{nil}.
182 If this is non-@code{nil}, it overrides @code{nonascii-insert-offset}.
183 @end defvar
185 The next three functions either return the argument @var{string}, or a
186 newly created string with no text properties.
188 @defun string-make-unibyte string
189 This function converts the text of @var{string} to unibyte
190 representation, if it isn't already, and returns the result.  If
191 @var{string} is a unibyte string, it is returned unchanged.  Multibyte
192 character codes are converted to unibyte according to
193 @code{nonascii-translation-table} or, if that is @code{nil}, using
194 @code{nonascii-insert-offset}.  If the lookup in the translation table
195 fails, this function takes just the low 8 bits of each character.
196 @end defun
198 @defun string-make-multibyte string
199 This function converts the text of @var{string} to multibyte
200 representation, if it isn't already, and returns the result.  If
201 @var{string} is a multibyte string or consists entirely of
202 @acronym{ASCII} characters, it is returned unchanged.  In particular,
203 if @var{string} is unibyte and entirely @acronym{ASCII}, the returned
204 string is unibyte.  (When the characters are all @acronym{ASCII},
205 Emacs primitives will treat the string the same way whether it is
206 unibyte or multibyte.)  If @var{string} is unibyte and contains
207 non-@acronym{ASCII} characters, the function
208 @code{unibyte-char-to-multibyte} is used to convert each unibyte
209 character to a multibyte character.
210 @end defun
212 @defun string-to-multibyte string
213 This function returns a multibyte string containing the same sequence
214 of character codes as @var{string}.  Unlike
215 @code{string-make-multibyte}, this function unconditionally returns a
216 multibyte string.  If @var{string} is a multibyte string, it is
217 returned unchanged.
218 @end defun
220 @defun multibyte-char-to-unibyte char
221 This convert the multibyte character @var{char} to a unibyte
222 character, based on @code{nonascii-translation-table} and
223 @code{nonascii-insert-offset}.
224 @end defun
226 @defun unibyte-char-to-multibyte char
227 This convert the unibyte character @var{char} to a multibyte
228 character, based on @code{nonascii-translation-table} and
229 @code{nonascii-insert-offset}.
230 @end defun
232 @node Selecting a Representation
233 @section Selecting a Representation
235   Sometimes it is useful to examine an existing buffer or string as
236 multibyte when it was unibyte, or vice versa.
238 @defun set-buffer-multibyte multibyte
239 Set the representation type of the current buffer.  If @var{multibyte}
240 is non-@code{nil}, the buffer becomes multibyte.  If @var{multibyte}
241 is @code{nil}, the buffer becomes unibyte.
243 This function leaves the buffer contents unchanged when viewed as a
244 sequence of bytes.  As a consequence, it can change the contents viewed
245 as characters; a sequence of two bytes which is treated as one character
246 in multibyte representation will count as two characters in unibyte
247 representation.  Character codes 128 through 159 are an exception.  They
248 are represented by one byte in a unibyte buffer, but when the buffer is
249 set to multibyte, they are converted to two-byte sequences, and vice
250 versa.
252 This function sets @code{enable-multibyte-characters} to record which
253 representation is in use.  It also adjusts various data in the buffer
254 (including overlays, text properties and markers) so that they cover the
255 same text as they did before.
257 You cannot use @code{set-buffer-multibyte} on an indirect buffer,
258 because indirect buffers always inherit the representation of the
259 base buffer.
260 @end defun
262 @defun string-as-unibyte string
263 This function returns a string with the same bytes as @var{string} but
264 treating each byte as a character.  This means that the value may have
265 more characters than @var{string} has.
267 If @var{string} is already a unibyte string, then the value is
268 @var{string} itself.  Otherwise it is a newly created string, with no
269 text properties.  If @var{string} is multibyte, any characters it
270 contains of charset @code{eight-bit-control} or @code{eight-bit-graphic}
271 are converted to the corresponding single byte.
272 @end defun
274 @defun string-as-multibyte string
275 This function returns a string with the same bytes as @var{string} but
276 treating each multibyte sequence as one character.  This means that the
277 value may have fewer characters than @var{string} has.
279 If @var{string} is already a multibyte string, then the value is
280 @var{string} itself.  Otherwise it is a newly created string, with no
281 text properties.  If @var{string} is unibyte and contains any individual
282 8-bit bytes (i.e.@: not part of a multibyte form), they are converted to
283 the corresponding multibyte character of charset @code{eight-bit-control}
284 or @code{eight-bit-graphic}.
285 @end defun
287 @node Character Codes
288 @section Character Codes
289 @cindex character codes
291   The unibyte and multibyte text representations use different character
292 codes.  The valid character codes for unibyte representation range from
293 0 to 255---the values that can fit in one byte.  The valid character
294 codes for multibyte representation range from 0 to 524287, but not all
295 values in that range are valid.  The values 128 through 255 are not
296 entirely proper in multibyte text, but they can occur if you do explicit
297 encoding and decoding (@pxref{Explicit Encoding}).  Some other character
298 codes cannot occur at all in multibyte text.  Only the @acronym{ASCII} codes
299 0 through 127 are completely legitimate in both representations.
301 @defun char-valid-p charcode &optional genericp
302 This returns @code{t} if @var{charcode} is valid (either for unibyte
303 text or for multibyte text).
305 @example
306 (char-valid-p 65)
307      @result{} t
308 (char-valid-p 256)
309      @result{} nil
310 (char-valid-p 2248)
311      @result{} t
312 @end example
314 If the optional argument @var{genericp} is non-@code{nil}, this
315 function also returns @code{t} if @var{charcode} is a generic
316 character (@pxref{Splitting Characters}).
317 @end defun
319 @node Character Sets
320 @section Character Sets
321 @cindex character sets
323   Emacs classifies characters into various @dfn{character sets}, each of
324 which has a name which is a symbol.  Each character belongs to one and
325 only one character set.
327   In general, there is one character set for each distinct script.  For
328 example, @code{latin-iso8859-1} is one character set,
329 @code{greek-iso8859-7} is another, and @code{ascii} is another.  An
330 Emacs character set can hold at most 9025 characters; therefore, in some
331 cases, characters that would logically be grouped together are split
332 into several character sets.  For example, one set of Chinese
333 characters, generally known as Big 5, is divided into two Emacs
334 character sets, @code{chinese-big5-1} and @code{chinese-big5-2}.
336   @acronym{ASCII} characters are in character set @code{ascii}.  The
337 non-@acronym{ASCII} characters 128 through 159 are in character set
338 @code{eight-bit-control}, and codes 160 through 255 are in character set
339 @code{eight-bit-graphic}.
341 @defun charsetp object
342 Returns @code{t} if @var{object} is a symbol that names a character set,
343 @code{nil} otherwise.
344 @end defun
346 @defvar charset-list
347 The value is a list of all defined character set names.
348 @end defvar
350 @defun charset-list
351 This function returns the value of @code{charset-list}.  It is only
352 provided for backward compatibility.
353 @end defun
355 @defun char-charset character
356 This function returns the name of the character set that @var{character}
357 belongs to, or the symbol @code{unknown} if @var{character} is not a
358 valid character.
359 @end defun
361 @defun charset-plist charset
362 This function returns the charset property list of the character set
363 @var{charset}.  Although @var{charset} is a symbol, this is not the same
364 as the property list of that symbol.  Charset properties are used for
365 special purposes within Emacs.
366 @end defun
368 @deffn Command list-charset-chars charset
369 This command displays a list of characters in the character set
370 @var{charset}.
371 @end deffn
373 @node Chars and Bytes
374 @section Characters and Bytes
375 @cindex bytes and characters
377 @cindex introduction sequence
378 @cindex dimension (of character set)
379   In multibyte representation, each character occupies one or more
380 bytes.  Each character set has an @dfn{introduction sequence}, which is
381 normally one or two bytes long.  (Exception: the @code{ascii} character
382 set and the @code{eight-bit-graphic} character set have a zero-length
383 introduction sequence.)  The introduction sequence is the beginning of
384 the byte sequence for any character in the character set.  The rest of
385 the character's bytes distinguish it from the other characters in the
386 same character set.  Depending on the character set, there are either
387 one or two distinguishing bytes; the number of such bytes is called the
388 @dfn{dimension} of the character set.
390 @defun charset-dimension charset
391 This function returns the dimension of @var{charset}; at present, the
392 dimension is always 1 or 2.
393 @end defun
395 @defun charset-bytes charset
396 This function returns the number of bytes used to represent a character
397 in character set @var{charset}.
398 @end defun
400   This is the simplest way to determine the byte length of a character
401 set's introduction sequence:
403 @example
404 (- (charset-bytes @var{charset})
405    (charset-dimension @var{charset}))
406 @end example
408 @node Splitting Characters
409 @section Splitting Characters
411   The functions in this section convert between characters and the byte
412 values used to represent them.  For most purposes, there is no need to
413 be concerned with the sequence of bytes used to represent a character,
414 because Emacs translates automatically when necessary.
416 @defun split-char character
417 Return a list containing the name of the character set of
418 @var{character}, followed by one or two byte values (integers) which
419 identify @var{character} within that character set.  The number of byte
420 values is the character set's dimension.
422 If @var{character} is invalid as a character code, @code{split-char}
423 returns a list consisting of the symbol @code{unknown} and @var{character}.
425 @example
426 (split-char 2248)
427      @result{} (latin-iso8859-1 72)
428 (split-char 65)
429      @result{} (ascii 65)
430 (split-char 128)
431      @result{} (eight-bit-control 128)
432 @end example
433 @end defun
435 @defun make-char charset &optional code1 code2
436 This function returns the character in character set @var{charset} whose
437 position codes are @var{code1} and @var{code2}.  This is roughly the
438 inverse of @code{split-char}.  Normally, you should specify either one
439 or both of @var{code1} and @var{code2} according to the dimension of
440 @var{charset}.  For example,
442 @example
443 (make-char 'latin-iso8859-1 72)
444      @result{} 2248
445 @end example
447 Actually, the eighth bit of both @var{code1} and @var{code2} is zeroed
448 before they are used to index @var{charset}.  Thus you may use, for
449 instance, an ISO 8859 character code rather than subtracting 128, as
450 is necessary to index the corresponding Emacs charset.
451 @end defun
453 @cindex generic characters
454   If you call @code{make-char} with no @var{byte-values}, the result is
455 a @dfn{generic character} which stands for @var{charset}.  A generic
456 character is an integer, but it is @emph{not} valid for insertion in the
457 buffer as a character.  It can be used in @code{char-table-range} to
458 refer to the whole character set (@pxref{Char-Tables}).
459 @code{char-valid-p} returns @code{nil} for generic characters.
460 For example:
462 @example
463 (make-char 'latin-iso8859-1)
464      @result{} 2176
465 (char-valid-p 2176)
466      @result{} nil
467 (char-valid-p 2176 t)
468      @result{} t
469 (split-char 2176)
470      @result{} (latin-iso8859-1 0)
471 @end example
473 The character sets @code{ascii}, @code{eight-bit-control}, and
474 @code{eight-bit-graphic} don't have corresponding generic characters.  If
475 @var{charset} is one of them and you don't supply @var{code1},
476 @code{make-char} returns the character code corresponding to the
477 smallest code in @var{charset}.
479 @node Scanning Charsets
480 @section Scanning for Character Sets
482   Sometimes it is useful to find out which character sets appear in a
483 part of a buffer or a string.  One use for this is in determining which
484 coding systems (@pxref{Coding Systems}) are capable of representing all
485 of the text in question.
487 @defun charset-after &optional pos
488 This function return the charset of a character in the current buffer
489 at position @var{pos}.  If @var{pos} is omitted or @code{nil}, it
490 defaults to the current value of point.  If @var{pos} is out of range,
491 the value is @code{nil}.
492 @end defun
494 @defun find-charset-region beg end &optional translation
495 This function returns a list of the character sets that appear in the
496 current buffer between positions @var{beg} and @var{end}.
498 The optional argument @var{translation} specifies a translation table to
499 be used in scanning the text (@pxref{Translation of Characters}).  If it
500 is non-@code{nil}, then each character in the region is translated
501 through this table, and the value returned describes the translated
502 characters instead of the characters actually in the buffer.
503 @end defun
505 @defun find-charset-string string &optional translation
506 This function returns a list of the character sets that appear in the
507 string @var{string}.  It is just like @code{find-charset-region}, except
508 that it applies to the contents of @var{string} instead of part of the
509 current buffer.
510 @end defun
512 @node Translation of Characters
513 @section Translation of Characters
514 @cindex character translation tables
515 @cindex translation tables
517   A @dfn{translation table} is a char-table that specifies a mapping
518 of characters into characters.  These tables are used in encoding and
519 decoding, and for other purposes.  Some coding systems specify their
520 own particular translation tables; there are also default translation
521 tables which apply to all other coding systems.
523   For instance, the coding-system @code{utf-8} has a translation table
524 that maps characters of various charsets (e.g.,
525 @code{latin-iso8859-@var{x}}) into Unicode character sets.  This way,
526 it can encode Latin-2 characters into UTF-8.  Meanwhile,
527 @code{unify-8859-on-decoding-mode} operates by specifying
528 @code{standard-translation-table-for-decode} to translate
529 Latin-@var{x} characters into corresponding Unicode characters.
531 @defun make-translation-table &rest translations
532 This function returns a translation table based on the argument
533 @var{translations}.  Each element of @var{translations} should be a
534 list of elements of the form @code{(@var{from} . @var{to})}; this says
535 to translate the character @var{from} into @var{to}.
537 The arguments and the forms in each argument are processed in order,
538 and if a previous form already translates @var{to} to some other
539 character, say @var{to-alt}, @var{from} is also translated to
540 @var{to-alt}.
542 You can also map one whole character set into another character set with
543 the same dimension.  To do this, you specify a generic character (which
544 designates a character set) for @var{from} (@pxref{Splitting Characters}).
545 In this case, if @var{to} is also a generic character, its character
546 set should have the same dimension as @var{from}'s.  Then the
547 translation table translates each character of @var{from}'s character
548 set into the corresponding character of @var{to}'s character set.  If
549 @var{from} is a generic character and @var{to} is an ordinary
550 character, then the translation table translates every character of
551 @var{from}'s character set into @var{to}.
552 @end defun
554   In decoding, the translation table's translations are applied to the
555 characters that result from ordinary decoding.  If a coding system has
556 property @code{translation-table-for-decode}, that specifies the
557 translation table to use.  (This is a property of the coding system,
558 as returned by @code{coding-system-get}, not a property of the symbol
559 that is the coding system's name. @xref{Coding System Basics,, Basic
560 Concepts of Coding Systems}.)  Otherwise, if
561 @code{standard-translation-table-for-decode} is non-@code{nil},
562 decoding uses that table.
564   In encoding, the translation table's translations are applied to the
565 characters in the buffer, and the result of translation is actually
566 encoded.  If a coding system has property
567 @code{translation-table-for-encode}, that specifies the translation
568 table to use.  Otherwise the variable
569 @code{standard-translation-table-for-encode} specifies the translation
570 table.
572 @defvar standard-translation-table-for-decode
573 This is the default translation table for decoding, for
574 coding systems that don't specify any other translation table.
575 @end defvar
577 @defvar standard-translation-table-for-encode
578 This is the default translation table for encoding, for
579 coding systems that don't specify any other translation table.
580 @end defvar
582 @defvar translation-table-for-input
583 Self-inserting characters are translated through this translation
584 table before they are inserted.  Search commands also translate their
585 input through this table, so they can compare more reliably with
586 what's in the buffer.
588 @code{set-buffer-file-coding-system} sets this variable so that your
589 keyboard input gets translated into the character sets that the buffer
590 is likely to contain.  This variable automatically becomes
591 buffer-local when set.
592 @end defvar
594 @node Coding Systems
595 @section Coding Systems
597 @cindex coding system
598   When Emacs reads or writes a file, and when Emacs sends text to a
599 subprocess or receives text from a subprocess, it normally performs
600 character code conversion and end-of-line conversion as specified
601 by a particular @dfn{coding system}.
603   How to define a coding system is an arcane matter, and is not
604 documented here.
606 @menu
607 * Coding System Basics::        Basic concepts.
608 * Encoding and I/O::            How file I/O functions handle coding systems.
609 * Lisp and Coding Systems::     Functions to operate on coding system names.
610 * User-Chosen Coding Systems::  Asking the user to choose a coding system.
611 * Default Coding Systems::      Controlling the default choices.
612 * Specifying Coding Systems::   Requesting a particular coding system
613                                     for a single file operation.
614 * Explicit Encoding::           Encoding or decoding text without doing I/O.
615 * Terminal I/O Encoding::       Use of encoding for terminal I/O.
616 * MS-DOS File Types::           How DOS "text" and "binary" files
617                                     relate to coding systems.
618 @end menu
620 @node Coding System Basics
621 @subsection Basic Concepts of Coding Systems
623 @cindex character code conversion
624   @dfn{Character code conversion} involves conversion between the encoding
625 used inside Emacs and some other encoding.  Emacs supports many
626 different encodings, in that it can convert to and from them.  For
627 example, it can convert text to or from encodings such as Latin 1, Latin
628 2, Latin 3, Latin 4, Latin 5, and several variants of ISO 2022.  In some
629 cases, Emacs supports several alternative encodings for the same
630 characters; for example, there are three coding systems for the Cyrillic
631 (Russian) alphabet: ISO, Alternativnyj, and KOI8.
633   Most coding systems specify a particular character code for
634 conversion, but some of them leave the choice unspecified---to be chosen
635 heuristically for each file, based on the data.
637   In general, a coding system doesn't guarantee roundtrip identity:
638 decoding a byte sequence using coding system, then encoding the
639 resulting text in the same coding system, can produce a different byte
640 sequence.  However, the following coding systems do guarantee that the
641 byte sequence will be the same as what you originally decoded:
643 @quotation
644 chinese-big5 chinese-iso-8bit cyrillic-iso-8bit emacs-mule
645 greek-iso-8bit hebrew-iso-8bit iso-latin-1 iso-latin-2 iso-latin-3
646 iso-latin-4 iso-latin-5 iso-latin-8 iso-latin-9 iso-safe
647 japanese-iso-8bit japanese-shift-jis korean-iso-8bit raw-text
648 @end quotation
650   Encoding buffer text and then decoding the result can also fail to
651 reproduce the original text.  For instance, if you encode Latin-2
652 characters with @code{utf-8} and decode the result using the same
653 coding system, you'll get Unicode characters (of charset
654 @code{mule-unicode-0100-24ff}).  If you encode Unicode characters with
655 @code{iso-latin-2} and decode the result with the same coding system,
656 you'll get Latin-2 characters.
658 @cindex end of line conversion
659   @dfn{End of line conversion} handles three different conventions used
660 on various systems for representing end of line in files.  The Unix
661 convention is to use the linefeed character (also called newline).  The
662 DOS convention is to use a carriage-return and a linefeed at the end of
663 a line.  The Mac convention is to use just carriage-return.
665 @cindex base coding system
666 @cindex variant coding system
667   @dfn{Base coding systems} such as @code{latin-1} leave the end-of-line
668 conversion unspecified, to be chosen based on the data.  @dfn{Variant
669 coding systems} such as @code{latin-1-unix}, @code{latin-1-dos} and
670 @code{latin-1-mac} specify the end-of-line conversion explicitly as
671 well.  Most base coding systems have three corresponding variants whose
672 names are formed by adding @samp{-unix}, @samp{-dos} and @samp{-mac}.
674   The coding system @code{raw-text} is special in that it prevents
675 character code conversion, and causes the buffer visited with that
676 coding system to be a unibyte buffer.  It does not specify the
677 end-of-line conversion, allowing that to be determined as usual by the
678 data, and has the usual three variants which specify the end-of-line
679 conversion.  @code{no-conversion} is equivalent to @code{raw-text-unix}:
680 it specifies no conversion of either character codes or end-of-line.
682   The coding system @code{emacs-mule} specifies that the data is
683 represented in the internal Emacs encoding.  This is like
684 @code{raw-text} in that no code conversion happens, but different in
685 that the result is multibyte data.
687 @defun coding-system-get coding-system property
688 This function returns the specified property of the coding system
689 @var{coding-system}.  Most coding system properties exist for internal
690 purposes, but one that you might find useful is @code{mime-charset}.
691 That property's value is the name used in MIME for the character coding
692 which this coding system can read and write.  Examples:
694 @example
695 (coding-system-get 'iso-latin-1 'mime-charset)
696      @result{} iso-8859-1
697 (coding-system-get 'iso-2022-cn 'mime-charset)
698      @result{} iso-2022-cn
699 (coding-system-get 'cyrillic-koi8 'mime-charset)
700      @result{} koi8-r
701 @end example
703 The value of the @code{mime-charset} property is also defined
704 as an alias for the coding system.
705 @end defun
707 @node Encoding and I/O
708 @subsection Encoding and I/O
710   The principal purpose of coding systems is for use in reading and
711 writing files.  The function @code{insert-file-contents} uses
712 a coding system for decoding the file data, and @code{write-region}
713 uses one to encode the buffer contents.
715   You can specify the coding system to use either explicitly
716 (@pxref{Specifying Coding Systems}), or implicitly using a default
717 mechanism (@pxref{Default Coding Systems}).  But these methods may not
718 completely specify what to do.  For example, they may choose a coding
719 system such as @code{undefined} which leaves the character code
720 conversion to be determined from the data.  In these cases, the I/O
721 operation finishes the job of choosing a coding system.  Very often
722 you will want to find out afterwards which coding system was chosen.
724 @defvar buffer-file-coding-system
725 This buffer-local variable records the coding system that was used to visit
726 the current buffer.  It is used for saving the buffer, and for writing part
727 of the buffer with @code{write-region}.  If the text to be written
728 cannot be safely encoded using the coding system specified by this
729 variable, these operations select an alternative encoding by calling
730 the function @code{select-safe-coding-system} (@pxref{User-Chosen
731 Coding Systems}).  If selecting a different encoding requires to ask
732 the user to specify a coding system, @code{buffer-file-coding-system}
733 is updated to the newly selected coding system.
735 @code{buffer-file-coding-system} does @emph{not} affect sending text
736 to a subprocess.
737 @end defvar
739 @defvar save-buffer-coding-system
740 This variable specifies the coding system for saving the buffer (by
741 overriding @code{buffer-file-coding-system}).  Note that it is not used
742 for @code{write-region}.
744 When a command to save the buffer starts out to use
745 @code{buffer-file-coding-system} (or @code{save-buffer-coding-system}),
746 and that coding system cannot handle
747 the actual text in the buffer, the command asks the user to choose
748 another coding system (by calling @code{select-safe-coding-system}).
749 After that happens, the command also updates
750 @code{buffer-file-coding-system} to represent the coding system that
751 the user specified.
752 @end defvar
754 @defvar last-coding-system-used
755 I/O operations for files and subprocesses set this variable to the
756 coding system name that was used.  The explicit encoding and decoding
757 functions (@pxref{Explicit Encoding}) set it too.
759 @strong{Warning:} Since receiving subprocess output sets this variable,
760 it can change whenever Emacs waits; therefore, you should copy the
761 value shortly after the function call that stores the value you are
762 interested in.
763 @end defvar
765   The variable @code{selection-coding-system} specifies how to encode
766 selections for the window system.  @xref{Window System Selections}.
768 @defvar file-name-coding-system
769 The variable @code{file-name-coding-system} specifies the coding
770 system to use for encoding file names.  Emacs encodes file names using
771 that coding system for all file operations.  If
772 @code{file-name-coding-system} is @code{nil}, Emacs uses a default
773 coding system determined by the selected language environment.  In the
774 default language environment, any non-@acronym{ASCII} characters in
775 file names are not encoded specially; they appear in the file system
776 using the internal Emacs representation.
777 @end defvar
779   @strong{Warning:} if you change @code{file-name-coding-system} (or
780 the language environment) in the middle of an Emacs session, problems
781 can result if you have already visited files whose names were encoded
782 using the earlier coding system and are handled differently under the
783 new coding system.  If you try to save one of these buffers under the
784 visited file name, saving may use the wrong file name, or it may get
785 an error.  If such a problem happens, use @kbd{C-x C-w} to specify a
786 new file name for that buffer.
788 @node Lisp and Coding Systems
789 @subsection Coding Systems in Lisp
791   Here are the Lisp facilities for working with coding systems:
793 @defun coding-system-list &optional base-only
794 This function returns a list of all coding system names (symbols).  If
795 @var{base-only} is non-@code{nil}, the value includes only the
796 base coding systems.  Otherwise, it includes alias and variant coding
797 systems as well.
798 @end defun
800 @defun coding-system-p object
801 This function returns @code{t} if @var{object} is a coding system
802 name or @code{nil}.
803 @end defun
805 @defun check-coding-system coding-system
806 This function checks the validity of @var{coding-system}.
807 If that is valid, it returns @var{coding-system}.
808 Otherwise it signals an error with condition @code{coding-system-error}.
809 @end defun
811 @cindex EOL conversion
812 @cindex end-of-line conversion
813 @cindex line end conversion
814 @defun coding-system-eol-type coding-system
815 This function returns the type of end-of-line (a.k.a.@: @dfn{eol})
816 conversion used by @var{coding-system}.  If @var{coding-system}
817 specifies a certain eol conversion, the return value is an integer 0,
818 1, or 2, standing for @code{unix}, @code{dos}, and @code{mac},
819 respectively.  If @var{coding-system} doesn't specify eol conversion
820 explicitly, the return value is a vector of coding systems, each one
821 with one of the possible eol conversion types, like this:
823 @lisp
824 (coding-system-eol-type 'latin-1)
825      @result{} [latin-1-unix latin-1-dos latin-1-mac]
826 @end lisp
828 @noindent
829 If this function returns a vector, Emacs will decide, as part of the
830 text encoding or decoding process, what eol conversion to use.  For
831 decoding, the end-of-line format of the text is auto-detected, and the
832 eol conversion is set to match it (e.g., DOS-style CRLF format will
833 imply @code{dos} eol conversion).  For encoding, the eol conversion is
834 taken from the appropriate default coding system (e.g.,
835 @code{default-buffer-file-coding-system} for
836 @code{buffer-file-coding-system}), or from the default eol conversion
837 appropriate for the underlying platform.
838 @end defun
840 @defun coding-system-change-eol-conversion coding-system eol-type
841 This function returns a coding system which is like @var{coding-system}
842 except for its eol conversion, which is specified by @code{eol-type}.
843 @var{eol-type} should be @code{unix}, @code{dos}, @code{mac}, or
844 @code{nil}.  If it is @code{nil}, the returned coding system determines
845 the end-of-line conversion from the data.
847 @var{eol-type} may also be 0, 1 or 2, standing for @code{unix},
848 @code{dos} and @code{mac}, respectively.
849 @end defun
851 @defun coding-system-change-text-conversion eol-coding text-coding
852 This function returns a coding system which uses the end-of-line
853 conversion of @var{eol-coding}, and the text conversion of
854 @var{text-coding}.  If @var{text-coding} is @code{nil}, it returns
855 @code{undecided}, or one of its variants according to @var{eol-coding}.
856 @end defun
858 @defun find-coding-systems-region from to
859 This function returns a list of coding systems that could be used to
860 encode a text between @var{from} and @var{to}.  All coding systems in
861 the list can safely encode any multibyte characters in that portion of
862 the text.
864 If the text contains no multibyte characters, the function returns the
865 list @code{(undecided)}.
866 @end defun
868 @defun find-coding-systems-string string
869 This function returns a list of coding systems that could be used to
870 encode the text of @var{string}.  All coding systems in the list can
871 safely encode any multibyte characters in @var{string}.  If the text
872 contains no multibyte characters, this returns the list
873 @code{(undecided)}.
874 @end defun
876 @defun find-coding-systems-for-charsets charsets
877 This function returns a list of coding systems that could be used to
878 encode all the character sets in the list @var{charsets}.
879 @end defun
881 @defun detect-coding-region start end &optional highest
882 This function chooses a plausible coding system for decoding the text
883 from @var{start} to @var{end}.  This text should be a byte sequence
884 (@pxref{Explicit Encoding}).
886 Normally this function returns a list of coding systems that could
887 handle decoding the text that was scanned.  They are listed in order of
888 decreasing priority.  But if @var{highest} is non-@code{nil}, then the
889 return value is just one coding system, the one that is highest in
890 priority.
892 If the region contains only @acronym{ASCII} characters except for such
893 ISO-2022 control characters ISO-2022 as @code{ESC}, the value is
894 @code{undecided} or @code{(undecided)}, or a variant specifying
895 end-of-line conversion, if that can be deduced from the text.
896 @end defun
898 @defun detect-coding-string string &optional highest
899 This function is like @code{detect-coding-region} except that it
900 operates on the contents of @var{string} instead of bytes in the buffer.
901 @end defun
903   @xref{Coding systems for a subprocess,, Process Information}, in
904 particular the description of the functions
905 @code{process-coding-system} and @code{set-process-coding-system}, for
906 how to examine or set the coding systems used for I/O to a subprocess.
908 @node User-Chosen Coding Systems
909 @subsection User-Chosen Coding Systems
911 @cindex select safe coding system
912 @defun select-safe-coding-system from to &optional default-coding-system accept-default-p file
913 This function selects a coding system for encoding specified text,
914 asking the user to choose if necessary.  Normally the specified text
915 is the text in the current buffer between @var{from} and @var{to}.  If
916 @var{from} is a string, the string specifies the text to encode, and
917 @var{to} is ignored.
919 If @var{default-coding-system} is non-@code{nil}, that is the first
920 coding system to try; if that can handle the text,
921 @code{select-safe-coding-system} returns that coding system.  It can
922 also be a list of coding systems; then the function tries each of them
923 one by one.  After trying all of them, it next tries the current
924 buffer's value of @code{buffer-file-coding-system} (if it is not
925 @code{undecided}), then the value of
926 @code{default-buffer-file-coding-system} and finally the user's most
927 preferred coding system, which the user can set using the command
928 @code{prefer-coding-system} (@pxref{Recognize Coding,, Recognizing
929 Coding Systems, emacs, The GNU Emacs Manual}).
931 If one of those coding systems can safely encode all the specified
932 text, @code{select-safe-coding-system} chooses it and returns it.
933 Otherwise, it asks the user to choose from a list of coding systems
934 which can encode all the text, and returns the user's choice.
936 @var{default-coding-system} can also be a list whose first element is
937 t and whose other elements are coding systems.  Then, if no coding
938 system in the list can handle the text, @code{select-safe-coding-system}
939 queries the user immediately, without trying any of the three
940 alternatives described above.
942 The optional argument @var{accept-default-p}, if non-@code{nil},
943 should be a function to determine whether a coding system selected
944 without user interaction is acceptable. @code{select-safe-coding-system}
945 calls this function with one argument, the base coding system of the
946 selected coding system.  If @var{accept-default-p} returns @code{nil},
947 @code{select-safe-coding-system} rejects the silently selected coding
948 system, and asks the user to select a coding system from a list of
949 possible candidates.
951 @vindex select-safe-coding-system-accept-default-p
952 If the variable @code{select-safe-coding-system-accept-default-p} is
953 non-@code{nil}, its value overrides the value of
954 @var{accept-default-p}.
956 As a final step, before returning the chosen coding system,
957 @code{select-safe-coding-system} checks whether that coding system is
958 consistent with what would be selected if the contents of the region
959 were read from a file.  (If not, this could lead to data corruption in
960 a file subsequently re-visited and edited.)  Normally,
961 @code{select-safe-coding-system} uses @code{buffer-file-name} as the
962 file for this purpose, but if @var{file} is non-@code{nil}, it uses
963 that file instead (this can be relevant for @code{write-region} and
964 similar functions).  If it detects an apparent inconsistency,
965 @code{select-safe-coding-system} queries the user before selecting the
966 coding system.
967 @end defun
969   Here are two functions you can use to let the user specify a coding
970 system, with completion.  @xref{Completion}.
972 @defun read-coding-system prompt &optional default
973 This function reads a coding system using the minibuffer, prompting with
974 string @var{prompt}, and returns the coding system name as a symbol.  If
975 the user enters null input, @var{default} specifies which coding system
976 to return.  It should be a symbol or a string.
977 @end defun
979 @defun read-non-nil-coding-system prompt
980 This function reads a coding system using the minibuffer, prompting with
981 string @var{prompt}, and returns the coding system name as a symbol.  If
982 the user tries to enter null input, it asks the user to try again.
983 @xref{Coding Systems}.
984 @end defun
986 @node Default Coding Systems
987 @subsection Default Coding Systems
989   This section describes variables that specify the default coding
990 system for certain files or when running certain subprograms, and the
991 function that I/O operations use to access them.
993   The idea of these variables is that you set them once and for all to the
994 defaults you want, and then do not change them again.  To specify a
995 particular coding system for a particular operation in a Lisp program,
996 don't change these variables; instead, override them using
997 @code{coding-system-for-read} and @code{coding-system-for-write}
998 (@pxref{Specifying Coding Systems}).
1000 @defvar auto-coding-regexp-alist
1001 This variable is an alist of text patterns and corresponding coding
1002 systems. Each element has the form @code{(@var{regexp}
1003 . @var{coding-system})}; a file whose first few kilobytes match
1004 @var{regexp} is decoded with @var{coding-system} when its contents are
1005 read into a buffer.  The settings in this alist take priority over
1006 @code{coding:} tags in the files and the contents of
1007 @code{file-coding-system-alist} (see below).  The default value is set
1008 so that Emacs automatically recognizes mail files in Babyl format and
1009 reads them with no code conversions.
1010 @end defvar
1012 @defvar file-coding-system-alist
1013 This variable is an alist that specifies the coding systems to use for
1014 reading and writing particular files.  Each element has the form
1015 @code{(@var{pattern} . @var{coding})}, where @var{pattern} is a regular
1016 expression that matches certain file names.  The element applies to file
1017 names that match @var{pattern}.
1019 The @sc{cdr} of the element, @var{coding}, should be either a coding
1020 system, a cons cell containing two coding systems, or a function name (a
1021 symbol with a function definition).  If @var{coding} is a coding system,
1022 that coding system is used for both reading the file and writing it.  If
1023 @var{coding} is a cons cell containing two coding systems, its @sc{car}
1024 specifies the coding system for decoding, and its @sc{cdr} specifies the
1025 coding system for encoding.
1027 If @var{coding} is a function name, the function should take one
1028 argument, a list of all arguments passed to
1029 @code{find-operation-coding-system}.  It must return a coding system
1030 or a cons cell containing two coding systems.  This value has the same
1031 meaning as described above.
1032 @end defvar
1034 @defvar process-coding-system-alist
1035 This variable is an alist specifying which coding systems to use for a
1036 subprocess, depending on which program is running in the subprocess.  It
1037 works like @code{file-coding-system-alist}, except that @var{pattern} is
1038 matched against the program name used to start the subprocess.  The coding
1039 system or systems specified in this alist are used to initialize the
1040 coding systems used for I/O to the subprocess, but you can specify
1041 other coding systems later using @code{set-process-coding-system}.
1042 @end defvar
1044   @strong{Warning:} Coding systems such as @code{undecided}, which
1045 determine the coding system from the data, do not work entirely reliably
1046 with asynchronous subprocess output.  This is because Emacs handles
1047 asynchronous subprocess output in batches, as it arrives.  If the coding
1048 system leaves the character code conversion unspecified, or leaves the
1049 end-of-line conversion unspecified, Emacs must try to detect the proper
1050 conversion from one batch at a time, and this does not always work.
1052   Therefore, with an asynchronous subprocess, if at all possible, use a
1053 coding system which determines both the character code conversion and
1054 the end of line conversion---that is, one like @code{latin-1-unix},
1055 rather than @code{undecided} or @code{latin-1}.
1057 @defvar network-coding-system-alist
1058 This variable is an alist that specifies the coding system to use for
1059 network streams.  It works much like @code{file-coding-system-alist},
1060 with the difference that the @var{pattern} in an element may be either a
1061 port number or a regular expression.  If it is a regular expression, it
1062 is matched against the network service name used to open the network
1063 stream.
1064 @end defvar
1066 @defvar default-process-coding-system
1067 This variable specifies the coding systems to use for subprocess (and
1068 network stream) input and output, when nothing else specifies what to
1071 The value should be a cons cell of the form @code{(@var{input-coding}
1072 . @var{output-coding})}.  Here @var{input-coding} applies to input from
1073 the subprocess, and @var{output-coding} applies to output to it.
1074 @end defvar
1076 @defvar auto-coding-functions
1077 This variable holds a list of functions that try to determine a
1078 coding system for a file based on its undecoded contents.
1080 Each function in this list should be written to look at text in the
1081 current buffer, but should not modify it in any way.  The buffer will
1082 contain undecoded text of parts of the file.  Each function should
1083 take one argument, @var{size}, which tells it how many characters to
1084 look at, starting from point.  If the function succeeds in determining
1085 a coding system for the file, it should return that coding system.
1086 Otherwise, it should return @code{nil}.
1088 If a file has a @samp{coding:} tag, that takes precedence, so these
1089 functions won't be called.
1090 @end defvar
1092 @defun find-operation-coding-system operation &rest arguments
1093 This function returns the coding system to use (by default) for
1094 performing @var{operation} with @var{arguments}.  The value has this
1095 form:
1097 @example
1098 (@var{decoding-system} . @var{encoding-system})
1099 @end example
1101 The first element, @var{decoding-system}, is the coding system to use
1102 for decoding (in case @var{operation} does decoding), and
1103 @var{encoding-system} is the coding system for encoding (in case
1104 @var{operation} does encoding).
1106 The argument @var{operation} should be a symbol, any one of
1107 @code{insert-file-contents}, @code{write-region},
1108 @code{start-process}, @code{call-process}, @code{call-process-region},
1109 or @code{open-network-stream}.  These are the names of the Emacs I/O
1110 primitives that can do character code and eol conversion.
1112 The remaining arguments should be the same arguments that might be given
1113 to the corresponding I/O primitive.  Depending on the primitive, one
1114 of those arguments is selected as the @dfn{target}.  For example, if
1115 @var{operation} does file I/O, whichever argument specifies the file
1116 name is the target.  For subprocess primitives, the process name is the
1117 target.  For @code{open-network-stream}, the target is the service name
1118 or port number.
1120 Depending on @var{operation}, this function looks up the target in
1121 @code{file-coding-system-alist}, @code{process-coding-system-alist},
1122 or @code{network-coding-system-alist}.  If the target is found in the
1123 alist, @code{find-operation-coding-system} returns its association in
1124 the alist; otherwise it returns @code{nil}.
1126 If @var{operation} is @code{insert-file-contents}, the argument
1127 corresponding to the target may be a cons cell of the form
1128 @code{(@var{filename} . @var{buffer})}).  In that case, @var{filename}
1129 is a file name to look up in @code{file-coding-system-alist}, and
1130 @var{buffer} is a buffer that contains the file's contents (not yet
1131 decoded).  If @code{file-coding-system-alist} specifies a function to
1132 call for this file, and that function needs to examine the file's
1133 contents (as it usually does), it should examine the contents of
1134 @var{buffer} instead of reading the file.
1135 @end defun
1137 @node Specifying Coding Systems
1138 @subsection Specifying a Coding System for One Operation
1140   You can specify the coding system for a specific operation by binding
1141 the variables @code{coding-system-for-read} and/or
1142 @code{coding-system-for-write}.
1144 @defvar coding-system-for-read
1145 If this variable is non-@code{nil}, it specifies the coding system to
1146 use for reading a file, or for input from a synchronous subprocess.
1148 It also applies to any asynchronous subprocess or network stream, but in
1149 a different way: the value of @code{coding-system-for-read} when you
1150 start the subprocess or open the network stream specifies the input
1151 decoding method for that subprocess or network stream.  It remains in
1152 use for that subprocess or network stream unless and until overridden.
1154 The right way to use this variable is to bind it with @code{let} for a
1155 specific I/O operation.  Its global value is normally @code{nil}, and
1156 you should not globally set it to any other value.  Here is an example
1157 of the right way to use the variable:
1159 @example
1160 ;; @r{Read the file with no character code conversion.}
1161 ;; @r{Assume @acronym{crlf} represents end-of-line.}
1162 (let ((coding-system-for-read 'emacs-mule-dos))
1163   (insert-file-contents filename))
1164 @end example
1166 When its value is non-@code{nil}, @code{coding-system-for-read} takes
1167 precedence over all other methods of specifying a coding system to use for
1168 input, including @code{file-coding-system-alist},
1169 @code{process-coding-system-alist} and
1170 @code{network-coding-system-alist}.
1171 @end defvar
1173 @defvar coding-system-for-write
1174 This works much like @code{coding-system-for-read}, except that it
1175 applies to output rather than input.  It affects writing to files,
1176 as well as sending output to subprocesses and net connections.
1178 When a single operation does both input and output, as do
1179 @code{call-process-region} and @code{start-process}, both
1180 @code{coding-system-for-read} and @code{coding-system-for-write}
1181 affect it.
1182 @end defvar
1184 @defvar inhibit-eol-conversion
1185 When this variable is non-@code{nil}, no end-of-line conversion is done,
1186 no matter which coding system is specified.  This applies to all the
1187 Emacs I/O and subprocess primitives, and to the explicit encoding and
1188 decoding functions (@pxref{Explicit Encoding}).
1189 @end defvar
1191 @node Explicit Encoding
1192 @subsection Explicit Encoding and Decoding
1193 @cindex encoding text
1194 @cindex decoding text
1196   All the operations that transfer text in and out of Emacs have the
1197 ability to use a coding system to encode or decode the text.
1198 You can also explicitly encode and decode text using the functions
1199 in this section.
1201   The result of encoding, and the input to decoding, are not ordinary
1202 text.  They logically consist of a series of byte values; that is, a
1203 series of characters whose codes are in the range 0 through 255.  In a
1204 multibyte buffer or string, character codes 128 through 159 are
1205 represented by multibyte sequences, but this is invisible to Lisp
1206 programs.
1208   The usual way to read a file into a buffer as a sequence of bytes, so
1209 you can decode the contents explicitly, is with
1210 @code{insert-file-contents-literally} (@pxref{Reading from Files});
1211 alternatively, specify a non-@code{nil} @var{rawfile} argument when
1212 visiting a file with @code{find-file-noselect}.  These methods result in
1213 a unibyte buffer.
1215   The usual way to use the byte sequence that results from explicitly
1216 encoding text is to copy it to a file or process---for example, to write
1217 it with @code{write-region} (@pxref{Writing to Files}), and suppress
1218 encoding by binding @code{coding-system-for-write} to
1219 @code{no-conversion}.
1221   Here are the functions to perform explicit encoding or decoding.  The
1222 encoding functions produce sequences of bytes; the decoding functions
1223 are meant to operate on sequences of bytes.  All of these functions
1224 discard text properties.
1226 @deffn Command encode-coding-region start end coding-system
1227 This command encodes the text from @var{start} to @var{end} according
1228 to coding system @var{coding-system}.  The encoded text replaces the
1229 original text in the buffer.  The result of encoding is logically a
1230 sequence of bytes, but the buffer remains multibyte if it was multibyte
1231 before.
1233 This command returns the length of the encoded text.
1234 @end deffn
1236 @defun encode-coding-string string coding-system &optional nocopy
1237 This function encodes the text in @var{string} according to coding
1238 system @var{coding-system}.  It returns a new string containing the
1239 encoded text, except when @var{nocopy} is non-@code{nil}, in which
1240 case the function may return @var{string} itself if the encoding
1241 operation is trivial.  The result of encoding is a unibyte string.
1242 @end defun
1244 @deffn Command decode-coding-region start end coding-system
1245 This command decodes the text from @var{start} to @var{end} according
1246 to coding system @var{coding-system}.  The decoded text replaces the
1247 original text in the buffer.  To make explicit decoding useful, the text
1248 before decoding ought to be a sequence of byte values, but both
1249 multibyte and unibyte buffers are acceptable.
1251 This command returns the length of the decoded text.
1252 @end deffn
1254 @defun decode-coding-string string coding-system &optional nocopy
1255 This function decodes the text in @var{string} according to coding
1256 system @var{coding-system}.  It returns a new string containing the
1257 decoded text, except when @var{nocopy} is non-@code{nil}, in which
1258 case the function may return @var{string} itself if the decoding
1259 operation is trivial.  To make explicit decoding useful, the contents
1260 of @var{string} ought to be a sequence of byte values, but a multibyte
1261 string is acceptable.
1262 @end defun
1264 @defun decode-coding-inserted-region from to filename &optional visit beg end replace
1265 This function decodes the text from @var{from} to @var{to} as if
1266 it were being read from file @var{filename} using @code{insert-file-contents}
1267 using the rest of the arguments provided.
1269 The normal way to use this function is after reading text from a file
1270 without decoding, if you decide you would rather have decoded it.
1271 Instead of deleting the text and reading it again, this time with
1272 decoding, you can call this function.
1273 @end defun
1275 @node Terminal I/O Encoding
1276 @subsection Terminal I/O Encoding
1278   Emacs can decode keyboard input using a coding system, and encode
1279 terminal output.  This is useful for terminals that transmit or display
1280 text using a particular encoding such as Latin-1.  Emacs does not set
1281 @code{last-coding-system-used} for encoding or decoding for the
1282 terminal.
1284 @defun keyboard-coding-system
1285 This function returns the coding system that is in use for decoding
1286 keyboard input---or @code{nil} if no coding system is to be used.
1287 @end defun
1289 @deffn Command set-keyboard-coding-system coding-system
1290 This command specifies @var{coding-system} as the coding system to
1291 use for decoding keyboard input.  If @var{coding-system} is @code{nil},
1292 that means do not decode keyboard input.
1293 @end deffn
1295 @defun terminal-coding-system
1296 This function returns the coding system that is in use for encoding
1297 terminal output---or @code{nil} for no encoding.
1298 @end defun
1300 @deffn Command set-terminal-coding-system coding-system
1301 This command specifies @var{coding-system} as the coding system to use
1302 for encoding terminal output.  If @var{coding-system} is @code{nil},
1303 that means do not encode terminal output.
1304 @end deffn
1306 @node MS-DOS File Types
1307 @subsection MS-DOS File Types
1308 @cindex DOS file types
1309 @cindex MS-DOS file types
1310 @cindex Windows file types
1311 @cindex file types on MS-DOS and Windows
1312 @cindex text files and binary files
1313 @cindex binary files and text files
1315   On MS-DOS and Microsoft Windows, Emacs guesses the appropriate
1316 end-of-line conversion for a file by looking at the file's name.  This
1317 feature classifies files as @dfn{text files} and @dfn{binary files}.  By
1318 ``binary file'' we mean a file of literal byte values that are not
1319 necessarily meant to be characters; Emacs does no end-of-line conversion
1320 and no character code conversion for them.  On the other hand, the bytes
1321 in a text file are intended to represent characters; when you create a
1322 new file whose name implies that it is a text file, Emacs uses DOS
1323 end-of-line conversion.
1325 @defvar buffer-file-type
1326 This variable, automatically buffer-local in each buffer, records the
1327 file type of the buffer's visited file.  When a buffer does not specify
1328 a coding system with @code{buffer-file-coding-system}, this variable is
1329 used to determine which coding system to use when writing the contents
1330 of the buffer.  It should be @code{nil} for text, @code{t} for binary.
1331 If it is @code{t}, the coding system is @code{no-conversion}.
1332 Otherwise, @code{undecided-dos} is used.
1334 Normally this variable is set by visiting a file; it is set to
1335 @code{nil} if the file was visited without any actual conversion.
1336 @end defvar
1338 @defopt file-name-buffer-file-type-alist
1339 This variable holds an alist for recognizing text and binary files.
1340 Each element has the form (@var{regexp} . @var{type}), where
1341 @var{regexp} is matched against the file name, and @var{type} may be
1342 @code{nil} for text, @code{t} for binary, or a function to call to
1343 compute which.  If it is a function, then it is called with a single
1344 argument (the file name) and should return @code{t} or @code{nil}.
1346 When running on MS-DOS or MS-Windows, Emacs checks this alist to decide
1347 which coding system to use when reading a file.  For a text file,
1348 @code{undecided-dos} is used.  For a binary file, @code{no-conversion}
1349 is used.
1351 If no element in this alist matches a given file name, then
1352 @code{default-buffer-file-type} says how to treat the file.
1353 @end defopt
1355 @defopt default-buffer-file-type
1356 This variable says how to handle files for which
1357 @code{file-name-buffer-file-type-alist} says nothing about the type.
1359 If this variable is non-@code{nil}, then these files are treated as
1360 binary: the coding system @code{no-conversion} is used.  Otherwise,
1361 nothing special is done for them---the coding system is deduced solely
1362 from the file contents, in the usual Emacs fashion.
1363 @end defopt
1365 @node Input Methods
1366 @section Input Methods
1367 @cindex input methods
1369   @dfn{Input methods} provide convenient ways of entering non-@acronym{ASCII}
1370 characters from the keyboard.  Unlike coding systems, which translate
1371 non-@acronym{ASCII} characters to and from encodings meant to be read by
1372 programs, input methods provide human-friendly commands.  (@xref{Input
1373 Methods,,, emacs, The GNU Emacs Manual}, for information on how users
1374 use input methods to enter text.)  How to define input methods is not
1375 yet documented in this manual, but here we describe how to use them.
1377   Each input method has a name, which is currently a string;
1378 in the future, symbols may also be usable as input method names.
1380 @defvar current-input-method
1381 This variable holds the name of the input method now active in the
1382 current buffer.  (It automatically becomes local in each buffer when set
1383 in any fashion.)  It is @code{nil} if no input method is active in the
1384 buffer now.
1385 @end defvar
1387 @defopt default-input-method
1388 This variable holds the default input method for commands that choose an
1389 input method.  Unlike @code{current-input-method}, this variable is
1390 normally global.
1391 @end defopt
1393 @deffn Command set-input-method input-method
1394 This command activates input method @var{input-method} for the current
1395 buffer.  It also sets @code{default-input-method} to @var{input-method}.
1396 If @var{input-method} is @code{nil}, this command deactivates any input
1397 method for the current buffer.
1398 @end deffn
1400 @defun read-input-method-name prompt &optional default inhibit-null
1401 This function reads an input method name with the minibuffer, prompting
1402 with @var{prompt}.  If @var{default} is non-@code{nil}, that is returned
1403 by default, if the user enters empty input.  However, if
1404 @var{inhibit-null} is non-@code{nil}, empty input signals an error.
1406 The returned value is a string.
1407 @end defun
1409 @defvar input-method-alist
1410 This variable defines all the supported input methods.
1411 Each element defines one input method, and should have the form:
1413 @example
1414 (@var{input-method} @var{language-env} @var{activate-func}
1415  @var{title} @var{description} @var{args}...)
1416 @end example
1418 Here @var{input-method} is the input method name, a string;
1419 @var{language-env} is another string, the name of the language
1420 environment this input method is recommended for.  (That serves only for
1421 documentation purposes.)
1423 @var{activate-func} is a function to call to activate this method.  The
1424 @var{args}, if any, are passed as arguments to @var{activate-func}.  All
1425 told, the arguments to @var{activate-func} are @var{input-method} and
1426 the @var{args}.
1428 @var{title} is a string to display in the mode line while this method is
1429 active.  @var{description} is a string describing this method and what
1430 it is good for.
1431 @end defvar
1433   The fundamental interface to input methods is through the
1434 variable @code{input-method-function}.  @xref{Reading One Event},
1435 and @ref{Invoking the Input Method}.
1437 @node Locales
1438 @section Locales
1439 @cindex locale
1441   POSIX defines a concept of ``locales'' which control which language
1442 to use in language-related features.  These Emacs variables control
1443 how Emacs interacts with these features.
1445 @defvar locale-coding-system
1446 @cindex keyboard input decoding on X
1447 This variable specifies the coding system to use for decoding system
1448 error messages and---on X Window system only---keyboard input, for
1449 encoding the format argument to @code{format-time-string}, and for
1450 decoding the return value of @code{format-time-string}.
1451 @end defvar
1453 @defvar system-messages-locale
1454 This variable specifies the locale to use for generating system error
1455 messages.  Changing the locale can cause messages to come out in a
1456 different language or in a different orthography.  If the variable is
1457 @code{nil}, the locale is specified by environment variables in the
1458 usual POSIX fashion.
1459 @end defvar
1461 @defvar system-time-locale
1462 This variable specifies the locale to use for formatting time values.
1463 Changing the locale can cause messages to appear according to the
1464 conventions of a different language.  If the variable is @code{nil}, the
1465 locale is specified by environment variables in the usual POSIX fashion.
1466 @end defvar
1468 @defun locale-info item
1469 This function returns locale data @var{item} for the current POSIX
1470 locale, if available.  @var{item} should be one of these symbols:
1472 @table @code
1473 @item codeset
1474 Return the character set as a string (locale item @code{CODESET}).
1476 @item days
1477 Return a 7-element vector of day names (locale items
1478 @code{DAY_1} through @code{DAY_7});
1480 @item months
1481 Return a 12-element vector of month names (locale items @code{MON_1}
1482 through @code{MON_12}).
1484 @item paper
1485 Return a list @code{(@var{width} @var{height})} for the default paper
1486 size measured in millimeters (locale items @code{PAPER_WIDTH} and
1487 @code{PAPER_HEIGHT}).
1488 @end table
1490 If the system can't provide the requested information, or if
1491 @var{item} is not one of those symbols, the value is @code{nil}.  All
1492 strings in the return value are decoded using
1493 @code{locale-coding-system}.  @xref{Locales,,, libc, The GNU Libc Manual},
1494 for more information about locales and locale items.
1495 @end defun
1497 @ignore
1498    arch-tag: be705bf8-941b-4c35-84fc-ad7d20ddb7cb
1499 @end ignore