diff -wu -rp gtk+-2.2.1.org/gtk/gtktextbtree.c gtk+-2.2.1/gtk/gtktextbtree.c
--- gtk+-2.2.1.org/gtk/gtktextbtree.c	2002-10-10 17:45:40.000000000 -0400
+++ gtk+-2.2.1/gtk/gtktextbtree.c	2003-04-22 12:48:51.000000000 -0400
@@ -57,13 +57,13 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <pango/pango-utils.h>
 #include "gtktexttag.h"
 #include "gtktexttagtable.h"
 #include "gtktextlayout.h"
 #include "gtktextiterprivate.h"
 #include "gtkdebug.h"
 #include "gtktextmarkprivate.h"
-
 /*
  * Types
  */
@@ -554,6 +554,178 @@ _gtk_text_btree_segments_changed (GtkTex
  * Indexable segment mutation
  */
 
+/*
+ *  The following function is responsible for resolving the bidi direction
+ *  for the lines between start and end. But it also calculates any
+ *  dependent bidi direction for surrounding lines that change as a result
+ *  of the bidi direction decisions within the range. The function is
+ *  trying to do as little propagation as is needed.
+ */
+static void
+_gtk_text_btree_resolve_bidi(GtkTextIter *start,
+			     GtkTextIter *end)
+{
+  GtkTextBTree *tree = _gtk_text_iter_get_btree (start);
+  GtkTextLine *start_line, *end_line, *start_line_prev, *end_line_next, *line;
+  PangoDirection last_strong, dir_above_propagated, dir_below_propagated;
+
+  /* Resolve the strong bidi direction for all lines between
+   * start and end.
+  */
+  start_line = _gtk_text_iter_get_text_line(start);
+  start_line_prev = _gtk_text_line_previous(start_line);
+  end_line = _gtk_text_iter_get_text_line (end);
+  end_line_next = _gtk_text_line_next(end_line);
+  
+  line = start_line;
+  last_strong = PANGO_DIRECTION_NEUTRAL;
+  while(line && line != end_line_next)
+    {
+      PangoDirection p_dir;
+      
+      /* Loop through the segments and search for a strong character
+       */
+      GtkTextLineSegment *seg = line->segments;
+      line->dir_strong = PANGO_DIRECTION_NEUTRAL;
+      
+      while(seg)
+	{
+	  if (seg->byte_count > 0)
+	    {
+	      p_dir = pango_find_base_dir(seg->body.chars,
+					  seg->byte_count);
+	      if (p_dir != PANGO_DIRECTION_NEUTRAL)
+		{
+		  line->dir_strong = p_dir;
+
+		  /* Remember the first resolved non-neutral paragraph */
+		  if (last_strong == PANGO_DIRECTION_NEUTRAL)
+		    last_strong = PANGO_DIRECTION_NEUTRAL;
+		  break;
+		}
+	    }
+	  seg = seg->next;
+	}
+
+      line = _gtk_text_line_next(line);
+    }
+
+  /* Sweep forward */
+
+  /* The variable above_strong contains the forward propagated direction
+   * before start. It is neutral if start is in the beginning of the buffer.
+   */
+  dir_above_propagated = PANGO_DIRECTION_NEUTRAL;
+  if (start_line_prev)
+    dir_above_propagated = start_line_prev->dir_propagated_forward;
+
+  /* Loop forward and propagate the direction of each paragraph 
+   * to all neutral lines.
+   */
+  line = start_line;
+  last_strong = dir_above_propagated;
+  while(line != end_line_next)
+    {
+      if (line->dir_strong != PANGO_DIRECTION_NEUTRAL)
+	last_strong = line->dir_strong;
+
+      line->dir_propagated_forward = last_strong;
+
+      line = _gtk_text_line_next(line);
+    }
+
+  /* Continue propagating as long as the previous resolved forward
+   * is different from last_strong.
+   */
+  {
+    GtkTextIter end_propagate;
+    
+    while(line
+	  && line->dir_strong == PANGO_DIRECTION_NEUTRAL
+	  && line->dir_propagated_forward != last_strong
+	  )
+      {
+	GtkTextLine *prev = line;
+	line->dir_propagated_forward = last_strong;
+	
+	line = _gtk_text_line_next(line);
+	if (!line)
+	  {
+	    line = prev;
+	    break;
+	  }
+      }
+
+    /* The last character to invalidate is the last line before the
+     * line with the strong character. Or in case of the end of the
+     * buffer, the last line of the buffer. (There seems to be an
+     * extra "virtual" last line in the buffer that must not be used
+     * calling _gtk_text_btree_get_iter_at_line (causes crash). Thus the
+     * _gtk_text_line_previous is ok in that case as well.)
+     */
+    line = _gtk_text_line_previous(line);
+    _gtk_text_btree_get_iter_at_line (tree, &end_propagate, line, 0);
+    _gtk_text_btree_invalidate_region(tree, end, &end_propagate);
+  }
+  
+  /* Sweep backward */
+
+  /* The variable below_strong contains the backward propagated direction
+   * after end. It is neutral if end is at the beginning of the buffer.
+  */
+  dir_below_propagated = PANGO_DIRECTION_NEUTRAL;
+  if (end_line_next)
+    dir_below_propagated = end_line_next->dir_propagated_back;
+
+  /* Loop backward and propagate the direction of each paragraph 
+   * to all neutral lines.
+   */
+  line = end_line;
+  last_strong = dir_below_propagated;
+  while (line != start_line_prev)
+    {
+      if (line->dir_strong != PANGO_DIRECTION_NEUTRAL)
+	last_strong = line->dir_strong;
+
+      line->dir_propagated_back = last_strong;
+
+      line = _gtk_text_line_previous(line);
+    }
+
+  /* Continue propagating as long as the resolved backward dir
+   * is different from last_strong.
+   */
+  {
+    GtkTextIter start_propagate;
+
+    while (line
+	   && line->dir_strong == PANGO_DIRECTION_NEUTRAL
+	   && line->dir_propagated_back != last_strong)
+      {
+	GtkTextLine *prev = line;
+	line->dir_propagated_back = last_strong;
+
+	line = _gtk_text_line_previous(line);
+	if (!line)
+	  {
+	    line = prev;
+	    break;
+	  }
+      }
+
+    /* We only need to invalidate for backwards propagation if on
+     * the line we ended up at, there is no valid dir_propagated_forward.
+     * Only in that case, will the backward propagation be used for
+     * rendering.
+     */
+    if (line && line->dir_propagated_forward == PANGO_DIRECTION_NEUTRAL)
+      {
+	_gtk_text_btree_get_iter_at_line (tree, &start_propagate, line, 0);
+	_gtk_text_btree_invalidate_region(tree, &start_propagate, start);
+      }
+  }
+}
+
 void
 _gtk_text_btree_delete (GtkTextIter *start,
                         GtkTextIter *end)
@@ -884,6 +1056,8 @@ _gtk_text_btree_delete (GtkTextIter *sta
   /* Re-initialize our iterators */
   _gtk_text_btree_get_iter_at_line (tree, start, start_line, start_byte_offset);
   *end = *start;
+  
+  _gtk_text_btree_resolve_bidi(start, end);
 }
 
 void
@@ -1031,7 +1205,6 @@ _gtk_text_btree_insert (GtkTextIter *ite
     GtkTextIter start;
     GtkTextIter end;
 
-
     _gtk_text_btree_get_iter_at_line (tree,
                                       &start,
                                       start_line,
@@ -1050,6 +1223,8 @@ _gtk_text_btree_insert (GtkTextIter *ite
 
     /* Convenience for the user */
     *iter = end;
+    
+    _gtk_text_btree_resolve_bidi(&start, &end);
   }
 }
 
@@ -4486,6 +4661,9 @@ gtk_text_line_new (void)
   GtkTextLine *line;
 
   line = g_new0(GtkTextLine, 1);
+  line->dir_strong = PANGO_DIRECTION_NEUTRAL;
+  line->dir_propagated_forward = PANGO_DIRECTION_NEUTRAL;
+  line->dir_propagated_back = PANGO_DIRECTION_NEUTRAL;
 
   return line;
 }
diff -wu -rp gtk+-2.2.1.org/gtk/gtktextbtree.h gtk+-2.2.1/gtk/gtktextbtree.h
--- gtk+-2.2.1.org/gtk/gtktextbtree.h	2002-09-23 15:20:48.000000000 -0400
+++ gtk+-2.2.1/gtk/gtktextbtree.h	2003-04-22 12:39:08.000000000 -0400
@@ -212,6 +212,9 @@ struct _GtkTextLine {
   GtkTextLineSegment *segments; /* First in ordered list of segments
                                  * that make up the line. */
   GtkTextLineData *views;      /* data stored here by views */
+  PangoDirection dir_strong;                /* BiDi algo dir of line */
+  PangoDirection dir_propagated_back;       /* BiDi algo dir of next line */
+  PangoDirection dir_propagated_forward;    /* BiDi algo dir of prev line */
 };
 
 
diff -wu -rp gtk+-2.2.1.org/gtk/gtktextlayout.c gtk+-2.2.1/gtk/gtktextlayout.c
--- gtk+-2.2.1.org/gtk/gtktextlayout.c	2003-01-15 17:01:21.000000000 -0500
+++ gtk+-2.2.1/gtk/gtktextlayout.c	2003-04-22 12:47:54.000000000 -0400
@@ -106,6 +106,24 @@ static void gtk_text_layout_invalidate_a
 
 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
 
+static void gtk_text_layout_mark_set_handler (GtkTextBuffer     *buffer,
+					      const GtkTextIter *location,
+					      GtkTextMark       *mark,
+					      gpointer           data);
+
+static void gtk_text_layout_buffer_insert_text(GtkTextBuffer *textbuffer,
+					       GtkTextIter   *iter,
+					       gchar         *str,
+					       gint           len,
+					       gpointer data);
+
+static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
+					  	GtkTextIter *start,
+						GtkTextIter *end,
+						gpointer data);
+
+static void gtk_text_layout_update_cursor_line (GtkTextLayout     *layout);
+
 enum {
   INVALIDATED,
   CHANGED,
@@ -289,6 +307,14 @@ gtk_text_layout_set_buffer (GtkTextLayou
       g_object_ref (buffer);
 
       _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
+
+      /* Bind to all signals that move the insert mark. */
+      g_signal_connect_after (layout->buffer, "mark_set",
+			      G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
+      g_signal_connect_after (layout->buffer, "insert_text",
+			      G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
+      g_signal_connect_after (layout->buffer, "delete_range",
+			      G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
     }
 }
 
@@ -345,11 +371,14 @@ gtk_text_layout_set_contexts (GtkTextLay
 }
 
 /**
- * gtk_text_layout_set_cursor_direction:
- * @direction: the new direction(s) for which to draw cursors.
+ * gtk_text_layout_set_cursor_and_keyboard_direction:
+ * @cursor_dir: the new direction(s) for which to draw cursors.
  *             %GTK_TEXT_DIR_NONE means draw cursors for both
  *             left-to-right insertion and right-to-left insertion.
  *             (The two cursors will be visually distinguished.)
+ * @keyboard_dir: the current direction of the keyboard. This direction
+ *              is used for setting the BiDi base direction if the
+ *              line contains only neutral characters.
  * 
  * Sets which text directions (left-to-right and/or right-to-left) for
  * which cursors will be drawn for the insertion point. The visual
@@ -358,16 +387,20 @@ gtk_text_layout_set_contexts (GtkTextLay
  * make the drawn position of the cursor depend on the keyboard state.
  **/
 void
-gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
-				      GtkTextDirection direction)
+gtk_text_layout_set_cursor_and_keyboard_direction (GtkTextLayout   *layout,
+						   GtkTextDirection cursor_dir,
+						   GtkTextDirection keyboard_dir)
 {
-  if (direction != layout->cursor_direction)
+  if (cursor_dir != layout->cursor_direction
+      || keyboard_dir != layout->keyboard_direction)
     {
-      layout->cursor_direction = direction;
+      layout->cursor_direction = cursor_dir;
+      layout->keyboard_direction = keyboard_dir;
       gtk_text_layout_invalidate_cursor_line (layout);
     }
 }
 
+
 /**
  * gtk_text_layout_get_buffer:
  * @layout: a #GtkTextLayout
@@ -675,24 +708,32 @@ gtk_text_layout_invalidate_cache (GtkTex
 static void
 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
 {
-  GtkTextIter iter;
-  GtkTextLine *line;
   GtkTextLineData *line_data;
 
-  gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
-				    gtk_text_buffer_get_mark (layout->buffer, "insert"));
+  if (layout->cursor_line == NULL)
+    return;
   
-  line = _gtk_text_iter_get_text_line (&iter);
-  line_data = _gtk_text_line_get_data (line, layout);
+  line_data = _gtk_text_line_get_data (layout->cursor_line, layout);
   if (line_data)
     {
-      gtk_text_layout_invalidate_cache (layout, line);
-      _gtk_text_line_invalidate_wrap (line, line_data);
+      gtk_text_layout_invalidate_cache (layout, layout->cursor_line);
+      _gtk_text_line_invalidate_wrap (layout->cursor_line, line_data);
       gtk_text_layout_invalidated (layout);
     }
 }
 
 static void
+gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
+{
+  GtkTextIter iter;
+
+  gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
+				    gtk_text_buffer_get_mark (layout->buffer, "insert"));
+  
+  layout->cursor_line = _gtk_text_iter_get_text_line (&iter);
+}
+
+static void
 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
                                  const GtkTextIter *start,
                                  const GtkTextIter *end)
@@ -1101,26 +1142,46 @@ totally_invisible_line (GtkTextLayout *l
 
 static void
 set_para_values (GtkTextLayout      *layout,
+		 PangoDirection base_dir,
                  GtkTextAttributes *style,
                  GtkTextLineDisplay *display)
 {
   PangoAlignment pango_align = PANGO_ALIGN_LEFT;
   int layout_width;
 
+  switch (base_dir)
+    {
+      /* If no base direction was found, then use the style */
+    case PANGO_DIRECTION_NEUTRAL :
   display->direction = style->direction;
 
-  if (display->direction == GTK_TEXT_DIR_LTR)
-    display->layout = pango_layout_new (layout->ltr_context);
+      /* Override the base direction */
+      if (display->direction == GTK_TEXT_DIR_RTL)
+	base_dir = PANGO_DIRECTION_RTL;
   else
+	base_dir = PANGO_DIRECTION_LTR;
+      
+      break;
+    case PANGO_DIRECTION_RTL :
+      display->direction = GTK_TEXT_DIR_RTL;
+      break;
+    default:
+      display->direction = GTK_TEXT_DIR_LTR;
+      break;
+    }
+  
+  if (display->direction == GTK_TEXT_DIR_RTL)
     display->layout = pango_layout_new (layout->rtl_context);
+  else
+    display->layout = pango_layout_new (layout->ltr_context);
 
   switch (style->justification)
     {
     case GTK_JUSTIFY_LEFT:
-      pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
+      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
       break;
     case GTK_JUSTIFY_RIGHT:
-      pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
+      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
       break;
     case GTK_JUSTIFY_CENTER:
       pango_align = PANGO_ALIGN_CENTER;
@@ -1670,6 +1731,7 @@ gtk_text_layout_get_line_display (GtkTex
   GSList *cursor_segs = NULL;
   GSList *tmp_list1, *tmp_list2;
   gboolean saw_widget = FALSE;
+  PangoDirection base_dir;
   
   g_return_val_if_fail (line != NULL, NULL);
 
@@ -1702,6 +1764,19 @@ gtk_text_layout_get_line_display (GtkTex
   if (totally_invisible_line (layout, line, &iter))
     return display;
 
+  /* Find the bidi base direction */
+  base_dir = line->dir_propagated_forward;
+  if (base_dir == PANGO_DIRECTION_NEUTRAL)
+      base_dir = line->dir_propagated_back;
+
+  if (line == layout->cursor_line &&
+      line->dir_strong == PANGO_DIRECTION_NEUTRAL)
+    {
+      GtkTextDirection dir = layout->keyboard_direction;
+
+      base_dir = (dir == GTK_TEXT_DIR_LTR) ? PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
+    }
+  
   /* Allocate space for flat text for buffer
    */
   text_allocated = _gtk_text_line_byte_count (line);
@@ -1732,7 +1807,7 @@ gtk_text_layout_get_line_display (GtkTex
            */
           if (!para_values_set)
             {
-              set_para_values (layout, style, display);
+              set_para_values (layout, base_dir, style, display);
               para_values_set = TRUE;
             }
 
@@ -1901,7 +1976,7 @@ gtk_text_layout_get_line_display (GtkTex
   if (!para_values_set)
     {
       style = get_style (layout, &iter);
-      set_para_values (layout, style, display);
+      set_para_values (layout, base_dir, style, display);
       release_style (layout, style);
     }
   
@@ -3027,3 +3102,40 @@ gtk_text_layout_spew (GtkTextLayout *lay
           layout->height, layout->screen_width);
 #endif
 }
+
+/* Catch all situations that move the insertion point.
+ */
+static void
+gtk_text_layout_mark_set_handler (GtkTextBuffer     *buffer,
+				  const GtkTextIter *location,
+				  GtkTextMark       *mark,
+				  gpointer           data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  if (mark == gtk_text_buffer_get_insert (buffer))
+    gtk_text_layout_update_cursor_line(layout);
+}
+
+static void
+gtk_text_layout_buffer_insert_text(GtkTextBuffer *textbuffer,
+				   GtkTextIter   *iter,
+				   gchar         *str,
+				   gint           len,
+				   gpointer       data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  gtk_text_layout_update_cursor_line(layout);
+}
+
+static void
+gtk_text_layout_buffer_delete_range(GtkTextBuffer *textbuffer,
+				    GtkTextIter *start,
+				    GtkTextIter *end,
+				    gpointer data)
+{
+  GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
+
+  gtk_text_layout_update_cursor_line(layout);
+}
diff -wu -rp gtk+-2.2.1.org/gtk/gtktextlayout.h gtk+-2.2.1/gtk/gtktextlayout.h
--- gtk+-2.2.1.org/gtk/gtktextlayout.h	2002-11-08 14:41:50.000000000 -0500
+++ gtk+-2.2.1/gtk/gtktextlayout.h	2003-03-16 23:13:51.000000000 -0500
@@ -165,6 +165,16 @@ struct _GtkTextLayout
    */
   guint cursor_direction : 2;
 
+  /* The keyboard direction is used to default the alignment when
+     there are no strong characters.
+  */
+  guint keyboard_direction : 2;
+
+  /* Cache the line that the cursor is positioned on, as the keyboard
+     direction only influences the direction of the cursor line.
+  */
+  GtkTextLine *cursor_line;
+  
   /* The preedit string and attributes, if any */
 
   gchar *preedit_string;
@@ -264,8 +274,9 @@ void               gtk_text_layout_set_d
 void               gtk_text_layout_set_contexts          (GtkTextLayout     *layout,
 							  PangoContext      *ltr_context,
 							  PangoContext      *rtl_context);
-void               gtk_text_layout_set_cursor_direction  (GtkTextLayout     *layout,
-							  GtkTextDirection   direction);
+void               gtk_text_layout_set_cursor_and_keyboard_direction  (GtkTextLayout     *layout,
+							  GtkTextDirection   cursor_dir,
+							  GtkTextDirection keyboard_dir);
 void               gtk_text_layout_default_style_changed (GtkTextLayout     *layout);
 
 void gtk_text_layout_set_screen_width       (GtkTextLayout     *layout,
diff -wu -rp gtk+-2.2.1.org/gtk/gtktextview.c gtk+-2.2.1/gtk/gtktextview.c
--- gtk+-2.2.1.org/gtk/gtktextview.c	2003-01-27 16:13:18.000000000 -0500
+++ gtk+-2.2.1/gtk/gtktextview.c	2003-04-22 12:46:24.000000000 -0400
@@ -5320,25 +5320,32 @@ gtk_text_view_check_keymap_direction (Gt
   if (text_view->layout)
     {
       gboolean split_cursor;
-      GtkTextDirection new_dir;
+      GtkTextDirection new_cursor_dir, new_keyboard_dir;
       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
+      GdkKeymap *keymap;
   
       g_object_get (G_OBJECT (settings),
 		    "gtk-split-cursor", &split_cursor,
 		    NULL);
+      
+      keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
+      
+      new_keyboard_dir 
+	  = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
+	    GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
+
       if (split_cursor)
 	{
-	  new_dir = GTK_TEXT_DIR_NONE;
+	  new_cursor_dir = GTK_TEXT_DIR_NONE;
 	}
       else
 	{
-	  GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
-	  new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
-	    GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
+	  new_cursor_dir = new_keyboard_dir;
 	}
       
-      if (text_view->layout->cursor_direction != new_dir)
-	gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
+	gtk_text_layout_set_cursor_and_keyboard_direction (text_view->layout,
+							   new_cursor_dir,
+							   new_keyboard_dir);
     }
 }
 
