Blob Blame History Raw
From 589f9dc5998dc5ea620d1e6fa1a64045574b1dc1 Mon Sep 17 00:00:00 2001
From: Daiki Ueno <ueno@unixuser.org>
Date: Mon, 7 Mar 2011 15:06:38 +0900
Subject: [PATCH] Support surrounding-text commands.

---
 src/engine.c   |   49 +++++++++++++++++++++++++++++++++++++++++++++++--
 src/m17nutil.c |    8 ++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/src/engine.c b/src/engine.c
index 62359c1..ebe7ccc 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -986,8 +986,53 @@ ibus_m17n_engine_callback (MInputContext *context,
     }
     else if (command == Minput_reset) {
     }
-    else if (command == Minput_get_surrounding_text) {
+    else if (command == Minput_get_surrounding_text &&
+             (((IBusEngine *) m17n)->client_capabilities &
+              IBUS_CAP_SURROUNDING_TEXT) != 0) {
+        IBusText *text;
+        guint cursor_pos, nchars, nbytes;
+        MText *mt, *surround;
+        int len, pos;
+
+        ibus_engine_get_surrounding_text ((IBusEngine *) m17n,
+                                          &text,
+                                          &cursor_pos);
+        nchars = ibus_text_get_length (text);
+        nbytes = g_utf8_offset_to_pointer (text->text, nchars) - text->text;
+        mt = mconv_decode_buffer (Mcoding_utf_8, text->text, nbytes);
+        g_object_unref (text);
+
+        len = (long) mplist_value (m17n->context->plist);
+        if (len < 0) {
+            pos = cursor_pos + len;
+            if (pos < 0)
+                pos = 0;
+            surround = mtext_duplicate (mt, pos, cursor_pos);
+        }
+        else if (len > 0) {
+            pos = cursor_pos + len;
+            if (pos > nchars)
+                pos = nchars;
+            surround = mtext_duplicate (mt, cursor_pos, pos);
+        }
+        else {
+            surround = mtext ();
+        }
+        m17n_object_unref (mt);
+        mplist_set (m17n->context->plist, Mtext, surround);
+        m17n_object_unref (surround);
     }
-    else if (command == Minput_delete_surrounding_text) {
+    else if (command == Minput_delete_surrounding_text &&
+             (((IBusEngine *) m17n)->client_capabilities &
+              IBUS_CAP_SURROUNDING_TEXT) != 0) {
+        int len;
+
+        len = (long) mplist_value (m17n->context->plist);
+        if (len < 0)
+            ibus_engine_delete_surrounding_text ((IBusEngine *) m17n,
+                                                 len, -len);
+        else if (len > 0)
+            ibus_engine_delete_surrounding_text ((IBusEngine *) m17n,
+                                                 0, len);
     }
 }
diff --git a/src/m17nutil.c b/src/m17nutil.c
index 1150cc4..db99686 100644
--- a/src/m17nutil.c
+++ b/src/m17nutil.c
@@ -91,6 +91,13 @@ ibus_m17n_parse_color (const gchar *hex)
     return color;
 }
 
+#define DEFAULT_REQUIRES (IBUS_CAP_PREEDIT_TEXT |   \
+                          IBUS_CAP_AUXILIARY_TEXT | \
+                          IBUS_CAP_LOOKUP_TABLE |   \
+                          IBUS_CAP_FOCUS |          \
+                          IBUS_CAP_PROPERTY | \
+                          IBUS_CAP_SURROUNDING_TEXT)
+
 static IBusEngineDesc *
 ibus_m17n_engine_new (MSymbol  lang,
                       MSymbol  name,
@@ -122,6 +129,7 @@ ibus_m17n_engine_new (MSymbol  lang,
                                            "icon",        engine_icon ? engine_icon : "",
                                            "layout",      "us",
                                            "rank",        config->rank,
+                                           "requires",    DEFAULT_REQUIRES,
                                            NULL);
 #else
     engine = ibus_engine_desc_new (engine_name,
-- 
1.7.4