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