diff options
author | Takao Fujiwara <tfujiwar@requiem.(none)> | 2009-08-12 18:07:47 +0900 |
---|---|---|
committer | Takao Fujiwara <tfujiwar@requiem.(none)> | 2009-08-12 18:07:47 +0900 |
commit | 2e4bf57cf5625ad825a86b55fdfbce39eab360ab (patch) | |
tree | 9d4c0f70e3189e1802f9bab68b3f12471c6e0cf9 | |
parent | de5a8392910777c72e42120ef4d8206b0c6d7014 (diff) | |
download | ibus-anthy-2e4bf57cf5625ad825a86b55fdfbce39eab360ab.tar.gz |
Author: Takao Fujiwara <takao.fujiwara1@gmail.com>
Date: Wed Aug 12 18:08:36 2009 +0900
Fix reconversion feature
Fix commit_first_segment feature
-rw-r--r-- | engine/engine.py | 97 |
1 files changed, 94 insertions, 3 deletions
diff --git a/engine/engine.py b/engine/engine.py index ca97a6c..dd2f139 100644 --- a/engine/engine.py +++ b/engine/engine.py @@ -30,6 +30,7 @@ from ibus import keysyms from ibus import modifier import jastring from segment import unichar_half_to_full +from gtk import clipboard_get sys.path.append(path.join(os.getenv('IBUS_ANTHY_PKGDATADIR'), 'setup')) from anthyprefs import AnthyPrefs @@ -59,6 +60,8 @@ CONV_MODE_WIDE_LATIN_2, \ CONV_MODE_WIDE_LATIN_3, \ CONV_MODE_PREDICTION = range(14) +CLIPBOARD_RECONVERT = range(1) + KP_Table = {} for k, v in zip(['KP_Add', 'KP_Decimal', 'KP_Divide', 'KP_Enter', 'KP_Equal', 'KP_Multiply', 'KP_Separator', 'KP_Space', 'KP_Subtract'], @@ -182,6 +185,14 @@ class Engine(ibus.EngineBase): return anthy_props + def __get_clipboard(self, clipboard, text, data): + clipboard_text = clipboard.wait_for_text () + + if data == CLIPBOARD_RECONVERT: + self.__update_reconvert(clipboard_text) + + return clipboard_text + def update_preedit(self, string, attrs, cursor_pos, visible): self.update_preedit_text(ibus.Text(string, attrs), cursor_pos, visible) @@ -1225,7 +1236,43 @@ class Engine(ibus.EngineBase): return True def __cmd_reconvert(self, keyval, state): - pass + if not self.__preedit_ja_string.is_empty(): + # if user has inputed some chars + return False + + # Use gtk.Clipboard.request_text() instead of + # gtk.Clipboard.wait_for_text() because DBus is timed out. + clipboard = clipboard_get ("PRIMARY") + if clipboard: + clipboard.request_text (self.__get_clipboard, CLIPBOARD_RECONVERT) + + return True + + def __update_reconvert(self, clipboard_text): + if clipboard_text == None: + return False + + self.__convert_chars = unicode (clipboard_text, "utf-8") + for i in xrange(0, len(self.__convert_chars)): + keyval = self.__convert_chars[i] + self.__preedit_ja_string.insert(unichr(ord (keyval))) + + self.__context.set_string(self.__convert_chars.encode("utf-8")) + conv_stat = anthy.anthy_conv_stat() + self.__context.get_stat(conv_stat) + + for i in xrange(0, conv_stat.nr_segment): + buf = self.__context.get_segment(i, 0) + text = unicode(buf, "utf-8") + self.__segments.append((0, text)) + + self.__convert_mode = CONV_MODE_ANTHY + self.__cursor_pos = 0 + self.__fill_lookup_table() + self.__lookup_table_visible = False + self.__invalidate() + + return True # def __cmd_do_nothing(self, keyval, state): # return True @@ -1302,11 +1349,55 @@ class Engine(ibus.EngineBase): self.__shrink_segment(1) return True + def __commit_nth_segment(self, commit_index, keyval, state): + + if commit_index >= len(self.__segments): + return False + + if self.__convert_mode == CONV_MODE_ANTHY: + for i in xrange(0, commit_index + 1): + (seg_index, text) = self.__segments[i] + self.commit_text(ibus.Text(text)) + + text, cursor = self.__get_preedit() + commit_length = 0 + for i in xrange(0, commit_index + 1): + buf = self.__context.get_segment(i, -1) + commit_length += len(unicode(buf, "utf-8")) + self.__preedit_ja_string.move_cursor(commit_length - cursor) + for i in xrange(0, commit_length): + self.__preedit_ja_string.remove_before() + self.__preedit_ja_string.move_cursor(cursor - commit_length) + + del self.__segments[0:commit_index + 1] + + if len(self.__segments) == 0: + self.__reset() + else: + if self.__cursor_pos > commit_index: + self.__cursor_pos -= (commit_index + 1) + else: + self.__cursor_pos = 0 + (seg_index, text) = self.__segments[self.__cursor_pos] + self.__convert_chars = text + self.__context.set_string(text.encode ("utf-8")) + + self.__lookup_table.clean() + self.__lookup_table.show_cursor (False) + self.__lookup_table_visible = False + self.update_aux_string(u"", ibus.AttrList(), + self.__lookup_table_visible) + self.__fill_lookup_table() + self.__invalidate() + self.__update_input_chars() + + return True + def __cmd_commit_first_segment(self, keyval, state): - pass + return self.__commit_nth_segment(0, keyval, state) def __cmd_commit_selected_segment(self, keyval, state): - pass + return self.__commit_nth_segment(self.__cursor_pos, keyval, state) #candidates_keys def __select_candidate(self, pos): |