--- gtkimcontextsimple.c.TOR	2007-12-27 17:02:06.000000000 +0000
+++ gtkimcontextsimple.c	2008-01-12 17:15:42.000000000 +0000
@@ -19,6 +19,7 @@
 
 #include <config.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <gdk/gdkkeysyms.h>
 #include "gtkaccelgroup.h"
@@ -942,7 +943,7 @@
 		
 		  g_signal_emit_by_name (context_simple, "preedit_changed");
 
-		  printf ("tentative_match=%x returning TRUE\n");
+		  printf ("tentative_match=%x returning TRUE\n", value);
 		  return TRUE;
 		}
 	    }
@@ -970,8 +971,11 @@
 
 {
   int i;
+  int k;
   gunichar combination_buffer[GTK_MAX_COMPOSE_LEN];
+  gunichar combination_buffer_temp[GTK_MAX_COMPOSE_LEN];
   gchar *combination_utf8, *nfc;
+  gchar *combination_utf8_temp, *nfc_temp;
 
   printf ("%s: ", __FUNCTION__);
 
@@ -998,7 +1002,8 @@
 	    CASE (grave, 0x0300);
 	    CASE (acute, 0x0301);
 	    CASE (circumflex, 0x0302);
-	    CASE (tilde, 0x0303);
+	    CASE (tilde, 0x0342);	// Normally is 0x303; Greek Polytonic needs 0x342.
+					// Filed report; pending, bug 14013 (Freedesktop).
 	    CASE (macron, 0x0304);
 	    CASE (breve, 0x0306);
 	    CASE (abovedot, 0x0307);
@@ -1007,15 +1012,15 @@
 	    CASE (doubleacute, 0x030B);
 	    CASE (caron, 0x030C);
 	    CASE (cedilla, 0x0327);
-	    CASE (ogonek, 0x0328);
-	    CASE (iota, 0x0345); /* ??? */
-	    CASE (voiced_sound, 0x032C);
-#if 0
-	    CASE (semivoiced_sound, ?);
-#endif
+	    CASE (ogonek, 0x0314);	// Normally is 0x328; Greek Polytonic needs 0x314.
+				        // Patch accepted in Xorg/GIT.
+	    CASE (iota, 0x0345); /* Used by Greek Polytonic layout only; "ypogegrammeni" */
+	    CASE (voiced_sound, 0x3099);	// Per Marcus Khun file.
+	    CASE (semivoiced_sound, 0x309a);	// Ditto.
 	    CASE (belowdot, 0x0323);
 	    CASE (hook, 0x0309);
-	    CASE (horn, 0x031B);
+	    CASE (horn, 0x0313);	// Normally is 0x31b; Greek Polytonic needs 0x313 (or 0x343).
+					// Patch accepted in Xorg/GIT.
 #undef CASE
 	    default:
 	      combination_buffer[i+1] = gdk_keyval_to_unicode (context_simple->compose_buffer[i]);
@@ -1023,16 +1028,51 @@
 	  i--;
 	}
       printf ("combination_buffer:");
-      for (i = 0; i < n_compose; i++)
-	printf ("%x ", combination_buffer[i]);
+      for (k = 0; k < n_compose; k++)
+	printf ("%x ", combination_buffer[k]);
+      printf ("\n");
       
       if (n_compose > 2)
 	{
-	  g_unicode_canonical_ordering (combination_buffer, n_compose);
-	  printf ("in canonical order:");
-	  for (i = 0; i < n_compose; i++)
-	    printf ("%x ", combination_buffer[i]);
+	  gint n_combinations;
+	  gunichar temp_swap;
+
+	  /* We calculate the number of permutations of the diacritic marks, factorial(n_compose-1).
+ 	   * When diacritic marks belong to the same Canonical Combining Class, 
+ 	   * a normalisation does not attempt reorder them, thus we do this ourselves.
+   	   */
+	  n_combinations = 1;
+	  for (k = 1; k < n_compose; k++ )
+	     n_combinations *= k;
+
+	  memcpy (combination_buffer_temp, combination_buffer, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
+
+	  for (k = 0; k < n_combinations; k++ )
+	     {
+	       g_unicode_canonical_ordering (combination_buffer_temp, n_compose);
+      	       combination_utf8_temp = g_ucs4_to_utf8 (combination_buffer_temp, -1, NULL, NULL, NULL);
+               nfc_temp = g_utf8_normalize (combination_utf8_temp, -1, G_NORMALIZE_NFC);	       	
+
+	       if (g_utf8_strlen (nfc_temp, -1) == 1)
+	         {
+	  	   memcpy (combination_buffer, combination_buffer_temp, GTK_MAX_COMPOSE_LEN * sizeof (gunichar) );
+		   break;
+		 }
+
+	       temp_swap = combination_buffer_temp[k % (n_compose - 1) + 1];
+	       combination_buffer_temp[k % (n_compose - 1) + 1] = combination_buffer_temp[(k+1) % (n_compose - 1) + 1];
+	       combination_buffer_temp[(k+1) % (n_compose - 1) + 1] = temp_swap;
+	     }
+
+  	  printf ("%s: final combination: ", __FUNCTION__);
+          for (k = 0; k < n_compose; k++)
+             printf ("%x ", combination_buffer[k]);
+	  printf ("\n");
+
+	  g_free (combination_utf8_temp);
+	  g_free (nfc_temp);
 	}
+
       combination_utf8 = g_ucs4_to_utf8 (combination_buffer, -1, NULL, NULL, NULL);
       nfc = g_utf8_normalize (combination_utf8, -1, G_NORMALIZE_NFC);
       if (g_utf8_strlen (nfc, -1) == 1)
@@ -1044,14 +1084,14 @@
 
 	  g_free (combination_utf8);
 	  g_free (nfc);
-	  printf ("returning TRUE\n");
+	  printf ("returning TRUE\n\n");
 	  return TRUE;
 	}
       g_free (combination_utf8);
       g_free (nfc);
     }
 
-  printf ("returning FALSE\n");
+  printf ("returning FALSE\n\n");
   return FALSE;
 }
 
