Index: screen.c
===================================================================
RCS file: /cvs/gnome/metacity/src/screen.c,v
retrieving revision 1.106
diff -u -r1.106 screen.c
--- screen.c	16 May 2003 21:59:07 -0000	1.106
+++ screen.c	29 May 2003 01:57:05 -0000
@@ -1204,37 +1204,19 @@
   /* don't show tab popup, since proper space isn't selected yet */
 }
 
+void
+meta_screen_focus_mru_window (MetaScreen *screen,
+	                      MetaWindow *not_this_one)
+{
+  meta_workspace_focus_mru_window (screen->active_workspace, not_this_one);
+}
+
 /* Focus top window on active workspace */
 void
 meta_screen_focus_top_window (MetaScreen *screen,
                               MetaWindow *not_this_one)
 {
-  MetaWindow *window;
-
-  if (not_this_one)
-    meta_topic (META_DEBUG_FOCUS,
-                "Focusing top window excluding %s\n", not_this_one->desc);
-  
-  window = meta_stack_get_default_focus_window (screen->stack,
-                                                screen->active_workspace,
-                                                not_this_one);
-
-  /* FIXME I'm a loser on the CurrentTime front */
-  if (window)
-    {
-      meta_topic (META_DEBUG_FOCUS,
-                  "Focusing top window %s\n", window->desc);
-
-      meta_window_focus (window, meta_display_get_current_time (screen->display));
-
-      /* Also raise the window if in click-to-focus */
-      if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_CLICK)
-        meta_window_raise (window);
-    }
-  else
-    {
-      meta_topic (META_DEBUG_FOCUS, "No top window to focus found\n");
-    }
+  meta_workspace_focus_top_window (screen->active_workspace, not_this_one);
 }
 
 void
@@ -1291,10 +1273,7 @@
 meta_screen_focus_default_window (MetaScreen *screen,
                                   MetaWindow *not_this_one)
 {
-  if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_CLICK)
-    meta_screen_focus_top_window (screen, not_this_one);
-  else
-    meta_screen_focus_mouse_window (screen, not_this_one);
+    meta_workspace_focus_default_window (screen->active_workspace, not_this_one);
 }
 
 const MetaXineramaScreenInfo*
Index: screen.h
===================================================================
RCS file: /cvs/gnome/metacity/src/screen.h,v
retrieving revision 1.49
diff -u -r1.49 screen.h
--- screen.h	16 May 2003 21:59:08 -0000	1.49
+++ screen.h	29 May 2003 01:57:05 -0000
@@ -134,6 +134,8 @@
 
 void          meta_screen_ensure_workspace_popup (MetaScreen *screen);
 
+void          meta_screen_focus_mru_window     (MetaScreen                 *screen,
+                                                MetaWindow                 *not_this_one);
 void          meta_screen_focus_top_window     (MetaScreen                 *screen,
                                                 MetaWindow                 *not_this_one);
 void          meta_screen_focus_mouse_window   (MetaScreen                 *screen,
Index: window.c
===================================================================
RCS file: /cvs/gnome/metacity/src/window.c,v
retrieving revision 1.277
diff -u -r1.277 window.c
--- window.c	18 May 2003 17:16:03 -0000	1.277
+++ window.c	29 May 2003 01:57:11 -0000
@@ -3199,6 +3199,9 @@
 void
 meta_window_stick (MetaWindow  *window)
 {
+  GList *tmp; 
+  MetaWorkspace *workspace;
+
   meta_verbose ("Sticking window %s current on_all_workspaces = %d\n",
                 window->desc, window->on_all_workspaces);
   
@@ -3211,6 +3214,16 @@
    */
   window->on_all_workspaces = TRUE;
 
+  /* We do, however, change the MRU lists of all the workspaces
+   */
+  tmp = window->workspaces;
+  while (tmp)
+    {
+      workspace = (MetaWorkspace *) tmp->data;
+      if (!g_list_find (workspace->mru_list, window))
+          g_list_append (workspace->mru_list, window);
+    }
+
   meta_window_set_current_workspace_hint (window);
   
   meta_window_queue_calc_showing (window);
@@ -3219,6 +3232,9 @@
 void
 meta_window_unstick (MetaWindow  *window)
 {
+  GList *tmp;
+  MetaWorkspace *workspace;
+
   if (!window->on_all_workspaces)
     return;
 
@@ -3226,6 +3242,15 @@
 
   window->on_all_workspaces = FALSE;
 
+  /* Remove window from MRU lists that it doesn't belong in */
+  tmp = window->workspaces;
+  while (tmp)
+    {
+      workspace = (MetaWorkspace *) tmp->data;
+      if (!meta_workspace_contains_window (workspace, window))
+          g_list_remove (workspace->mru_list, window);
+    }
+
   /* We change ourselves to the active workspace, since otherwise you'd get
    * a weird window-vaporization effect. Once we have UI for being
    * on more than one workspace this should probably be add_workspace
@@ -3991,7 +4016,22 @@
                       "* Focus --> %s\n", window->desc);
           window->display->focus_window = window;
           window->has_focus = TRUE;
-          /* Move to the front of the MRU list */
+
+          /* Move to the front of the focusing workspace's MRU list
+	   * FIXME: is the active workspace guaranteed to be the focusing
+	   *        workspace?
+           */
+          if (window->screen->active_workspace)
+            {
+              window->screen->active_workspace->mru_list = 
+                g_list_remove (window->screen->active_workspace->mru_list, 
+                               window);
+              window->screen->active_workspace->mru_list = 
+                g_list_prepend (window->screen->active_workspace->mru_list, 
+                                window);
+            }
+
+          /* Move to the front of the global MRU list */
           window->display->mru_list =
             g_list_remove (window->display->mru_list, window);
           window->display->mru_list =
Index: window.h
===================================================================
RCS file: /cvs/gnome/metacity/src/window.h,v
retrieving revision 1.86
diff -u -r1.86 window.h
--- window.h	17 Mar 2003 06:36:39 -0000	1.86
+++ window.h	29 May 2003 01:57:12 -0000
@@ -54,6 +54,7 @@
   MetaDisplay *display;
   MetaScreen *screen;
   GList *workspaces;
+
   Window xwindow;
   /* may be NULL! not all windows get decorated */
   MetaFrame *frame;
Index: workspace.c
===================================================================
RCS file: /cvs/gnome/metacity/src/workspace.c,v
retrieving revision 1.42
diff -u -r1.42 workspace.c
--- workspace.c	23 Feb 2003 17:09:46 -0000	1.42
+++ workspace.c	29 May 2003 01:57:13 -0000
@@ -41,6 +41,7 @@
   workspace->screen->workspaces =
     g_list_append (workspace->screen->workspaces, workspace);
   workspace->windows = NULL;
+  workspace->mru_list = NULL;
 
   workspace->work_areas = NULL;
   workspace->work_areas_invalid = TRUE;
@@ -102,6 +103,8 @@
   workspace->windows = g_list_prepend (workspace->windows, window);
   window->workspaces = g_list_prepend (window->workspaces, workspace);
 
+  workspace->mru_list = g_list_append (workspace->mru_list, window);
+
   meta_window_set_current_workspace_hint (window);
   
   meta_window_queue_calc_showing (window);
@@ -127,6 +130,7 @@
 
   workspace->windows = g_list_remove (workspace->windows, window);
   window->workspaces = g_list_remove (window->workspaces, workspace);
+  workspace->mru_list = g_list_remove (workspace->mru_list, window);
 
   meta_window_set_current_workspace_hint (window);
   
@@ -219,7 +223,7 @@
   meta_workspace_queue_calc_showing (workspace);
 
   meta_topic (META_DEBUG_FOCUS, "Focusing default window on new workspace\n");
-  meta_screen_focus_default_window (workspace->screen, NULL);
+  meta_workspace_focus_default_window (workspace, NULL);
 }
 
 int
@@ -618,4 +622,91 @@
 meta_workspace_get_name (MetaWorkspace *workspace)
 {
   return meta_prefs_get_workspace_name (meta_workspace_index (workspace));
+}
+
+void
+meta_workspace_focus_default_window (MetaWorkspace *workspace,
+                                     MetaWindow *not_this_one)
+{
+  if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_CLICK)
+    meta_workspace_focus_mru_window (workspace, not_this_one);
+  else
+    meta_screen_focus_mouse_window (workspace->screen, not_this_one);
+}
+
+/* Focus MRU window (or top window if failed) on active workspace */
+void
+meta_workspace_focus_mru_window (MetaWorkspace *workspace,
+                                 MetaWindow *not_this_one)
+{
+  MetaWindow *window = NULL;
+  GList *tmp;
+
+  if (not_this_one)
+    meta_topic (META_DEBUG_FOCUS,
+                "Focusing MRU window excluding %s\n", not_this_one->desc);
+  
+  tmp = workspace->mru_list;  
+
+  while (tmp)
+    {
+      if ((MetaWindow *) tmp->data != not_this_one)
+        {
+          window = (MetaWindow *) tmp->data;
+	  break;
+        }
+
+      tmp = tmp->next;
+    }
+
+  if (window)
+    {
+      meta_topic (META_DEBUG_FOCUS,
+                  "Focusing MRU window %s\n", window->desc);
+      
+      meta_window_focus (window, meta_display_get_current_time (workspace->screen->display));
+
+      /* Also raise the window if in click-to-focus */
+      if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_CLICK)
+        meta_window_raise (window);
+    }
+  else
+    {
+      meta_topic (META_DEBUG_FOCUS, "No MRU window to focus found\n");
+      meta_screen_focus_top_window (workspace->screen, not_this_one);
+    }
+}
+
+/* Focus top window on workspace */
+void
+meta_workspace_focus_top_window (MetaWorkspace *workspace,
+                                 MetaWindow *not_this_one)
+{
+  MetaWindow *window;
+
+  if (not_this_one)
+    meta_topic (META_DEBUG_FOCUS,
+                "Focusing top window excluding %s\n", not_this_one->desc);
+  
+  window = meta_stack_get_default_focus_window (workspace->screen->stack,
+                                                workspace,
+                                                not_this_one);
+
+  /* FIXME I'm a loser on the CurrentTime front */
+  if (window)
+    {
+      meta_topic (META_DEBUG_FOCUS,
+                  "Focusing top window %s\n", window->desc);
+
+      meta_window_focus (window, 
+	                 meta_display_get_current_time (workspace->screen->display));
+
+      /* Also raise the window if in click-to-focus */
+      if (meta_prefs_get_focus_mode () == META_FOCUS_MODE_CLICK)
+        meta_window_raise (window);
+    }
+  else
+    {
+      meta_topic (META_DEBUG_FOCUS, "No top window to focus found\n");
+    }
 }
Index: workspace.h
===================================================================
RCS file: /cvs/gnome/metacity/src/workspace.h,v
retrieving revision 1.14
diff -u -r1.14 workspace.h
--- workspace.h	23 Feb 2003 17:09:46 -0000	1.14
+++ workspace.h	29 May 2003 01:57:13 -0000
@@ -40,6 +40,7 @@
   MetaScreen *screen;
   
   GList *windows;
+  GList *mru_list;
 
   MetaRectangle all_work_areas;
   
@@ -58,6 +59,14 @@
 /* don't confuse with meta_window_visible_on_workspace() */
 gboolean       meta_workspace_contains_window (MetaWorkspace *workspace,
                                                MetaWindow  *window);
+
+void           meta_workspace_focus_mru_window (MetaWorkspace *workspace,
+                                                MetaWindow *not_this_one);
+void           meta_workspace_focus_default_window (MetaWorkspace *workspace,
+                                                    MetaWindow *not_this_one);
+void	       meta_workspace_focus_top_window (MetaWorkspace *workspace,
+	                                        MetaWindow *not_this_one);
+
 void           meta_workspace_activate      (MetaWorkspace *workspace);
 int            meta_workspace_index         (MetaWorkspace *workspace);
 GList*         meta_workspace_list_windows  (MetaWorkspace *workspace);
