(delete_char): Fix the case of deleting the previous
[m17n/m17n-lib.git] / example / medit.c
index 0b8a0db..df7c7ba 100644 (file)
@@ -950,6 +950,8 @@ delete_char (int n)
     from = cursor.from, to = from + n;
   else
     {
+      from = cursor.from + n;
+      to = cursor.from;
       if (cursor.from == cur.from)
        {
          /* We are at the beginning of line.  */
@@ -963,13 +965,6 @@ delete_char (int n)
              reseat (info.line_from);
            }
          update_cursor (pos, 1);
-         from = cursor.from;
-         to = cursor.to;
-       }
-      else
-       {
-         from = cursor.from - 1;
-         to = cursor.from;
        }
     }
 
@@ -2171,6 +2166,54 @@ input_status (MInputContext *ic, MSymbol command)
   XtSetValues (CurIMStatus, arg, 1);
 }
 
+void
+surrounding_text_handler (MInputContext *ic, MSymbol command)
+{
+  if (command == Minput_get_surrounding_text)
+    {
+      int len = (int) mplist_value (ic->plist);
+      int pos;
+      MText *surround = NULL;
+
+      if (len < 0)
+       {
+         pos = cursor.from + len;
+         if (pos < 0)
+           pos = 0;
+         surround = mtext_duplicate (mt, pos, cursor.from);
+       }
+      else if (len > 0)
+       {
+         pos = cursor.from + len;
+         if (pos > nchars)
+           pos = nchars;
+         surround = mtext_duplicate (mt, cursor.from, pos);
+       }
+      if (surround)
+       {
+         mplist_set (ic->plist, Mtext, surround);
+         m17n_object_unref (surround);
+       }
+    }
+  else if (command == Minput_delete_surrounding_text)
+    {
+      int len = (int) mplist_value (ic->plist);      
+
+      if (len < 0)
+       {
+         if (corsor.from + len < 0)
+           len = - corsor.from;
+         delete_char (len);
+       }
+      else if (len > 0)
+       {
+         if (corsor.from + len > nchars)
+           len = nchars - corsor.from;
+         delete_char (len);
+       }
+    }
+}
+
 int
 compare_input_method (const void *elt1, const void *elt2)
 {
@@ -2211,8 +2254,13 @@ setup_input_methods (int with_xim, char *initial_input_method)
          MDatabase *mdb = mplist_value (pl);
          MSymbol *tag = mdatabase_tag (mdb);
 
-         input_method_table[i].language = tag[1];
-         input_method_table[i].name = tag[2];
+         if (tag[2] == Mnil)
+           i--, num_input_methods--;
+         else
+           {
+             input_method_table[i].language = tag[1];
+             input_method_table[i].name = tag[2];
+           }
        }
       m17n_object_unref (plist);
     }
@@ -2231,6 +2279,10 @@ setup_input_methods (int with_xim, char *initial_input_method)
              (void *) input_status);
   mplist_put (minput_driver->callback_list, Minput_status_done,
              (void *) input_status);
+  mplist_put (minput_driver->callback_list, Minput_get_surrounding_text,
+             (void *) surrounding_text_handler);
+  mplist_put (minput_driver->callback_list, Minput_delete_surrounding_text,
+             (void *) surrounding_text_handler);
 
   current_input_context = NULL;
   current_input_method = -1;