+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;
+
+ 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);
+ }
+ else
+ surround = mtext ();
+ 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 (cursor.from + len < 0)
+ len = - cursor.from;
+ mtext_del (mt, cursor.from + len, cursor.from);
+ nchars += len;
+ update_cursor (cursor.from + len, 1);
+ }
+ else if (len > 0)
+ {
+ if (cursor.from + len > nchars)
+ len = nchars - cursor.from;
+ mtext_del (mt, cursor.from, cursor.from + len);
+ nchars -= len;
+ update_cursor (cursor.from, 1);
+ }
+ if (len)
+ mt_modified = 1;
+ }
+}
+