Fix help string for --enable-gui.
[m17n/m17n-lib.git] / example / mimx-anthy.c
index 5f0349d..997a726 100644 (file)
@@ -1,4 +1,4 @@
-/* mimx-anthy.c -- Input method external module for Anthy.
+/* mimx-anthy.c -- Anthy input method external module. -*- coding: euc-jp; -*-
    Copyright (C) 2003, 2004
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H15PRO112
@@ -17,7 +17,7 @@
 
    You should have received a copy of the GNU Lesser General Public
    License along with the m17n library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    02111-1307, USA.  */
 
 /***en
 
 #include <stdio.h>
 #include <string.h>
-#include <config.h>
 #include <m17n.h>
 
 #ifdef HAVE_ANTHY
@@ -115,6 +114,7 @@ static MSymbol Manthy, Msegment;
 /* A structure to record in MInputContext->plist with key Manthy.  */
 
 typedef struct {
+  MInputContext *ic;
   anthy_context_t ac;
   /* Which candidate is selected in each segment.  */
   int *candidate_numbers;
@@ -127,22 +127,44 @@ typedef struct {
 static AnthyContext *
 new_context (MInputContext *ic)
 {
-  AnthyContext *context = NULL;
+  AnthyContext *context;
+  anthy_context_t ac;
   MSymbol euc_jp = msymbol ("euc-jp");
   /* Rebound to an actual buffer just before being used.  */
   MConverter *converter = mconv_buffer_converter (euc_jp, NULL, 0);
 
-  if (converter)
+  if (! converter)
+    return NULL;
+  ac = anthy_create_context ();
+  if (! ac)
+    return NULL;
+  context = calloc (1, sizeof (AnthyContext));
+  context->ic = ic;
+  context->ac = ac;
+  context->num_segments = 0;
+  context->candidate_numbers = NULL;
+  context->converter = converter;
+  return context;
+}
+
+static AnthyContext *
+get_context (MInputContext *ic)
+{
+  MPlist *plist = ic->plist;
+  AnthyContext *context;
+
+  for (; plist && mplist_key (plist) != Mnil; plist = mplist_next (plist))
     {
-      context = calloc (1, sizeof (AnthyContext));
-      context->ac = anthy_create_context ();
-      context->num_segments = 0;
-      context->candidate_numbers = NULL;
-      context->converter = converter;
+      if (mplist_key (plist) != Manthy)
+       continue;
+      context = mplist_value (plist);
+      if (context->ic == ic)
+       return context;
     }
-  return context;
+  return NULL;
 }
 
+
 static void
 free_context (AnthyContext *context)
 {
@@ -218,15 +240,17 @@ MPlist *
 init (MPlist *args)
 {
   MInputContext *ic = mplist_value (args);
+  AnthyContext *context;
 
-  if (! initialized)
+  if (! initialized++)
     {
       anthy_init ();
       Manthy = msymbol (" anthy");
       Msegment = msymbol (" segment");
-      initialized = 1;
     }
-  mplist_push (ic->plist, Manthy, new_context (ic));
+  context = new_context (ic);
+  if (context)
+    mplist_push (ic->plist, Manthy, context);
   return NULL;
 }
 
@@ -234,7 +258,7 @@ MPlist *
 fini (MPlist *args)
 {
   MInputContext *ic = mplist_value (args);
-  AnthyContext *context = mplist_get (ic->plist, Manthy);
+  AnthyContext *context = get_context (ic);
 
   if (context)
     free_context (context);
@@ -245,7 +269,7 @@ MPlist *
 convert (MPlist *args)
 {
   MInputContext *ic = mplist_value (args);
-  AnthyContext *context = mplist_get (ic->plist, Manthy);
+  AnthyContext *context = get_context (ic);
   struct anthy_conv_stat cs;
   MPlist *action, *actions;
   int i;
@@ -283,7 +307,7 @@ MPlist *
 change (MPlist *args)
 {
   MInputContext *ic = mplist_value (args);
-  AnthyContext *context = mplist_get (ic->plist, Manthy);
+  AnthyContext *context = get_context (ic);
   int segment;
 
   if (! context)
@@ -302,7 +326,7 @@ MPlist *
 resize (MPlist *args)
 {
   MInputContext *ic = mplist_value (args);
-  AnthyContext *context = mplist_get (ic->plist, Manthy);
+  AnthyContext *context = get_context (ic);
   struct anthy_conv_stat cs;
   MSymbol shorten;
   int segment;
@@ -347,10 +371,12 @@ MPlist *
 commit (MPlist *args)
 {
   MInputContext *ic = mplist_value (args);
-  AnthyContext *context = mplist_get (ic->plist, Manthy);
+  AnthyContext *context = get_context (ic);
   struct anthy_conv_stat cs;
   int i;
 
+  if (! context)
+    return NULL;
   anthy_get_stat (context->ac, &cs);
   for (i = 0; i < cs.nr_segment; i++)
     anthy_commit_segment (context->ac, i, context->candidate_numbers[i]);