(Recognize Coding): Clarify previous change.
[bpt/emacs.git] / man / mule.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004,
3 @c 2005, 2006 Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node International, Major Modes, Frames, Top
6 @chapter International Character Set Support
7 @cindex MULE
8 @cindex international scripts
9 @cindex multibyte characters
10 @cindex encoding of characters
11
12 @cindex Celtic
13 @cindex Chinese
14 @cindex Cyrillic
15 @cindex Czech
16 @cindex Devanagari
17 @cindex Hindi
18 @cindex Marathi
19 @cindex Ethiopic
20 @cindex German
21 @cindex Greek
22 @cindex Hebrew
23 @cindex IPA
24 @cindex Japanese
25 @cindex Korean
26 @cindex Lao
27 @cindex Latin
28 @cindex Polish
29 @cindex Romanian
30 @cindex Slovak
31 @cindex Slovenian
32 @cindex Thai
33 @cindex Tibetan
34 @cindex Turkish
35 @cindex Vietnamese
36 @cindex Dutch
37 @cindex Spanish
38 Emacs supports a wide variety of international character sets,
39 including European and Vietnamese variants of the Latin alphabet, as
40 well as Cyrillic, Devanagari (for Hindi and Marathi), Ethiopic, Greek,
41 Han (for Chinese and Japanese), Hangul (for Korean), Hebrew, IPA,
42 Kannada, Lao, Malayalam, Tamil, Thai, Tibetan, and Vietnamese scripts.
43 Emacs also supports various encodings of these characters used by
44 other internationalized software, such as word processors and mailers.
45
46 Emacs allows editing text with international characters by supporting
47 all the related activities:
48
49 @itemize @bullet
50 @item
51 You can visit files with non-@acronym{ASCII} characters, save non-@acronym{ASCII} text, and
52 pass non-@acronym{ASCII} text between Emacs and programs it invokes (such as
53 compilers, spell-checkers, and mailers). Setting your language
54 environment (@pxref{Language Environments}) takes care of setting up the
55 coding systems and other options for a specific language or culture.
56 Alternatively, you can specify how Emacs should encode or decode text
57 for each command; see @ref{Text Coding}.
58
59 @item
60 You can display non-@acronym{ASCII} characters encoded by the various
61 scripts. This works by using appropriate fonts on graphics displays
62 (@pxref{Defining Fontsets}), and by sending special codes to text-only
63 displays (@pxref{Terminal Coding}). If some characters are displayed
64 incorrectly, refer to @ref{Undisplayable Characters}, which describes
65 possible problems and explains how to solve them.
66
67 @item
68 You can insert non-@acronym{ASCII} characters or search for them. To do that,
69 you can specify an input method (@pxref{Select Input Method}) suitable
70 for your language, or use the default input method set up when you set
71 your language environment. If
72 your keyboard can produce non-@acronym{ASCII} characters, you can select an
73 appropriate keyboard coding system (@pxref{Terminal Coding}), and Emacs
74 will accept those characters. Latin-1 characters can also be input by
75 using the @kbd{C-x 8} prefix, see @ref{Unibyte Mode}.
76
77 On X Window systems, your locale should be set to an appropriate value
78 to make sure Emacs interprets keyboard input correctly; see
79 @ref{Language Environments, locales}.
80 @end itemize
81
82 The rest of this chapter describes these issues in detail.
83
84 @menu
85 * International Chars:: Basic concepts of multibyte characters.
86 * Enabling Multibyte:: Controlling whether to use multibyte characters.
87 * Language Environments:: Setting things up for the language you use.
88 * Input Methods:: Entering text characters not on your keyboard.
89 * Select Input Method:: Specifying your choice of input methods.
90 * Multibyte Conversion:: How single-byte characters convert to multibyte.
91 * Coding Systems:: Character set conversion when you read and
92 write files, and so on.
93 * Recognize Coding:: How Emacs figures out which conversion to use.
94 * Text Coding:: Choosing conversion to use for file text.
95 * Communication Coding:: Coding systems for interprocess communication.
96 * File Name Coding:: Coding systems for file @emph{names}.
97 * Terminal Coding:: Specifying coding systems for converting
98 terminal input and output.
99 * Fontsets:: Fontsets are collections of fonts
100 that cover the whole spectrum of characters.
101 * Defining Fontsets:: Defining a new fontset.
102 * Undisplayable Characters:: When characters don't display.
103 * Unibyte Mode:: You can pick one European character set
104 to use without multibyte characters.
105 * Charsets:: How Emacs groups its internal character codes.
106 @end menu
107
108 @node International Chars
109 @section Introduction to International Character Sets
110
111 The users of international character sets and scripts have
112 established many more-or-less standard coding systems for storing
113 files. Emacs internally uses a single multibyte character encoding,
114 so that it can intermix characters from all these scripts in a single
115 buffer or string. This encoding represents each non-@acronym{ASCII}
116 character as a sequence of bytes in the range 0200 through 0377.
117 Emacs translates between the multibyte character encoding and various
118 other coding systems when reading and writing files, when exchanging
119 data with subprocesses, and (in some cases) in the @kbd{C-q} command
120 (@pxref{Multibyte Conversion}).
121
122 @kindex C-h h
123 @findex view-hello-file
124 @cindex undisplayable characters
125 @cindex @samp{?} in display
126 The command @kbd{C-h h} (@code{view-hello-file}) displays the file
127 @file{etc/HELLO}, which shows how to say ``hello'' in many languages.
128 This illustrates various scripts. If some characters can't be
129 displayed on your terminal, they appear as @samp{?} or as hollow boxes
130 (@pxref{Undisplayable Characters}).
131
132 Keyboards, even in the countries where these character sets are used,
133 generally don't have keys for all the characters in them. So Emacs
134 supports various @dfn{input methods}, typically one for each script or
135 language, to make it convenient to type them.
136
137 @kindex C-x RET
138 The prefix key @kbd{C-x @key{RET}} is used for commands that pertain
139 to multibyte characters, coding systems, and input methods.
140
141 @node Enabling Multibyte
142 @section Enabling Multibyte Characters
143
144 By default, Emacs starts in multibyte mode, because that allows you to
145 use all the supported languages and scripts without limitations.
146
147 @cindex turn multibyte support on or off
148 You can enable or disable multibyte character support, either for
149 Emacs as a whole, or for a single buffer. When multibyte characters
150 are disabled in a buffer, we call that @dfn{unibyte mode}. Then each
151 byte in that buffer represents a character, even codes 0200 through
152 0377.
153
154 The old features for supporting the European character sets, ISO
155 Latin-1 and ISO Latin-2, work in unibyte mode as they did in Emacs 19
156 and also work for the other ISO 8859 character sets. However, there
157 is no need to turn off multibyte character support to use ISO Latin;
158 the Emacs multibyte character set includes all the characters in these
159 character sets, and Emacs can translate automatically to and from the
160 ISO codes.
161
162 To edit a particular file in unibyte representation, visit it using
163 @code{find-file-literally}. @xref{Visiting}. To convert a buffer in
164 multibyte representation into a single-byte representation of the same
165 characters, the easiest way is to save the contents in a file, kill the
166 buffer, and find the file again with @code{find-file-literally}. You
167 can also use @kbd{C-x @key{RET} c}
168 (@code{universal-coding-system-argument}) and specify @samp{raw-text} as
169 the coding system with which to find or save a file. @xref{Text
170 Coding}. Finding a file as @samp{raw-text} doesn't disable format
171 conversion, uncompression and auto mode selection as
172 @code{find-file-literally} does.
173
174 @vindex enable-multibyte-characters
175 @vindex default-enable-multibyte-characters
176 To turn off multibyte character support by default, start Emacs with
177 the @samp{--unibyte} option (@pxref{Initial Options}), or set the
178 environment variable @env{EMACS_UNIBYTE}. You can also customize
179 @code{enable-multibyte-characters} or, equivalently, directly set the
180 variable @code{default-enable-multibyte-characters} to @code{nil} in
181 your init file to have basically the same effect as @samp{--unibyte}.
182
183 @findex toggle-enable-multibyte-characters
184 To convert a unibyte session to a multibyte session, set
185 @code{default-enable-multibyte-characters} to @code{t}. Buffers which
186 were created in the unibyte session before you turn on multibyte support
187 will stay unibyte. You can turn on multibyte support in a specific
188 buffer by invoking the command @code{toggle-enable-multibyte-characters}
189 in that buffer.
190
191 @cindex Lisp files, and multibyte operation
192 @cindex multibyte operation, and Lisp files
193 @cindex unibyte operation, and Lisp files
194 @cindex init file, and non-@acronym{ASCII} characters
195 @cindex environment variables, and non-@acronym{ASCII} characters
196 With @samp{--unibyte}, multibyte strings are not created during
197 initialization from the values of environment variables,
198 @file{/etc/passwd} entries etc.@: that contain non-@acronym{ASCII} 8-bit
199 characters.
200
201 Emacs normally loads Lisp files as multibyte, regardless of whether
202 you used @samp{--unibyte}. This includes the Emacs initialization file,
203 @file{.emacs}, and the initialization files of Emacs packages such as
204 Gnus. However, you can specify unibyte loading for a particular Lisp
205 file, by putting @w{@samp{-*-unibyte: t;-*-}} in a comment on the first
206 line (@pxref{File Variables}). Then that file is always loaded as
207 unibyte text, even if you did not start Emacs with @samp{--unibyte}.
208 The motivation for these conventions is that it is more reliable to
209 always load any particular Lisp file in the same way. However, you can
210 load a Lisp file as unibyte, on any one occasion, by typing @kbd{C-x
211 @key{RET} c raw-text @key{RET}} immediately before loading it.
212
213 The mode line indicates whether multibyte character support is enabled
214 in the current buffer. If it is, there are two or more characters (most
215 often two dashes) before the colon near the beginning of the mode line.
216 When multibyte characters are not enabled, nothing precedes the colon
217 except a single dash.
218
219 @node Language Environments
220 @section Language Environments
221 @cindex language environments
222
223 All supported character sets are supported in Emacs buffers whenever
224 multibyte characters are enabled; there is no need to select a
225 particular language in order to display its characters in an Emacs
226 buffer. However, it is important to select a @dfn{language environment}
227 in order to set various defaults. The language environment really
228 represents a choice of preferred script (more or less) rather than a
229 choice of language.
230
231 The language environment controls which coding systems to recognize
232 when reading text (@pxref{Recognize Coding}). This applies to files,
233 incoming mail, netnews, and any other text you read into Emacs. It may
234 also specify the default coding system to use when you create a file.
235 Each language environment also specifies a default input method.
236
237 @findex set-language-environment
238 @vindex current-language-environment
239 To select a language environment, you can customize the variable
240 @code{current-language-environment} or use the command @kbd{M-x
241 set-language-environment}. It makes no difference which buffer is
242 current when you use this command, because the effects apply globally to
243 the Emacs session. The supported language environments include:
244
245 @cindex Euro sign
246 @cindex UTF-8
247 @quotation
248 Belarusian, Brazilian Portuguese, Bulgarian, Chinese-BIG5,
249 Chinese-CNS, Chinese-EUC-TW, Chinese-GB, Croatian, Cyrillic-ALT,
250 Cyrillic-ISO, Cyrillic-KOI8, Czech, Devanagari, Dutch, English,
251 Ethiopic, French, Georgian, German, Greek, Hebrew, IPA, Italian,
252 Japanese, Kannada, Korean, Lao, Latin-1, Latin-2, Latin-3,
253 Latin-4, Latin-5, Latin-6, Latin-7, Latin-8 (Celtic),
254 Latin-9 (updated Latin-1 with the Euro sign), Latvian,
255 Lithuanian, Malayalam, Polish, Romanian, Russian, Slovak,
256 Slovenian, Spanish, Swedish, Tajik, Tamil, Thai, Tibetan,
257 Turkish, UTF-8 (for a setup which prefers Unicode characters and
258 files encoded in UTF-8), Ukrainian, Vietnamese, Welsh, and
259 Windows-1255 (for a setup which prefers Cyrillic characters and
260 files encoded in Windows-1255).
261 @end quotation
262
263 @cindex fonts for various scripts
264 @cindex Intlfonts package, installation
265 To display the script(s) used by your language environment on a
266 graphical display, you need to have a suitable font. If some of the
267 characters appear as empty boxes, you should install the GNU Intlfonts
268 package, which includes fonts for most supported scripts.@footnote{If
269 you run Emacs on X, you need to inform the X server about the location
270 of the newly installed fonts with the following commands:
271
272 @example
273 xset fp+ /usr/local/share/emacs/fonts
274 xset fp rehash
275 @end example
276 }
277 @xref{Fontsets}, for more details about setting up your fonts.
278
279 @findex set-locale-environment
280 @vindex locale-language-names
281 @vindex locale-charset-language-names
282 @cindex locales
283 Some operating systems let you specify the character-set locale you
284 are using by setting the locale environment variables @env{LC_ALL},
285 @env{LC_CTYPE}, or @env{LANG}.@footnote{If more than one of these is
286 set, the first one that is nonempty specifies your locale for this
287 purpose.} During startup, Emacs looks up your character-set locale's
288 name in the system locale alias table, matches its canonical name
289 against entries in the value of the variables
290 @code{locale-charset-language-names} and @code{locale-language-names},
291 and selects the corresponding language environment if a match is found.
292 (The former variable overrides the latter.) It also adjusts the display
293 table and terminal coding system, the locale coding system, the
294 preferred coding system as needed for the locale, and---last but not
295 least---the way Emacs decodes non-@acronym{ASCII} characters sent by your keyboard.
296
297 If you modify the @env{LC_ALL}, @env{LC_CTYPE}, or @env{LANG}
298 environment variables while running Emacs, you may want to invoke the
299 @code{set-locale-environment} function afterwards to readjust the
300 language environment from the new locale.
301
302 @vindex locale-preferred-coding-systems
303 The @code{set-locale-environment} function normally uses the preferred
304 coding system established by the language environment to decode system
305 messages. But if your locale matches an entry in the variable
306 @code{locale-preferred-coding-systems}, Emacs uses the corresponding
307 coding system instead. For example, if the locale @samp{ja_JP.PCK}
308 matches @code{japanese-shift-jis} in
309 @code{locale-preferred-coding-systems}, Emacs uses that encoding even
310 though it might normally use @code{japanese-iso-8bit}.
311
312 You can override the language environment chosen at startup with
313 explicit use of the command @code{set-language-environment}, or with
314 customization of @code{current-language-environment} in your init
315 file.
316
317 @kindex C-h L
318 @findex describe-language-environment
319 To display information about the effects of a certain language
320 environment @var{lang-env}, use the command @kbd{C-h L @var{lang-env}
321 @key{RET}} (@code{describe-language-environment}). This tells you
322 which languages this language environment is useful for, and lists the
323 character sets, coding systems, and input methods that go with it. It
324 also shows some sample text to illustrate scripts used in this
325 language environment. If you give an empty input for @var{lang-env},
326 this command describes the chosen language environment.
327
328 @vindex set-language-environment-hook
329 You can customize any language environment with the normal hook
330 @code{set-language-environment-hook}. The command
331 @code{set-language-environment} runs that hook after setting up the new
332 language environment. The hook functions can test for a specific
333 language environment by checking the variable
334 @code{current-language-environment}. This hook is where you should
335 put non-default settings for specific language environment, such as
336 coding systems for keyboard input and terminal output, the default
337 input method, etc.
338
339 @vindex exit-language-environment-hook
340 Before it starts to set up the new language environment,
341 @code{set-language-environment} first runs the hook
342 @code{exit-language-environment-hook}. This hook is useful for undoing
343 customizations that were made with @code{set-language-environment-hook}.
344 For instance, if you set up a special key binding in a specific language
345 environment using @code{set-language-environment-hook}, you should set
346 up @code{exit-language-environment-hook} to restore the normal binding
347 for that key.
348
349 @node Input Methods
350 @section Input Methods
351
352 @cindex input methods
353 An @dfn{input method} is a kind of character conversion designed
354 specifically for interactive input. In Emacs, typically each language
355 has its own input method; sometimes several languages which use the same
356 characters can share one input method. A few languages support several
357 input methods.
358
359 The simplest kind of input method works by mapping @acronym{ASCII} letters
360 into another alphabet; this allows you to use one other alphabet
361 instead of @acronym{ASCII}. The Greek and Russian input methods
362 work this way.
363
364 A more powerful technique is composition: converting sequences of
365 characters into one letter. Many European input methods use composition
366 to produce a single non-@acronym{ASCII} letter from a sequence that consists of a
367 letter followed by accent characters (or vice versa). For example, some
368 methods convert the sequence @kbd{a'} into a single accented letter.
369 These input methods have no special commands of their own; all they do
370 is compose sequences of printing characters.
371
372 The input methods for syllabic scripts typically use mapping followed
373 by composition. The input methods for Thai and Korean work this way.
374 First, letters are mapped into symbols for particular sounds or tone
375 marks; then, sequences of these which make up a whole syllable are
376 mapped into one syllable sign.
377
378 Chinese and Japanese require more complex methods. In Chinese input
379 methods, first you enter the phonetic spelling of a Chinese word (in
380 input method @code{chinese-py}, among others), or a sequence of
381 portions of the character (input methods @code{chinese-4corner} and
382 @code{chinese-sw}, and others). One input sequence typically
383 corresponds to many possible Chinese characters. You select the one
384 you mean using keys such as @kbd{C-f}, @kbd{C-b}, @kbd{C-n},
385 @kbd{C-p}, and digits, which have special meanings in this situation.
386
387 The possible characters are conceptually arranged in several rows,
388 with each row holding up to 10 alternatives. Normally, Emacs displays
389 just one row at a time, in the echo area; @code{(@var{i}/@var{j})}
390 appears at the beginning, to indicate that this is the @var{i}th row
391 out of a total of @var{j} rows. Type @kbd{C-n} or @kbd{C-p} to
392 display the next row or the previous row.
393
394 Type @kbd{C-f} and @kbd{C-b} to move forward and backward among
395 the alternatives in the current row. As you do this, Emacs highlights
396 the current alternative with a special color; type @code{C-@key{SPC}}
397 to select the current alternative and use it as input. The
398 alternatives in the row are also numbered; the number appears before
399 the alternative. Typing a digit @var{n} selects the @var{n}th
400 alternative of the current row and uses it as input.
401
402 @key{TAB} in these Chinese input methods displays a buffer showing
403 all the possible characters at once; then clicking @kbd{Mouse-2} on
404 one of them selects that alternative. The keys @kbd{C-f}, @kbd{C-b},
405 @kbd{C-n}, @kbd{C-p}, and digits continue to work as usual, but they
406 do the highlighting in the buffer showing the possible characters,
407 rather than in the echo area.
408
409 In Japanese input methods, first you input a whole word using
410 phonetic spelling; then, after the word is in the buffer, Emacs
411 converts it into one or more characters using a large dictionary. One
412 phonetic spelling corresponds to a number of different Japanese words;
413 to select one of them, use @kbd{C-n} and @kbd{C-p} to cycle through
414 the alternatives.
415
416 Sometimes it is useful to cut off input method processing so that the
417 characters you have just entered will not combine with subsequent
418 characters. For example, in input method @code{latin-1-postfix}, the
419 sequence @kbd{e '} combines to form an @samp{e} with an accent. What if
420 you want to enter them as separate characters?
421
422 One way is to type the accent twice; this is a special feature for
423 entering the separate letter and accent. For example, @kbd{e ' '} gives
424 you the two characters @samp{e'}. Another way is to type another letter
425 after the @kbd{e}---something that won't combine with that---and
426 immediately delete it. For example, you could type @kbd{e e @key{DEL}
427 '} to get separate @samp{e} and @samp{'}.
428
429 Another method, more general but not quite as easy to type, is to use
430 @kbd{C-\ C-\} between two characters to stop them from combining. This
431 is the command @kbd{C-\} (@code{toggle-input-method}) used twice.
432 @ifnottex
433 @xref{Select Input Method}.
434 @end ifnottex
435
436 @cindex incremental search, input method interference
437 @kbd{C-\ C-\} is especially useful inside an incremental search,
438 because it stops waiting for more characters to combine, and starts
439 searching for what you have already entered.
440
441 To find out how to input the character after point using the current
442 input method, type @kbd{C-u C-x =}. @xref{Position Info}.
443
444 @vindex input-method-verbose-flag
445 @vindex input-method-highlight-flag
446 The variables @code{input-method-highlight-flag} and
447 @code{input-method-verbose-flag} control how input methods explain
448 what is happening. If @code{input-method-highlight-flag} is
449 non-@code{nil}, the partial sequence is highlighted in the buffer (for
450 most input methods---some disable this feature). If
451 @code{input-method-verbose-flag} is non-@code{nil}, the list of
452 possible characters to type next is displayed in the echo area (but
453 not when you are in the minibuffer).
454
455 @node Select Input Method
456 @section Selecting an Input Method
457
458 @table @kbd
459 @item C-\
460 Enable or disable use of the selected input method.
461
462 @item C-x @key{RET} C-\ @var{method} @key{RET}
463 Select a new input method for the current buffer.
464
465 @item C-h I @var{method} @key{RET}
466 @itemx C-h C-\ @var{method} @key{RET}
467 @findex describe-input-method
468 @kindex C-h I
469 @kindex C-h C-\
470 Describe the input method @var{method} (@code{describe-input-method}).
471 By default, it describes the current input method (if any). This
472 description should give you the full details of how to use any
473 particular input method.
474
475 @item M-x list-input-methods
476 Display a list of all the supported input methods.
477 @end table
478
479 @findex set-input-method
480 @vindex current-input-method
481 @kindex C-x RET C-\
482 To choose an input method for the current buffer, use @kbd{C-x
483 @key{RET} C-\} (@code{set-input-method}). This command reads the
484 input method name from the minibuffer; the name normally starts with the
485 language environment that it is meant to be used with. The variable
486 @code{current-input-method} records which input method is selected.
487
488 @findex toggle-input-method
489 @kindex C-\
490 Input methods use various sequences of @acronym{ASCII} characters to
491 stand for non-@acronym{ASCII} characters. Sometimes it is useful to
492 turn off the input method temporarily. To do this, type @kbd{C-\}
493 (@code{toggle-input-method}). To reenable the input method, type
494 @kbd{C-\} again.
495
496 If you type @kbd{C-\} and you have not yet selected an input method,
497 it prompts for you to specify one. This has the same effect as using
498 @kbd{C-x @key{RET} C-\} to specify an input method.
499
500 When invoked with a numeric argument, as in @kbd{C-u C-\},
501 @code{toggle-input-method} always prompts you for an input method,
502 suggesting the most recently selected one as the default.
503
504 @vindex default-input-method
505 Selecting a language environment specifies a default input method for
506 use in various buffers. When you have a default input method, you can
507 select it in the current buffer by typing @kbd{C-\}. The variable
508 @code{default-input-method} specifies the default input method
509 (@code{nil} means there is none).
510
511 In some language environments, which support several different input
512 methods, you might want to use an input method different from the
513 default chosen by @code{set-language-environment}. You can instruct
514 Emacs to select a different default input method for a certain
515 language environment, if you wish, by using
516 @code{set-language-environment-hook} (@pxref{Language Environments,
517 set-language-environment-hook}). For example:
518
519 @lisp
520 (defun my-chinese-setup ()
521 "Set up my private Chinese environment."
522 (if (equal current-language-environment "Chinese-GB")
523 (setq default-input-method "chinese-tonepy")))
524 (add-hook 'set-language-environment-hook 'my-chinese-setup)
525 @end lisp
526
527 @noindent
528 This sets the default input method to be @code{chinese-tonepy}
529 whenever you choose a Chinese-GB language environment.
530
531 @findex quail-set-keyboard-layout
532 Some input methods for alphabetic scripts work by (in effect)
533 remapping the keyboard to emulate various keyboard layouts commonly used
534 for those scripts. How to do this remapping properly depends on your
535 actual keyboard layout. To specify which layout your keyboard has, use
536 the command @kbd{M-x quail-set-keyboard-layout}.
537
538 @findex quail-show-key
539 You can use the command @kbd{M-x quail-show-key} to show what key (or
540 key sequence) to type in order to input the character following point,
541 using the selected keyboard layout. The command @kbd{C-u C-x =} also
542 shows that information in addition to the other information about the
543 character.
544
545 @findex list-input-methods
546 To display a list of all the supported input methods, type @kbd{M-x
547 list-input-methods}. The list gives information about each input
548 method, including the string that stands for it in the mode line.
549
550 @node Multibyte Conversion
551 @section Unibyte and Multibyte Non-@acronym{ASCII} characters
552
553 When multibyte characters are enabled, character codes 0240 (octal)
554 through 0377 (octal) are not really legitimate in the buffer. The valid
555 non-@acronym{ASCII} printing characters have codes that start from 0400.
556
557 If you type a self-inserting character in the range 0240 through
558 0377, or if you use @kbd{C-q} to insert one, Emacs assumes you
559 intended to use one of the ISO Latin-@var{n} character sets, and
560 converts it to the Emacs code representing that Latin-@var{n}
561 character. You select @emph{which} ISO Latin character set to use
562 through your choice of language environment
563 @iftex
564 (see above).
565 @end iftex
566 @ifnottex
567 (@pxref{Language Environments}).
568 @end ifnottex
569 If you do not specify a choice, the default is Latin-1.
570
571 If you insert a character in the range 0200 through 0237, which
572 forms the @code{eight-bit-control} character set, it is inserted
573 literally. You should normally avoid doing this since buffers
574 containing such characters have to be written out in either the
575 @code{emacs-mule} or @code{raw-text} coding system, which is usually
576 not what you want.
577
578 @node Coding Systems
579 @section Coding Systems
580 @cindex coding systems
581
582 Users of various languages have established many more-or-less standard
583 coding systems for representing them. Emacs does not use these coding
584 systems internally; instead, it converts from various coding systems to
585 its own system when reading data, and converts the internal coding
586 system to other coding systems when writing data. Conversion is
587 possible in reading or writing files, in sending or receiving from the
588 terminal, and in exchanging data with subprocesses.
589
590 Emacs assigns a name to each coding system. Most coding systems are
591 used for one language, and the name of the coding system starts with the
592 language name. Some coding systems are used for several languages;
593 their names usually start with @samp{iso}. There are also special
594 coding systems @code{no-conversion}, @code{raw-text} and
595 @code{emacs-mule} which do not convert printing characters at all.
596
597 @cindex international files from DOS/Windows systems
598 A special class of coding systems, collectively known as
599 @dfn{codepages}, is designed to support text encoded by MS-Windows and
600 MS-DOS software. The names of these coding systems are
601 @code{cp@var{nnnn}}, where @var{nnnn} is a 3- or 4-digit number of the
602 codepage. You can use these encodings just like any other coding
603 system; for example, to visit a file encoded in codepage 850, type
604 @kbd{C-x @key{RET} c cp850 @key{RET} C-x C-f @var{filename}
605 @key{RET}}@footnote{
606 In the MS-DOS port of Emacs, you need to create a @code{cp@var{nnn}}
607 coding system with @kbd{M-x codepage-setup}, before you can use it.
608 @iftex
609 @xref{MS-DOS and MULE,,,emacs-extra,Specialized Emacs Features}.
610 @end iftex
611 @ifnottex
612 @xref{MS-DOS and MULE}.
613 @end ifnottex
614 }.
615
616 In addition to converting various representations of non-@acronym{ASCII}
617 characters, a coding system can perform end-of-line conversion. Emacs
618 handles three different conventions for how to separate lines in a file:
619 newline, carriage-return linefeed, and just carriage-return.
620
621 @table @kbd
622 @item C-h C @var{coding} @key{RET}
623 Describe coding system @var{coding}.
624
625 @item C-h C @key{RET}
626 Describe the coding systems currently in use.
627
628 @item M-x list-coding-systems
629 Display a list of all the supported coding systems.
630 @end table
631
632 @kindex C-h C
633 @findex describe-coding-system
634 The command @kbd{C-h C} (@code{describe-coding-system}) displays
635 information about particular coding systems, including the end-of-line
636 conversion specified by those coding systems. You can specify a coding
637 system name as the argument; alternatively, with an empty argument, it
638 describes the coding systems currently selected for various purposes,
639 both in the current buffer and as the defaults, and the priority list
640 for recognizing coding systems (@pxref{Recognize Coding}).
641
642 @findex list-coding-systems
643 To display a list of all the supported coding systems, type @kbd{M-x
644 list-coding-systems}. The list gives information about each coding
645 system, including the letter that stands for it in the mode line
646 (@pxref{Mode Line}).
647
648 @cindex end-of-line conversion
649 @cindex line endings
650 @cindex MS-DOS end-of-line conversion
651 @cindex Macintosh end-of-line conversion
652 Each of the coding systems that appear in this list---except for
653 @code{no-conversion}, which means no conversion of any kind---specifies
654 how and whether to convert printing characters, but leaves the choice of
655 end-of-line conversion to be decided based on the contents of each file.
656 For example, if the file appears to use the sequence carriage-return
657 linefeed to separate lines, DOS end-of-line conversion will be used.
658
659 Each of the listed coding systems has three variants which specify
660 exactly what to do for end-of-line conversion:
661
662 @table @code
663 @item @dots{}-unix
664 Don't do any end-of-line conversion; assume the file uses
665 newline to separate lines. (This is the convention normally used
666 on Unix and GNU systems.)
667
668 @item @dots{}-dos
669 Assume the file uses carriage-return linefeed to separate lines, and do
670 the appropriate conversion. (This is the convention normally used on
671 Microsoft systems.@footnote{It is also specified for MIME @samp{text/*}
672 bodies and in other network transport contexts. It is different
673 from the SGML reference syntax record-start/record-end format which
674 Emacs doesn't support directly.})
675
676 @item @dots{}-mac
677 Assume the file uses carriage-return to separate lines, and do the
678 appropriate conversion. (This is the convention normally used on the
679 Macintosh system.)
680 @end table
681
682 These variant coding systems are omitted from the
683 @code{list-coding-systems} display for brevity, since they are entirely
684 predictable. For example, the coding system @code{iso-latin-1} has
685 variants @code{iso-latin-1-unix}, @code{iso-latin-1-dos} and
686 @code{iso-latin-1-mac}.
687
688 @cindex @code{undecided}, coding system
689 The coding systems @code{unix}, @code{dos}, and @code{mac} are
690 aliases for @code{undecided-unix}, @code{undecided-dos}, and
691 @code{undecided-mac}, respectively. These coding systems specify only
692 the end-of-line conversion, and leave the character code conversion to
693 be deduced from the text itself.
694
695 The coding system @code{raw-text} is good for a file which is mainly
696 @acronym{ASCII} text, but may contain byte values above 127 which are
697 not meant to encode non-@acronym{ASCII} characters. With
698 @code{raw-text}, Emacs copies those byte values unchanged, and sets
699 @code{enable-multibyte-characters} to @code{nil} in the current buffer
700 so that they will be interpreted properly. @code{raw-text} handles
701 end-of-line conversion in the usual way, based on the data
702 encountered, and has the usual three variants to specify the kind of
703 end-of-line conversion to use.
704
705 In contrast, the coding system @code{no-conversion} specifies no
706 character code conversion at all---none for non-@acronym{ASCII} byte values and
707 none for end of line. This is useful for reading or writing binary
708 files, tar files, and other files that must be examined verbatim. It,
709 too, sets @code{enable-multibyte-characters} to @code{nil}.
710
711 The easiest way to edit a file with no conversion of any kind is with
712 the @kbd{M-x find-file-literally} command. This uses
713 @code{no-conversion}, and also suppresses other Emacs features that
714 might convert the file contents before you see them. @xref{Visiting}.
715
716 The coding system @code{emacs-mule} means that the file contains
717 non-@acronym{ASCII} characters stored with the internal Emacs encoding. It
718 handles end-of-line conversion based on the data encountered, and has
719 the usual three variants to specify the kind of end-of-line conversion.
720
721 @node Recognize Coding
722 @section Recognizing Coding Systems
723
724 Emacs tries to recognize which coding system to use for a given text
725 as an integral part of reading that text. (This applies to files
726 being read, output from subprocesses, text from X selections, etc.)
727 Emacs can select the right coding system automatically most of the
728 time---once you have specified your preferences.
729
730 Some coding systems can be recognized or distinguished by which byte
731 sequences appear in the data. However, there are coding systems that
732 cannot be distinguished, not even potentially. For example, there is no
733 way to distinguish between Latin-1 and Latin-2; they use the same byte
734 values with different meanings.
735
736 Emacs handles this situation by means of a priority list of coding
737 systems. Whenever Emacs reads a file, if you do not specify the coding
738 system to use, Emacs checks the data against each coding system,
739 starting with the first in priority and working down the list, until it
740 finds a coding system that fits the data. Then it converts the file
741 contents assuming that they are represented in this coding system.
742
743 The priority list of coding systems depends on the selected language
744 environment (@pxref{Language Environments}). For example, if you use
745 French, you probably want Emacs to prefer Latin-1 to Latin-2; if you use
746 Czech, you probably want Latin-2 to be preferred. This is one of the
747 reasons to specify a language environment.
748
749 @findex prefer-coding-system
750 However, you can alter the coding system priority list in detail
751 with the command @kbd{M-x prefer-coding-system}. This command reads
752 the name of a coding system from the minibuffer, and adds it to the
753 front of the priority list, so that it is preferred to all others. If
754 you use this command several times, each use adds one element to the
755 front of the priority list.
756
757 If you use a coding system that specifies the end-of-line conversion
758 type, such as @code{iso-8859-1-dos}, what this means is that Emacs
759 should attempt to recognize @code{iso-8859-1} with priority, and should
760 use DOS end-of-line conversion when it does recognize @code{iso-8859-1}.
761
762 @vindex file-coding-system-alist
763 Sometimes a file name indicates which coding system to use for the
764 file. The variable @code{file-coding-system-alist} specifies this
765 correspondence. There is a special function
766 @code{modify-coding-system-alist} for adding elements to this list. For
767 example, to read and write all @samp{.txt} files using the coding system
768 @code{china-iso-8bit}, you can execute this Lisp expression:
769
770 @smallexample
771 (modify-coding-system-alist 'file "\\.txt\\'" 'chinese-iso-8bit)
772 @end smallexample
773
774 @noindent
775 The first argument should be @code{file}, the second argument should be
776 a regular expression that determines which files this applies to, and
777 the third argument says which coding system to use for these files.
778
779 @vindex inhibit-eol-conversion
780 @cindex DOS-style end-of-line display
781 Emacs recognizes which kind of end-of-line conversion to use based on
782 the contents of the file: if it sees only carriage-returns, or only
783 carriage-return linefeed sequences, then it chooses the end-of-line
784 conversion accordingly. You can inhibit the automatic use of
785 end-of-line conversion by setting the variable @code{inhibit-eol-conversion}
786 to non-@code{nil}. If you do that, DOS-style files will be displayed
787 with the @samp{^M} characters visible in the buffer; some people
788 prefer this to the more subtle @samp{(DOS)} end-of-line type
789 indication near the left edge of the mode line (@pxref{Mode Line,
790 eol-mnemonic}).
791
792 @vindex inhibit-iso-escape-detection
793 @cindex escape sequences in files
794 By default, the automatic detection of coding system is sensitive to
795 escape sequences. If Emacs sees a sequence of characters that begin
796 with an escape character, and the sequence is valid as an ISO-2022
797 code, that tells Emacs to use one of the ISO-2022 encodings to decode
798 the file.
799
800 However, there may be cases that you want to read escape sequences
801 in a file as is. In such a case, you can set the variable
802 @code{inhibit-iso-escape-detection} to non-@code{nil}. Then the code
803 detection ignores any escape sequences, and never uses an ISO-2022
804 encoding. The result is that all escape sequences become visible in
805 the buffer.
806
807 The default value of @code{inhibit-iso-escape-detection} is
808 @code{nil}. We recommend that you not change it permanently, only for
809 one specific operation. That's because many Emacs Lisp source files
810 in the Emacs distribution contain non-@acronym{ASCII} characters encoded in the
811 coding system @code{iso-2022-7bit}, and they won't be
812 decoded correctly when you visit those files if you suppress the
813 escape sequence detection.
814
815 @vindex coding
816 You can specify the coding system for a particular file using the
817 @w{@samp{-*-@dots{}-*-}} construct at the beginning of a file, or a
818 local variables list at the end (@pxref{File Variables}). You do this
819 by defining a value for the ``variable'' named @code{coding}. Emacs
820 does not really have a variable @code{coding}; instead of setting a
821 variable, this uses the specified coding system for the file. For
822 example, @samp{-*-mode: C; coding: latin-1;-*-} specifies use of the
823 Latin-1 coding system, as well as C mode. When you specify the coding
824 explicitly in the file, that overrides
825 @code{file-coding-system-alist}.
826
827 If you add the character @samp{!} at the end of the coding system
828 name, it disables any character translation while decoding the file.
829 For instance, it effectively cancels the effect of
830 @code{unify-8859-on-decoding-mode}. This is useful when you need to
831 make sure that the character codes in the Emacs buffer will not
832 according to user settings; for instance, for the sake of strings in
833 Emacs Lisp source files.
834
835 @vindex auto-coding-alist
836 @vindex auto-coding-regexp-alist
837 @vindex auto-coding-functions
838 The variables @code{auto-coding-alist},
839 @code{auto-coding-regexp-alist} and @code{auto-coding-functions} are
840 the strongest way to specify the coding system for certain patterns of
841 file names, or for files containing certain patterns; these variables
842 even override @samp{-*-coding:-*-} tags in the file itself. Emacs
843 uses @code{auto-coding-alist} for tar and archive files, to prevent it
844 from being confused by a @samp{-*-coding:-*-} tag in a member of the
845 archive and thinking it applies to the archive file as a whole.
846 Likewise, Emacs uses @code{auto-coding-regexp-alist} to ensure that
847 RMAIL files, whose names in general don't match any particular
848 pattern, are decoded correctly. One of the builtin
849 @code{auto-coding-functions} detects the encoding for XML files.
850
851 If Emacs recognizes the encoding of a file incorrectly, you can
852 reread the file using the correct coding system by typing @kbd{C-x
853 @key{RET} r @var{coding-system} @key{RET}}. To see what coding system
854 Emacs actually used to decode the file, look at the coding system
855 mnemonic letter near the left edge of the mode line (@pxref{Mode
856 Line}), or type @kbd{C-h C @key{RET}}.
857
858 @findex unify-8859-on-decoding-mode
859 The command @code{unify-8859-on-decoding-mode} enables a mode that
860 ``unifies'' the Latin alphabets when decoding text. This works by
861 converting all non-@acronym{ASCII} Latin-@var{n} characters to either
862 Latin-1 or Unicode characters. This way it is easier to use various
863 Latin-@var{n} alphabets together. In a future Emacs version we hope
864 to move towards full Unicode support and complete unification of
865 character sets.
866
867 @vindex buffer-file-coding-system
868 Once Emacs has chosen a coding system for a buffer, it stores that
869 coding system in @code{buffer-file-coding-system} and uses that coding
870 system, by default, for operations that write from this buffer into a
871 file. This includes the commands @code{save-buffer} and
872 @code{write-region}. If you want to write files from this buffer using
873 a different coding system, you can specify a different coding system for
874 the buffer using @code{set-buffer-file-coding-system} (@pxref{Text
875 Coding}).
876
877 You can insert any possible character into any Emacs buffer, but
878 most coding systems can only handle some of the possible characters.
879 This means that it is possible for you to insert characters that
880 cannot be encoded with the coding system that will be used to save the
881 buffer. For example, you could start with an @acronym{ASCII} file and insert a
882 few Latin-1 characters into it, or you could edit a text file in
883 Polish encoded in @code{iso-8859-2} and add some Russian words to it.
884 When you save the buffer, Emacs cannot use the current value of
885 @code{buffer-file-coding-system}, because the characters you added
886 cannot be encoded by that coding system.
887
888 When that happens, Emacs tries the most-preferred coding system (set
889 by @kbd{M-x prefer-coding-system} or @kbd{M-x
890 set-language-environment}), and if that coding system can safely
891 encode all of the characters in the buffer, Emacs uses it, and stores
892 its value in @code{buffer-file-coding-system}. Otherwise, Emacs
893 displays a list of coding systems suitable for encoding the buffer's
894 contents, and asks you to choose one of those coding systems.
895
896 If you insert the unsuitable characters in a mail message, Emacs
897 behaves a bit differently. It additionally checks whether the
898 most-preferred coding system is recommended for use in MIME messages;
899 if not, Emacs tells you that the most-preferred coding system is
900 not recommended and prompts you for another coding system. This is so
901 you won't inadvertently send a message encoded in a way that your
902 recipient's mail software will have difficulty decoding. (If you do
903 want to use the most-preferred coding system, you can still type its
904 name in response to the question.)
905
906 @vindex sendmail-coding-system
907 When you send a message with Mail mode (@pxref{Sending Mail}), Emacs has
908 four different ways to determine the coding system to use for encoding
909 the message text. It tries the buffer's own value of
910 @code{buffer-file-coding-system}, if that is non-@code{nil}. Otherwise,
911 it uses the value of @code{sendmail-coding-system}, if that is
912 non-@code{nil}. The third way is to use the default coding system for
913 new files, which is controlled by your choice of language environment,
914 if that is non-@code{nil}. If all of these three values are @code{nil},
915 Emacs encodes outgoing mail using the Latin-1 coding system.
916
917 @vindex rmail-decode-mime-charset
918 When you get new mail in Rmail, each message is translated
919 automatically from the coding system it is written in, as if it were a
920 separate file. This uses the priority list of coding systems that you
921 have specified. If a MIME message specifies a character set, Rmail
922 obeys that specification, unless @code{rmail-decode-mime-charset} is
923 @code{nil}.
924
925 @vindex rmail-file-coding-system
926 For reading and saving Rmail files themselves, Emacs uses the coding
927 system specified by the variable @code{rmail-file-coding-system}. The
928 default value is @code{nil}, which means that Rmail files are not
929 translated (they are read and written in the Emacs internal character
930 code).
931
932 @node Text Coding
933 @section Specifying a Coding System for File Text
934
935 In cases where Emacs does not automatically choose the right coding
936 system for a file's contents, you can use these commands to specify
937 one:
938
939 @table @kbd
940 @item C-x @key{RET} f @var{coding} @key{RET}
941 Use coding system @var{coding} for saving or revisiting the visited
942 file in the current buffer.
943
944 @item C-x @key{RET} c @var{coding} @key{RET}
945 Specify coding system @var{coding} for the immediately following
946 command.
947
948 @item C-x @key{RET} r @var{coding} @key{RET}
949 Revisit the current file using the coding system @var{coding}.
950
951 @item M-x recode-region @key{RET} @var{right} @key{RET} @var{wrong} @key{RET}
952 Convert a region that was decoded using coding system @var{wrong},
953 decoding it using coding system @var{right} instead.
954 @end table
955
956 @kindex C-x RET f
957 @findex set-buffer-file-coding-system
958 The command @kbd{C-x @key{RET} f}
959 (@code{set-buffer-file-coding-system}) sets the file coding system for
960 the current buffer---in other words, it says which coding system to
961 use when saving or reverting the visited file. You specify which
962 coding system using the minibuffer. If you specify a coding system
963 that cannot handle all of the characters in the buffer, Emacs warns
964 you about the troublesome characters when you actually save the
965 buffer.
966
967 @cindex specify end-of-line conversion
968 You can also use this command to specify the end-of-line conversion
969 (@pxref{Coding Systems, end-of-line conversion}) for encoding the
970 current buffer. For example, @kbd{C-x @key{RET} f dos @key{RET}} will
971 cause Emacs to save the current buffer's text with DOS-style CRLF line
972 endings.
973
974 @kindex C-x RET c
975 @findex universal-coding-system-argument
976 Another way to specify the coding system for a file is when you visit
977 the file. First use the command @kbd{C-x @key{RET} c}
978 (@code{universal-coding-system-argument}); this command uses the
979 minibuffer to read a coding system name. After you exit the minibuffer,
980 the specified coding system is used for @emph{the immediately following
981 command}.
982
983 So if the immediately following command is @kbd{C-x C-f}, for example,
984 it reads the file using that coding system (and records the coding
985 system for when you later save the file). Or if the immediately following
986 command is @kbd{C-x C-w}, it writes the file using that coding system.
987 When you specify the coding system for saving in this way, instead
988 of with @kbd{C-x @key{RET} f}, there is no warning if the buffer
989 contains characters that the coding system cannot handle.
990
991 Other file commands affected by a specified coding system include
992 @kbd{C-x C-i} and @kbd{C-x C-v}, as well as the other-window variants
993 of @kbd{C-x C-f}. @kbd{C-x @key{RET} c} also affects commands that
994 start subprocesses, including @kbd{M-x shell} (@pxref{Shell}). If the
995 immediately following command does not use the coding system, then
996 @kbd{C-x @key{RET} c} ultimately has no effect.
997
998 An easy way to visit a file with no conversion is with the @kbd{M-x
999 find-file-literally} command. @xref{Visiting}.
1000
1001 @vindex default-buffer-file-coding-system
1002 The variable @code{default-buffer-file-coding-system} specifies the
1003 choice of coding system to use when you create a new file. It applies
1004 when you find a new file, and when you create a buffer and then save it
1005 in a file. Selecting a language environment typically sets this
1006 variable to a good choice of default coding system for that language
1007 environment.
1008
1009 @kindex C-x RET r
1010 @findex revert-buffer-with-coding-system
1011 If you visit a file with a wrong coding system, you can correct this
1012 with @kbd{C-x @key{RET} r} (@code{revert-buffer-with-coding-system}).
1013 This visits the current file again, using a coding system you specify.
1014
1015 @findex recode-region
1016 If a piece of text has already been inserted into a buffer using the
1017 wrong coding system, you can redo the decoding of it using @kbd{M-x
1018 recode-region}. This prompts you for the proper coding system, then
1019 for the wrong coding system that was actually used, and does the
1020 conversion. It first encodes the region using the wrong coding system,
1021 then decodes it again using the proper coding system.
1022
1023 @node Communication Coding
1024 @section Coding Systems for Interprocess Communication
1025
1026 This section explains how to specify coding systems for use
1027 in communication with other processes.
1028
1029 @table @kbd
1030 @item C-x @key{RET} x @var{coding} @key{RET}
1031 Use coding system @var{coding} for transferring selections to and from
1032 other window-based applications.
1033
1034 @item C-x @key{RET} X @var{coding} @key{RET}
1035 Use coding system @var{coding} for transferring @emph{one}
1036 selection---the next one---to or from another window-based application.
1037
1038 @item C-x @key{RET} p @var{input-coding} @key{RET} @var{output-coding} @key{RET}
1039 Use coding systems @var{input-coding} and @var{output-coding} for
1040 subprocess input and output in the current buffer.
1041
1042 @item C-x @key{RET} c @var{coding} @key{RET}
1043 Specify coding system @var{coding} for the immediately following
1044 command.
1045 @end table
1046
1047 @kindex C-x RET x
1048 @kindex C-x RET X
1049 @findex set-selection-coding-system
1050 @findex set-next-selection-coding-system
1051 The command @kbd{C-x @key{RET} x} (@code{set-selection-coding-system})
1052 specifies the coding system for sending selected text to other windowing
1053 applications, and for receiving the text of selections made in other
1054 applications. This command applies to all subsequent selections, until
1055 you override it by using the command again. The command @kbd{C-x
1056 @key{RET} X} (@code{set-next-selection-coding-system}) specifies the
1057 coding system for the next selection made in Emacs or read by Emacs.
1058
1059 @kindex C-x RET p
1060 @findex set-buffer-process-coding-system
1061 The command @kbd{C-x @key{RET} p} (@code{set-buffer-process-coding-system})
1062 specifies the coding system for input and output to a subprocess. This
1063 command applies to the current buffer; normally, each subprocess has its
1064 own buffer, and thus you can use this command to specify translation to
1065 and from a particular subprocess by giving the command in the
1066 corresponding buffer.
1067
1068 You can also use @kbd{C-x @key{RET} c} just before the command that
1069 runs or starts a subprocess, to specify the coding system to use for
1070 communication with that subprocess.
1071
1072 The default for translation of process input and output depends on the
1073 current language environment.
1074
1075 @vindex locale-coding-system
1076 @cindex decoding non-@acronym{ASCII} keyboard input on X
1077 The variable @code{locale-coding-system} specifies a coding system
1078 to use when encoding and decoding system strings such as system error
1079 messages and @code{format-time-string} formats and time stamps. That
1080 coding system is also used for decoding non-@acronym{ASCII} keyboard input on X
1081 Window systems. You should choose a coding system that is compatible
1082 with the underlying system's text representation, which is normally
1083 specified by one of the environment variables @env{LC_ALL},
1084 @env{LC_CTYPE}, and @env{LANG}. (The first one, in the order
1085 specified above, whose value is nonempty is the one that determines
1086 the text representation.)
1087
1088 @node File Name Coding
1089 @section Coding Systems for File Names
1090
1091 @table @kbd
1092 @item C-x @key{RET} F @var{coding} @key{RET}
1093 Use coding system @var{coding} for encoding and decoding file
1094 @emph{names}.
1095 @end table
1096
1097 @vindex file-name-coding-system
1098 @cindex file names with non-@acronym{ASCII} characters
1099 The variable @code{file-name-coding-system} specifies a coding
1100 system to use for encoding file names. It has no effect on reading
1101 and writing the @emph{contents} of files.
1102
1103 @findex set-file-name-coding-system
1104 @kindex C-x @key{RET} F
1105 If you set the variable to a coding system name (as a Lisp symbol or
1106 a string), Emacs encodes file names using that coding system for all
1107 file operations. This makes it possible to use non-@acronym{ASCII}
1108 characters in file names---or, at least, those non-@acronym{ASCII}
1109 characters which the specified coding system can encode. Use @kbd{C-x
1110 @key{RET} F} (@code{set-file-name-coding-system}) to specify this
1111 interactively.
1112
1113 If @code{file-name-coding-system} is @code{nil}, Emacs uses a
1114 default coding system determined by the selected language environment.
1115 In the default language environment, any non-@acronym{ASCII}
1116 characters in file names are not encoded specially; they appear in the
1117 file system using the internal Emacs representation.
1118
1119 @strong{Warning:} if you change @code{file-name-coding-system} (or the
1120 language environment) in the middle of an Emacs session, problems can
1121 result if you have already visited files whose names were encoded using
1122 the earlier coding system and cannot be encoded (or are encoded
1123 differently) under the new coding system. If you try to save one of
1124 these buffers under the visited file name, saving may use the wrong file
1125 name, or it may get an error. If such a problem happens, use @kbd{C-x
1126 C-w} to specify a new file name for that buffer.
1127
1128 @findex recode-file-name
1129 If a mistake occurs when encoding a file name, use the command
1130 @kbd{M-x recode-file-name} to change the file name's coding
1131 system. This prompts for an existing file name, its old coding
1132 system, and the coding system to which you wish to convert.
1133
1134 @node Terminal Coding
1135 @section Coding Systems for Terminal I/O
1136
1137 @table @kbd
1138 @item C-x @key{RET} k @var{coding} @key{RET}
1139 Use coding system @var{coding} for keyboard input.
1140
1141 @item C-x @key{RET} t @var{coding} @key{RET}
1142 Use coding system @var{coding} for terminal output.
1143 @end table
1144
1145 @kindex C-x RET t
1146 @findex set-terminal-coding-system
1147 The command @kbd{C-x @key{RET} t} (@code{set-terminal-coding-system})
1148 specifies the coding system for terminal output. If you specify a
1149 character code for terminal output, all characters output to the
1150 terminal are translated into that coding system.
1151
1152 This feature is useful for certain character-only terminals built to
1153 support specific languages or character sets---for example, European
1154 terminals that support one of the ISO Latin character sets. You need to
1155 specify the terminal coding system when using multibyte text, so that
1156 Emacs knows which characters the terminal can actually handle.
1157
1158 By default, output to the terminal is not translated at all, unless
1159 Emacs can deduce the proper coding system from your terminal type or
1160 your locale specification (@pxref{Language Environments}).
1161
1162 @kindex C-x RET k
1163 @findex set-keyboard-coding-system
1164 @vindex keyboard-coding-system
1165 The command @kbd{C-x @key{RET} k} (@code{set-keyboard-coding-system})
1166 or the variable @code{keyboard-coding-system} specifies the coding
1167 system for keyboard input. Character-code translation of keyboard
1168 input is useful for terminals with keys that send non-@acronym{ASCII}
1169 graphic characters---for example, some terminals designed for ISO
1170 Latin-1 or subsets of it.
1171
1172 By default, keyboard input is translated based on your system locale
1173 setting. If your terminal does not really support the encoding
1174 implied by your locale (for example, if you find it inserts a
1175 non-@acronym{ASCII} character if you type @kbd{M-i}), you will need to set
1176 @code{keyboard-coding-system} to @code{nil} to turn off encoding.
1177 You can do this by putting
1178
1179 @lisp
1180 (set-keyboard-coding-system nil)
1181 @end lisp
1182
1183 @noindent
1184 in your @file{~/.emacs} file.
1185
1186 There is a similarity between using a coding system translation for
1187 keyboard input, and using an input method: both define sequences of
1188 keyboard input that translate into single characters. However, input
1189 methods are designed to be convenient for interactive use by humans, and
1190 the sequences that are translated are typically sequences of @acronym{ASCII}
1191 printing characters. Coding systems typically translate sequences of
1192 non-graphic characters.
1193
1194 @node Fontsets
1195 @section Fontsets
1196 @cindex fontsets
1197
1198 A font typically defines shapes for a single alphabet or script.
1199 Therefore, displaying the entire range of scripts that Emacs supports
1200 requires a collection of many fonts. In Emacs, such a collection is
1201 called a @dfn{fontset}. A fontset is defined by a list of fonts, each
1202 assigned to handle a range of character codes.
1203
1204 Each fontset has a name, like a font. However, while fonts are
1205 stored in the system and the available font names are defined by the
1206 system, fontsets are defined within Emacs itself. Once you have
1207 defined a fontset, you can use it within Emacs by specifying its name,
1208 anywhere that you could use a single font. Of course, Emacs fontsets
1209 can use only the fonts that the system supports; if certain characters
1210 appear on the screen as hollow boxes, this means that the fontset in
1211 use for them has no font for those characters.@footnote{The Emacs
1212 installation instructions have information on additional font
1213 support.}
1214
1215 Emacs creates two fontsets automatically: the @dfn{standard fontset}
1216 and the @dfn{startup fontset}. The standard fontset is most likely to
1217 have fonts for a wide variety of non-@acronym{ASCII} characters;
1218 however, this is not the default for Emacs to use. (By default, Emacs
1219 tries to find a font that has bold and italic variants.) You can
1220 specify use of the standard fontset with the @samp{-fn} option. For
1221 example,
1222
1223 @example
1224 emacs -fn fontset-standard
1225 @end example
1226
1227 @noindent
1228 You can also specify a fontset with the @samp{Font} resource (@pxref{X
1229 Resources}).
1230
1231 A fontset does not necessarily specify a font for every character
1232 code. If a fontset specifies no font for a certain character, or if it
1233 specifies a font that does not exist on your system, then it cannot
1234 display that character properly. It will display that character as an
1235 empty box instead.
1236
1237 @node Defining Fontsets
1238 @section Defining fontsets
1239
1240 @vindex standard-fontset-spec
1241 @cindex standard fontset
1242 Emacs creates a standard fontset automatically according to the value
1243 of @code{standard-fontset-spec}. This fontset's name is
1244
1245 @example
1246 -*-fixed-medium-r-normal-*-16-*-*-*-*-*-fontset-standard
1247 @end example
1248
1249 @noindent
1250 or just @samp{fontset-standard} for short.
1251
1252 Bold, italic, and bold-italic variants of the standard fontset are
1253 created automatically. Their names have @samp{bold} instead of
1254 @samp{medium}, or @samp{i} instead of @samp{r}, or both.
1255
1256 @cindex startup fontset
1257 If you specify a default @acronym{ASCII} font with the @samp{Font} resource or
1258 the @samp{-fn} argument, Emacs generates a fontset from it
1259 automatically. This is the @dfn{startup fontset} and its name is
1260 @code{fontset-startup}. It does this by replacing the @var{foundry},
1261 @var{family}, @var{add_style}, and @var{average_width} fields of the
1262 font name with @samp{*}, replacing @var{charset_registry} field with
1263 @samp{fontset}, and replacing @var{charset_encoding} field with
1264 @samp{startup}, then using the resulting string to specify a fontset.
1265
1266 For instance, if you start Emacs this way,
1267
1268 @example
1269 emacs -fn "*courier-medium-r-normal--14-140-*-iso8859-1"
1270 @end example
1271
1272 @noindent
1273 Emacs generates the following fontset and uses it for the initial X
1274 window frame:
1275
1276 @example
1277 -*-*-medium-r-normal-*-14-140-*-*-*-*-fontset-startup
1278 @end example
1279
1280 With the X resource @samp{Emacs.Font}, you can specify a fontset name
1281 just like an actual font name. But be careful not to specify a fontset
1282 name in a wildcard resource like @samp{Emacs*Font}---that wildcard
1283 specification matches various other resources, such as for menus, and
1284 menus cannot handle fontsets.
1285
1286 You can specify additional fontsets using X resources named
1287 @samp{Fontset-@var{n}}, where @var{n} is an integer starting from 0.
1288 The resource value should have this form:
1289
1290 @smallexample
1291 @var{fontpattern}, @r{[}@var{charsetname}:@var{fontname}@r{]@dots{}}
1292 @end smallexample
1293
1294 @noindent
1295 @var{fontpattern} should have the form of a standard X font name, except
1296 for the last two fields. They should have the form
1297 @samp{fontset-@var{alias}}.
1298
1299 The fontset has two names, one long and one short. The long name is
1300 @var{fontpattern}. The short name is @samp{fontset-@var{alias}}. You
1301 can refer to the fontset by either name.
1302
1303 The construct @samp{@var{charset}:@var{font}} specifies which font to
1304 use (in this fontset) for one particular character set. Here,
1305 @var{charset} is the name of a character set, and @var{font} is the
1306 font to use for that character set. You can use this construct any
1307 number of times in defining one fontset.
1308
1309 For the other character sets, Emacs chooses a font based on
1310 @var{fontpattern}. It replaces @samp{fontset-@var{alias}} with values
1311 that describe the character set. For the @acronym{ASCII} character font,
1312 @samp{fontset-@var{alias}} is replaced with @samp{ISO8859-1}.
1313
1314 In addition, when several consecutive fields are wildcards, Emacs
1315 collapses them into a single wildcard. This is to prevent use of
1316 auto-scaled fonts. Fonts made by scaling larger fonts are not usable
1317 for editing, and scaling a smaller font is not useful because it is
1318 better to use the smaller font in its own size, which is what Emacs
1319 does.
1320
1321 Thus if @var{fontpattern} is this,
1322
1323 @example
1324 -*-fixed-medium-r-normal-*-24-*-*-*-*-*-fontset-24
1325 @end example
1326
1327 @noindent
1328 the font specification for @acronym{ASCII} characters would be this:
1329
1330 @example
1331 -*-fixed-medium-r-normal-*-24-*-ISO8859-1
1332 @end example
1333
1334 @noindent
1335 and the font specification for Chinese GB2312 characters would be this:
1336
1337 @example
1338 -*-fixed-medium-r-normal-*-24-*-gb2312*-*
1339 @end example
1340
1341 You may not have any Chinese font matching the above font
1342 specification. Most X distributions include only Chinese fonts that
1343 have @samp{song ti} or @samp{fangsong ti} in @var{family} field. In
1344 such a case, @samp{Fontset-@var{n}} can be specified as below:
1345
1346 @smallexample
1347 Emacs.Fontset-0: -*-fixed-medium-r-normal-*-24-*-*-*-*-*-fontset-24,\
1348 chinese-gb2312:-*-*-medium-r-normal-*-24-*-gb2312*-*
1349 @end smallexample
1350
1351 @noindent
1352 Then, the font specifications for all but Chinese GB2312 characters have
1353 @samp{fixed} in the @var{family} field, and the font specification for
1354 Chinese GB2312 characters has a wild card @samp{*} in the @var{family}
1355 field.
1356
1357 @findex create-fontset-from-fontset-spec
1358 The function that processes the fontset resource value to create the
1359 fontset is called @code{create-fontset-from-fontset-spec}. You can also
1360 call this function explicitly to create a fontset.
1361
1362 @xref{Font X}, for more information about font naming in X.
1363
1364 @node Undisplayable Characters
1365 @section Undisplayable Characters
1366
1367 There may be a some non-@acronym{ASCII} characters that your terminal cannot
1368 display. Most text-only terminals support just a single character
1369 set (use the variable @code{default-terminal-coding-system}
1370 (@pxref{Terminal Coding}) to tell Emacs which one); characters which
1371 can't be encoded in that coding system are displayed as @samp{?} by
1372 default.
1373
1374 Graphical displays can display a broader range of characters, but
1375 you may not have fonts installed for all of them; characters that have
1376 no font appear as a hollow box.
1377
1378 If you use Latin-1 characters but your terminal can't display
1379 Latin-1, you can arrange to display mnemonic @acronym{ASCII} sequences
1380 instead, e.g.@: @samp{"o} for o-umlaut. Load the library
1381 @file{iso-ascii} to do this.
1382
1383 @vindex latin1-display
1384 If your terminal can display Latin-1, you can display characters
1385 from other European character sets using a mixture of equivalent
1386 Latin-1 characters and @acronym{ASCII} mnemonics. Customize the variable
1387 @code{latin1-display} to enable this. The mnemonic @acronym{ASCII}
1388 sequences mostly correspond to those of the prefix input methods.
1389
1390 @node Unibyte Mode
1391 @section Unibyte Editing Mode
1392
1393 @cindex European character sets
1394 @cindex accented characters
1395 @cindex ISO Latin character sets
1396 @cindex Unibyte operation
1397 The ISO 8859 Latin-@var{n} character sets define character codes in
1398 the range 0240 to 0377 octal (160 to 255 decimal) to handle the
1399 accented letters and punctuation needed by various European languages
1400 (and some non-European ones). If you disable multibyte characters,
1401 Emacs can still handle @emph{one} of these character codes at a time.
1402 To specify @emph{which} of these codes to use, invoke @kbd{M-x
1403 set-language-environment} and specify a suitable language environment
1404 such as @samp{Latin-@var{n}}.
1405
1406 For more information about unibyte operation, see @ref{Enabling
1407 Multibyte}. Note particularly that you probably want to ensure that
1408 your initialization files are read as unibyte if they contain
1409 non-@acronym{ASCII} characters.
1410
1411 @vindex unibyte-display-via-language-environment
1412 Emacs can also display those characters, provided the terminal or font
1413 in use supports them. This works automatically. Alternatively, on a
1414 graphical display, Emacs can also display single-byte characters
1415 through fontsets, in effect by displaying the equivalent multibyte
1416 characters according to the current language environment. To request
1417 this, set the variable @code{unibyte-display-via-language-environment}
1418 to a non-@code{nil} value.
1419
1420 @cindex @code{iso-ascii} library
1421 If your terminal does not support display of the Latin-1 character
1422 set, Emacs can display these characters as @acronym{ASCII} sequences which at
1423 least give you a clear idea of what the characters are. To do this,
1424 load the library @code{iso-ascii}. Similar libraries for other
1425 Latin-@var{n} character sets could be implemented, but we don't have
1426 them yet.
1427
1428 @findex standard-display-8bit
1429 @cindex 8-bit display
1430 Normally non-ISO-8859 characters (decimal codes between 128 and 159
1431 inclusive) are displayed as octal escapes. You can change this for
1432 non-standard ``extended'' versions of ISO-8859 character sets by using the
1433 function @code{standard-display-8bit} in the @code{disp-table} library.
1434
1435 There are two ways to input single-byte non-@acronym{ASCII}
1436 characters:
1437
1438 @itemize @bullet
1439 @cindex 8-bit input
1440 @item
1441 You can use an input method for the selected language environment.
1442 @xref{Input Methods}. When you use an input method in a unibyte buffer,
1443 the non-@acronym{ASCII} character you specify with it is converted to unibyte.
1444
1445 @item
1446 If your keyboard can generate character codes 128 (decimal) and up,
1447 representing non-@acronym{ASCII} characters, you can type those character codes
1448 directly.
1449
1450 On a graphical display, you should not need to do anything special to use
1451 these keys; they should simply work. On a text-only terminal, you
1452 should use the command @code{M-x set-keyboard-coding-system} or the
1453 variable @code{keyboard-coding-system} to specify which coding system
1454 your keyboard uses (@pxref{Terminal Coding}). Enabling this feature
1455 will probably require you to use @kbd{ESC} to type Meta characters;
1456 however, on a console terminal or in @code{xterm}, you can arrange for
1457 Meta to be converted to @kbd{ESC} and still be able type 8-bit
1458 characters present directly on the keyboard or using @kbd{Compose} or
1459 @kbd{AltGr} keys. @xref{User Input}.
1460
1461 @kindex C-x 8
1462 @cindex @code{iso-transl} library
1463 @cindex compose character
1464 @cindex dead character
1465 @item
1466 For Latin-1 only, you can use the key @kbd{C-x 8} as a ``compose
1467 character'' prefix for entry of non-@acronym{ASCII} Latin-1 printing
1468 characters. @kbd{C-x 8} is good for insertion (in the minibuffer as
1469 well as other buffers), for searching, and in any other context where
1470 a key sequence is allowed.
1471
1472 @kbd{C-x 8} works by loading the @code{iso-transl} library. Once that
1473 library is loaded, the @key{ALT} modifier key, if the keyboard has
1474 one, serves the same purpose as @kbd{C-x 8}: use @key{ALT} together
1475 with an accent character to modify the following letter. In addition,
1476 if the keyboard has keys for the Latin-1 ``dead accent characters,''
1477 they too are defined to compose with the following character, once
1478 @code{iso-transl} is loaded.
1479
1480 Use @kbd{C-x 8 C-h} to list all the available @kbd{C-x 8} translations.
1481 @end itemize
1482
1483 @node Charsets
1484 @section Charsets
1485 @cindex charsets
1486
1487 Emacs groups all supported characters into disjoint @dfn{charsets}.
1488 Each character code belongs to one and only one charset. For
1489 historical reasons, Emacs typically divides an 8-bit character code
1490 for an extended version of @acronym{ASCII} into two charsets:
1491 @acronym{ASCII}, which covers the codes 0 through 127, plus another
1492 charset which covers the ``right-hand part'' (the codes 128 and up).
1493 For instance, the characters of Latin-1 include the Emacs charset
1494 @code{ascii} plus the Emacs charset @code{latin-iso8859-1}.
1495
1496 Emacs characters belonging to different charsets may look the same,
1497 but they are still different characters. For example, the letter
1498 @samp{o} with acute accent in charset @code{latin-iso8859-1}, used for
1499 Latin-1, is different from the letter @samp{o} with acute accent in
1500 charset @code{latin-iso8859-2}, used for Latin-2.
1501
1502 @findex list-charset-chars
1503 @cindex characters in a certain charset
1504 @findex describe-character-set
1505 There are two commands for obtaining information about Emacs
1506 charsets. The command @kbd{M-x list-charset-chars} prompts for a name
1507 of a character set, and displays all the characters in that character
1508 set. The command @kbd{M-x describe-character-set} prompts for a
1509 charset name and displays information about that charset, including
1510 its internal representation within Emacs.
1511
1512 To find out which charset a character in the buffer belongs to,
1513 put point before it and type @kbd{C-u C-x =}.
1514
1515 @ignore
1516 arch-tag: 310ba60d-31ef-4ce7-91f1-f282dd57b6b3
1517 @end ignore