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