Print debug information to mdebug__output instead of stderr.
[m17n/m17n-lib.git] / src / input.c
index 9c9ed6b..61baa7a 100644 (file)
@@ -1,5 +1,5 @@
 /* input.c -- input method module.
 /* input.c -- input method module.
-   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H15PRO112
 
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H15PRO112
 
     ¹Ô¤ï¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£¾ÜºÙ¤Ë¤Ä¤¤¤Æ¤Ï´Ø¿ô minput_event_to_key () ¤Î
     ÀâÌÀ¤ò»²¾È¡£
 
     ¹Ô¤ï¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£¾ÜºÙ¤Ë¤Ä¤¤¤Æ¤Ï´Ø¿ô minput_event_to_key () ¤Î
     ÀâÌÀ¤ò»²¾È¡£
 
-    <li> ³°ÉôÆþÎϥ᥽¥Ã¥É
+    <li> ³°ÉôÆþÎϥ᥽¥Ã¥É @anchor foreign-input-method
 
     ³°ÉôÆþÎϥ᥽¥Ã¥É¤È¤Ï LANGUAGE ¤¬ @c Mnil ¤Î¤â¤Î¤Ç¤¢¤ê¡¢¤½¤ÎËÜÂΤϳ°
     Éô¤Î¥ê¥½¡¼¥¹¤È¤·¤ÆÄêµÁ¤µ¤ì¤ë¡£¡Ê¤¿¤È¤¨¤ÐX Window System ¤ÎXIM ¤Ê
 
     ³°ÉôÆþÎϥ᥽¥Ã¥É¤È¤Ï LANGUAGE ¤¬ @c Mnil ¤Î¤â¤Î¤Ç¤¢¤ê¡¢¤½¤ÎËÜÂΤϳ°
     Éô¤Î¥ê¥½¡¼¥¹¤È¤·¤ÆÄêµÁ¤µ¤ì¤ë¡£¡Ê¤¿¤È¤¨¤ÐX Window System ¤ÎXIM ¤Ê
@@ -269,7 +269,7 @@ static int update_custom_info (void);
 static MInputMethodInfo *get_im_info (MSymbol, MSymbol, MSymbol, MSymbol);
 
 \f
 static MInputMethodInfo *get_im_info (MSymbol, MSymbol, MSymbol, MSymbol);
 
 \f
-void
+static void
 fully_initialize ()
 {
   char *key_names[32]
 fully_initialize ()
 {
   char *key_names[32]
@@ -401,7 +401,7 @@ fully_initialize ()
        msymbol_put (alias[j], M_key_alias, alias[j + 1]);
     }
 
        msymbol_put (alias[j], M_key_alias, alias[j + 1]);
     }
 
-  buf3[0] = 255;
+  buf3[0] = (char) 255;
   alias[0] = alias[3] = msymbol (buf3);
   alias[1] = one_char_symbol[255] = msymbol ("M-Delete");
   alias[2] = msymbol ("A-Delete");
   alias[0] = alias[3] = msymbol (buf3);
   alias[1] = one_char_symbol[255] = msymbol ("M-Delete");
   alias[2] = msymbol ("A-Delete");
@@ -656,7 +656,7 @@ get_following_char (MInputContext *ic, int pos)
 }
 
 static int
 }
 
 static int
-surrounding_pos (MSymbol sym)
+surrounding_pos (MSymbol sym, int *pos)
 {
   char *name;
 
 {
   char *name;
 
@@ -664,9 +664,13 @@ surrounding_pos (MSymbol sym)
     return 0;
   name = MSYMBOL_NAME (sym);
   if (name[0] == '@'
     return 0;
   name = MSYMBOL_NAME (sym);
   if (name[0] == '@'
-      && (name[1] == '-' || name[1] == '+')
-      && name[2] >= '1' && name[2] <= '9')
-    return (name[1] == '-' ? - atoi (name + 2) : atoi (name + 2));
+      && (name[1] == '-' ? (name[2] >= '1' && name[2] <= '9')
+         : name[1] == '+' ? (name[2] >= '0' && name[2] <= '9')
+         : 0))
+    {
+      *pos = name[1] == '-' ? - atoi (name + 2) : atoi (name + 2);
+      return 1;
+    }
   return 0;
 }
 
   return 0;
 }
 
@@ -697,12 +701,9 @@ integer_value (MInputContext *ic, MPlist *arg, int surrounding)
       if (name[2])
        {
          pos = atoi (name + 1);
       if (name[2])
        {
          pos = atoi (name + 1);
-         if (pos == 0)
+         if (pos == 0 && code == '-')
            return get_preceding_char (ic, 0);
            return get_preceding_char (ic, 0);
-         if (pos < 0)
-           pos = ic->cursor_pos + pos;
-         else
-           pos = ic->cursor_pos + pos - 1;
+         pos = ic->cursor_pos + pos;
          if (pos < 0)
            {
              if (ic->produced && mtext_len (ic->produced) + pos >= 0)
          if (pos < 0)
            {
              if (ic->produced && mtext_len (ic->produced) + pos >= 0)
@@ -1218,9 +1219,11 @@ load_external_module (MInputMethodInfo *im_info, MPlist *plist)
     module = msymbol ((char *) MTEXT_DATA (MPLIST_MTEXT (plist)));
   else if (MPLIST_SYMBOL_P (plist))
     module = MPLIST_SYMBOL (plist);
     module = msymbol ((char *) MTEXT_DATA (MPLIST_MTEXT (plist)));
   else if (MPLIST_SYMBOL_P (plist))
     module = MPLIST_SYMBOL (plist);
-  module_file = alloca (strlen (MSYMBOL_NAME (module))
+  module_file = alloca (strlen (M17N_MODULE_DIR) + 1
+                       + strlen (MSYMBOL_NAME (module))
                        + strlen (DLOPEN_SHLIB_EXT) + 1);
                        + strlen (DLOPEN_SHLIB_EXT) + 1);
-  sprintf (module_file, "%s%s", MSYMBOL_NAME (module), DLOPEN_SHLIB_EXT);
+  sprintf (module_file, "%s/%s%s",
+          M17N_MODULE_DIR, MSYMBOL_NAME (module), DLOPEN_SHLIB_EXT);
 
   handle = dlopen (module_file, RTLD_NOW);
   if (MFAILP (handle))
 
   handle = dlopen (module_file, RTLD_NOW);
   if (MFAILP (handle))
@@ -2392,6 +2395,10 @@ load_im_info (MPlist *plist, MInputMethodInfo *im_info)
 static int take_action_list (MInputContext *ic, MPlist *action_list);
 static void preedit_commit (MInputContext *ic, int need_prefix);
 
 static int take_action_list (MInputContext *ic, MPlist *action_list);
 static void preedit_commit (MInputContext *ic, int need_prefix);
 
+/* Shift to the state of name STATE_NAME.  If STATE_NAME is `t', shift
+   to the previous state (if any).  If STATE_NAME is `nil', shift to
+   the initial state.  */
+
 static void
 shift_state (MInputContext *ic, MSymbol state_name)
 {
 static void
 shift_state (MInputContext *ic, MSymbol state_name)
 {
@@ -2438,7 +2445,7 @@ shift_state (MInputContext *ic, MSymbol state_name)
     preedit_commit (ic, 0);
   mtext_cpy (ic_info->preedit_saved, ic->preedit);
   ic_info->state_pos = ic->cursor_pos;
     preedit_commit (ic, 0);
   mtext_cpy (ic_info->preedit_saved, ic->preedit);
   ic_info->state_pos = ic->cursor_pos;
-  if (state != orig_state)
+  if (state != orig_state || state_name == Mnil)
     {
       if (state == (MIMState *) MPLIST_VAL (im_info->states))
        {
     {
       if (state == (MIMState *) MPLIST_VAL (im_info->states))
        {
@@ -2455,13 +2462,7 @@ shift_state (MInputContext *ic, MSymbol state_name)
       else
        ic->status = im_info->title;
       ic->status_changed = 1;
       else
        ic->status = im_info->title;
       ic->status_changed = 1;
-      if (ic_info->map == ic_info->state->map
-         && ic_info->map->map_actions)
-       {
-         MDEBUG_PRINT1 ("  [IM] [%s] init-actions:",
-                        MSYMBOL_NAME (state->name));
-         take_action_list (ic, ic_info->map->map_actions);
-       }
+      ic_info->state_hook = ic_info->map->map_actions;
     }
 }
 
     }
 }
 
@@ -3154,7 +3155,7 @@ take_action_list (MInputContext *ic, MPlist *action_list)
          int to;
 
          if (MPLIST_SYMBOL_P (args)
          int to;
 
          if (MPLIST_SYMBOL_P (args)
-             && (pos = surrounding_pos (MPLIST_SYMBOL (args))) != 0)
+             && surrounding_pos (MPLIST_SYMBOL (args), &pos))
            {
              to = ic->cursor_pos + pos;
              if (to < 0)
            {
              to = ic->cursor_pos + pos;
              if (to < 0)
@@ -3513,6 +3514,14 @@ handle_key (MInputContext *ic)
   MSymbol alias = Mnil;
   int i;
 
   MSymbol alias = Mnil;
   int i;
 
+  if (ic_info->state_hook)
+    {
+      MDEBUG_PRINT1 ("  [IM] [%s] init-actions:",
+                    MSYMBOL_NAME (ic_info->state->name));
+      take_action_list (ic, ic_info->state_hook);
+      ic_info->state_hook = NULL;
+    }
+
   MDEBUG_PRINT2 ("  [IM] [%s] handle `%s'", 
                 MSYMBOL_NAME (ic_info->state->name), msymbol_name (key));
 
   MDEBUG_PRINT2 ("  [IM] [%s] handle `%s'", 
                 MSYMBOL_NAME (ic_info->state->name), msymbol_name (key));
 
@@ -3603,6 +3612,7 @@ handle_key (MInputContext *ic)
              && ic_info->key_head < ic_info->used)
            {
              MDEBUG_PRINT (" unhandled\n");
              && ic_info->key_head < ic_info->used)
            {
              MDEBUG_PRINT (" unhandled\n");
+             ic_info->state_hook = map->map_actions;
              return -1;
            }
 
              return -1;
            }
 
@@ -4001,24 +4011,24 @@ dump_im_map (MPlist *map_list, int indent)
   memset (prefix, 32, indent);
   prefix[indent] = '\0';
 
   memset (prefix, 32, indent);
   prefix[indent] = '\0';
 
-  fprintf (stderr, "(\"%s\" ", msymbol_name (key));
+  fprintf (mdebug__output, "(\"%s\" ", msymbol_name (key));
   if (map->map_actions)
     mdebug_dump_plist (map->map_actions, indent + 2);
   if (map->submaps)
     {
       MPLIST_DO (map_list, map->submaps)
        {
   if (map->map_actions)
     mdebug_dump_plist (map->map_actions, indent + 2);
   if (map->submaps)
     {
       MPLIST_DO (map_list, map->submaps)
        {
-         fprintf (stderr, "\n%s  ", prefix);
+         fprintf (mdebug__output, "\n%s  ", prefix);
          dump_im_map (map_list, indent + 2);
        }
     }
   if (map->branch_actions)
     {
          dump_im_map (map_list, indent + 2);
        }
     }
   if (map->branch_actions)
     {
-      fprintf (stderr, "\n%s  (branch\n%s    ", prefix, prefix);
+      fprintf (mdebug__output, "\n%s  (branch\n%s    ", prefix, prefix);
       mdebug_dump_plist (map->branch_actions, indent + 4);
       mdebug_dump_plist (map->branch_actions, indent + 4);
-      fprintf (stderr, ")");      
+      fprintf (mdebug__output, ")");      
     }
     }
-  fprintf (stderr, ")");
+  fprintf (mdebug__output, ")");
 }
 
 
 }
 
 
@@ -4032,16 +4042,16 @@ dump_im_state (MIMState *state, int indent)
   memset (prefix, 32, indent);
   prefix[indent] = '\0';
 
   memset (prefix, 32, indent);
   prefix[indent] = '\0';
 
-  fprintf (stderr, "(%s", msymbol_name (state->name));
+  fprintf (mdebug__output, "(%s", msymbol_name (state->name));
   if (state->map->submaps)
     {
       MPLIST_DO (map_list, state->map->submaps)
        {
   if (state->map->submaps)
     {
       MPLIST_DO (map_list, state->map->submaps)
        {
-         fprintf (stderr, "\n%s  ", prefix);
+         fprintf (mdebug__output, "\n%s  ", prefix);
          dump_im_map (map_list, indent + 2);
        }
     }
          dump_im_map (map_list, indent + 2);
        }
     }
-  fprintf (stderr, ")");
+  fprintf (mdebug__output, ")");
 }
 
 \f
 }
 
 \f
@@ -6343,16 +6353,18 @@ minput_callback (MInputContext *ic, MSymbol command)
     @brief Dump an input method.
 
     The mdebug_dump_im () function prints the input method $IM in a
     @brief Dump an input method.
 
     The mdebug_dump_im () function prints the input method $IM in a
-    human readable way to the stderr.  $INDENT specifies how many
-    columns to indent the lines but the first one.
+    human readable way to the stderr or to what specified by the
+    environment variable MDEBUG_OUTPUT_FILE.  $INDENT specifies how
+    many columns to indent the lines but the first one.
 
     @return
     This function returns $IM.  */
 /***ja
     @brief ÆþÎϥ᥽¥Ã¥É¤ò¥À¥ó¥×¤¹¤ë.
 
 
     @return
     This function returns $IM.  */
 /***ja
     @brief ÆþÎϥ᥽¥Ã¥É¤ò¥À¥ó¥×¤¹¤ë.
 
-    ´Ø¿ô mdebug_dump_im () ¤ÏÆþÎϥ᥽¥Ã¥É $IM ¤ò stderr 
-    ¤Ë¿Í´Ö¤Ë²ÄÆɤʷÁ¤Ç°õºþ¤¹¤ë¡£$INDENT ¤Ï£²¹ÔÌܰʹߤΥ¤¥ó¥Ç¥ó¥È¤ò»ØÄꤹ¤ë¡£
+    ´Ø¿ô mdebug_dump_im () ¤ÏÆþÎϥ᥽¥Ã¥É $IM ¤òɸ½à¥¨¥é¡¼½ÐÎϤ⤷¤¯¤Ï
+    ´Ä¶­ÊÑ¿ô MDEBUG_DUMP_FONT ¤Ç»ØÄꤵ¤ì¤¿¥Õ¥¡¥¤¥ë¤Ë¿Í´Ö¤Ë²ÄÆɤʷÁ¤Ç½Ð
+    ÎϤ¹¤ë¡£$INDENT ¤Ï£²¹ÔÌܰʹߤΥ¤¥ó¥Ç¥ó¥È¤ò»ØÄꤹ¤ë¡£
 
     @return
     ¤³¤Î´Ø¿ô¤Ï $IM ¤òÊÖ¤¹¡£  */
 
     @return
     ¤³¤Î´Ø¿ô¤Ï $IM ¤òÊÖ¤¹¡£  */
@@ -6367,7 +6379,7 @@ mdebug_dump_im (MInputMethod *im, int indent)
   memset (prefix, 32, indent);
   prefix[indent] = '\0';
 
   memset (prefix, 32, indent);
   prefix[indent] = '\0';
 
-  fprintf (stderr, "(input-method %s %s ", msymbol_name (im->language),
+  fprintf (mdebug__output, "(input-method %s %s ", msymbol_name (im->language),
           msymbol_name (im->name));
   mdebug_dump_mtext (im_info->title, 0, 0);
   if (im->name != Mnil)
           msymbol_name (im->name));
   mdebug_dump_mtext (im_info->title, 0, 0);
   if (im->name != Mnil)
@@ -6376,11 +6388,11 @@ mdebug_dump_im (MInputMethod *im, int indent)
 
       MPLIST_DO (state, im_info->states)
        {
 
       MPLIST_DO (state, im_info->states)
        {
-         fprintf (stderr, "\n%s  ", prefix);
+         fprintf (mdebug__output, "\n%s  ", prefix);
          dump_im_state (MPLIST_VAL (state), indent + 2);
        }
     }
          dump_im_state (MPLIST_VAL (state), indent + 2);
        }
     }
-  fprintf (stderr, ")");
+  fprintf (mdebug__output, ")");
   return im;
 }
 
   return im;
 }