Index: src/window.c
===================================================================
RCS file: /cvs/gnome/metacity/src/window.c,v
retrieving revision 1.333
diff -p -u -r1.333 window.c
--- src/window.c	31 Jul 2004 21:26:05 -0000	1.333
+++ src/window.c	5 Aug 2004 04:08:34 -0000
@@ -96,6 +96,9 @@ static void meta_window_move_resize_inte
                                               int                 w,
                                               int                 h);
 
+static void     ensure_mru_position_after (MetaWindow *window,
+                                           MetaWindow *after_this_one);
+
 
 void meta_window_move_resize_now (MetaWindow  *window);
 
@@ -1715,8 +1718,10 @@ meta_window_show (MetaWindow *window)
   takes_focus_on_map = window_takes_focus_on_map (window);
 
   if ( (!takes_focus_on_map) && (window->display->focus_window != NULL) )
-    meta_window_stack_just_below (window,
-                                  window->display->focus_window);
+    {
+      meta_window_stack_just_below (window, window->display->focus_window);
+      ensure_mru_position_after (window, window->display->focus_window);
+    }
 
   window->focus_despite_user_time = FALSE;
  
@@ -7194,6 +7199,75 @@ meta_window_update_layer (MetaWindow *wi
   else
     meta_stack_update_layer (window->screen->stack, window);
   meta_stack_thaw (window->screen->stack);
+}
+
+/* ensure_mru_position_after ensures that window appears after
+ * below_this_one in the active_workspace's mru_list (i.e. it treats
+ * window as having been less recently used than below_this_one)
+ */
+static void
+ensure_mru_position_after (MetaWindow *window,
+                           MetaWindow *after_this_one)
+{
+  GList*   tmp;
+  GList*   window_position;
+  GList*   after_this_one_position;
+  gboolean is_already_after;
+
+  tmp = window->screen->active_workspace->mru_list;
+  after_this_one_position = NULL;
+  window_position = NULL;
+
+  is_already_after = FALSE;
+
+  while (tmp)
+    {
+      MetaWindow* tmp_window;
+      tmp_window = ((MetaWindow*) tmp->data);
+      if (tmp_window == window)
+        {
+          window_position = tmp;
+        }
+      if (tmp_window == after_this_one)
+        {
+          after_this_one_position = tmp;
+          if (window_position == NULL)
+            is_already_after = TRUE;
+          break;
+        }
+
+      tmp = tmp->next;
+    }
+
+  if (is_already_after)
+    return;
+
+  window->screen->active_workspace->mru_list =
+    g_list_delete_link (window->screen->active_workspace->mru_list,
+                        window_position);
+
+  window->screen->active_workspace->mru_list =
+    g_list_insert_before (window->screen->active_workspace->mru_list,
+                          after_this_one_position->next,
+                          window);
+
+  /* Alternate implementation (easier to understand, but perhaps slower...)
+  GList* active_mru_list = window->screen->active_workspace->mru_list;
+  GList* window_position         = g_list_find (active_mru_list, window);
+  GList* after_this_one_position = g_list_find (active_mru_list, after_this_one);
+
+  if (g_list_length (window_position) > g_list_length (after_this_one_position))
+    {
+      window->screen->active_workspace->mru_list =
+        g_list_delete_link (window->screen->active_workspace->mru_list,
+                            window_position);
+
+      window->screen->active_workspace->mru_list =
+        g_list_insert_before (window->screen->active_workspace->mru_list,
+                              after_this_one_position->next,
+                              window);
+    }
+  */
 }
 
 void
