*** empty log message ***
[m17n/m17n-lib.git] / src / input.c
index 0410150..be065bd 100644 (file)
 #include <dlfcn.h>
 #endif
 
-#include "m17n-gui.h"
+#include "m17n.h"
 #include "m17n-misc.h"
 #include "internal.h"
 #include "mtext.h"
 #include "database.h"
 #include "charset.h"
 
-static int mdebug_mask = MDEBUG_INPUT;
+static int mdebug_flag = MDEBUG_INPUT;
 
 static int fully_initialized;
 
@@ -1073,7 +1073,7 @@ load_branch (MInputMethodInfo *im_info, MPlist *plist, MIMMap *map)
   else if (im_info->maps) 
     {
       plist = (MPlist *) mplist_get (im_info->maps, map_name);
-      if (! plist)
+      if (! plist && im_info->configured_vars)
        {
          MPlist *p = mplist__assq (im_info->configured_vars, map_name);
 
@@ -1493,6 +1493,8 @@ update_global_info (void)
     {
       MDatabase *mdb = mdatabase_find (Minput_method, Mt, Mnil, Mglobal);
 
+      if (! mdb)
+       return -1;
       global_info = new_im_info (mdb, Mt, Mnil, Mglobal, im_info_list);
     }
   if (! global_info->mdb
@@ -2324,7 +2326,7 @@ load_im_info (MPlist *plist, MInputMethodInfo *im_info)
 \f
 
 static int take_action_list (MInputContext *ic, MPlist *action_list);
-static void preedit_commit (MInputContext *ic);
+static void preedit_commit (MInputContext *ic, int need_prefix);
 
 static void
 shift_state (MInputContext *ic, MSymbol state_name)
@@ -2352,7 +2354,15 @@ shift_state (MInputContext *ic, MSymbol state_name)
        state = (MIMState *) MPLIST_VAL (im_info->states);
     }
 
-  MDEBUG_PRINT1 ("\n  [IM] (shift %s)", MSYMBOL_NAME (state->name));
+  if (MDEBUG_FLAG ())
+    {
+      if (orig_state)
+       MDEBUG_PRINT2 ("\n  [IM] [%s] (shift %s)\n",
+                      MSYMBOL_NAME (orig_state->name),
+                      MSYMBOL_NAME (state->name));
+      else
+       MDEBUG_PRINT1 (" (shift %s)\n", MSYMBOL_NAME (state->name));
+    }
 
   /* Enter the new state.  */
   ic_info->state = state;
@@ -2361,7 +2371,7 @@ shift_state (MInputContext *ic, MSymbol state_name)
   if (state == (MIMState *) MPLIST_VAL (im_info->states)
       && orig_state)
     /* We have shifted to the initial state.  */
-    preedit_commit (ic);
+    preedit_commit (ic, 0);
   mtext_cpy (ic_info->preedit_saved, ic->preedit);
   ic_info->state_pos = ic->cursor_pos;
   if (state != orig_state)
@@ -2384,7 +2394,8 @@ shift_state (MInputContext *ic, MSymbol state_name)
       if (ic_info->map == ic_info->state->map
          && ic_info->map->map_actions)
        {
-         MDEBUG_PRINT (" init-actions:");
+         MDEBUG_PRINT1 ("  [IM] [%s] init-actions:",
+                        MSYMBOL_NAME (state->name));
          take_action_list (ic, ic_info->map->map_actions);
        }
     }
@@ -2468,9 +2479,15 @@ preedit_insert (MInputContext *ic, int pos, MText *mt, int c)
   int nchars = mt ? mtext_nchars (mt) : 1;
 
   if (mt)
-    mtext_ins (ic->preedit, pos, mt);
+    {
+      mtext_ins (ic->preedit, pos, mt);
+      MDEBUG_PRINT1 ("(\"%s\")", MTEXT_DATA (mt));
+    }
   else
-    mtext_ins_char (ic->preedit, pos, c, 1);
+    {
+      mtext_ins_char (ic->preedit, pos, c, 1);
+      MDEBUG_PRINT1 ("('%c')", c);
+    }
   adjust_markers (ic, pos, pos, nchars);
   ic->preedit_changed = 1;
 }
@@ -2506,7 +2523,7 @@ preedit_replace (MInputContext *ic, int from, int to, MText *mt, int c)
 
 
 static void
-preedit_commit (MInputContext *ic)
+preedit_commit (MInputContext *ic, int need_prefix)
 {
   MInputContextInfo *ic_info = (MInputContextInfo *) ic->info;
   int preedit_len = mtext_nchars (ic->preedit);
@@ -2520,10 +2537,13 @@ preedit_commit (MInputContext *ic)
       mtext_put_prop_values (ic->preedit, 0, mtext_nchars (ic->preedit),
                             Mcandidate_index, NULL, 0);
       mtext_cat (ic->produced, ic->preedit);
-      if (mdebug__flag & mdebug_mask)
+      if (MDEBUG_FLAG ())
        {
          int i;
 
+         if (need_prefix)
+           MDEBUG_PRINT1 ("\n  [IM] [%s]",
+                          MSYMBOL_NAME (ic_info->state->name));
          MDEBUG_PRINT (" (commit");
          for (i = 0; i < mtext_nchars (ic->preedit); i++)
            MDEBUG_PRINT1 (" U+%04X", mtext_ref_char (ic->preedit, i));
@@ -2600,6 +2620,7 @@ update_candidate (MInputContext *ic, MTextProperty *prop, int idx)
   int ingroup_index = idx - start;
   MText *mt;
 
+  candidate_list = mplist_copy (candidate_list);
   if (MPLIST_MTEXT_P (group))
     {
       mt = MPLIST_MTEXT (group);
@@ -2618,6 +2639,7 @@ update_candidate (MInputContext *ic, MTextProperty *prop, int idx)
       to = from + mtext_nchars (mt);
     }
   mtext_put_prop (ic->preedit, from, to, Mcandidate_list, candidate_list);
+  M17N_OBJECT_UNREF (candidate_list);
   mtext_put_prop (ic->preedit, from, to, Mcandidate_index, (void *) idx);
   ic->cursor_pos = to;
 }
@@ -2945,9 +2967,11 @@ take_action_list (MInputContext *ic, MPlist *action_list)
              preedit_insert (ic, ic->cursor_pos, mt, 0);
              len = mtext_nchars (mt);
            }
+         plist = mplist_copy (plist);
          mtext_put_prop (ic->preedit,
                          ic->cursor_pos - len, ic->cursor_pos,
                          Mcandidate_list, plist);
+         M17N_OBJECT_UNREF (plist);
          mtext_put_prop (ic->preedit,
                          ic->cursor_pos - len, ic->cursor_pos,
                          Mcandidate_index, (void *) 0);
@@ -3342,11 +3366,11 @@ take_action_list (MInputContext *ic, MPlist *action_list)
        }
       else if (name == Mcommit)
        {
-         preedit_commit (ic);
+         preedit_commit (ic, 0);
        }
       else if (name == Munhandle)
        {
-         preedit_commit (ic);
+         preedit_commit (ic, 0);
          return -1;
        }
       else
@@ -3406,8 +3430,8 @@ handle_key (MInputContext *ic)
   MSymbol alias = Mnil;
   int i;
 
-  MDEBUG_PRINT2 ("  [IM] handle `%s' in state %s", 
-                msymbol_name (key), MSYMBOL_NAME (ic_info->state->name));
+  MDEBUG_PRINT2 ("  [IM] [%s] handle `%s'", 
+                MSYMBOL_NAME (ic_info->state->name), msymbol_name (key));
 
   if (map->submaps)
     {
@@ -3646,8 +3670,20 @@ re_init_ic (MInputContext *ic, int reload)
   fini_ic_info (ic);
   if (reload)
     reload_im_info (im_info);
+  if (! im_info->states)
+    {
+      struct MIMState *state;
+
+      M17N_OBJECT (state, free_state, MERROR_IM);
+      state->name = msymbol ("init");
+      state->title = mtext__from_data ("ERROR!", 6, MTEXT_FORMAT_US_ASCII, 0);
+      MSTRUCT_CALLOC (state->map, MERROR_IM);
+      im_info->states = mplist ();
+      mplist_add (im_info->states, state->name, state);
+    }
   init_ic_info (ic);
   shift_state (ic, Mnil);
+
   ic->status_changed = status_changed;
   ic->preedit_changed = preedit_changed;
   ic->cursor_pos_changed = cursor_pos_changed;
@@ -3666,7 +3702,7 @@ open_im (MInputMethod *im)
 {
   MInputMethodInfo *im_info = get_im_info (im->language, im->name, Mnil, Mnil);
 
-  if (! im_info)
+  if (! im_info || ! im_info->states)
     MERROR (MERROR_IM, -1);
   im->info = im_info;
 
@@ -3805,23 +3841,21 @@ filter (MInputContext *ic, MSymbol key, void *arg)
   /* If the current map is the root of the initial state, we should
      produce any preedit text in ic->produced.  */
   if (ic_info->map == ((MIMState *) MPLIST_VAL (im_info->states))->map)
-    preedit_commit (ic);
+    preedit_commit (ic, 1);
 
   if (mtext_nchars (ic->produced) > 0)
     {
-      MSymbol lang = msymbol_get (ic->im->language, Mlanguage);
-
-      if (mdebug__flag & mdebug_mask)
+      if (MDEBUG_FLAG ())
        {
-         MDEBUG_PRINT (" (produced");
+         MDEBUG_PRINT1 ("\n  [IM] [%s] (produced",
+                        MSYMBOL_NAME (ic_info->state->name));
          for (i = 0; i < mtext_nchars (ic->produced); i++)
            MDEBUG_PRINT1 (" U+%04X", mtext_ref_char (ic->produced, i));
          MDEBUG_PRINT (")");
        }
 
-      if (lang != Mnil)
-       mtext_put_prop (ic->produced, 0, mtext_nchars (ic->produced),
-                       Mlanguage, ic->im->language);
+      mtext_put_prop (ic->produced, 0, mtext_nchars (ic->produced),
+                     Mlanguage, ic->im->language);
     }
   if (ic_info->commit_key_head > 0)
     {
@@ -4788,7 +4822,7 @@ minput_get_description (MSymbol language, MSymbol name)
 
     If $COMMAND is #Mnil, information about all commands is returned.
 
-    The return value is a @e well-formed plist (#m17nPlist) of this
+    The return value is a @e well-formed plist (@ref m17nPlist) of this
     format:
 @verbatim
   ((NAME DESCRIPTION STATUS [KEYSEQ ...]) ...)
@@ -4842,7 +4876,7 @@ minput_get_description (MSymbol language, MSymbol name)
 
     $COMMAND ¤¬ #Mnil ¤Î¾ì¹ç¤Ï¡¢¤¹¤Ù¤Æ¤Î¥³¥Þ¥ó¥É¤Ë´Ø¤¹¤ë¾ðÊó¤òÊÖ¤¹¡£
 
-    Ìá¤êÃͤϰʲ¼¤Î·Á¼°¤Î @e well-formed plist (#m17nPlist) ¤Ç¤¢¤ë¡£
+    Ìá¤êÃͤϰʲ¼¤Î·Á¼°¤Î @e well-formed plist (@ref m17nPlist) ¤Ç¤¢¤ë¡£
 
 @verbatim
   ((NAME DESCRIPTION STATUS [KEYSEQ ...]) ...)
@@ -5186,7 +5220,7 @@ minput_config_command (MSymbol language, MSymbol name, MSymbol command,
     If $VARIABLE is #Mnil, information about all variables is
     returned.
 
-    The return value is a @e well-formed plist (#m17nPlist) of this
+    The return value is a @e well-formed plist (@ref m17nPlist) of this
     format:
 @verbatim
   ((NAME DESCRIPTION STATUS VALUE [VALID-VALUE ...]) ...)
@@ -5246,7 +5280,7 @@ minput_config_command (MSymbol language, MSymbol name, MSymbol command,
 
     $VARIABLE ¤¬ #Mnil ¤Î¾ì¹ç¤Ï¡¢¤¹¤Ù¤Æ¤Î¥³¥Þ¥ó¥É¤Ë´Ø¤¹¤ë¾ðÊó¤òÊÖ¤¹¡£
 
-    Ìá¤êÃͤϰʲ¼¤Î·Á¼°¤Î @e well-formed plist (#m17nPlist) ¤Ç¤¢¤ë¡£
+    Ìá¤êÃͤϰʲ¼¤Î·Á¼°¤Î @e well-formed plist (@ref m17nPlist) ¤Ç¤¢¤ë¡£
 @verbatim
   ((NAME DESCRIPTION STATUS VALUE [VALID-VALUE ...]) ...)
 @endverbatim
@@ -5801,7 +5835,7 @@ minput_save_config (void)
     The minput_get_variables () function returns a plist (#MPlist) of
     variables used to control the behavior of the input method
     specified by $LANGUAGE and $NAME.  The plist is @e well-formed
-    (#m17nPlist) of the following format:
+    (@ref m17nPlist) of the following format:
 
 @verbatim
     (VARNAME (DOC-MTEXT DEFAULT-VALUE [ VALUE ... ] )
@@ -5850,7 +5884,7 @@ minput_save_config (void)
 
     ´Ø¿ô minput_get_variables () ¤Ï¡¢$LANGUAGE ¤È $NAME ¤Ë¤è¤Ã¤Æ»ØÄꤵ
     ¤ì¤¿ÆþÎϥ᥽¥Ã¥É¤Î¿¶¤ëÉñ¤¤¤òÀ©¸æ¤¹¤ëÊÑ¿ô¤Î¥×¥í¥Ñ¥Æ¥£¥ê¥¹¥È
-    (#MPlist) ¤òÊÖ¤¹¡£¤³¤Î¥ê¥¹¥È¤Ï @e well-formed ¤Ç¤¢¤ê(#m17nPlist) °Ê
+    (#MPlist) ¤òÊÖ¤¹¡£¤³¤Î¥ê¥¹¥È¤Ï @e well-formed ¤Ç¤¢¤ê(@ref m17nPlist) °Ê
     ²¼¤Î·Á¼°¤Ç¤¢¤ë¡£
 
 @verbatim