diff --git a/gtk/gtkimmodule.c b/gtk/gtkimmodule.c
index 97967d4..907c141 100644
--- a/gtk/gtkimmodule.c
+++ b/gtk/gtkimmodule.c
@@ -100,6 +100,7 @@ static GType gtk_im_module_get_type (void);
 static gint n_loaded_contexts = 0;
 static GHashTable *contexts_hash = NULL;
 static GSList *modules_list = NULL;
+static gchar **immodules_env = NULL;
 
 static GObjectClass *parent_class = NULL;
 
@@ -666,7 +667,7 @@ _gtk_im_module_get_default_context_id (GdkWindow *client_window)
   const gchar *context_id = NULL;
   gint best_goodness = 0;
   gint i;
-  gchar *tmp_locale, *tmp;
+  gchar *tmp_locale, *tmp, **p;
   const gchar *envvar;
   GdkScreen *screen;
   GtkSettings *settings;
@@ -674,11 +675,23 @@ _gtk_im_module_get_default_context_id (GdkWindow *client_window)
   if (!contexts_hash)
     gtk_im_module_initialize ();
 
-  envvar = g_getenv ("GTK_IM_MODULE");
-  if (envvar &&
-      (strcmp (envvar, SIMPLE_ID) == 0 ||
-       g_hash_table_lookup (contexts_hash, envvar))) 
-    return envvar;
+  /* XXX: We may need to think about the trade-off issue,
+   *      the performance v.s. the memory leak later.
+   */
+  if (!immodules_env)
+    {
+      envvar = g_getenv ("GTK_IM_MODULE");
+      if (envvar)
+	  immodules_env = g_strsplit(envvar, ":", 0);
+    }
+  p = immodules_env;
+  while (p && *p)
+    {
+      if (strcmp (*p, SIMPLE_ID) == 0 ||
+	  g_hash_table_lookup (contexts_hash, *p))
+	return *p;
+      p++;
+    }
 
   /* Check if the certain immodule is set in XSETTINGS.
    */
