Right-to-left writing
=====================

This document describes how Mule handles a text written from right to left.

Right-to-left Character Set
===========================

In Mule, each character set has the attribute direction.  The value is either 0 (l2r, left-to-right) or 1 (r2l, right-to-left).  For the moment, Mule has four r2l character sets, namely, Hebrew (ISO8859-8), ASCII-r2l (private) Arabic1 (private) and Arabic2 (private).  ASCII-r2l is for providing r2l space and punctuation characters to Hebrew.  Consecutive r2l characters are shown from right to left on the screen.

Hereafter, we denote r2l characters by capital letters and l2r characters by lowercase letters in strings.  Punctuation marks (e.g., period) mean normal ASCII characters.  For r2l space and punctuations, we use the letter _ (underscore).

Direction of buffer
===================

Each buffer also has the attribute direction (buffer local variable display-direction).  By default, the value is nil (means l2r direction).  Provided that a buffer contains:

     this is a DESREVER word.

If display-direction is nil (this is the default), what you see on your screen is:

     +---------------------------------+
     |this is a DESREVER word.         |
     |                                 |
     +---------------------------------+

but, if the direction is non-nil, what you see is:

     +---------------------------------+
     |          word.DESREVERthis is a |
     |                                 |
     +---------------------------------+

If you are in visual-mode, the value of display-direction is reflected in the mode-line.  The string L2R means display-direction is nil; R2L means non-nil. In visual-mode, you can set display-direction to nil by typing C-c <, and to t (this means non-nil) by typing C-c >.

If you read a file that has the extension .l2r, the buffer automatically turns into visual-mode and display-direction is set to nil.  Likewise, if a file has the extension .r2l, the buffer automatically goes in visual-mode and display-direction is set to t.

Cursor Motion
=============

If you are not in visual-mode and type C-f (or M-x forward-char, the cursor moves as follows:

     |this is a DESREVER word.      |
             ^
     |this is a DESREVER word.      |
              ^
     |this is a DESREVER word.      |
               ^
     |this is a DESREVER word.      |
                       ^
     |this is a DESREVER word.      |
                      ^
     |this is a DESREVER word.      |
                     ^
     |this is a DESREVER word.      |
                    ^
     |this is a DESREVER word.      |
                   ^
     |this is a DESREVER word.      |
                  ^
     |this is a DESREVER word.      |
                 ^
     |this is a DESREVER word.      |
                ^
     |this is a DESREVER word.      |
                        ^
     |this is a DESREVER word.      |
                         ^
     |this is a DESREVER word.      |
                          ^

This is rather complicated and misleading.  To avoid confusions, the following cursor motion commands are prepared in visual-mode to handle bi-directional texts easily.  All these commands accept an additional prefix numeric argument.

C-f or M-x visual-forward-char
     Move the cursor visually forward in characters.

C-b or M-x visual-backward-char
     Move the cursor visually backward in characters.

C-p, UP or M-x visual-previous-line
     Move the cursor up in lines.

C-n, DOWN or M-x visual-next-line
     Move the cursor down in lines.

C-a or M-x visual-beginning-of-line
     Move the cursor to the visual beginning of the current line.

M-f or M-x visual-forward-word
     Move the cursor visually forward in words.

M-b or M-x visual-backward-word
     Move the cursor visually backward in words.

M-<, HOME or M-x visual-beginning-of-buffer
     Move the cursor to the visual beginning of the current buffer.

M->, END or M-x visual-end-of-buffer
     Move the cursor to the visual end of the current buffer.

Note that ordinary cursor motion commands (forward-char, backward-char, etc.) behave according to the logical order of the text, whilst the above commands behave according to the visual order.  Compare the difference of the two C-f commands.  (You can exit visual-mode by typing C-c C-c.)

Some of you may be confused by the words forward and backward.  Here is a summary:

     display-direction       nil             non-nil
     ------------------------------------------------
     forward                 right           left
     backward                left            right

LR Commands
===========

If you have been using Emacs or Mule for a long time, it is possible that C-b and move to left are so strongly associated in your mind that you want to move the cursor to left by C-b no matter how display-direction is set.  Likewise, you may want to move the cursor to the right, to the left-most column and to the right-most column by C-f, C-a and C-e, respectively.

In such cases, include the following line in your ~/.emacs file:

     (setq visual-use-lr-commands t)

This lisp command enables so called LR commands.  LR commands act according to the absolute screen direction (such as left or right) rather than the relative direction (forward or backward).

C-b, LEFT or M-x visual-move-to-left-char
     Move the cursor to the left in characters.

C-f, RIGHT or M-x visual-move-to-right-char
     Move the cursor to the right in characters.

M-b, M-LEFT or M-x visual-move-to-left-word
     Move the cursor to the left in words.

M-f, M-RIGHT or M-x visual-move-to-right-word
     Move the cursor to the right in words.

C-a or M-x visual-left-end-of-line
     Move the cursor to the left-most column of the current line.

C-e or M-x visual-right-end-of-line
     Move the cursor to the right-most column of the current line.

As a matter of fact, the four keys, LEFT, RIGHT, M-LEFT and M-RIGHT are active even though your ~/.emacs file does not contain the setq line above.  So if you are confused, use the arrow keys.