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